$(document).ready(function(){
	topNavFixes();
	//fixLeftNavHeight();
	initHoverTogglers();
	quickViewFixes();
	setSwatchesHover();
	setOnFocusClear();
	initAutoTab();
	//fixNavigationHeight();
	//leftNavFix();
	
	if( $.browser.msie && $.browser.version < 8 ){
		fixMiniCartForIE();
		fixFilterHoverForIE();
		
	}
	if( $.browser.msie && $.browser.version < 7 ){
		ie6BtnHover();
	}
	
	$('#search a.close').click(function() {
		$('#search').hide();
		return false;
	});
	
	$('.search_launcher').click(function() {
		onTopNavItemOpen();
		$($(this).attr('href')).show();
		return false;
	});
	
	jQuery('#nav ul li.shops .subnav .rColumn ul li a img:first').show();
	jQuery('#nav ul li.shops .subnav .rColumn ul li a').hoverIntent(function(){jQuery('#nav ul li.shops .subnav .rColumn ul li a img').filter(':visible').hide();jQuery(this).find('img').show(); },function(){});
});

/*
$(document).bind('load', function() {
	fixOrderHistoryLeftNavHeight();
});
*/
var tallestcolumn = 0;
function fixNavigationHeight(topCat)  
{  
	tallestcolumn = 0;
	$(topCat).find('.Features,.Collections,.Categories').each(
						function()
						{
							currentHeight = $(this).height();
							if(currentHeight > tallestcolumn) 
							{  
								tallestcolumn  = currentHeight;  
							}
						
						}).height(tallestcolumn);
	
	if($.browser.msie && (parseInt($.browser.version, 10) < 8) && tallestcolumn > 0)
	{
		$(topCat).find('.subnav').height(tallestcolumn + 102);
	}
}  

function leftNavFix()
{
	var contentHeight = $('.cat_sidebar').height();
	if($('.content_wrap').height() > 0 && contentHeight < $('.content_wrap').height())
	{
		$('.cat_sidebar').height($('.content_wrap').height());
	}
	else if($('.page_content').height() > 0 && contentHeight < $('.page_content').height())
	{
		$('.cat_sidebar').height($('.page_content').height());
	}
}

function topNavFixes(){
	$('#nav > ul > li').hover( 
		function(){ 
			$(this).addClass('hover').find('a:first').addClass('hover');
			if( $.browser.msie && $.browser.version < 7 ){
				if( $(this).find('.subnav').size() ){ $('select').hide(); }; 
			}
			
		}, 
		function(){ 
			$(this).removeClass('hover').find('a:first').removeClass('hover');
			$('select').show(); 
		}
	);
	
	$("#nav > ul > li:has('.subnav')").hover( 
		function(){ onTopNavItemOpen();fixNavigationHeight(this); }, 
		function(){  }
	);
	
	

}

function onTopNavItemOpen(){
	//$('#search').hide();
	$('.minicartcontent').hide();
	$('#nav li#minicart').removeClass('open').find('a:first').removeClass('open');
}

function fixLeftNavHeight(){
	var pageContent = $('.page_content');
	var leftNav = $('.cat_sidebar');
	
	//if (pageContent.size() && leftNav.size() && !$('.pt_productsearchresult').size() && !$('#pdpMain.productdetail').size()) {
		var customerServiceBox = $('.customer_service_box');
		if (customerServiceBox.height() == null && jQuery('.wishlist_box')) {
			customerServiceBox = jQuery('.wishlist_box');
		} else if (customerServiceBox.height() == null && jQuery('.play')) {
			customerServiceBox = jQuery('.play');
		}

		var csbHeight = 0;
		if (customerServiceBox.height() != null) {
			csbHeight = customerServiceBox.height();
		} 
		/*else if (jQuery('#subsection_home').size() > 0 && jQuery('.play').height() != null) {
			csbHeight = (jQuery('.play').height());
		}*/
		
		var csbMarginTop = 40;
		/*if (jQuery('#subsection_home').size() > 0) {
			csbMarginTop = 0;
		}*/
		
		if (csbHeight != null && csbHeight != 0) {
			csbHeight = csbHeight + csbMarginTop;
		}
		
		if (csbHeight == null || csbHeight == 0) {
			jQuery('.cat_sidebar').css('padding-bottom', '0px');
		}
		
		var pageContentHeight = pageContent.height();
		var leftNavHeight = leftNav.height() + parseInt(leftNav.css('margin-top')) + parseInt(leftNav.css('margin-bottom')) + parseInt(leftNav.css('padding-top')) + csbHeight; 
		alert(pageContentHeight + "   " + leftNavHeight);
		if (leftNavHeight < pageContentHeight) {
			// left nav is shorter than page content
			//leftNav.height(pageContentHeight - csbHeight);
			//alert(csbHeight);
			//alert(leftNavHeight);
			//alert(pageContentHeight);
			leftNav.height(pageContentHeight - csbHeight);
		}
		
	}
	


function fixMiniCartForIE(){
	$('.checkoutminicart .summaryproduct .productavailibity').each( function(){
		if( $(this).find('div').html() == '' ){
			$(this).remove();
		}
	});
}

function fixFilterHoverForIE(){
	$('.filter #filter_products .size li').hover( 
		function(){ $(this).addClass('hover') }, 
		function(){ $(this).removeClass('hover') }
	);
	$('.filter #filter_products .color li img').hover( 
		function(){ $(this).addClass('hover') }, 
		function(){ $(this).removeClass('hover') }
	);
	$('.filter #filter_products .genders img').hover( 
		function(){ $(this).addClass('hover') }, 
		function(){ $(this).removeClass('hover') }
	);
	$('.filter #filter_products .art li').hover( 
		function(){ $(this).addClass('hover') }, 
		function(){ $(this).removeClass('hover') }
	);
	$('.filter #filter_products .price li').hover( 
		function(){ $(this).addClass('hover') }, 
		function(){ $(this).removeClass('hover') }
	);
}

function initHoverTogglers(){
	$('.hoverToggler').each( function(){ 
		var toggleTargetId = $(this).attr('toggleTargetId');
		$(this).mouseover( function(){
			$('#' + toggleTargetId ).show();
			if( $.browser.msie && $.browser.version < 7 ){
				$('select').hide();
			}
			
		});
		$('#' + toggleTargetId ).mouseout( function(){
			$('#' + toggleTargetId ).hide();
			$('select').show();
		});
	});	
}

function quickViewFixes(){
	$('.quickview').hover( 
		function(){ 
			if( $.browser.msie && $.browser.version < 7 ){
				$(this).find('.badge').css('display','block');
			}
		}, 
		function(){ 
			if( $.browser.msie && $.browser.version < 7){
				$(this).find('.badge').css('display','none');
			} 
		}
	);
}

function ie6BtnHover(){
	var h = '';
	$('.btn')
		.each( function(){
			h = getButtonClass($(this).attr('class')) + '_hover';
			$(this).attr('hoverClass', h);
		})
		.hover( 
			function(){ $(this).addClass($(this).attr('hoverClass')); }, 
			function(){ $(this).removeClass($(this).attr('hoverClass')); }
		);
}

function getButtonClass(s){
	var a = s.split(' ');
	for( var i = 0; i < a.length; i++ ){
		if( a[i].substring(0,2) == 'b_' ) return a[i];
	}
	return '';
}

function setSwatchesHover(){
	if( $('ul.swatches').size() ){
		$('ul.swatches img').hover(
			function(){ $(this).addClass('hover') },
			function(){ $(this).removeClass('hover') }
		);
	}
}

function setOnFocusClear(){
	$('.onFocusClear').each( function(){
		$(this)
			.attr('defaultVal', $(this).attr('value'))
			.focus( function(){ if( $(this).attr('defaultVal') == $(this).attr('value') ){$(this).attr('value', '')} } )
			.blur( function(){ if( $(this).attr('value') == '' ){$(this).attr('value', $(this).attr('defaultVal'))} } )
	});
}

function initAutoTab(){
	$('input[autotab=true]').each( function(){
		$(this).keyup( function(){
			if( $(this).val().length == $(this).attr('maxlength') ){
				$('input[autotabid=' + $(this).attr('autotabto') + ']').focus();
			}
		})
	});
}
