var timeout = 2000;
var timeoutHandle;
var submitting = false;

function init() {		
	customerService();
	detectThinViewport();
	
	Cufon.replace('#crNews h4 a',{ fontFamily: 'Futura', hover: true});
	
	$("div.carousel ul").cycle({ 
	    fx:      'fade', 
	    timeout:  5000 
	});

	$("#utilNav li").hover(
		function () {
			$(this).children(".more ul").css({'display' : 'block'});
		}, 
		function () {
			$(this).children(".more ul").css({'display' : 'none'});
		}
	);

	$(".selectBoxSelected").hover(function () { $(this).children(".selectBox").css({'display' : 'block'}); }, function () { $(this).children(".selectBox").css({'display' : 'none'}); } );

	$("#globalNav > ul > li").hover(function() {
		$(this).find(".customNav").show();
	}, function() {
		$(this).find(".customNav").hide();
	});
		
	$('.tooltip').tooltip({ track: false, delay: 0, fade: 100 });
	
	$("#sizeConversion a").click(function(){
		var s = $(this).html();
		setSizeConversion(s);				
	});	
	
	$("input[type=text].spinner").each(function() { $(this).spinner({max: 100, min: 1}).parent().find("button").html(""); });		
	
	$("input").focus(function() { $(this).addClass("focus"); }).blur(function() { $(this).removeClass("focus");	});
	
	$("textarea").focus(function() {
		$(this).addClass("focus");
	}).blur(function() {
		$(this).removeClass("focus");
	});

	$("div.media").each(function(){ renderFlash(this); });	
}



function initCategories() {
	if ($("#filterList").size() == 0)
		return false;
			
	$("button.order").click(function() 
	{
		var oRow = $(this).parents(".order");
		var nID = oRow.attr("id");
		var nQuantity = oRow.find("input[name=quantity]").val();                                                                      
		var options = {onreturn : function() {}};
		addToCart(nID, nQuantity, options);
	});

	
	$("form#pageFilter select[name=pagesize]").change(function() {
		$("form#pageFilter").submit();
	});

	
	$("form#pageFilter select[name=orderby]").change(function() {
		$("form#pageFilter").submit();
	});


	$("form#pageFilterBottom select[name=pagesize]").change(function() {
		$("form#pageFilterBottom").submit();
	});
	

	$("form#pageFilterBottom select[name=orderby]").change(function() {
		$("form#pageFilterBottom").submit();
	});


	$("#priceList a").click(function() 
	{
		$("input[name=price]").val($(this).attr("rel"));
		$("form#filter").submit();
		return false;
	});
	

	$("#genderList a").click(function() {
		$(this).parent().toggleClass("active");
		
		var sGenders = "";
		
		$(this).parents("ul").find("li.active").each(function() {
			if(sGenders != "")
				sGenders += ","
			sGenders += $(this).find("a").attr("rel");
		});
		
		if(sGenders==""){sGenders="-1"}
	
		$("input[name=gender]").val(sGenders);
		$("#filter").submit();
		
		return false;

	});


	$("#colorList a").click(function() {
		$(this).parent().toggleClass("active");
		
		var sColors = "";
		
		$(this).parents("ul").find("li.active").each(function() {
			if(sColors != "")
				sColors += ","
			sColors += $(this).find("a").attr("rel");
		});
		
		if(sColors==""){sColors="-1"}

		$("input[name=color]").val(sColors);
		$("form#filter").submit();
		return false;
	});
	
	$("#filter_Color a").click(function() {
		$(this).parent().toggleClass("active");
		
		var sColors = "";
		
		$(this).parents("div.itemList").find("li.active").each(function() {
			if(sColors != "")
				sColors += ","
			sColors += $(this).find("a").attr("rel");
		});
		
		if(sColors==""){sColors="-1"}

		$("input[name=color]").val(sColors);
		$("form#filter").submit();
		return false;
	});	
		

	$("#sizeList a").click(function() {
		// toggle active class
		$(this).parent().toggleClass("active");
		
		var sSizes = "";
		
		$(this).parents("div.itemList").find("li.active").each(function() {
		
			if(sSizes != "")
				sSizes += ","

			sValue = $(this).find("a").attr("rel");
			
			if (sValue != "undefined")
				sSizes += sValue;
		});
		
		if(sSizes==""){sSizes="-1"}

		$("input[name=size]").val(sSizes);
		$("form#filter").submit();
		return false;
	});		
			

	$("#filter_Size a").click(function() {
		// toggle active class
		$(this).parent().toggleClass("active");
		
		var sSizes = "";
		
		$(this).parents("div.itemList").find("li.active").each(function() {
		
			if(sSizes != "")
				sSizes += ","

			sValue = $(this).find("a").attr("rel");
			
			if (sValue != "undefined")
				sSizes += sValue;
		});
		
		if(sSizes==""){sSizes="-1"}

		$("input[name=size]").val(sSizes);
		$("form#filter").submit();
		return false;
	});		

	
	$("#brandList a").click(function() {
		$(this).parent().toggleClass("active");
		
		var sBrands = "";
		
		$(this).parents("ul").find("li.active").each(function() {
		
			if(sBrands != "")
				sBrands += ","
			
			sBrands += $(this).find("a").attr("rel");
		});
		
		if(sBrands==""){sBrands="-1"}
	
		$("input[name=brand]").val(sBrands);
		$("#filter").submit();
		return false;
	});	


	$("#filter_Brand a").click(function() {
		$(this).parent().toggleClass("active");
		
		var sBrands = "";
		
		$(this).parents("ul").find("li.active").each(function() {
		
			if(sBrands != "")
				sBrands += ","
			
			sBrands += $(this).find("a").attr("rel");
		});
		
		if(sBrands==""){sBrands="-1"}
	
		$("input[name=brand]").val(sBrands);
		$("#filter").submit();
		return false;
	});	
	

	// Categories
	$("#categoryList a").click(function() {
	
		// toggle active class
		$(this).parent().toggleClass("active");
		
		var sCategories = "";
		
		$(this).parents("ul").find("li.active").each(function() {
		
			if(sCategories != "")
				sCategories += ","
			
			sCategories += $(this).find("a").attr("rel");
		});
	
		if(sCategories==""){sCategories="-1"}
		
		$("input[name=category]").val(sCategories);
		$("#filter").submit();
		return false;
	});	


	$("#filter_Type a").click(function() {
		// toggle active class
		$(this).parent().toggleClass("active");
		
		var sCategories = "";
		
		$(this).parents("ul").find("li.active").each(function() {
		
			if(sCategories != "")
				sCategories += ","
			
			sCategories += $(this).find("a").attr("rel");
		});
	
		if(sCategories==""){sCategories="-1"}
		
		$("input[name=category]").val(sCategories);
		$("#filter").submit();
		return false;
	});			
	

	$("#dynamicCategoryList a").click(function() { 
	// all new specials
		// toggle active class
		$(this).parent().toggleClass("active");
		
		var sCategories = "";
		
		$(this).parents("ul").find("li.active").each(function() {
		
			if(sCategories != "")
				sCategories += ","
			
			sCategories += $(this).find("a").attr("rel");
		});
		
		if(sCategories==""){sCategories="-1"}
		
		if(sCategories.indexOf("-1,") >= 0)
			sCategories = sCategories.replace("-1,","");
		
		if($(this).attr("rel") == "-1")
			sCategories = "-1";
	
		if ($(this).attr("rel") == "37")
			$("input[name=category]").val("37");	
	
		$("input[name=dynamiccategory]").val(sCategories);
		$("#filter").submit();
		return false;
	});	


	$(".remove a").click(function() {
		$("input[name=" + $(this).attr("rel") + "]").val("-1");
		$("#filter").submit();
	});	
	
	
	$('#filter_Brand, #filter_Color, #filter_Size, #filter_Type, #FilterMore').hide();

//	item = menu 		: .showmore
//	inner= uitklapdiv 	: #FilterMore


	var myTime;
	
	
	
	$(".showmore").mouseout(function(){
		if(eval(myTime) != undefined){
			clearTimeout(myTime);
		}
		myTime = setTimeout(function(){$('#FilterMore').hide('slow');},2000);
	  // $('#FilterMore').show('slow');
	},function(){
		//$('#FilterMore').stop(true, true).hide('slow');
	});	
	$(".showmore").hover(function(){
		if(eval(myTime) != undefined){
			clearTimeout(myTime);
		}
	},function(){
		//$('#FilterMore').stop(true, true).hide('slow');
	});
	
	
	
	$("#FilterMore").hover(function(){
		clearTimeout(myTime);
	},function(){
		myTime = setTimeout(function(){$('#FilterMore').hide('slow');},100);
		//$('#FilterMore').stop(true, true).hide('slow');
	});
		
	
	$('#FilterMoreBrands').mouseover(function() {
		$('#filter_Brand, #filter_Color, #filter_Size, #filter_Type').hide();
		$('#filter_Brand, #FilterMore').show();
	});
	
	$('#FilterMoreColors').mouseover(function() {
		$('#filter_Brand, #filter_Color, #filter_Size, #filter_Type').hide();
		$('#filter_Color, #FilterMore').show();
	});

	$('#FilterMoreSizes').mouseover(function() {
		$('#filter_Brand, #filter_Color, #filter_Size, #filter_Type').hide();
		$('#filter_Size, #FilterMore').show();
	});	
	
	$('#FilterMoreTypes').mouseover(function() {
		$('#filter_Brand, #filter_Color, #filter_Size, #filter_Type').hide();
		$('#filter_Type, #FilterMore').show();
	});	

	$('.showless').mouseover(function() {
		$('#filter_Brand, #filter_Color, #filter_Size, #filter_Type, #FilterMore').hide();
		return false;
	});

	$('.showless').click(function(e) {
		e.preventDefault();
	});		
	$('.showmore').click(function(e) {
		e.preventDefault();
	});		
}

$(document).ready(function(){
	init();
	initCategories();
	connectioncard();
});


