﻿// Detail Product page.
$(document).ready(
	function()
	{ 
		$.setMenuOffset("menuShowPrev",{'eX':0,'eY':14});
		$.setMenuOffset("menuShowNext",{'eX':0,'eY':14});
		$('#browseitems_linkPrev').hover(function(){ $(this).hoverMenu('#menuShowPrev',null,true); },function(){ $(this).hoverMenu('#menuShowPrev',null,false); });
		$('#browseitems_linkNext').hover(function(){ $(this).hoverMenu('#menuShowNext',null,true); },function(){ $(this).hoverMenu('#menuShowNext',null,false); });
		//$('#menuShowPrev').hover(function(){ $('#linkPrev').addClass('menu-item-hover'); },function(){ $('#navlink1').removeClass('menu-item-hover'); });		
		
		// jqModal windows.
		$('#prodimg_window').jqm({ onShow:centerProdimg ,closeClass: 'jqmClose', trigger: '.jqModal', overlay: 30, toTop: true });
		$('.slideImg').hover( function() { hProdimgHover(this); },function(){ });
		$('.clickImg').click( function() { return hProdimgClick(this); });
		$('#modalwindow').jqm({onHide:jqMiframe_closed,trigger: '#jqm1',overlay: 30});//{ajax: '@href', trigger: 'a.modalwin'});
		$('a.modalwin').click( function() { jqMiframe(this.href,'#modalwindow'); return false; });
		if ($.browser.mozilla) { $('#modalwindow').css("position","fixed"); }
	}
);
/* simple image loader */
$.fn.image = function(src, f, a){ 
  return this.each(function(){ 
		$(this).empty().append($("<img />").load(f).attr('src',src));
		if (a!=null) {  $(this).wrapInner(a); }
		//alert($(this).html());
  }); 
}
function displayImage(src,container,loading,a){
	$(container).addClass(loading);
	$(container).image(src,function() { $(container).removeClass(loading); },a);
}
/* end simple image loader */

function hProdimgClick(obj) {
	displayImage(obj.href,'#prodimg_large','image_loading',null);
	return false;
}
function hProdimgHover(obj) {
	var a = null;
	if (obj.href.indexOf("#") < 0) {
		a = $("<a onclick=\"$('#prodimg_window').jqmShow(); return hProdimgClick(this);\"></a>").attr("href",obj.href);
	}
	displayImage(obj.title,'#prodimg_main','image_loading',a);
}
function jqMiframe(href,modalwin) {
	$(modalwin).append('<div class="image_loading" style="height:30px;"><a href="#" class="jqmClose"><img src="/images/icons/cross_small_b.png" width="16" height="16" alt="" style="border:none;vertical-align:middle;" />Cancel &raquo;</a></div>');
	$(modalwin).append($("<iframe frameborder='0'>").attr('src',href).load(function() { $(modalwin+" > div").removeClass('image_loading'); }));
	centerWin(modalwin,true);
	$(modalwin).jqmShow();
}
function jqMiframe_close(modalwin) {
	$(modalwin).jqmHide().empty();
	return false;
}
var jqMiframe_closed=function(hash) { hash.w.fadeOut('1000',function(){ $(hash.w).empty(); hash.o.remove(); }); };
function centerProdimg(h){
	centerWin(h.w,false); 
	//h.w.fadeIn(700,function() { h.w.show(); });
	h.w.show();
}
function centerWin(modalwin,modHeight) {
	var yScrollTop = self.pageYOffset ? self.pageYOffset : (document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : (document.body ? document.body.scrollTop:0));
	if (modHeight && $.browser.mozilla) { yScrollTop=0; } // ff positions 0 as scrolltop.
	$(modalwin).css("top",(yScrollTop + 15) + "px");
	if (modHeight)
	{
		var windowHeight = self.innerHeight ? self.innerHeight : ((document.documentElement && document.documentElement.clientHeight)? document.documentElement.clientHeight : document.body.clientHeight);
		if (windowHeight > 0) { $(modalwin + " > iframe").css("height",(windowHeight - 80) + "px"); }
	}
}
/* switch to print css style sheet */
function switchPrint() {
	// set CSS file.
	if (window.print) { window.print(); }
}
// display video
function displayVideo(obj) {
	if (obj.title!="") {
		var modalwin='#modalwindow';
		var sw = obj.title.split("|");
		$(modalwin).append('<a href="#" class="jqmClose"><img src="/images/icons/cross_small_b.png" width="16" height="16" alt="" style="border:none;vertical-align:middle;" />Close &raquo;</a><br />');
		$(modalwin).append('<div id="display_mvideo_container" style="width:'+sw[2]+'px"><div id="display_mvideo"><a href="http://www.adobe.com/go/getflashplayer" target="_blank">Get flash player</a></div></div>');
		//centerWin(modalwin,false);
		$(modalwin).css({'width':(parseInt(sw[2])+10)+ 'px'});
		$(modalwin).jqmShow();
		
		if (sw[0]=="ytembed")
		{
			swfobject.embedSWF(sw[1],'display_mvideo',sw[2],sw[3],'7',"/js/flashvideo/expressInstall.swf",{},{allowFullScreen:"true",allowscriptaccess:"always",wmode:"opaque"},{});
		}
		else // (sw[0]=="flash360")
		{
			swfobject.embedSWF('/prodimgdetail/'+sw[1],'display_mvideo',sw[2],sw[3],'9',"/js/flashvideo/expressInstall.swf",{},{wmode:"opaque"},{});
		}		
	}
	return false;
}

/* display stock */
function displayStock(webcode,hData){
	$('#detail_stock').slideToggle(800, function() {
		if(this.style.display=='block') {
			$.post('/products/stock.aspx',{WebCode:webcode,h:hData},function(data,st){
				$('#detail_stock').removeClass('image_loading').html(data);
			});
		}
	});
}

// Compare functions.
function getCompare(categoryID,attrib,awebcode) {
	var webcodes = new Array();
	$('input.compare').each(function(index){
		if (this.value==awebcode) { this.checked = true; }
		if (this.checked) { webcodes[webcodes.length] = this.value; }
	});
	if (webcodes.length > 0 && categoryID != "" && attrib!="") {
		buildCompareUrl(categoryID,attrib,webcodes);
	}
}
// Go to CompareUrl.  string categoryID, string attrib, array webcodes
function buildCompareUrl(categoryID,attrib,webcodes) {
	if (categoryID!="" && attrib != "" && webcodes.length > 0)
	{
		var url = "/compare/" + categoryID + "/" + attrib.toLowerCase() + "/" + webcodes.join("-") + ".aspx";
		window.setTimeout(function() {
			window.location.assign(url); },10);
	}
}

