var popupTimeoutId;

$(document).ready(function() {
	$('body').append('<div id="tooltiptrack"><div class="top"></div><div class="content"></div><div class="bottom"></div></div>');

	lastAddress = "";
	$.address.init(function(e) {
		if ($.address.value() != '' && $.address.value() != "/"){
			submitFilterForm("/products"+$.address.value()+"&a=1", "", "address"); 
		}
	}).change(function(e) {
		if ($.address.value() != ''){
	//		submitFilterForm($.address.value()+"&a=1", ""); 
		}
	}).history(false);

	setupGridClicks();
});

function setupGridClicks(){
	$('.pagination a').each(function(){
		$(this).click(function(){
			window.scrollTo(0, $('#product-grid').offset().top);
			submitFilterForm($(this).attr("href")+"&a=1", "", "page"); 
			return false;
		});
	});

	$('.sortoptions a').each(function(){
		$(this).click(function(){
			submitFilterForm($(this).attr("href")+"&a=1", "", "sort"); 
			return false;
		});
	});


	$('#filterForm input').click(function(){
		if($(this).attr('id') == "category_1_" && $(this).attr("checked") == false){
			$('#sub_category_4').attr("checked", false);
			$('#sub_category_6').attr("checked", false);
			$('#sub_category_2').attr("checked", false);
			$('#sub_category_3').attr("checked", false);
		}
		if($(this).attr('id') == "category_5_" && $(this).attr("checked") == false){
			$('#sub_category_19').attr("checked", false);
			$('#sub_category_20').attr("checked", false);
			$('#sub_category_21').attr("checked", false);
		}
		if($(this).attr('id') == "category_2_" && $(this).attr("checked") == false){
			$('#sub_category_6').attr("checked", false);
			$('#sub_category_7').attr("checked", false);
			$('#sub_category_8').attr("checked", false);
			$('#sub_category_29').attr("checked", false);
		}
		if($(this).attr('id') == "category_6_0" && $(this).attr("checked") == false){
			$('#sub_category_40').attr("checked", false);
		}

		submitFilterForm($('#filterForm').attr("action")+"?a=1", $('#filterForm').serialize(), "form"); 		
	});
	customTrackTooltip();
}

function submitFilterForm(link, postData, type){
	blockGrid();		

	$.ajax({  
	  type: "POST",  
	  url: link,  
	  data: postData,  
	  success: function(html) {
	  	unblockGrid();
	    $('#product-grid').html(html);
	    if(type == "form"){
	    	filterData = "?"+$('#filterForm').serialize();
		}else{
			if(type == "address"){
				filterData = link.replace("/products", "").replace("&a=1", "");			
			}else{
				filterData = link;
			}
		}
		$.address.value(filterData);
	    setupGridClicks();
	  }  
	});
}


function unblockGrid(){
	$("#product-grid").unblock()
}

function blockGrid(){
	$productgrid = $('#product-grid');
	
	$productgrid.block({ message: '<img src="/public/img/preloaders/large.gif" /><br />Loading',
		css: {
			width: '100%',
			height: '88%',
			top: '43%',
			left: 0,
			border: 'none',
			padding: '0',
			backgroundColor: '#fff',
			opacity: .5,
			color: '#000'
		},
		overlayCSS:  {
			backgroundColor: '#fff',
			opacity: 0.9
		}
	  });
}

function customTrackTooltip() {
	$('.tooltiptrack').hover(
		function(e) {

			// check to see if a timeout has already been started
			if (popupTimeoutId) {
				clearTimeout(popupTimeoutId);
				popupTimeoutId = null;
			}

			$tooltip = $('#tooltiptrack');
			var id = '#tt_' + $(this).attr('id').match(/[a-zA-Z0-9]+$/);

			// position tooltip on element
			$('#tooltiptrack .content').html($(id).html());
			var h = $tooltip.height();
			var w = $tooltip.width();
			var offset = $(this).offset();
			$('#tooltiptrack').css({ top: (offset.top - (h - 20)) + 'px', left: (offset.left - ((w / 2) - $(this).width() / 2)) + 'px'}).fadeIn('fast');
			
			var sAjax = s_gi(s_account);
			sAjax.events = 'event35';
			sAjax.prop31 = $(this).attr("product_id");
			sAjax.linkTrackVars = 'events,prop31';
			sAjax.linkTrackEvents = 'event35';
			sAjax.tl(this,'o','Product Grid Quick Look');
		},
		function() {
			popupTimeoutId = setTimeout(hideTooltip, 200);
		}
	);

	$('#tooltiptrack').hover(
		function() {
			// check to see if a timeout has already been started
			if (popupTimeoutId) {
				clearTimeout(popupTimeoutId);
				popupTimeoutId = null;
			}
		},
		function() {
			popupTimeoutId = setTimeout(hideTooltip, 200);
		}
	);
}

function hideTooltip(){
	$('#tooltiptrack').fadeOut('fast');
	if ($.browser.msie && $.browser.version == "6.0") {
		$('#dd_go_h').show();
		$('#dd_producttype_h').show();
		$('#dd_gender_h').show();
		$('#dd_category_h').show();
		$('#dd_sub_category_h').show();
	}
}

function callOmniture(pageName){
	s.pageName=pageName;
	s.t();
}

