
var h, h2, h3, sh = true, hs;

$(function(){
	
	/**
	 * Browse Menu
	 */
	$('#menu_browse').hover(function(){
		clearTimeout(h);
		$('#browse_submenu').show();
		$(this).addClass('hl');
	}, function(){
		h = setTimeout(function(){
			$('#browse_submenu').hide();
			$('#menu_browse').removeClass('hl');
		}, 2);
	});
	
	$('#browse_submenu > div').hover(function(){
		clearTimeout(h);
	}, function(){
		h = setTimeout(function(){
			$('#browse_submenu').hide();
			$('#menu_browse').removeClass('hl');
		}, 2);
	});
	
	$('#menu_cust').hover(function(){
		clearTimeout(h);
		$('#browse_cust').show();
		$(this).addClass('hl');
	}, function(){
		h2 = setTimeout(function(){
			$('#browse_cust').hide();
			$('#menu_cust').removeClass('hl');
		}, 2);
	});
	
	$('#browse_cust > div').hover(function(){
		clearTimeout(h2);
	}, function(){
		h2 = setTimeout(function(){
			$('#browse_cust').hide();
			$('#menu_cust').removeClass('hl');
		}, 2);
	});
	
	$('#menu_help').hover(function(){
		clearTimeout(h);
		$('#browse_help').show();
		$(this).addClass('hl');
	}, function(){
		h3 = setTimeout(function(){
			$('#browse_help').hide();
			$('#menu_help').removeClass('hl');
		}, 2);
	});
	
	$('#browse_help > div').hover(function(){
		clearTimeout(h3);
	}, function(){
		h3 = setTimeout(function(){
			$('#browse_help').hide();
			$('#menu_help').removeClass('hl');
		}, 2);
	});
	
	//search
	$('#search-bar input[name="keyword"]').attr('autocomplete', 'off');
	$('input.search').click(function(){
		if ($(this).val() == 'Start Searching ...') {
			$(this).val('');
		};
		sh = true;
		clearTimeout(hs);
		$('.search-panel').fadeIn('fast');
	}).focus(function(){
		if ($(this).val() == 'Start Searching ...') {
			$(this).val('');
		};
		sh = true;
		clearTimeout(hs);
		$('.search-panel').fadeIn('fast');
	}).blur(function(){
		if (sh){
			$('.search-panel').fadeOut('fast');
		}
	});
	$('.search-panel').hover(function() {
		sh = false;
		clearTimeout(hs);
	}, function(){
		hs = setTimeout(function(){
			$('.search-panel').fadeOut('fast');
		}, 500);
	});
	$('#all_types_check').change(function(){
		$('input[name="type[]"]').attr('checked', $(this).attr('checked'));
	});
	$('input[name="type[]"]').change(function(){
		if ($('input[name="type[]"]:checked').size() == $('input[name="type[]"]:checkbox').size()){
			$('#all_types_check').attr('checked', true);
		}else{
			$('#all_types_check').attr('checked', false);
		}
	});
	
	//carusel
	$('div.carusel').each(function(){
		$(this).data('index', 0);
	});
	$('div.carusel_down').click(function(){
		var carusel = $(this).prev();
		$(carusel).prev().css('visibility', 'visible');
		var obj = $('.carusel_in', carusel);
		var url = $('input:hidden', carusel).val();
		var index = $(carusel).data('index');

		if (index < 2) {
			obj.animate({
				'margin-top': '-=161px'
			}, 'slow');
			++index;
			$(carusel).data('index', index);
		}else{
			top.location = url;
		}
		
		return false;
	});
	$('div.carusel_up').click(function(){
		var carusel = $(this).next();
		var obj = $('.carusel_in', carusel);
		var index = $(carusel).data('index');
		
		if (index > 0) {
			obj.animate({
				'margin-top': '+=161px'
			}, 'slow');
			--index;
			if (index == 0) {
				$(this).css('visibility', 'hidden');
			}
			$(carusel).data('index', index);
		}
		
		return false;
	});
	
	// rating
	$(".rating").stars({
		inputType: "select",
		oneVoteOnly: true,
		callback: function(ui, type, value)
		{
			var temp = $(ui.$selec).attr('id').split('_');
			prod_id = temp[1];
			$.post('rateit.php', {
				'rating': value,
				'prod_id': prod_id
			}, function(data){
				ui.select(Math.round(data.value));
			}, 'json');
		}

	});
	
	// advanced search
	
	$('#adv_types_ch').click(function(){
		if (window.type_state) {
			$('input[name="type[]"]').attr('checked', false);
			window.type_state = false;
		}else{
			$('input[name="type[]"]').attr('checked', true);
			window.type_state = true;
		}
		return false;
	});
	
	$('#adv_cats_ch').click(function(){
		if (window.cat_state) {
			$('input[name="cat[]"]').attr('checked', false);
			window.cat_state = false;
		}else{
			$('input[name="cat[]"]').attr('checked', true);
			window.cat_state = true;
		}
		return false;
	});
});
