var $j = jQuery.noConflict();
   
// Use jQuery via $j(...)
$j(document).ready(function(){
	$j("#menu ul:first").addClass('sf-menu');
	$j("#aux-menu ul:first").addClass('sf-menu');
	$j("ul.sf-menu").show().supersubs({ 
		minWidth:    30,	// minimum width of sub-menus in em units 
		maxWidth:    30,	// maximum width of sub-menus in em units 
		extraWidth:  20,	// extra width can ensure lines don't sometimes turn over 
		dropShadows: true	// due to slight rounding differences and font-family 
	}).superfish({
		animation: {height: 'toggle'},
		speed: 'fast'
	});			// call supersubs first, then superfish, so that subs are 
				// not display:none when measuring. Call before initialising 
				// containing tabs for same reason. 
	
	
	
	$j('#navigation').supersleight();
	$j('#branding').supersleight();
	$j('#content-mainbody').supersleight();
	$j('#search-form').supersleight();
	$j('#footer-links').supersleight();
	
	var t = setTimeout('houseKeeping()', 1000);
});

$j(window).resize(function() {
  positionActiveIndicators();
  positionMenu();
});


function houseKeeping() {
	insertMenuDrop();
	positionMenu();
	renderHR();
	positionActiveIndicators();
	setShadowHeight();
	copyMenuBranch();
	generateProductPopup();
	generateVideoPopup();
	updateCartQty();
}

function insertMenuDrop() {
	$j('.sf-menu li ul').each(function() {
		if ($j(this).parent('li').parent('ul').hasClass('sf-menu')) {
			$j(this).children('li:first').css({'padding-top':'0'}).prepend('<div class="menu-shadow"></div>');
		}
	});
}

function positionMenu() {
	$j('.sf-menu li ul').each(function() {
		if ($j(this).parent('li').parent('ul').hasClass('sf-menu')) {
			ulw = $j(this).width();
			ulh = Math.floor(ulw / 2);
			
			liw = $j(this).parent('li').width();
			lih = Math.floor(liw / 2);
			
			m = Math.floor(parseInt($j(this).parent('li').children('a').css('margin-left')) / 2);
			
			$j(this).css({'left':'-'+(ulh-lih+m)+'px'});
		}
	});
}

function setShadowHeight() {
	var h = $j('#content-mainbody').height();
	$j('#content-mainbody-shadow-left').css({'height':h+'px'});
	$j('#content-mainbody-shadow-left').css({'height':h+'px'});
	
}

function positionActiveIndicators() {
	if ($j('#navigation .active a').size()) {
		var navpos = $j('#navigation .active a').offset();
		var divpos = $j('#search-panel').offset();
		$j('#menu-active-indicator').css({'top':divpos.top, 'left':(navpos.left+(($j('#navigation .active a').width()-$j('#menu-active-indicator').width())/2)), 'visibility':'visible'});
	}
	
	if ($j('#footer-links .active a').size()) {
		var navpos = $j('#footer-links .active a').offset();
		var divpos = $j('#footer').offset();
		$j('#footer-active-indicator').css({'top':(divpos.top-$j('#footer-active-indicator').height()), 'left':(navpos.left+(($j('#footer-links .active a').width()-$j('#footer-active-indicator').width())/2)), 'visibility':'visible'});
	}
}

function clearSearchField(el) {
	if (el.value=='enter keyword or zip') {
		el.value='';
		//$j(el).css({'text-align':'left'});
	} else {
		//$j(el).css({'text-align':'center'});
	}
}

function resetSearchField(el) {
	if (el.value=='') {
		//$j(el).css({'text-align':'center'});
		el.value='enter keyword or zip';
	} else {
		//$j(el).css({'text-align':'left'});
	}
}

function setSearchFormAction(actn) {
	$j('#search-form').attr('action', actn);
	return true;
}

function loadStoreLocator(url) {
	if (document.forms['search-form'].elements['q'].value.length==5 && !isNaN(document.forms['search-form'].elements['q'].value)) {
		setSearchFormAction(url+'?origin='+document.forms['search-form'].elements['q'].value+'&distance=50')
	} else {
		alert('Please enter a 5 digit zip code.');
		return false;
	}
}

function renderHR() {
	$j('hr').each(function(){
		if ($j(this).css('visibility')=='hidden') {
			$j(this).wrap('<div class="hr" />');
		}
	});
}

function addToCart(obj) {
	$j('body').append('<div id="modal-window"><div id="lightbox"><div id="lightbox-content"></div></div><div id="mask"></div></div>');
	
	//Get the screen height and width
	var maskHeight = $j(document).height();
	var maskWidth = $j(window).width();

	//Set height and width to mask to fill up the whole screen
	$j('#mask').css({'width':maskWidth,'height':maskHeight});
	$j('#mask').css({opacity:0.35});
	$j('#mask').click(function() {
		$j('#modal-window').remove();
	});
	$j(document).keyup(function(e) {
		if(e.keyCode == 27) {   
			$j('#modal-window').remove();
		}   
	});
	
	//Get the window height and width
	var winH = $j(window).height();
	var winW = $j(window).width();
	
	$j('.suggestions').css({'display':'none'});
	
	$j('#lightbox-content').append('<a href="javascript:void(0)" id="modal-window-close" onclick="\$j(\'#modal-window\').remove();">Click here to close panel</a><div class="reset"></div>');
	
	$j('#lightbox-content').append($j(obj).parent().children('.lightbox-product-content:first').html());
	
	//Set the popup window to center
	var lightboxTop = $j(window).scrollTop()+((winH-$j('#lightbox').height())/2);
	var lightboxLeft = (winW/2)-($j('#lightbox').width()/2);
	
	if (lightboxTop<=0) {lightboxTop=10;}
	if (lightboxLeft<=0) {lightboxLeft=10;}
	
	$j('#lightbox').css({'left':lightboxLeft,'top':lightboxTop});
	
	$j('#lightbox').supersleight();
	
	$j('#mask').fadeIn(400);
	$j('#lightbox').fadeIn(400, function() {
		$j('.suggestions').delay(5000).fadeIn('slow');
	});
}

function copyMenuBranch() {
	$j('#menu li').each(function() {
		if ($j(this).parent('ul').hasClass('sf-menu') && $j(this).hasClass('active')) {
			var ul = $j(this).children('ul').clone(false);
			
			$j(ul).removeAttr('style');
			
			$j(ul).find('ul').removeAttr('style').css({'display':'none'});
			$j(ul).find('li').removeAttr('style');
			
			$j(ul).find('div').remove();
			$j(ul).find('span').remove();
			
			$j(ul).find('a').removeAttr('class').removeAttr('style');
			
			$j(ul).find('li.active').parent('ul').css({'display':'block'});
			$j(ul).find('li.active').children('ul:first').css({'display':'block'});
			
			$j(ul).insertAfter('#sidebar hr:first');
			$j('#sidebar hr:first').after('<h2>'+$j(this).children('a').eq(0).html()+'</h2>');
			
		}
	});
}

function generateProductPopup() {
	$j('#mainbody a.product').each(function() {
		$j(this).click(function() {
			generatePopUp($j(this).attr('href'));
			return false;
		});
	});
}

function generateVideoPopup() {
	$j('#mainbody a.video').each(function() {
		$j(this).click(function() {
			generatePopUp($j(this).attr('href'));
			return false;
		});
	});
}

function generatePopUp(url) {
	$j.ajax({
	    url: url,
	    type: 'GET',
		dataType: 'html',
	    timeout: 10000,
	    error: function(){
			alert('Could not locate content.');
	    },
		beforeSend: function(){
			//do nothing
		},
	    success: function(html){
			$j('body').append('<div id="modal-window"><div id="lightbox"><div id="lightbox-content"></div></div><div id="mask"></div></div>');
			
			//Get the screen height and width
			var maskHeight = $j(document).height();
			var maskWidth = $j(window).width();
		
			//Set height and width to mask to fill up the whole screen
			$j('#mask').css({'width':maskWidth,'height':maskHeight});
			$j('#mask').css({opacity:0.35});
			$j('#mask').click(function() {
				$j('#modal-window').remove();
			});
			$j(document).keyup(function(e) {
				if(e.keyCode == 27) {   
					$j('#modal-window').remove();
				}   
			});
			
			//Get the window height and width
			var winH = $j(window).height();
			var winW = $j(window).width();
			
			if ($j('.suggestions').length>=1)
				$j('.suggestions').css({'display':'none'});
			
			$j('#lightbox-content').append('<a href="javascript:void(0)" id="modal-window-close" onclick="\$j(\'#modal-window\').remove();">Click here to close panel</a><div class="reset"></div>');
			
			$j('#lightbox-content').append(html);
			
			//Set the popup window to center
			var lightboxTop = $j(window).scrollTop()+((winH-$j('#lightbox').height())/2);
			var lightboxLeft = (winW/2)-($j('#lightbox').width()/2);
			
			if (lightboxTop<=0) {lightboxTop=10;}
			if (lightboxLeft<=0) {lightboxLeft=10;}
			
			$j('#lightbox').css({'left':lightboxLeft,'top':lightboxTop});
			
			$j('#lightbox').supersleight();
			
			$j('#mask').fadeIn(400);
			
			$j('#lightbox').fadeIn(400, function() {
				if ($j('.suggestions').length>=1)
					$j('.suggestions').delay(5000).fadeIn('slow');
			});
			
			return false;
	    }
	});
}

function updateCartQty() {
	var c = document.cookie.toString();
	c = c.split(/=|&/ig);
	for(var i=0;i<c.length;i++) {
		if (c[i]=='num_of_items')
			$j('#cart-summary').html(c[i+1]);
	}
}
