Gallery = function(){};
GalleryStatic = function(){};

GalleryStatic.findPos = function(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];
}

GalleryStatic.SetOpacity = function(opacity, ob){
    ob.style.opacity = (opacity / 100);
    ob.style.MozOpacity = (opacity / 100);
    ob.style.KhtmlOpacity = (opacity / 100);
    ob.style.filter = "alpha(opacity=" + opacity + ")"; 
}

GalleryStatic.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;
}

GalleryStatic.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;
}

GalleryStatic.clone = function(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 = GalleryStatic.clone;

Gallery.prototype.GlobalStepInterval = 30;
Gallery.prototype.globalrunning = false;
Gallery.prototype.ImgCheckInterval = 500;

Gallery.prototype.GlobalStart = function(){
    if(this.globalrunning == true) return;
    this.GlobalStep();
}

Gallery.prototype.GlobalStep = function(){
    var _self=this;
    var cont=false;
    if(this.RunStep() == true) cont = true;
    if(this.dimmer && this.dimmer.RunStep() == true) cont = true;
    if(this.ev.tick) this.ev.tick(this);
    if(cont == true){
        window.setTimeout(function(){_self.GlobalStep();},this.GlobalStepInterval);
    }else{
        if(this.ShowImage() == false){
            window.setTimeout(function(){_self.GlobalStep();},this.ImgCheckInterval);
        }
    }
    this.globalrunning = cont;
}

Dimmer = function(){};

Dimmer.prototype.Init = function(){
    this.target=0;
    this.current=0;
    this.rate=0;
    this.fadeob=null;
    this.hidewhenzero=true;
}

Dimmer.prototype.Run=function(destalpha, eta){
    this.fadeob.style.display='block';
    this.target=destalpha;
    this.rate=Math.abs(this.target-this.current)*this.stepinterval/eta;
}
Dimmer.prototype.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;
    GalleryStatic.SetOpacity(Math.round(this.current),this.fadeob);
    return true;
}
Dimmer.prototype.RunStep = function(){
    var cont=false;
    if(this.ShiftAlpha() == true) cont = true;
    
    if(this.current < 0.001) this.fadeob.style.display='none'
    return cont;
}

Gallery.prototype.Init = function(){
    this.runfade=0; //0 - no, 1 - fade in, 2 - fade out
    this.divxc=0;
    this.divyc=0;
    this.divwc=0;
    this.divhc=0;

    this.divx=10;
    this.divy=10;
    this.divw=10;
    this.divh=10;

    this.stepx=10;
    this.stepy=10;
    this.stepw=10;
    this.steph=10;
    
    this.paddingx=0;
    this.paddingy=0;
    this.minx=0;
    this.miny=0;
    
    this.focusob=null;
    this.imgdiv=null;
    this.img=null;
    this.limg=null;

    this.currentsection=-1;
    this.currentimage=-1;

    this.stepinterval=this.GlobalStepInterval;
    this.movetime=1000;
    this.movespeed=15;
    
    this.fadeamount=60;
    this.fadetime=1;
    this.unfadetime=1;
    
    this.DoneHide=false;
    this.lastsrc=null;
    
    this.dimmer.Init();
    this.dimmer.stepinterval=this.GlobalStepInterval;
    
    this.ev = function(){} //event container
    
    this.enabled = 0;
}

Gallery.prototype.ShowImage = function(){
    if(this.DoneHide){
        if(this.ev.hidealldone) this.ev.hidealldone(this);
        //this.imgdiv.style.display='none';
        //TESTING
        this.imgdiv.style.visibility='hidden';
        return true;
    }
    if(this.limg.complete == true){
        this.img.src = this.limg.src;
        this.img.style.display='inline';
        //this.imgdiv.style.visibility='visible';
        
        if(this.ev.loadcomplete) this.ev.loadcomplete(this);
        try{
            this.FocusDefault();
        }catch(ex){}
        
        return true;
    }
    return false;
}

Gallery.prototype.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.prototype.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.GlobalStart();
}

Gallery.prototype.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.width = Math.round(this.divwc) + 'px';
    this.imgdiv.style.height = Math.round(this.divhc) + 'px';
    
    return res;
}

Gallery.prototype.RunStep = function(){
    var cont=false;
    if(this.DivResize() == true) cont = true;
    return cont;
}

Gallery.prototype.KeyDown = function(e){
    var keynum;
    if(window.event) // IE
	    keynum = e.keyCode;
    else if(e.which) // Netscape/Firefox/Opera
	    keynum = e.which;
	    
	switch(keynum){
	    case 37: //left arrow
	        this.PrevImageSection();
	        return false;
	    case 39: //right arrow
	        this.NextImageSection();
	        return false;
	    case 27: //escape
	        this.Close();
	        return false;
	}
	
	return true;
}

Gallery.prototype.GetFirstFocusable = function(){
    var alist=this.imgdiv.getElementsByTagName('A');
    if(alist.length==0) return null;
    return alist[0];
}

Gallery.prototype.FocusDefault = function(){
    if(this.focusob){
        this.focusob.focus();
    }
}

Gallery.prototype.dimmer = new Dimmer();

//Interface
Gallery.prototype.DoResize = function(){
    if(this.enabled && this.imgdiv.style.visibility!='hidden'){ //this.imgdiv.style.display!='none' //TESTING
        this.divx=Math.max(0,(GalleryStatic.WinWidth()-this.divw)/2);
        this.divy=Math.max(0,(GalleryStatic.WinHeight()-this.divh)/2);
	    this.SetMoveTime();
        this.StartMove();
    }
}
Gallery.prototype.Close = function(){
    if(!this.enabled) return;
    
    this.currentsection=-1;
    this.currentimage=-1;
    
    this.DoneHide=true;
    this.divw=1;
    this.divh=1;
    this.divx=Math.max(0,(GalleryStatic.WinWidth()-this.divw)/2);
    this.divy=Math.max(0,(GalleryStatic.WinHeight()-this.divh)/2);
    this.movetime=500;
    this.StartMove();
    if(this.dimmer) this.dimmer.Run(0,this.unfadetime);
    this.GlobalStart();
    
    if(this.ev.hideallstart) this.ev.hideallstart(this);
}

Gallery.prototype.PrevImageSection = function(src){
    if(this.currentimage==0){
        if(this.currentsection==0) return;
        this.SetImage(this.currentsection-1,this.imagecount[this.currentsection-1]-1,src);
    }else{
        this.PrevImage(src);
    }
}
Gallery.prototype.NextImageSection = function(src){
    if(this.currentimage+1 >= this.imagecount[this.currentsection]){
        this.NextSection(src);
    }else{
        this.NextImage(src);
    }
}
Gallery.prototype.NextImage = function(src){
    this.SetImage(this.currentsection,this.currentimage+1,src);
}
Gallery.prototype.PrevImage = function(src){
    this.SetImage(this.currentsection,this.currentimage-1,src);
}
Gallery.prototype.NextSection = function(src){
    this.SetImage(this.currentsection+1,0,src);
}
Gallery.prototype.PrevSection = function(src){
    this.SetImage(this.currentsection-1,0,src);
}
Gallery.prototype.SetImage = function(sectionid,imageid,src){
    if(!this.enabled) return;
    
    if(sectionid >= this.sectioncount || sectionid < 0) return false;
    if(imageid >= this.imagecount[sectionid] || imageid < 0) return false;
    
    this.lastsrc=null;
    if(src)
        this.lastsrc=src;
    
    if(this.currentsection!=sectionid){
        this.currentimage=imageid;
        this.currentsection=sectionid;
        if(this.ev.sectionchange) this.ev.sectionchange(this);
        if(this.ev.imagechange) this.ev.imagechange(this);
    }else if(this.currentimage!=imageid){
        this.currentimage=imageid;
        this.currentsection=sectionid;
        if(this.ev.imagechange) this.ev.imagechange(this);
    }else return;
    
    //this.imgdiv.style.display='block';
    //TESTING
    this.imgdiv.style.visibility='visible';
    this.img.style.display='none';

    this.currentimage=imageid;
    this.currentsection=sectionid;
    
    if(src){
        if(src.isfixedpoint){
            this.divxc=src.left;
            this.divyc=src.top;
        }else{
            var pos=GalleryStatic.findPos(src);
            this.divxc=pos[0];
            this.divyc=pos[1];
        }
        this.divwc=src.width;
        this.divhc=src.height;
    }
    
    this.divw=Math.max(this.dimx[sectionid][imageid],this.minx) + this.paddingx;
    this.divh=Math.max(this.dimy[sectionid][imageid],this.miny) + this.paddingy;

    this.divx=Math.max(0,(GalleryStatic.WinWidth()-this.divw)/2);
    this.divy=Math.max(0,(GalleryStatic.WinHeight()-this.divh)/2);
    
    this.limg = new Image();
    this.limg.src = this.iurl[sectionid][imageid];
    
    this.DoneHide = false;
    
    this.SetMoveTime();
    if(this.dimmer) this.dimmer.Run(this.fadeamount,this.fadetime);
    this.StartMove();
    
    return true;
}
//End Interface