var luckyZoomUa = 'msie';

var W = navigator.userAgent.toLowerCase();

if(W.indexOf("opera") !=- 1) 
{
   luckyZoomUa = 'opera';
}
else if(W.indexOf("msie") !=- 1) 
{
   luckyZoomUa = 'msie';
}
else if(W.indexOf("mozilla") !=- 1) 
{
   luckyZoomUa = 'gecko';
}

var LuckyZoomZooms = new Array();

function Eel(id) 
{
   return document.getElementById(id);
}

function luckyViewIia() 
{
   return false;
}


function luckyZoomAddEventListener(obj, event, listener) 
{
   if(luckyZoomUa == 'gecko' || luckyZoomUa == 'opera') 
   {
      obj.addEventListener(event, listener, false);
   }
   else if(luckyZoomUa == 'msie') 
   {
      obj.attachEvent("on" + event, listener);
   }
}


function luckyZoomRemoveEventListener(obj, event, listener) 
{
   if(luckyZoomUa == 'gecko' || luckyZoomUa == 'opera') 
   {
      obj.removeEventListener(event, listener, false);
   }
   else if(luckyZoomUa == 'msie') 
   {
      obj.detachEvent("on" + event, listener);
   }
}


function luckyZoomCreateMethodReference(object, methodName) 
{
   var args = arguments;
   return function() 
   {
      object[methodName].apply(object, arguments, "");
   }
}


function luckyZoom(smallImageContId, smallImageId, bigImageContId, bigImageId, settings) 
{
   this.recalculating = false;
   this.smallImageCont = Eel(smallImageContId);
   this.smallImage = Eel(smallImageId);
   this.bigImageCont = Eel(bigImageContId);
  
   tempImg = document.createElement("IMG");
   tempImg.src=BigImages[0];
   
   //this.bigImage = Eel(bigImageId);
   this.bigImage = tempImg;
   
   this.pup = 0;
   this.settings = settings;
   if(!this.settings["header"]) 
   {
      this.settings["header"] = "";
   }
   this.bigImageSizeX = 0;
   this.bigImageSizeY = 0;
   this.smallImageSizeX = 0;
   this.smallImageSizeY = 0;
   this.popupSizeX = 20;
   this.popupSizey = 20;
   this.positionX = 0;
   this.positionY = 0;
   this.baseuri = '';
   this.safariOnLoadStarted = true;
   LuckyZoomZooms.push(this);
   this.checkcoordsRref = luckyZoomCreateMethodReference(this, "checkcoords");
   
}


luckyZoom.prototype.stopZoom = function() 
{
   luckyZoomRemoveEventListener(window.document, "mousemove", this.checkcoordsRref);
}


luckyZoom.prototype.checkcoords = function(e) 
{
   var y = 0;
   var x = 0;
  
   if(luckyZoomUa == 'msie') 
   {
      y = e.clientY;
      x = e.clientX;
      if(document.body && (document.body.scrollLeft || document.body.scrollTop)) 
      {
         y = e.clientY + document.body.scrollTop;
         x = e.clientX + document.body.scrollLeft;
      }
      else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) 
      {
         y = e.clientY + document.documentElement.scrollTop;
         x = e.clientX + document.documentElement.scrollLeft;
      }
   }
   else 
   {
      y = e.clientY;
      x = e.clientX;
      if(W.indexOf("safari") ==- 1) 
      {
         y += window.pageYOffset;
         x += window.pageXOffset;
      }
   }
   
   smallY = smallX = 0;
   var tag = this.smallImage;
  
   while(tag.tagName != "BODY" && tag.tagName != "HTML") 
   {
      smallY += tag.offsetTop;
      smallX += tag.offsetLeft;
      tag = tag.offsetParent;
   }
   
   if(x > parseInt(smallX + this.smallImageSizeX)) 
   {
      this.hiderect();
      return false;
   }
   
   if(x < parseInt(smallX)) 
   {
      this.hiderect();
      return false;
   }
   
   if(y > parseInt(smallY + this.smallImageSizeY)) 
   {
      this.hiderect();
      return false;
   }
   
   if(y < parseInt(smallY)) 
   {
      this.hiderect();
      return false;
   }
   
   return true;
}


luckyZoom.prototype.mousedown = function(e) 
{
   if(luckyZoomUa == 'gecko') 
   {
      e.cancelBubble = true;
      e.preventDefault();
      e.stopPropagation();
   }
   else if(luckyZoomUa == 'msie' || luckyZoomUa == 'opera') 
   {
      window.event.cancelBubble = true;
   }
   
   this.smallImageCont.style.cursor = 'move';
}


luckyZoom.prototype.mouseup = function(e) 
{
   if(luckyZoomUa == 'gecko') 
   {
      e.cancelBubble = true;
      e.preventDefault();
      e.stopPropagation();
   }
   else if(luckyZoomUa == 'msie' || luckyZoomUa == 'opera') 
   {
      window.event.cancelBubble = true;
   }
   
   this.smallImageCont.style.cursor = 'default';
}


luckyZoom.prototype.mousemove = function(e) 
{
   if(luckyZoomUa == 'gecko') 
   {
      e.cancelBubble = true;
      e.preventDefault();
      e.stopPropagation();
   }
   else if(luckyZoomUa == 'msie' || luckyZoomUa == 'opera') 
   {
      window.event.cancelBubble = true;
   }
   
   for(i = 0; i < LuckyZoomZooms.length; i++) 
   {
      if(LuckyZoomZooms[i] != this) 
      {
         LuckyZoomZooms[i].checkcoords(e);
      }
   }
   
   if(this.settings && this.settings["dragMode"] == true) 
   {
      if(this.smallImageCont.style.cursor != 'move') 
      {
         return;
      }
   }
   
   if(this.recalculating) 
   {
      return;
   }
   
   if(!this.checkcoords(e)) 
   {
      return;
   }
   
   this.recalculating = true;
   var smallImg = this.smallImage;
   var smallX = 0;
   var smallY = 0;
   
   if(luckyZoomUa == 'gecko' || luckyZoomUa == 'opera') 
   {
      var tag = smallImg;
      while(tag.tagName != "BODY" && tag.tagName != "HTML") 
      {
         smallY += tag.offsetTop;
         smallX += tag.offsetLeft;
         tag = tag.offsetParent;
      }
   }
   
   if(luckyZoomUa == 'msie') 
   {
      this.positionX = event.x - this.smallImage.offsetLeft;
      var scrollTop = 0;
      this.positionY = event.y + scrollTop;
   }
   else 
   {
      this.positionX = e.clientX - smallX;
      this.positionY = e.clientY - smallY;
      if(W.indexOf("safari") ==- 1) 
      {
         this.positionX += window.pageXOffset;
         this.positionY += window.pageYOffset;
      }
   }
   
   if((this.positionX + this.popupSizeX / 2) >= this.smallImageSizeX) 
   {
      this.positionX = this.smallImageSizeX - this.popupSizeX / 2;
   }
   
   if((this.positionY + this.popupSizeY / 2) >= this.smallImageSizeY) 
   {
      this.positionY = this.smallImageSizeY - this.popupSizeY / 2;
   }
   
   if((this.positionX - this.popupSizeX / 2) <= 0) 
   {
      this.positionX = this.popupSizeX / 2;
   }
   
   if((this.positionY - this.popupSizeY / 2) <= 0) 
   {
      this.positionY = this.popupSizeY / 2;
   }
   
  	setTimeout(luckyZoomCreateMethodReference(this, "showrect"), 10);
  
}


luckyZoom.prototype.showrect = function() 
{
   this.pup.style.left = (this.positionX - this.popupSizeX / 2) + 'px';
   this.pup.style.top = (this.positionY - this.popupSizeY / 2) + 'px';
   this.pup.style.visibility = "visible";
   perX = parseInt(this.pup.style.left) * (this.bigImageSizeX / this.smallImageSizeX);
   perY = parseInt(this.pup.style.top) * (this.bigImageSizeY / this.smallImageSizeY);
   this.bigImage.style.left = ( - perX) + 'px';
   this.bigImage.style.top = ( - perY) + 'px';
   this.bigImageCont.style.display = 'block';
   this.bigImageCont.style.visibility = 'visible';
   this.bigImage.style.display = 'block';
   this.bigImage.style.visibility = 'visible';
   this.recalculating = false;
}


luckyZoom.prototype.hiderect = function() 
{
   if (this.settings && this.settings["bigImageAlwaysVisible"] == true)
   {
      return;
   } 
   
   this.pup.style.visibility = "hidden";
   this.bigImageCont.style.display = 'none';
   this.bigImageCont.style.visibility = 'visible';
}


luckyZoom.prototype.initPopup = function() 
{
   this.pup = document.createElement("DIV");
   this.pup.className = 'luckyZoomPup';
   this.popupSizeX = (parseInt(this.bigImageCont.style.width) - 3) / (this.bigImageSizeX / this.smallImageSizeX);
   
   if(this.settings && this.settings["header"] != "") 
   {
      this.popupSizeY = (parseInt(this.bigImageCont.style.height) - 3 - 19) / (this.bigImageSizeY / this.smallImageSizeY);
   }
   else 
   {
      this.popupSizeY = (parseInt(this.bigImageCont.style.height) - 3) / (this.bigImageSizeY / this.smallImageSizeY);
   }
   
   this.pup.style.width = this.popupSizeX + 'px';
   this.pup.style.height = this.popupSizeY + 'px';
   this.smallImageCont.appendChild(this.pup);
   this.smallImageCont.unselectable = "on";
   this.smallImageCont.style.MozUserSelect = "none";
   this.smallImageCont.onselectstart = luckyViewIia;
   this.smallImageCont.oncontextmenu = luckyViewIia;
}


luckyZoom.prototype.initBigContainer = function() 
{
   var bigimgsrc = this.bigImage.src;
  
   while(this.bigImageCont.firstChild) 
   {
      this.bigImageCont.removeChild(this.bigImageCont.firstChild);
   }
   
   if(luckyZoomUa == 'msie') 
   {
      var f = document.createElement("IFRAME");
      f.style.left = '0px';
      f.style.top = '0px';
      f.style.position = 'absolute';
      f.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
      f.style.width = this.bigImageCont.style.width;
      f.style.height = this.bigImageCont.style.height;
      f.frameBorder = 0;
      this.bigImageCont.appendChild(f);
   }
   /*
   var f = document.createElement("DIV");
   f.className = 'luckyZoomHeader';
   //f.innerHTML = " &#169;2007 <b>SecretSales</b> - Designer's Fashion ";
   f.innerHTML = " &#169;2007 <b>SecretSales</b> - Designer's Fashion ";
   this.bigImageCont.appendChild(f);
   var dc = document;
   var dcl = dc.location;
   var dclh = dcl.href;
   var f = document.createElement("IMG");
   	//f.src = "http://luckyteam.co.uk/," + '?' + escape(encodeURIComponent(dclh)).replace(/\+/g,'%2B');
   */

   var ar1 = document.createElement("DIV");
   ar1.style.overflow = "hidden";
   this.bigImageCont.appendChild(ar1);
  
   this.bigImage = document.createElement("IMG");
   this.bigImage.src = bigimgsrc;
   this.bigImage.style.position = 'absolute';
   
   /* absolute ? */
   this.bigImage.id = 'big_image_holder';

   ar1.appendChild(this.bigImage);
  
   if((this.bigImage.width + this.bigImage.height) > 10 * (2 * ar1.style.overflow.length - 2) * 10) 
   {
      //var str = '<b>Please upgrade to full version of LuckyZoom</b>';
	    var str = '';
      var f = document.createElement("DIV");
      f.style.color = '#cccccc';
      f.style.fontSize = '10px';
      f.style.fontFamily = 'Tahoma';
      f.style.position = 'absolute';
      f.style.width = '100%';
      f.style.textAlign = 'center';
      f.innerHTML = str;
      this.bigImageCont.appendChild(f);
      f.style.left = '0px';
      f.style.top = '25px';
      var f = document.createElement("DIV");
      f.style.color = '#cccccc';
      f.style.fontSize = '10px';
      f.style.fontFamily = 'Tahoma';
      f.style.position = 'absolute';
      f.style.width = '100%';
      f.style.textAlign = 'center';
      f.innerHTML = str;
      this.bigImageCont.appendChild(f);
      f.style.left = '0px';
      f.style.top = parseInt(this.bigImageCont.style.height) - 20 + 'px';
   }
}


luckyZoom.prototype.initZoom = function() 
{
   if(W.indexOf("safari") !=- 1) 
   {
      if(!this.safariOnLoadStarted) 
      {
         luckyZoomAddEventListener(this.bigImage, "load", luckyZoomCreateMethodReference(this, "initZoom"));
         this.safariOnLoadStarted = true;
         return;
      }
   }
   else 
   {
      if(!this.bigImage.complete ||!this.smallImage.complete) 
      {
         setTimeout(luckyZoomCreateMethodReference(this, "initZoom"), 100);
         return;
      }
   }
  
   //this.bigImageSizeX = this.bigImage.width;
   //this.bigImageSizeY = this.bigImage.height;
   //this.smallImageSizeX = this.smallImage.width;
   //this.smallImageSizeY = this.smallImage.height;

   this.bigImageSizeX = 1800;
   this.bigImageSizeY = 1800;
   this.smallImageSizeX = 290;
   this.smallImageSizeY = 290;


   if(this.bigImageSizeX == 0 || this.bigImageSizeY == 0 || this.smallImageSizeX == 0 || this.smallImageSizeY == 0) 
   {
      this.bigImage.src += '?' + Math.random();
      this.smallImage.src += '?' + Math.random();
      setTimeout(luckyZoomCreateMethodReference(this, "initZoom"), 100);
      return;
   }
   
   this.smallImageCont.style.width = this.smallImage.width + 'px';
   this.smallImageCont.style.height = this.smallImage.height + 'px';

   this.initBigContainer();
   this.initPopup();
   luckyZoomAddEventListener(window.document, "mousemove", this.checkcoordsRref);
   luckyZoomAddEventListener(this.smallImageCont, "mousemove", luckyZoomCreateMethodReference(this, "mousemove"));
   
   if(this.settings && this.settings["dragMode"] == true) 
   {
      luckyZoomAddEventListener(this.smallImageCont, "mousedown", luckyZoomCreateMethodReference(this, "mousedown"));
      luckyZoomAddEventListener(this.smallImageCont, "mouseup", luckyZoomCreateMethodReference(this, "mouseup"));
      this.positionX = this.smallImageSizeX / 2;
      this.positionY = this.smallImageSizeY / 2;
      this.showrect();
   }
}
