$(document).ready(function(){
	topNavFixes();
	initFilters();
	initHoverTogglers();
	setOnFocusClear();
	quickViewFixes();
		
	if( $.browser.msie && $.browser.version < 8 ){
		fixMiniCartForIE();
		//fixTopNavHoverForIE();
		fixFilterHoverForIE();
	}
	
	$('#search a.close').click(function() {
		$('#search').hide();
		return false;
	});
	
	$('.search_launcher').click(function() {
		onTopNavItemOpen();
		$($(this).attr('href')).show();
		return false;
	});
	jQuery('#nav .shops .subnav .shopThumbnail:first').show();
	jQuery('#nav .shops .subnav li a').hoverIntent(function(){jQuery('#nav .shops .subnav li a img').filter(':visible').hide();jQuery(this).find('img').show(); },function(){});

});

$(document).bind('load', function() {
	fixOrderHistoryLeftNavHeight();
});

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(); }
	);
	
	if( $('#minicart .minicarttotal .cartquantity strong').html().length > 1 ){
		$('#minicart .minicarttotal').addClass('dig_2');
	}
}

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('.animal-balloon').size() > 0) {
			customerServiceBox = jQuery('.animal-balloon');
		}

		var csbHeight = 0;
		if (customerServiceBox.height() != null) {
			csbHeight = customerServiceBox.height();
		} 

		
		var csbMarginTop = 40;

		
		if (csbHeight != null && csbHeight != 0) {
			csbHeight = csbHeight + csbMarginTop;
		}
		
		if ((csbHeight == null || csbHeight == 0) && jQuery('.animal-balloon').size() < 1) {
			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; 

		if (leftNavHeight < pageContentHeight) {
			// left nav is shorter than page content
			leftNav.height(pageContentHeight - csbHeight);
		}
		
	}


function fixOrderHistoryLeftNavHeight(){
	var pageContent = $('.page_content');
	var leftNav = $('.cat_sidebar');

	if (pageContent.size() && leftNav.size() && $('.pt_order').size()) {
		var customerServiceBox = $('.customer_service_box');
		if (customerServiceBox.height() == null) {
			customerServiceBox = $('.wishlist_box');
		}

		var csbHeight = 0;
		if (customerServiceBox.height() != null) {
			csbHeight = customerServiceBox.height() + parseInt(leftNav.css('margin-top'));
		}
		
		var pageContentHeight = pageContent.height();
		var leftNavHeight = leftNav.height() + parseInt(leftNav.css('margin-top')) + parseInt(leftNav.css('margin-bottom')) + csbHeight; 

		if (leftNavHeight < pageContentHeight) {
			// left nav is shorter than page content
			leftNav.height(pageContentHeight + parseInt(customerServiceBox.css('margin-top')));
			//alert('left nav is shorter than page content');
		}
		else {
			// left nav is longer than page content
			pageContent.height(leftNavHeight);
			//alert('left nav is longer than page content');
			
				// to extend the right border line of the left nav
				leftNav.height(leftNavHeight);
		}
	}
}

function fixMiniCartForIE(){
	$('.checkoutminicart .summaryproduct .productavailibity').each( function(){
		if( $(this).find('div').html() == '' ){
			$(this).remove();
		}
	});
}

function fixTopNavHoverForIE(){
	$('#nav > ul > li').hover( 
		function(){ $('#welcome').hide(); $('#main *').hide(); }, 
		function(){ $('#welcome').show(); $('#main *').show(); }
	);
}

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 initFilters(){
	if( $('.filter #filter_products').size() ){
		$('.filter #filter_products li').click( function(){
			$(this).hasClass('active') ? $(this).removeClass('active') : $(this).addClass('active');
		});
	}
	if( $('.filter #filter_products .genders').size() ){
		$('.filter #filter_products .genders img').click( function(){
			$(this).hasClass('active') ? $(this).removeClass('active') : $(this).addClass('active');
		});
	}
}

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 getElementPosition(e){
	var str = {};
	str.left = 0;
	str.top = 0;
	
	if (e.offsetParent) {
		do {
			str.left += e.offsetLeft;
			str.top += e.offsetTop;
		} while (e = e.offsetParent);
	}
	
	return str;
}

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 quickViewFixes(){
	$('.quickviewbutton').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');
			} 
		}
	);
}


