var keepNewsOpen = false;
function setKeepNewsOpen(n){
    keepNewsOpen = n;
    if(n){
        $("#news-box").toggle();
        $("#news").toggleClass("on");
    }
}
$(document).ready(function() {

    // NEWS-BOX
	$("#news").click(function(e) {
		e.preventDefault();
        if(!keepNewsOpen){
            $("#news-box").toggle();
            $("#news").toggleClass("on");
        }
	});
	$("#news-box").mouseup(function() {
		return false
	});
	$(document).mouseup(function(e) {
        if(!keepNewsOpen){
            if($(e.target).parent("li#news").length==0) {
                $("#news").removeClass("on");
                $("#news-box").hide();
            }
		}
	});

	// TOOLTIP
	$(".itemTooltip").tooltip({
		effect: 'slide',
		relative: 'true',
		position: 'top right',
		offset: [15, -65],
		opacity: 0.9
	 }).dynamic({ bottom: { direction: 'down', bounce: true } });


// TOOLTIP
    $(".itemTooltipPressreview").tooltip({
        effect: 'slide',
        relative: 'true',
        position: 'top right',
        offset: [150, -275],
        delay: 150,
        opacity: 1
     }).dynamic({
        bottom: {
            direction: 'down',
            bounce: true
        },
        left: {
            offset: [95, -95]
        }
     });



	// FANCYBOX
	function formatTitle(title, currentArray, currentIndex, currentOpts) {
		return '<div id="resize-title"><div class="left-title">' + (title && title.length ? '<b>' + title + '</b></div> ' : '' ) + '<div class="right-title"><strong>' + (currentIndex + 1) + '</strong> of <strong>' + currentArray.length + '</strong></div></div> <div class="resize-close"><a href="javascript:;" onclick="$.fancybox.close();"><img src="'+customDirPath+'images/system/resize-close.gif" /></a></span>';
	}
//	$(".item a").fancybox({
    $(".item a:not(.product-gallery .item a)").fancybox({    
		'showCloseButton': false,
		'overlayColor': "#000",
		'overlayOpacity': 0.7,
		'titlePosition': 'inside',
		'titleFormat': formatTitle
	});

	// NAVIGATION
    if(jQuery().hoverIntent){
        $("#nav-global .level1 li").hoverIntent(nav_showLevel2, nav_hideLevel2);
        $("#nav-global .l2").mouseenter(nav_keepLevel2).mouseleave(nav_unkeepLevel2);
        $("#nav-global .l2 li").hoverIntent(nav_showLevel3, nav_hideLevel3);
        $("#nav-global .l3").mouseenter(nav_keepLevel3).mouseleave(nav_unkeepLevel3);
        $("#nav-global .l3 li").hoverIntent(nav_showLevel4, nav_hideLevel4);
        $("#nav-global .l4").mouseenter(nav_keepLevel4).mouseleave(nav_unkeepLevel4);
        $("#nav-global .l4 li").hoverIntent(nav_showLevel5, nav_hideLevel5);
        $("#nav-global .l5").mouseenter(nav_keepLevel5).mouseleave(nav_unkeepLevel5);

        $("#nav-global .fl li").mouseenter(function(){nav_removeTempOnL1($(this))});
        $("#nav-global .l2 li").mouseenter(function(){nav_removeTempOn(".l2", $(this))});
        $("#nav-global .l3 li").mouseenter(function(){nav_removeTempOn(".l3", $(this))});
        $("#nav-global .l4 li").mouseenter(function(){nav_removeTempOn(".l4", $(this))});
        $("#nav-global .l5 li").mouseenter(function(){nav_removeTempOn(".l5", $(this))});

        $("#nav-global .l2, #nav-global .l3,#nav-global .l4,#nav-global .l5").mouseover(function() {
            nav_blockLevel($(this));
        });

        /* calculate height and hide subnav to avoid slideOut "jumping"  */
        $("#nav-global div ul:not(.inlinesubnav)").each (function() {
            $(this).css("height", $(this).height());
        });
        $("#nav-global div ul:not(.inlinesubnav)").hide();
        $(".navoffset").removeClass("navoffset")
    }

});
$(window).load(function () {
    //resize content zone if news are displayed
    var nh = $("#news-box").height()-130;
    $("#content").css("min-height",nh+"px");
    //fade footer in
    $("#footer").fadeIn("fast");
});

var nav_overIDs = new Array(0,0,0,0,0);
var nav_outIDs = new Array(0,0,0,0,0);
var nav_timeOutIDs = new Array(0,0,0,0,0);
var nav_timeOutSpeeds = new Array(700,600,500,400,300,200);

function nav_showLevel2(){
    var i = ($(".level1 li").index(this))+1;
    if(nav_outIDs[0] != i){
        nav_removeLevel2();
    }else{
        clearTimeout(nav_timeOutIDs[0]);
    }
    nav_overIDs[0] = i;
    if(customDebugEnabled){console.log(nav_overIDs[0]+" / "+i)};
    $s = $(".l2", $("#subnav"+(i)));
    if(!$s.is(":visible")){$s.slideDown();}

}
function nav_hideLevel2(){
    nav_outIDs[0] = nav_overIDs[0];
    nav_overIDs[0] = 0;
    nav_timeOutIDs[0] = setTimeout(nav_removeLevel2, nav_timeOutSpeeds[0]);
}
function nav_keepLevel2(){
    var l1id = $(this).parent().attr("id").substring(6);
    nav_addTempOnL1(l1id);
    var id = $(this).attr("id");
    nav_overIDs[0] = l1id;
    nav_overIDs[1] = id;
}
function nav_unkeepLevel2(){
    nav_overIDs[0] = 0;
    nav_overIDs[1] = 0;
    nav_timeOutIDs[0] = setTimeout(nav_removeLevel2, nav_timeOutSpeeds[0]);
}


function nav_showLevel3(){
    clearTimeout(nav_timeOutIDs[0]);
    clearTimeout(nav_timeOutIDs[1]);
    var id = $(this).attr("id");
    id = id.substring(1);
    $(".l3").each(function(){
        var subid = $(this).attr("id").substring(1);
        if(id!=subid){$(this).hide(0)}
    });
    $("#n"+id).slideDown();
}
function nav_hideLevel3(){
    if(customDebugEnabled){console.log("nav_hideLevel3")};
    nav_overIDs[1] = 0;
    nav_timeOutIDs[1] = setTimeout(nav_removeLevel3, nav_timeOutSpeeds[0]);
}
function nav_keepLevel3(){
    if(customDebugEnabled){console.log("nav_keepLevel3")};
    var l1id = $(".l2:visible").parent().attr("id").substring(6);
    var l2id = $(".l2:visible").attr("id");
    var id = $(this).attr("id");
    nav_addTempOn(id);
    nav_overIDs[0] = l1id;
    nav_overIDs[1] = l2id;
    nav_overIDs[2] = id;

}
function nav_unkeepLevel3(){
    nav_overIDs[0] = 0;
    nav_overIDs[1] = 0;
    nav_overIDs[2] = 0;
    nav_timeOutIDs[0] = setTimeout(nav_removeLevel2, nav_timeOutSpeeds[0]);
    nav_timeOutIDs[1] = setTimeout(nav_removeLevel3, nav_timeOutSpeeds[2]);
}


function nav_showLevel4(){
    clearTimeout(nav_timeOutIDs[0]);
    clearTimeout(nav_timeOutIDs[1]);
    clearTimeout(nav_timeOutIDs[2]);
    var id = $(this).attr("id");
    nav_overIDs[2] = id;
    id = id.substring(1);
    $(".l4").each(function(){
        var subid = $(this).attr("id").substring(1);
        if(id!=subid){$(this).hide(0)}
    });
    $("#n"+id).slideDown();
}
function nav_hideLevel4(){
    if(customDebugEnabled){console.log("nav_hideLevel4")};
    nav_overIDs[2] = 0;
    nav_timeOutIDs[2] = setTimeout(nav_removeLevel4, nav_timeOutSpeeds[0]);
}

function nav_keepLevel4(){
    if(customDebugEnabled){console.log("nav_keepLevel4")};
    var l1id = $(".l2:visible").parent().attr("id").substring(6);
    var l2id = $(".l2:visible").attr("id");
    var l3id = $(".l3:visible").attr("id");
    var id = $(this).attr("id");
    nav_addTempOn(id);
    nav_overIDs[0] = l1id;
    nav_overIDs[1] = l2id;
    nav_overIDs[2] = l3id;
    nav_overIDs[3] = id;

}
function nav_unkeepLevel4(){
    if(customDebugEnabled){console.log("nav_unkeepLevel4")};
    nav_overIDs[0] = 0;
    nav_overIDs[1] = 0;
    nav_overIDs[2] = 0;
    nav_overIDs[3] = 0;
    nav_timeOutIDs[0] = setTimeout(nav_removeLevel2, nav_timeOutSpeeds[0]);
    nav_timeOutIDs[1] = setTimeout(nav_removeLevel3, nav_timeOutSpeeds[1]);
    nav_timeOutIDs[2] = setTimeout(nav_removeLevel4, nav_timeOutSpeeds[2]);
}
function nav_showLevel5(){
    if(customDebugEnabled){console.log("nav_showLevel5")};
    clearTimeout(nav_timeOutIDs[0]);
    clearTimeout(nav_timeOutIDs[1]);
    clearTimeout(nav_timeOutIDs[2]);
    clearTimeout(nav_timeOutIDs[3]);
    var id = $(this).attr("id");
    if(customDebugEnabled){console.log(id)};
//    nav_overIDs[0] = l1id;
    nav_overIDs[3] = id;
    id = id.substring(1);
    $(".l5").each(function(){
        var subid = $(this).attr("id").substring(1);
        if(id!=subid){$(this).hide(0)}
    });
    $("#n"+id).slideDown();


}
function nav_hideLevel5(){
    if(customDebugEnabled){console.log("nav_hideLevel4")};
    nav_overIDs[3] = 0;
    nav_timeOutIDs[3] = setTimeout(nav_removeLevel5, nav_timeOutSpeeds[0]);

//        var i = $(".level1 li").index(this);
//        $(".l2", $(".subnav"+(i+1))).slideUp();
}

function nav_keepLevel5(){
    if(customDebugEnabled){console.log("nav_keepLevel5")};
    var l1id = $(".l2:visible").parent().attr("id").substring(6);
    var l2id = $(".l2:visible").attr("id");
    var l3id = $(".l3:visible").attr("id");
    var l4id = $(".l4:visible").attr("id");
    var id = $(this).attr("id");
    nav_addTempOn(id);
    nav_overIDs[0] = l1id;
    nav_overIDs[1] = l2id;
    nav_overIDs[2] = l3id;
    nav_overIDs[3] = l4id;
    nav_overIDs[4] = id;

}
function nav_unkeepLevel5(){
    if(customDebugEnabled){console.log("nav_unkeepLevel5")};
//        nav_removeLevel1();
    nav_overIDs[0] = 0;
    nav_overIDs[1] = 0;
    nav_overIDs[2] = 0;
    nav_overIDs[3] = 0;
    nav_overIDs[4] = 0;
    nav_timeOutIDs[0] = setTimeout(nav_removeLevel2, nav_timeOutSpeeds[0]);
    nav_timeOutIDs[1] = setTimeout(nav_removeLevel3, nav_timeOutSpeeds[1]);
    nav_timeOutIDs[2] = setTimeout(nav_removeLevel4, nav_timeOutSpeeds[2]);
    nav_timeOutIDs[3] = setTimeout(nav_removeLevel5, nav_timeOutSpeeds[3]);
}




function nav_removeLevel2(){
    if(nav_overIDs[1]==0&&nav_overIDs[2]==0&&nav_overIDs[3]==0){
        $("#nav-global .fl .temp-on").removeClass("temp-on");
        $(".l2:not(#subnav"+nav_overIDs[0]+" .l2)").slideUp(0);
        $(".l3").slideUp(0);
        $(".l4").slideUp(0);
        $(".l5").slideUp(0);
    }
}
function nav_removeLevel3(){
        if(nav_overIDs[1]==0&&nav_overIDs[2]==0&&nav_overIDs[3]==0){
            $(".l2 .temp-on").removeClass("temp-on");
            $(".l3").slideUp(0);
        }
}
function nav_removeLevel4(){
        if(nav_overIDs[2]==0&&nav_overIDs[3]==0){
            $(".l4").slideUp(0);
        }
}
function nav_removeLevel5(){
        if(nav_overIDs[3]==0){
            $(".l5").slideUp(0);
        }
}
function nav_addTempOnL1(id){
    $("#nav-global .mn"+pid).addClass("temp-on");
}

function nav_removeTempOnL1(ctn){
    $("#nav-global .temp-on").removeClass("temp-on");
    ctn.addClass("temp-on");
}
function nav_addTempOn(id){
    var pid = id.substring(1);
    $("#nav-global #c"+pid).addClass("temp-on");
}

function nav_removeTempOn(lvl, ctn){
    $("#nav-global "+lvl+" .temp-on").removeClass("temp-on");
    ctn.addClass("temp-on");
}

function nav_blockLevel($jq){
    var l1id = $(".l2:visible").parent().attr("id").substring(6);
    var l2id = $(".l2:visible").attr("id");
    nav_overIDs[0] = l1id;
    nav_overIDs[1] = l2id;

}
