//Javascript Document
//onLoad:
$(document).ready(function() {

	$('.goToIndex').click(function() {
	window.location.href='/';
	});
	
	//moved from right nav js
	$('#sidebar .block.expandableBlock').mouseenter(function() {
		if($(this).hasClass('blockActivate'))
			$(this).addClass('blockActivateOn');
		else
			$(this).addClass('blockOn');
	}).mouseleave(function() {
		if($(this).hasClass('blockActivate'))
			$(this).removeClass('blockActivateOn');
		else
			$(this).removeClass('blockOn');
	});

	$('#sidebar .block.expandableBlock').click(function() {
		$(this).toggleClass('blockActivate');
		$(this).removeClass('blockActivateOn');
		$(this).next('.expandableBlockContent').toggleClass('blockExpanded');
		return false;
	});
	//end from right nav js

	$('.shoppingCartMini').each(function() {
		var h = $(this).height();
		if(h>410) {
			$(this).find('.overflowContainer').css({
				height: '400px'
			});
			$(this).find('th.qtyColumn').css('padding-right', '16px');
		}
	});

	$(document).pngFix({blankgif: 'blank.gif'});

	$('.cartRounded').mouseover(function() {
		ajaxCartCall();
	});

	$('.quoteRounded').mouseover(function() {
		ajaxQuoteCall();
	});


	$('em.closeCart').click(function() {
		$(this).parent().parent().parent().unbind('mouseleave');
		var b = $(this).parent().parent().parent();
		$(b).removeClass('focus');
		$(this).parent().parent().parent().hide();
	});

	//left Menu hover events
	$('.leftMenu .sub').hover(function() {
		$(this).addClass('focus');
		$(this).children('ul').clearQueue();
		$(this).children('ul').delay(250);
		$(this).children('ul').slideDown('fast');
		return false;
	}, function() { //unhover
		$(this).removeClass('focus');
		$(this).children('ul').clearQueue();
		$(this).children('ul').css('display','none');
		return false;
	});


	//main (center 3) nav menus, delayed hovers
	var mnt, mnout;
	var firstTime = true;
	var menuHoverTimout = 500;
	var timeout = 1;
	$('.centerMenu .firstMenu, .centerMenu .secondMenu, .centerMenu .thirdMenu, .centerMenu .fourthMenu').mouseenter(function() {
		clearInterval(mnt);
		clearInterval(mnout);
		var m = $(this);
		mnt = setInterval(function () {
			if($(m).find('a.mainMenu').hasClass('focus')){
				clearInterval(mnt);
				return false;
			}

			//hide menus
			$(m).parent().find('a.mainMenu').removeClass('focus');
			$(m).parent().find('.submenu').css('display','none');
			//show menu
			$(m).find('a.mainMenu').addClass('focus');
			$(m).children('.submenu').slideDown('fast');
			clearInterval(mnt);
		}, timeout);
		if(firstTime) {
			firstTime = false;
			timeout = menuHoverTimout;
		}
		return false;
	}).mouseleave(function() {
		clearInterval(mnt);
		var m = $(this);
		mnout = setInterval(function () {
			//hide menus
			$(m).parent().find('a.mainMenu').removeClass('focus');
			$(m).parent().find('.submenu').css('display','none');
			clearInterval(mnout);
		}, menuHoverTimout);
	});
	$('.centerMenu').find('div.submenu').mouseleave(function(){
		//hide menus
		$(this).parent().find('a.mainMenu').removeClass('focus');
		$(this).css('display','none');
	});
	//END main (center 3) nav menus, delayed hovers

	//right (shopping) menu events
	$('.rightMenu .secondRight').hover(function() {
		$(this).addClass('focus');
		$(this).find('ul').clearQueue();
		$(this).find('ul').delay(400);
		$(this).find('ul').slideDown('fast');
		return false;
	}, function() {	//unhover
		$(this).removeClass('focus');
		$(this).find('ul').clearQueue();
		$(this).find('ul').css('display','none');
		return false;
	});

	//right - special binds for search pop-over -- renloe
	function closeSearch(){
		var that = $('.rightMenu .thirdRight');
		$(that).removeClass('focus');
		$(that).children('ul').css('display','none');
		$(that).children('div.subright').css('display','none');

		$(that).children('a.search').unbind();
		$(that).children('a.search').click(function(){
			$(that).addClass('focus');
			$(that).children('ul').css('display','block');
			$(that).children('div.subright').css('display','block');
			$('#searchFormDiv input').select();
			$(that).children('a.search').unbind();
			$(that).children('a.search').click(function() {
				closeSearch();
			});
			return false;
		});
		return false;
	}
	$('#searchCloseBtn').click(function(){
		closeSearch();
	});
	closeSearch(); //init search click event

	$('#searchFormDiv select').change( function(){
		$('#searchFormDiv input').select();
	});

	$("#searchFormDiv input").focus(function(){
		if( $(this).val() == "Enter Keyword or item number")
			$(this).val('');
		return false;
	});
	//END search calls


	$(window).scroll(function() {
		var y = $('.shoppingCartMini').offset();
		$('.shoppingCartMini').css({
			top: y.top+26+"px"
		});
	});

	$('.rightMenu .overflowPopup em').delay(500).animate( {
		bottom: '0'
	}, 250).animate( {
		bottom: '-3px'
	}, 100).animate( {
		bottom: '0'
	}, 100).delay(1500).animate( {
		bottom: '-40px'
	}, 1000);
	

}); //END onLoad

function ajaxCartCall(){
	$.ajax({
		type:'get',
		url:'/wsrportal/ajax/populateCartContent',
		success: function(data){
			renderItems(data);
		}
	});
}
function ajaxQuoteCall(){
	$.ajax({
		type:'get',
		url:'/wsrportal/ajax/populateQuoteContent',
		success: function(data){
			renderItems(data);
		}
	});
}

function displayCart(bool) {
	if(bool) {
		ajaxCartCall();
		jQuery('.firstTop').children('.shoppingCartMini').fadeIn(function(){
			$(this).parent().addClass('focus');
		}).delay(2000).fadeOut(function(){
			$(this).parent().removeClass('focus');
		});
	} else {
		ajaxQuoteCall();
		jQuery('.secondTop').children('.shoppingCartMini').fadeIn(function(){
			$(this).parent().addClass('focus');
		}).delay(2000).fadeOut(function(){
			$(this).parent().removeClass('focus');
		});
	}
}

