var resizedH = false;
var link = '';
jQuery(function($){
	$("#content .content .top ul a").click(function(){
		link = $(this)
		$("#content .content .middle").load($(this).attr('href'), function(){
			$('.foxycart a.add-to-cart').fancybox({
				type: 'iframe',
				onStart: function(url){
					var path = this.href.split('/');
					this.href = 'https://checkout.stageplays.com/cart?'+$("a[href='"+path[path.length-1]+"']").parents('form').serialize();
				}
			});
			$("#content .content .top ul li").removeClass('active');
			link.parent().addClass('active');
		});
		return false;
	})
	$("#content .menu li a").click(function(){
		$("body").focus();
	});
	
	$("#header form a#searchLink").click(function(){
		$(this).parents('form').submit();
		return false;
	});
	$("#header .dropdown-menu li ul").wrap('<div class="submenu"/>').wrap('<div class="inner-wrapper"/>');
	$("#content .menu li ul").wrap('<div class="submenu"/>');
	$("#fancybox-close").click(function(){
		alert(1)
//		$.fancybox.close();
//		return false;
	});

	$('.foxycart a.add-to-cart').fancybox({
		type: 'iframe',
		width: 614,
		height: 276,
		onStart: function(url){
			var path = this.href.split('/');
			this.href = 'https://checkout.stageplays.com/cart?'+$("a[href='"+path[path.length-1]+"']").parents('form').serialize();
		}
	});
    $('a.product-image').fancybox();
	if($(".carousel .jCarouselLite").length)
		$(".carousel .jCarouselLite").jCarouselLite({
		    btnNext: ".carousel .next",
		    btnPrev: ".carousel .prev",
		    visible: 4
		});
//	$(".carousel ul").cycle({
//		next: ".carousel .next",
//		prev: ".carousel .prev",
//		fx: "scrollHorz",
//		timeout: 0 
//	});
	if($("#content .menu .menu").length)
    $("#content .menu .menu").accordion({
    	change: function() {
    		$("#content .menu .active").removeClass("active");
    		$(".ui-state-active").parent().addClass("active");
    	},
    	clearStyle: true,
    	active: '#content .menu>li.active'
    });
    
    var configRollover = {    
			sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
			interval: 100,  // number = milliseconds for onMouseOver polling interval    
			over: doOpenRollover,   // function = onMouseOver callback (REQUIRED)    
			timeout: 200,   // number = milliseconds delay before onMouseOut    
			out: doCloseRollover    // function = onMouseOut callback (REQUIRED)    
	};
	
	var configDropdown = {    
    	sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
         interval: 100,  // number = milliseconds for onMouseOver polling interval    
         over: doOpen,   // function = onMouseOver callback (REQUIRED)    
         timeout: 200,   // number = milliseconds delay before onMouseOut    
         out: doClose    // function = onMouseOut callback (REQUIRED)    
    };
    
    function doOpen() {
    	if($("div.dropdown-menu", this).length) {
    		$("div.dropdown-menu", this).show();
    	}
    }
    
    function doClose() {
    	if($("div.dropdown-menu", this).length) {
    		$("div.dropdown-menu", this).hide();
    	}
    }
    
    function doOpenRollover() {
    	resizeBottom(this);
    	$(".dropdown-menu h3").css("background", 'transparent url("/images/39.png") 0px -68px no-repeat');
    	$("div.dropdown-menu div.bottom").css("background", 'transparent url("/images/42.png") 0px -26px no-repeat');

    	$(".submenu", this).show();
    }
    
    function doCloseRollover() {
    	$(".dropdown-menu h3").css("background", 'transparent url("/images/39.png") 0px 0px no-repeat');
    	$("div.dropdown-menu div.bottom").css("background", 'transparent url("/images/42.png") 0px 0 no-repeat');
    	$(".submenu", this).hide();
    }

    $("#header .menu li:has(div.dropdown-menu)").hoverIntent(configDropdown);
    $("#header .menu .dropdown-menu li:has(div.submenu)").hoverIntent(configRollover);
    
});

function resizeBottom(liElem) {
	if(!resizedH) {
		var h1 = $(".submenu", liElem).height();
		var h2 = $("div.dropdown-menu").height();
		resizedH = h2 - 109;
		$(".submenu .inner-wrapper", liElem).height(h2-109)
	} else {
		$(".submenu .inner-wrapper", liElem).height(resizedH)
	}
}


