﻿function findPos(obj)
{
	var curleft = 0;
	var curtop = 0;
	while (obj.offsetParent) {
		curleft += obj.offsetLeft;
		curtop += obj.offsetTop;
		obj = obj.offsetParent;
		if(obj.scrollTop){
		    curtop -= obj.scrollTop;
		    curleft -= obj.scrollLeft;
		}
	}
	return [curleft,curtop];
}

function SetOpacity(opacity, ob){
    ob.style.opacity = (opacity / 100); 
    ob.style.MozOpacity = (opacity / 100); 
    ob.style.KhtmlOpacity = (opacity / 100); 
    ob.style.filter = "alpha(opacity=" + opacity + ")"; 
}

function clone(deep) {
  var objectClone = new this.constructor();
  for (var property in this)
    if (!deep)
      objectClone[property] = this[property];
    else if (typeof this[property] == 'object')
      objectClone[property] = this[property].clone(deep);
    else
      objectClone[property] = this[property];
  return objectClone;
}

//Object.prototype.clone = clone;

var GlobalStepInterval = 30;
var globalrunning = false;
var ImgCheckInterval = 500;

function GlobalStart(){
    if(globalrunning == true) return;
    GlobalStep();
}

function GlobalStep(){
    var cont=false;
    if(Gallery.RunStep() == true) cont = true;
    if(Dimmer.RunStep() == true) cont = true;
    if(cont == true){
        window.setTimeout("GlobalStep();",GlobalStepInterval);
    }else{
        if(Gallery.ShowImage() == false){
            window.setTimeout("GlobalStep();",ImgCheckInterval);
        }
    }
    globalrunning = cont;
}

var Dimmer=new Object();

function InitDimmer(){
    Dimmer.target=0;
    Dimmer.current=0;
    Dimmer.rate=0;
    Dimmer.stepinterval=GlobalStepInterval;
    Dimmer.fadeob;
    Dimmer.hidewhenzero=true;

    Dimmer.Run=function(destalpha, eta){
        this.fadeob.style.display='block';
        this.target=destalpha;
        this.rate=Math.abs(this.target-this.current)*this.stepinterval/eta;
        //this.RunStep();
        GlobalStart();
    }
    Dimmer.ShiftAlpha=function(){
        var diff;
        diff=this.target-this.current;
        diff=Math.max(diff,-this.rate);
        diff=Math.min(diff,this.rate);
        if(Math.abs(diff) < 0.001) return false;
        this.current+=diff;
        SetOpacity(Math.round(this.current),this.fadeob);
        return true;
    }
    Dimmer.RunStep = function(){
        var cont=false;
        if(this.ShiftAlpha() == true) cont = true;
        
        if(this.current < 0.001) this.fadeob.style.display='none'
        return cont;
        /*if(cont == true){
            var self = this;
            window.setTimeout(function(){ self.RunStep(); },this.stepinterval);
        }else{
            if(this.current < 0.001) this.fadeob.style.display='none';
        }*/
    }
}

var Gallery=new Object();

function InitGallery(){
    Gallery=new Object();
    Gallery.runfade=0; //0 - no, 1 - fade in, 2 - fade out
    Gallery.divxc=0;
    Gallery.divyc=0;
    Gallery.divwc=0;
    Gallery.divhc=0;

    Gallery.divx=10;
    Gallery.divy=10;
    Gallery.divw=10;
    Gallery.divh=10;

    Gallery.stepx=10;
    Gallery.stepy=10;
    Gallery.stepw=10;
    Gallery.steph=10;

    Gallery.imgdiv=null;
    Gallery.divOuter=null;
    Gallery.divOuterOuter=null;
    Gallery.img=null;
    Gallery.tblimg=null;
    Gallery.limg=null;
    Gallery.loadimg=null;
    Gallery.loadx=1;
    Gallery.loady=1;
    Gallery.controls=null;
    Gallery.controls2=null;
    Gallery.stepinterval=GlobalStepInterval;
    Gallery.movetime=1000;
    Gallery.movespeed=15;
    
    Gallery.DoneHide=false;

    Gallery.ShowImage = function(){
        if(this.DoneHide){
            this.divOuterOuter.style.display='none';
            return true;
        }else{
            this.controls.style.display='inline';
            this.controls2.style.display='inline';
        }
        if(this.limg.complete == true){
            this.img.src = this.limg.src;
            this.img.style.display='inline';
            this.tblimg.style.display='inline';
            this.loadimg.style.display='none';
            return true;
        }
        return false;
    }

    Gallery.WinWidth = function (){
      var myWidth = 0;
      if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        myWidth = window.innerWidth;
      } else if( document.documentElement && ( document.documentElement.clientWidth  ) ) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
      } else if( document.body && ( document.body.clientWidth  ) ) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
      }
      
      return myWidth;
    }

    Gallery.WinHeight = function(){
      var myHeight = 0;
      if( typeof( window.innerHeight ) == 'number' ) {
        //Non-IE
        myHeight = window.innerHeight;
      } else if( document.documentElement && ( document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        myHeight = document.documentElement.clientHeight;
      } else if( document.body && ( document.body.clientHeight ) ) {
        //IE 4 compatible
        myHeight = document.body.clientHeight;
      }
      
      return myHeight;
    }

    Gallery.SetMoveTime = function(){
        this.movetime=this.stepinterval;
        this.movetime=Math.max(this.movetime,Math.abs(this.divx-this.divxc)*this.stepinterval/this.movespeed);
        this.movetime=Math.max(this.movetime,Math.abs(this.divy-this.divyc)*this.stepinterval/this.movespeed);
        this.movetime=Math.max(this.movetime,Math.abs(this.divw-this.divwc)*this.stepinterval/this.movespeed);
        this.movetime=Math.max(this.movetime,Math.abs(this.divh-this.divhc)*this.stepinterval/this.movespeed);
    }

    Gallery.StartMove = function(){
        var steps;
        steps=this.movetime/this.stepinterval;
        this.stepx=Math.abs(this.divxc-this.divx)/steps;
        this.stepy=Math.abs(this.divyc-this.divy)/steps;
        this.stepw=Math.abs(this.divwc-this.divw)/steps;
        this.steph=Math.abs(this.divhc-this.divh)/steps;
        
        //this.RunStep();
        GlobalStart();
    }

    Gallery.DivResize = function(){
        var res=false;
        var diff;
        
        diff=this.divy-this.divyc;
        if(Math.abs(diff) > 0.001){
            diff=Math.max(diff,-this.stepy);
            diff=Math.min(diff,this.stepy);
            this.divyc += diff;
            res=true;
        }
        
        diff=this.divx-this.divxc;
        if(Math.abs(diff) > 0.001){
            diff=Math.max(diff,-this.stepx);
            diff=Math.min(diff,this.stepx);
            this.divxc += diff;
            res=true;
        }

        diff=this.divw-this.divwc;
        if(Math.abs(diff) > 0.001){
            diff=Math.max(diff,-this.stepw);
            diff=Math.min(diff,this.stepw);
            this.divwc += diff;
            res=true;
        }
        
        diff=this.divh-this.divhc;
        if(Math.abs(diff) > 0.001){
            diff=Math.max(diff,-this.steph);
            diff=Math.min(diff,this.steph);
            this.divhc += diff;
            res=true;
        }
        
        //this.imgdiv.style.top = Math.round(this.divyc) + 'px';
        //this.imgdiv.style.left = Math.round(this.divxc) + 'px';
        
        /*
        this.imgdiv.style.top = '5px';
        this.imgdiv.style.left = '5px';
        this.imgdiv.style.width = Math.round(this.divwc) + 'px';
        this.imgdiv.style.height = Math.round(this.divhc) + 'px';
        
        var outerXPad = (Math.round(this.divwc) + 15)
        var outerYPad = (Math.round(this.divhc) + 10)
        
        this.divOuter.style.top = '10px'
        this.divOuter.style.left = '10px'
        this.divOuter.style.width = outerXPad + 'px';
        this.divOuter.style.height = outerYPad + 'px';
        
        this.divOuterOuter.style.top = Math.round(this.divyc) + 'px';
        this.divOuterOuter.style.left = Math.round(this.divxc) + 'px';
        this.divOuterOuter.style.width = (outerXPad + 22) + 'px';
        this.divOuterOuter.style.height = (outerYPad + 22) + 'px';
        
        */
        
        this.divOuterOuter.style.top = Math.round(this.divyc) + 'px';
        this.divOuterOuter.style.left = Math.round(this.divxc) + 'px';
        this.divOuterOuter.style.width = Math.round(this.divwc + 10) + 'px';
        this.divOuterOuter.style.height = Math.round(this.divhc + 95) + 'px';
        this.imgdiv.style.width = Math.round(this.divwc) + 'px';
        if (this.divhc < 20)
        this.imgdiv.style.height = Math.round(this.divhc) + 'px';
        else
            this.imgdiv.style.height = Math.round(this.divhc)-5 + 'px';
        
        
        //this.loadimg.style.left=Math.floor((this.divwc-this.loadx)/2) + 'px';
        //this.loadimg.style.top=Math.floor((this.divhc-this.loady)/2) + 'px';
        
        return res;
    }

    Gallery.RunStep = function(){
        var cont=false;
        if(this.DivResize() == true) cont = true;
        /*if(cont == true){
            var self = this;
            window.setTimeout(function(){ self.RunStep(); },this.stepinterval);
        }*/
        return cont;
    }
}
