/* ========= */
/* FUNCTIONS */
/* ========= */


function alignWelcomeMsg () {
	if(j$('li.welcome').length > 0)	{
		var qsnLinkW = j$('#topLinksDropDowns').width()  + 10;
		j$('li.welcome').css('right', qsnLinkW);
		j$('li.welcome').css('display', 'block');
	}
}


function fixH3() {
	j$('div.profileForm h3').each(function() {
		var currentH3 = j$(this);
		
		if( j$(currentH3).width() > j$(currentH3).parents('.profileForm').width())		{
			if(window.location.href.indexOf("myronfrance") || window.location.href.indexOf("cadeauxadler.com"))			{
				j$(currentH3).css('width', '290px');
			}			
			else	{
				var h3maxWidth = j$(currentH3).parents('.profileForm').width() - 16;			
				j$(currentH3).css('width', h3maxWidth);
			}
		}
	});
}


//	WRAPPER MIN HEIGHT
function wrapperMinHeight() {	
	var viewportHeight = j$(window).height();
	var websiteHeight = j$('#wrapper').height();
	
//	alert('viewport: ' + viewportHeight );
//	alert('websiteHeight: ' + websiteHeight );	
	
	if (viewportHeight > websiteHeight) {
		j$('#wrapper2').css('min-height', viewportHeight)
	}
}


//	TEMPORARY FIX FOR PROD
function fixAsterixLabelWidth() {
	j$('label.asteriskLabel').each(function() {
		var selectedAsterixLabel = j$(this).width();
		var newAsterixLabelWidth = selectedAsterixLabel - 13;
		j$(this).css('width', newAsterixLabelWidth);		
	});
}


function temporaryRequiredFieldsFix() {
	j$('span.asterisk').parent().addClass('asteriskLabel');
}


// SFHOVER
function targetsfHoverLI() {
	j$('#topLinksDropDowns li').addClass('no_sfhover');
	j$('#navigationMenu li').addClass('no_sfhover');
}


//	SHOP ALL CATEGORIES LEVEL 3 FIX FOR MULTICOLUMNS
function identifyLevel3Banners() {
	j$('li.level2').each(function(){		
		if(j$('.shopAllBanner1 img').length > 0)		{
			j$(this).children('div.level3').addClass('shopAllBanner1_padding');
		}
	});
}


function eliminateEmptyColumns() {
	j$('ul.level3').each(function() {
		if(j$(this).children('li').length <= 0)		{
			j$(this).remove();
		}
	});
	
	j$('ul.level2 li.mainNavBanner').each(function() {		
		if(j$(this).find('img').length > 0  ) {
			j$(this).parents('ul.level2').addClass('bannerWidth');
		}else{			
			j$(this).remove();
		}
	});
}


function level3multiColumns() {
	identifyLevel3Banners();
	eliminateEmptyColumns();	
	
	j$('.level3innerwrapper').each(function() {				
//		alert(j$('.shopAllBanner1 img').length);
		
		if (j$(this).find('.shopAllBanner1 img').length == 0 ){			
			j$(this).find('.shopAllBanner1').remove(); 
//			alert('oh hai');
		}
		
		var numberOfUl = j$(this).children('ul').size();
		
		if (j$(this).find('.shopAllBanner1').length > 0){
			numberOfUl = numberOfUl + 1;
		}
		
//		alert(numberOfUl);
		
		if (numberOfUl == 2)		{
			j$(this).addClass('level3_2columns');
		} 		
		else if (numberOfUl == 3)	{
			j$(this).addClass('level3_3columns');
		}		
		else if (numberOfUl == 4)	{
			j$(this).addClass('level3_4columns');
		}			
		else if (numberOfUl == 5)		{		
			j$(this).addClass('level3_maxColumns');
		}		
	});
}


function topNavAlignLastRight() {
	j$('ul#topMenuSections li.level1:last ul').addClass('jquery-alignRight');	
}


function testDropdownMinWidth() {
	j$('#topMenuSections li.level1').each(function(){
		
		if(j$(this).children('ul.level2').width() < j$(this).width())		{
			j$(this).children('ul.level2').css("width", j$(this).width() - 4);
			j$(this).children('ul#storeNavSlide1kartki_i_pamiatki_swiateczne').css("width", j$(this).width() - 24);
			j$(this).children('ul#storeNavSlide1kartki_i_pamiatki_swiateczne').css("padding-right", "20px");
		}
	})	
}

/* ============== */
/* DOCUMENT READY */
/* ============== */

j$(document).ready(function() {
	topNavAlignLastRight();
	wrapperMinHeight();
	alignWelcomeMsg();
	fixH3();
	temporaryRequiredFieldsFix();
	targetsfHoverLI();
	level3multiColumns();
	testDropdownMinWidth();
	
		
	
	
	/* We implement the NORMAL sfhover behaviour on the specified links */
	/* We exclude some links, because we need a different behaviour on those */
	j$('li.no_sfhover, #topMenuSections li, .topLinksUl li').not('#qsnLink, #emailSignUpLink, #lev1optALL').hover(function() {
		j$(this).addClass('sfhover');
	},function() {
		j$(this).removeClass('sfhover');
	});
	
	
	
	
	/* For these special links, we implement the sfhover bahaviour, but also add a 2sec delay on mouseout */
	/* We also increment the z-index, mostly for QSN and EMAIL so that the hovered popup is on top of the other */
	var z = 50;
	j$('#qsnLink, #emailSignUpLink, #lev1optALL').hover(function(){
		z++;
		var timer = j$(this).data('timer');
		if(timer) clearTimeout(timer);
		j$(this).addClass('sfhover').css("z-index",z);
	}, function(){
		var li = j$(this);
		li.data('timer', setTimeout(function(){ li.removeClass('sfhover'); }, 2000));
	});

		
	
	

	/* SCRIPT to reposition the level2 sub-menu in the shop all menu */
	/* ------------------------------------------------------------- */
	j$("li.level2").hover(function(){	
		
		/* We get the top position of the level2.li to know where it's child is positionned */
		var thisTemporaryOffset = j$(this).offset();
		var thisTemporaryTop = thisTemporaryOffset.top;
		j$(this).attr("data-offset-top",thisTemporaryTop);
		
		
		/* Now we get the height of each sub-menu */
		/* With the top position and the height, we can calculate the bottom */
		j$(this).find('.level3innerwrapper').parent().each(function(){
			
			/* Because of IE7-8, we need to make the element visible before we can get its height */
			/* We make it temporarily visible, get the height, then hide it again */
			j$(this).addClass("tempVisible");
			j$(this).find("*").addClass("tempVisible");			
			var thisTemporaryHeight = j$(this).outerHeight();		
			j$(this).parent().attr("data-height",thisTemporaryHeight);			
			j$(this).removeClass("tempVisible");
			j$(this).find("*").removeClass("tempVisible");			
			
			var windowHeight = j$(window).height();
			var windowScrollTop = j$(window).scrollTop();
			var thisHeight = Math.round(j$(this).parent().attr("data-height"));
			var thisTop = Math.round(j$(this).parent().attr("data-offset-top"));
			var thisBottom = thisTop + thisHeight - windowScrollTop;			
			//var tempCSS = "border:3px solid green;";
			
			/* if the bottom is lower than the viewport, we move it back up */
			if(thisBottom > windowHeight){
				//tempCSS = "border:3px solid red;";
				if(thisHeight < windowHeight){
					var thisBottomMove = thisBottom - windowHeight - 3;
					var thisBgMove = 800 - thisBottomMove;
					j$(this).css({"top":"-"+thisBottomMove+"px", "background-position":"0 -"+thisBgMove+"px"});
				}else{
					var thisBottomMove = thisTop - windowScrollTop - 3;
					var thisBgMove = 800 - thisBottomMove;
					j$(this).css({"top":"-"+thisBottomMove+"px", "background-position":"0 -"+thisBgMove+"px"});
				}
			}
			//j$("#foo").remove();
			//j$("body").append("<div id='foo' style='padding:10px; top:"+windowScrollTop+"px; left:0; z-index:999999; position:absolute; background-color:white; "+tempCSS+"'></div>");
			//j$("#foo").html("windowHeight: "+windowHeight+"<br>windowScrollTop: "+windowScrollTop+"<br>thisTop: "+thisTop+"<br>thisHeight: "+thisHeight+"<br>thisBottom: "+thisBottom+"<br>thisBottomMove: "+thisBottomMove+"<br>thisBgMove: "+thisBgMove);
		});	
	},function(){
		/* On MOUSEOUT we remove the styling we added */
		j$(this).find('.level3innerwrapper').parent().each(function(){
			j$(this).attr("style","").removeAttr("style");
		});
	});	
	
});
