/*ow
Free version provided by Gproxy, commercial version without Gproxy link are available at www.gproxy.com

Owner: Gproxy Design Inc.
Copyright (c) 2008, Gproxy Design Inc. All rights reserved.

Agreement
Redistribution and use of this software in source and binary forms, with or 
without modification, are permitted provided that all of the following 
conditions are met:
(1) Redistributions of source code must retain:
 (i) the above copyright notice,
  (ii) this list of redistribution conditions, and 
  (iii) the disclaimer appearing below.
  (iiii) the logos, hyperlinks or publicity provided in the original version.
(2) Redistributions in binary form must retain: 
 (i) the above copyright notice, 
 (ii) this list of redistribution conditions, and 
 (iii) the disclaimer appearing below in the documentation and/or any other 
 materials provided with the distribution.
 (iiii) the logos, hyperlinks or publicity provided in the original version.
 
(3) Neither the name of the Gproxy Design nor the names of its 
 contributors may be used to endorse or promote products derived from this 
 software without specific prior written permission. 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
 * Company  Gproxy Design Inc - www.gproxy.com 
  
 * Name of Script (freezoom.js)
  
 * Date   08/11/2008
  
 * Version  1.0
  
 * Type   Client
  
 * Sub-Type  Item Template
  
 * Categories  Zoom the item image
  
 * Description: When you click at the item image, show's a pop up with the image zoomed, and, when you roll over the mouse over the image, you will see the image magnified like a magnifier glass.
 
 * NetSuite Ver. 11.0.4 or later 
 * 
 * Installation: 
 			-You have to call the function zoom(); in the link to zoom the image, this could be the item image itself, or a link.
			-Item template "add to head" field, yo have to call this script flie: <script type="text/javascript" src="/site/freezoom.js"></script>
			-Item temaple HTML: you will use severals divisions (DIV)
				-> zoomPopUp, contains the entire zoom
				-> zoomTitle, contains zoom  name and the close link
				-> other 2 divs that will display the large image and the zoom it self
 * 
 * 
 * License   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 *   EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 *   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 *   THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 *   OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 *   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 *   TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *   COMMERCIAL VERSIONS WITH MORE FUNCTIONS and WITHOUT LOGO, WATERMARKS OR PUBLICITY ARE AVAILABLE CONTACT WWW.GPROXY.COM FOR DETAILS
**/

// Use the following variables to set the zoom appearance
var zoomw=200; // zoom width 
var zoomh=200; // zoom height
var defzoomamount=3;

var zoomamountstep=1.2;
var zoomsizemin=1000;
var zoomsizemax=100000;
var zoomsizestep=1.2;
var zoomamountmin=1;
var zoomamountmax=12;

var omo="";

function zoom_set(evt) {
 var evt = evt?evt:window.event?window.event:null; if(!evt){ return;}
 if(zoomid=='' || parseInt(document.getElementById('zoomid_container').style.width) == 0) {return true;}
 if(evt.keyCode==37 || evt.keyCode==100) {zoomw/=zoomsizestep; zoomh/=zoomsizestep; if(zoomw*zoomh<zoomsizemin) {zoomh=Math.sqrt(zoomsizemin/zoomratio); zoomw=zoomh*zoomratio;} zoom_init(); zoom_move(); return;} //left
 if(evt.keyCode==39 || evt.keyCode==102) {
  zoomw*=zoomsizestep; zoomh*=zoomsizestep;
  if(zoomw*zoomh>zoomsizemax) {zoomh=Math.sqrt(zoomsizemax/zoomratio); zoomw=zoomh*zoomratio;}
  if(zoomw>objw) {zoomw=objw; zoomh=objw/zoomratio;}
  else if(zoomh>objh) {zoomh=objh; zoomw=objh*zoomratio}
  zoom_init(); zoom_move(); return;
 } //right
 if(evt.keyCode==40 || evt.keyCode==98)  {zoomamount/=zoomamountstep; if(zoomamount<zoomamountmin) {zoomamount=zoomamountmin;} zoom_init(); zoom_move(); return;} //down
 if(evt.keyCode==38 || evt.keyCode==104) {zoomamount*=zoomamountstep; if(zoomamount>zoomamountmax) {zoomamount=zoomamountmax;} zoom_init(); zoom_move(); return;} //up
 return;
}

function zoom_init() {
 document.getElementById('zoomid_clip').style.width=objw*zoomamount+'px';
 document.getElementById('zoomid_clip').style.height=objh*zoomamount+'px';
}

function zoom_move(evt) {
 if(typeof(evt) == 'object') {
  var evt = evt?evt:window.event?window.event:null; if(!evt){ return;}
  if(evt.pageX) {
   xx=evt.pageX - ffox;
   yy=evt.pageY - ffoy;
  } else {
   if(typeof(document.getElementById(zoomid)+1) == 'number') {return true;} //mert az ie egy ocska kurva
//   xx=evt.x - ieox;
//   yy=evt.y - ieoy;
   xx=evt.clientX - ieox;
   yy=evt.clientY - ieoy;
  }
 } else {
  xx=lastxx; yy=lastyy;
 }
 lastxx=xx; lastyy=yy;
 document.getElementById('zoomid_clip').style.margin=((yy-zoomh/2 > 0)?(zoomh/2-yy*zoomamount):(yy*(1-zoomamount)))+'px 0px 0px '+((xx-zoomw/2 > 0)?(zoomw/2-xx*zoomamount):(xx*(1-zoomamount)))+'px';
 document.getElementById('zoomid_container').style.margin=((yy-zoomh/2 > 0)?(yy-zoomh/2):(0))+'px 0px 0px '+((xx-zoomw/2 > 0)?(xx-zoomw/2):(0))+'px';

 w2=((xx+zoomw/2<objw)?((zoomw/2<xx)?(zoomw):(zoomw/2+xx)):(zoomw/2+objw-xx)); if(w2<0) {w2=0;} document.getElementById('zoomid_container').style.width=w2+'px';
 h2=((yy+zoomh/2<objh)?((zoomh/2<yy)?(zoomh):(zoomh/2+yy)):(zoomh/2+objh-yy)); if(h2<0) {h2=0;} document.getElementById('zoomid_container').style.height=h2+'px';
 return false;
}

// function zoom_off() {
// var d = document.getElementById("zoom_D");
// if (d.hasAttribute("onmouseover")) {
//  d.setAttribute("onmouseover","zoom_move(event)");
// }
//
// if(zoomid != '') {
//  document.getElementById('zoomid_container').style.width='0px';
//  document.getElementById('zoomid_container').style.height='0px';
// }
// zoomid='zoom_D';
// }

function zoom_off() {
  var d = document.getElementById("zoom_D");
  if (navigator.appName!="Microsoft Internet Explorer"){   
    if (d.hasAttribute("onmouseover")) {
     d.setAttribute("onmouseover","zoom_move(event)");
    }
  }else{
	 d.onMouseOver="onmouseover('zoom_move(event)')";
  }

 if(zoomid != '') {
  document.getElementById('zoomid_container').style.width='0px';
  document.getElementById('zoomid_container').style.height='0px';
 }
 zoomid='zoom_D';
}

function countoffset() {
 ieox=0;ieoy=0;
 for(zmi=0;zmi<50;zmi++) {
  zme='document.getElementById(zoomid)';
  for(zmj=1;zmj<=zmi;zmj++) {
   zme+='.offsetParent';
  }
  if(eval(zme)+1 == 1) {zmi=100} else {ieox+=eval(zme+'.offsetLeft'); ieoy+=eval(zme+'.offsetTop');}
 }
 ffox=ieox;
 ffoy=ieoy;
 if(document.documentElement && document.documentElement.scrollTop) {
  ieox-=document.documentElement.scrollLeft;
  ieoy-=document.documentElement.scrollTop;
 } else {
  ieox-=document.body.scrollLeft;
  ieoy-=document.body.scrollTop;
 }
}

function zoom_on(evt,ow,oh,lowres,highres) {
// thisid=lowres+highres+ow+oh;
// thisid='zoom_'+thisid.replace(/[^a-z0-9]/ig,'')
 thisid="zoom_D"
 if(zoomid==thisid) {return;}
 if(typeof(highres) == typeof(nemistudom)) {highres=lowres;}
 var evt = evt?evt:window.event?window.event:null; if(!evt){ return;}
 zoomamount=defzoomamount;

 // added by Kathy Schmidt to dynamically change the amount of zoom (defzoomamount) 9/22/09
 var highresW=getImgWidth(highres);
 if (highresW<=700)
 {
  zoomamount=1;
 }
 else if (highresW<=1300)
 {
  zoomamount=2;
 }
 // end added by Kathy Schmidt

 objw=ow;
 objh=oh;
 zoomid=thisid;
 if(evt.pageX) {
  evt.target.parentNode.id=thisid;
  countoffset();
  lastxx=evt.pageX - ffox; //mert a ff egy ocska kurva
  lastyy=evt.pageY - ffoy;
 } else {
  evt.srcElement.parentNode.id=thisid;
  countoffset();
//  lastxx=evt.x - ieox; //mert az ie egy ocska kurva
//  lastyy=evt.y - ieoy;
  lastxx=evt.clientX - ieox;
  lastyy=evt.clientY - ieoy; 
 }
 document.getElementById(thisid).style.width=ow+'px';
 document.getElementById(thisid).style.height=oh+'px';
 document.getElementById(thisid).style.overflow='hidden';
 
 document.getElementById(thisid).innerHTML='<div style="position:absolute; overflow:hidden; border:#CCCCCC solid 1px;" id="zoomid_container" onmousemove="zoom_move(event);" onmouseout="zoom_off()"><img src="'+highres+'" alt="" id="zoomid_clip" class="zero"></div><img src="'+lowres+'" id="zoomid_pic" alt="" class="zero" style="width:'+ow+'px; height: '+oh+'px;">';
 if(zoomw>objw) {zoomw=objw; zoomh=objw/zoomratio;}
 else if(zoomh>objh) {zoomh=objh; zoomw=objh*zoomratio}
 zoom_init();
 zoom_move('');
 if(document.all) {
  document.onkeydown=zoom_set;
 } else {
  window.captureEvents(Event.KEYDOWN);
  window.onkeydown = zoom_set;
 }
 return false;
}

var zoomamount=defzoomamount; var objw;var objh;var zoomid=''; var zoomratio=zoomw/zoomh; var ieox=0; var ieoy=0; var ffox=0; var ffoy=0;

// confirm there's at least one image to zoom
if(document.getElementById('linkZoomGallery')) {
	var hasEnlarged = false;
	for (h=0;h<ZoomImg.length;h++) {
		if (ZoomImg[h]!="")
			hasEnlarged = true;
	}
	document.getElementById('linkZoomGallery').style.display=(hasEnlarged)?'block':'none'; 	
}

var current_image=0;															// global var stores image being showed
function displayImage(id) {
	if (id < NewImg.length) {
		current_image = id;														// keeps 'num id' of image showed
		document.getElementById('mainImage').src = NewImg[id]+resizeURL; 		// changes main image and resizes it
		for (i=0; i < NewImg.length; i++)	{
			document.getElementById('thumb_'+i).className = (current_image!=i)?'thumb-off':'thumb-on';
		}
	}
}

function placeBlur(){
	if (!document.getElementById("BlurWrapper")) { 				// check if the black background exist
		var div=document.createElement("div");					// create the blur element
		div.id="BlurWrapper";
		//create and place a blur div (with z-index:9999) inside the blur wrapper 
		div.innerHTML= "<div id=\"Blur\" style=\"position:absolute;background:#000;opacity: 0.30;-moz-opacity: .3;filter: alpha(opacity=30);width:100%;height:500px;top:0;left:0;right:0;bottom:0;z-index:9999;border:0 \"> </div>";
		//more settings to put the blur in the right position in IE
	 	if (document.all) 
			div.style.width=(document.body.offsetWidth-20)+"px";
		var tt=document.createTextNode(" ");
		div.appendChild(tt);
		document.getElementsByTagName("body")[0].appendChild(div)		
		window.scroll(0,0)
		resizeBlur();
		addEvent(window, 'resize', resizeBlur, false);
	}
}
function resizeBlur(){
	var pageDimentions = getPageSize();
	document.getElementById("Blur").style.width=pageDimentions[0]+'px';
	document.getElementById("Blur").style.height=pageDimentions[1]+'px';
}
function removeBlur(){
	if (document.getElementById("BlurWrapper")) {
		popOBJ(document.getElementById("BlurWrapper"));		// here the blur is removed 
		removeEvent(window, 'resize', resizeBlur, false);
	}
}

function closePopUpImg(){
	document.getElementById('zoomPopUp').style.display = 'none';
	removeBlur();
	showInterferingElements();
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
function addEvent(obj, evType, fn, useCapture){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.attachEvent){
    return (obj.attachEvent("on"+evType, fn));
  } else 
	return false;
}
function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    return (obj.detachEvent("on"+evType, fn));
  } else 
	return false;
} 

function popOBJ(pObj){
	if (pObj)
		pObj.parentNode.removeChild(pObj);
}

function hideInterferingElements(){
	displaySelectBoxes(false);
}

function showInterferingElements(){
	displaySelectBoxes(true);
}

function displaySelectBoxes(state){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.display = (state)?"":"none";
	}
}

function zoom(){
  document.getElementById("zoomPopUp").style.display="";
  hideInterferingElements();
  placeBlur();
}


// Is this code below needed .... Gerald says it is already called in the theme
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
// Is this code above needed .... Gerald says it is already called in the theme


function getImgHeight(imgSrc){
   var newImg = new Image();
   newImg.src = imgSrc;
   var height = newImg.height;
   return height;
}
function getImgWidth(imgSrc){
   var newImg = new Image();
   newImg.src = imgSrc;
   var width = newImg.width;
   return width;
}
function setdivZoom(smallImgsrc,bigImgsrc){
	imageB = new Image();
	imageB.src = bigImgsrc;
	imageS = new Image();
	imageS.src = smallImgsrc;
	var srtPre = "'" + bigImgsrc + "','" + smallImgsrc + "'";
	MM_preloadImages(srtPre);
	var imgH=getImgHeight(smallImgsrc);
	var imgW=getImgWidth(smallImgsrc);
	if (imgW==0){
		setTimeout("setdivZoom('"+smallImgsrc+"','"+bigImgsrc+"')",1000)
	}
	omo="onmouseover=\"zoom_on(event,"+imgW+","+imgH+",'"+smallImgsrc+"', '"+bigImgsrc+"');";
	var divzf1="<div style=\"float:left;\" onmouseover=\"zoom_on(event,"+imgW+","+imgH+",'"+smallImgsrc+"', '"+bigImgsrc+"');\" onmousemove=\"zoom_move(event);\" onmouseout=\"zoom_off();\"><img src=\""+smallImgsrc+"\" alt=\"Zoomed Image\" style=\"padding:0px;margin:0px;border:none;\"></div>";
	divZfl=document.getElementById("zcont").innerHTML=divzf1;
	document.getElementById("zoomPopUp").style.width=imgW+"px";
}