
//On Hover Over
function megaHoverOver(){
   $(this).find("a").addClass('selected'); //Find sub and fade it in
   $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
	(function($) {
        //Function to calculate total width of all ul's
        jQuery.fn.calcSubWidth = function() {
            rowWidth = 0;
            //Calculate row
            $(this).find("ul").each(function() { //for each ul...
                rowWidth += $(this).width(); //Add each ul's width together
            });
        };
		
        jQuery.fn.getSubWidth = function() {
            rowWidth = 0;
            //Calculate row
            $(this).find("ul").each(function() { //for each ul...
                rowWidth = $(this).width(); //Add each ul's width together
            });
        };
		
    })(jQuery); 

    if ( $(this).find(".row").length > 0 ) { //If row exists...

        var biggestRow = 0;	

        $(this).find(".row").each(function() {	//for each row...
            $(this).calcSubWidth(); //Call function to calculate width of all ul's
            //Find biggest row
            if(rowWidth > biggestRow) {
                biggestRow = rowWidth;
            }
        });
		$(this).getSubWidth();
        $(this).find(".sub").css({'width' :biggestRow,'margin-left':'-'+rowWidth+'px'}); //Set width
        $(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin

    } else { //If row does not exist...

        $(this).calcSubWidth();  //Call function to calculate width of all ul's
        $(this).find(".sub").css({'width' : rowWidth }); //Set Width

    }
}
//On Hover Out
function megaHoverOut(){
  $(this).find("a").removeClass('selected'); //Find sub and fade it in
   $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
      $(this).hide();  //after fading, hide it
  });
}




var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupProd").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupProd").fadeOut("slow");
		popupStatus = 0;
	}
	$("#popupProd").find("#contentareapopup").empty();
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth 	= document.documentElement.clientWidth;
	var windowHeight 	= document.documentElement.clientHeight;
	var popupHeight 	= $("#popupProd").position().top;
	var popupWidth 		= $("#popupProd").width();
	//centering
	$("#popupProd").css({
		"position": "absolute",
		"top": popupHeight + (windowHeight/3),
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}




$(document).ready(function(){ 
	var config = { sensitivity: 2, interval: 50, over: megaHoverOver,  timeout: 300, out: megaHoverOut};

	$("ul#topnav li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
	$("ul#topnav li").hoverIntent(config);
	var $popup = $("#popupProd");
	
	$("li.oneproductcontainer").hover(function(){ 
		$(this).addClass("oneproductcontainerhover");	
	},function(){
		$(this).removeClass("oneproductcontainerhover");
	});
	$("li.oneproductcontainer").click(function(){ 
		$popup.insertAfter("li.oneproductcontainer");
		var html = $.ajax({ url: $(this).attr('href'),  async: false}).responseText;
		$popup.find("#contentareapopup").empty().append(html);
		centerPopup();
		loadPopup();

	});
	
		
	//CLOSING POPUP
	//Click the x event!
	$("#popupProdClose").click(function(){	disablePopup();	});
	//Click out event!
	$("#backgroundPopup").click(function(){		disablePopup();	});
	//Press Escape event!
	$(document).keypress(function(e){		if(e.keyCode==27 && popupStatus==1){	disablePopup();		}	});









	
	
	$(".jqzoom").jqzoom({ zoomWidth: 250, zoomHeight: 250,zoomType:'reverse'});

	//alert($(location).attr('href'));
	$("#results-sortBy").change (function() {
		var url = $(this).attr('href')+"?order="+$(this).val();
		$(location).attr('href',url);
		return false;
	});	
	
});
