﻿$(window).load(function () {
		
	/* ------ SEARCH FIELD FOCUS-BLUR ------ */
	var searchWasEmpty = false;
	$('input#searchField').focus(function (event) {
		$('input#'+event.currentTarget.id).addClass("searchFieldFocus");
		if ( !searchWasEmpty ){
			$('input#'+event.currentTarget.id).val("");
			searchWasEmpty = true;
		}
	});
	
	$('input#searchField').blur(function (event) {
		if ($('input#'+event.currentTarget.id).hasClass("searchFieldFocus")){
			$('input#'+event.currentTarget.id).removeClass("searchFieldFocus");
		}
	});
	
	
	/* ------ PRODUCT MENU EXPAND_COLLAPSE ------ */
	/*$('li.expandable').click(function (event) {
		if ($(this).hasClass("selected")) {
			$(this).removeClass("selected");
		}
		else {
			$(this).addClass("selected");
		}
	});*/
	
	/* ------ PRODUCT PALETTE EQUAL HEIGHT BOXES ------ */
	$('div.productBoxRow').each(function(i) {
		var height = 0;
		
		$(this).children('div.productBox').each(function(j) {
			if ($(this).height() > height) {
				height = $(this).height();
			}
		});
		
		$(this).children('div.productBox').css('height', height + 'px');
		
	});
	
	/* ------ OFFER PALETTE EQUAL HEIGHT BOXES ------ */
	$('div.offerBoxRow').each(function(i) {
		var height = 0;
		
		$(this).children('div.offerBox').each(function(j) {
			if ($(this).height() > height) {
				height = $(this).height();
			}
		});
		
		$(this).children('div.offerBox').css('height', height + 'px');
		
	});
	
	/* ------ PRINT PAGE LINK ------ */
	$('a.print').click(function(e){
        window.print();
        e.preventDefault();
    });

	/* ------ TARGET GROUP NAVIGATION MOUSEOVER ------ */
	$("div#productSelectorMenu ul li").mouseover(function (event) {
		if ( !$(this).hasClass('selected') ) {
			$(this).addClass('mouseover');
		}
	});
	
	$("div#productSelectorMenu ul li").mouseout(function (event) {
		if ( $(this).hasClass('mouseover') ) {
			$(this).removeClass('mouseover');
		}
	});

	/* ------ SMALLER FONT SIZES WITHOUT CALIBRI FONT ------ */
	if (!$.font.test("'Calibri','Calibri'", "Arial, serif")) {
		$('#bodyWrapper').addClass('noCalibri');
	}


	/* ------ TARGET GROUP NAVIGATION MENU ITEM WIDTH ------ */
	setMenuItemWidth();
	
	
	/* ------ JQUERY "DATEPICKER" CALENDAR FOR CURRENCY SEARCH PLUGIN ------*/
	$(".calendar").datepicker({ dateFormat: 'yy.mm.dd.' });
	
});

function setMenuItemWidth() {
	var menuItemCount = $("div#productSelectorMenu ul li").length;
	
	var menuPosition = $("div#productSelectorMenu ul").position().left;
	var menuWidth = $("div#productSelectorMenu ul").width();
	var menuLengthFrom0 = menuPosition + menuWidth;
	
	var searchBoxPosition = $("div.searchBox").position().left;
	var menuDesiredLength = searchBoxPosition - 3;
	
	if ((menuWidth >0) && ( menuLengthFrom0 <= menuDesiredLength - menuItemCount )) {
	
		var difference = menuDesiredLength - menuLengthFrom0;
		var cnt = parseInt(difference / menuItemCount);
		
		var addpadding = Math.floor(cnt/2);
		
		var padding_left = parseInt($("div#productSelectorMenu ul li").css('padding-left'));
		var padding_right = parseInt($("div#productSelectorMenu ul li").css('padding-right'));
		
		$("div#productSelectorMenu ul li").each(function (i) {
			/*
			every 6px of difference adds 1px padding, first 6px adds 1px padding to right, second 6px to left, etc
			(cnt is even: left &right padding =
			cnt is odd: right padding = left padding +1)
			*/
			$(this).css('padding-left', padding_left + addpadding);
			$(this).css('padding-right', padding_left + addpadding + (cnt % 2));
		});
	}
}
