//Change search box status
//Source: http://buildinternet.com/2009/01/changing-form-input-styles-on-focus-with-jquery/

$(document).ready(function() {
	$('.cboxClose').live('click',function(){
		$.colorbox.close();
		return false;
	})
	
	$('input[name="search_term"]').addClass("idleField");
	$('input[name="search_term"]').focus(function() {
		$(this).removeClass("idleField").addClass("focusField");
		if (this.value == this.defaultValue){ 
			this.value = '';
		}
		if(this.value != this.defaultValue){
			this.select();
		}
	});
	$('input[name="searchTerm"]').blur(function() {
		$(this).removeClass("focusField").addClass("idleField");
		if ($.trim(this.value) == ''){
			this.value = (this.defaultValue ? this.defaultValue : '');
		}
	});
	synchronizeDivHeights("ul.categoryBox");
	synchronizeDivHeights("div.gridView");
	$('.nonAjaxNav').click(function(){
		showLoading();
	})
	
	$('.sortDropDown').change(function(){
		if (!$(this).hasClass('ajaxSorting')){
			showLoading();
			var url = $(this).attr('rel');
			var val = $(this).val();
			var newUrl = url + val;
			window.location = newUrl;
		}
	});
	
	$('.quickView').click(function(){
		var prdId = $(this).attr('prdId');
		if (prdId){
			quickView(prdId);
		} else {
			alert('no product id specified');
		}
		return false;
	})
	//adding quick view image change
	$('.smallImageLink').live('click', function() {
		try {
			var newURL = $(this).attr('rel');
			$('#quickViewLargeImage').attr('src',newURL);
		  } catch (e){
		  }
		  return false;
		});
	$('#eventSelect').change(function(){
		var value=$(this).val();
		window.location = '/pal/currentSaleView.jsp?eventId=' + value;
	})
	
	$('a.ajaxPageNav').live('click', ajaxPaging);
	$('select.ajaxSorting').live('change', ajaxSorting);
	
	setInfoPopup();
	$(document).bind('cbox_complete', function(){
		$('img.productBrandLogoResize').each(function(){
			resizeOnNamedContainer(this, 'div.brandContainer', true);
		})
	});
	$('img.brandImageResize').live('load',function(){
		alert('brand image resize');
	})
	$('img.brandHeaderImageResize').live('load', function(){
		alert('brand header image resize');
	});
	$('img.productBrandLogoResize').live('load', function(){
		alert('brand logo resize');
	})
	
	$.ajaxSetup({
		contentType: "application/x-www-form-urlencoded; charset=iso-8859-1"
	});

});	

function setInfoPopup(){
	$('a.infoPopup').colorbox({width:"80%"})
}

$(window).load(function(){
	
	$('img.brandImageResize').each(resizeOnContainer);
	$('img.brandHeaderImageResize').each(resizeBrandHeader);
	$('img.productBrandLogoResize').each(function(){
		resizeOnNamedContainer(this, 'div.brandContainer', true);
	})
	$('.brandNameDisplay').live('mouseover', function(){
		var sibs = $(this).siblings('.brandContainer');
		if (sibs.length>0){
			var html = $(sibs[0]).html();
			showTooltip(html, this);
		}
	})
	$('.brandNameDisplay').live('mouseleave', function(){
		hideTooltip();
	})
})

function showTooltip(html, parentElement){
	var position = $(parentElement).offset();
	position.left = position.left + 20;
	position.top = position.top + 20;
	$('#brandTooltip').css(position);
	$('#brandTooltip').css('position','absolute');
	$('#brandTooltip').css('z-index', '10000');
	$('#brandTooltip').html(html).show(444);
}

function hideTooltip(){
	$('#brandTooltip').hide(444);
}

function quickView(productId){
	var queryData = {prdId: productId};
	jQuery.get('/pal/navigation/quickView.jsp', queryData, function(data){
		showQuickViewPopup(data.responseHTML, data.productId);
	}, 'json');
}

function showQuickViewPopup(data, id){
	//find where the top of the original product box is...
	//var offset = $("#productBox-" + id).offset();
	//var position = $("#productBox-" + id).position();
	
	var windowHeight = $(window).height();
	var scrollTop = $(window).scrollTop();
	var windowHeightEst = 400;
	var windowWidthEst = 600;
	var windowWidth = $(window).width();
	
	var qvElement = $("#quickViewDetails");
	var modalWidthOff = 321;
	var modalHeightOff = 199;
	if (qvElement.html()!=""){
		var modalWidthOff = Math.floor($(qvElement).width()/2);
		var modalHeightOff = Math.floor($(qvElement).height()/2);
	}
	var topPosition = scrollTop + (windowHeight/2) - modalHeightOff;
	var leftPosition = (windowWidth/2) - modalWidthOff;
	$(qvElement).html(data);
	$(qvElement).jqm(
			{
				modal:true,
				closeClass:'quickViewClose',
				onShow: function(h) {
					h.w.fadeIn(888);
				},
				onHide: function(h) { 
				      h.o.remove(); // remove overlay
				      h.w.fadeOut(888); // hide window
				    }
			});
	$(qvElement).jqmShow();
	$(qvElement).css("top", (topPosition) + "px");
	$(qvElement).css("left", (leftPosition) + "px");
	$(window).scrollTop(scrollTop);
}

function synchronizeDivHeights(selector){
	var height = 0;
	$(selector).each(function(index,item){
		if ($(item).height()>height){
			height = $(item).height();
		}
	})
	$(selector).css("height", height + "px");
}

function ajaxPaging(){
	//get the URL
	var url = $(this).attr('href');
	//get the replacement id
	var ul = $(this).parents("ul")[0];
	var replacementId = $(ul).attr('replaceId');
	//call the ajax load based on the URL
	var replaceId = '#' + replacementId;
	showLoading();
	$(replaceId).load(url, function(){hideLoading();}).error(function(a,b,c){
		alert('there was an error: ' + a);
	});
	return false;
}

function ajaxSorting(){
	var url = $(this).attr('href');
	var sortOption = $(this).val();
	var replaceId = $(this).attr('replaceId');
	var data = {
		sorting: sortOption,
		ajaxURL: url,
		ajaxReplace: replaceId,
		size: 5
	}
	replaceId = '#' + replaceId;
	showLoading();
	$(replaceId).load(url,data,function(){hideLoading();}).error(function(a,b,c){
		alert('there was an error: ' + a);
	});
	return false;
	
}

function resizeOnContainer(){
	resizeOnNamedContainer(this, 'div.brandBox');
}

function resizeBrandHeader(){
	resizeOnNamedContainer(this, 'div.containers', true);
}
	
function resizeOnNamedContainer(obj, parentSelector, resizeParent){
	//get element height and width
	var height = $(obj).height();
	var width = $(obj).width();
	
	//get the container height and width
	var container = $(obj).parents(parentSelector)[0];
	var cHeight = $(container).height();
	var cWidth = $(container).width();
	
	
	
	if (height>cHeight || width>cWidth) {
		var hDiff = height / cHeight;
		var wDiff = width / cWidth;
		
		var newHeight = height;
		var newWidth = width;
		
		if (hDiff > 0 || wDiff > 0) {
			if (hDiff > wDiff){
				newHeight = Math.floor(height / hDiff);
				newWidth = Math.floor(width / hDiff);
			} else if (wDiff > hDiff){
				newHeight = Math.floor(height / wDiff);
				newWidth = Math.floor(width / wDiff);
			}
			
		}
		$(obj).width(newWidth);
		$(obj).height(newHeight);
	} else {
		if (resizeParent){
			$(container).width(width);
			$(container).height(height);
		}
	}
}

function showLoading() {
  $("#loading").show();
}

function hideLoading() {
  $("#loading").hide();
}

function imgError(source){
	var className = source.className;
	if (className == 'largeProductView') {
		source.src = "/pal/images/noimagelarge.png";
	} else {
    	source.src = "/pal/images/noimage.png";
	}
    source.onerror = "";
    return true;
} 
