var gallery={
	
	iid: 0,
	divid: 1,
	action: 0,
	move: 0,
	nextid: 0,
	currid: 0,
	width: 810,
	left: 0,
	totpages: 0,
	urls: new Array(),
	desc: new Array(),
	img: new Array(),
	loaded: new Array(),
	root: '',
	
	init:function(){
		
		root=this;
		
	
		//GALLERY IMAGE LISTENERS
		$('#jsgalleryimgsrc1').load(function(){
		
				$('#jsgalleryimg1').animate({opacity:1},300, "swing",function(){
					$('#jsgalleryimgsrc2').attr('src','#');
					root.divid=2;
					root.action=0;
				});
				$('#jsgalleryimg2').animate({opacity:0},300, "swing");
				
		});
		
		$('#jsgalleryimgsrc2').load(function(){
				
				$('#jsgalleryimg2').animate({opacity:1},300, "swing",function(){
					$('#jsgalleryimgsrc1').attr('src','#');
					root.divid=1;
					root.action=0;
				});
				$('#jsgalleryimg1').animate({opacity:0},300, "swing");
				
		});
	
		
	},
	
	page:function(attr){
		
		this.nextid=parseInt(attr);
		
		if(this.nextid>this.currid){
			
			this.left=this.width*-1;
			$('#gallery_page'+this.nextid).css('left',this.width+'px');
			
		}else if(this.nextid<this.currid){
			
			this.left=this.width;
			$('#gallery_page'+this.nextid).css('left','-'+this.width+'px');
			
		}else{
			this.left=0;
		}
		
		this.currid=this.nextid;
		
		$('#gallery_pageslider').animate({left:this.left+'px'},500, function() {
			
			for(i=0;i<root.totpages;i++){
				$('#gallery_page'+i).css('left','2000px');
				$('#gallery_pagingcell'+i).removeClass('gallery_pagingcellon');
			}
			$('#gallery_page'+root.currid).css('left','0px');
			$('#gallery_pageslider').css('left','0px');
			$('#gallery_pagingcell'+root.currid).addClass('gallery_pagingcellon');
			root.move=0;
			
		 });
		
	},
	
	start:function(iid){
		
		this.iid=iid;
		
		$('#mask').css('display','block');
		$('#jsgallery').css('display','block');
		$('#site').css({'height':'100%','overflow':'hidden'});
		
		$('#devvideo').css('display','none');
		
		$('#jsgalleryimg1').css('opacity',0);
		$('#jsgalleryimg2').css('opacity',0);
		
		$('#jsgalleryimgsrc1').attr('src',this.urls[this.iid]);
		$('#jsgallerydesc1').text(this.desc[this.iid]);
		
		this.preload();
		
	},
	
	exit:function(){
		
		$('#devvideo').css('display','block');
		$('#mask').css('display','none');
		$('#jsgallery').css('display','none');
		$('#site').css({'height':'auto','overflow':'visible'});

	},
	
	preload:function(){
		
		miniid=this.iid-4;
		maxiid=this.iid+4;
		if(miniid<0) miniid=0;
		
		//PRELOAD FIRST
		if(this.loaded[0]==undefined){
			this.img[0] = new Image(1,1);
			this.img[0].src = this.urls[0];
			this.loaded[0]=1;
		}
			
		//PRELOAD LAST
		if(this.loaded[(this.urls.length-1)]==undefined){
			this.img[(this.urls.length-1)] = new Image(1,1);
			this.img[(this.urls.length-1)].src = this.urls[(this.urls.length-1)];
			this.loaded[(this.urls.length-1)]=1;
		}
		
		for(i=miniid;i<maxiid;i++){
			
			if(this.urls[i]!='' && this.loaded[i]==undefined){
				
				this.img[i] = new Image(1,1);
				this.img[i].src = this.urls[i];
				this.loaded[i]=1;
				
			}
		
		}
		
	},
	
	next:function(){
		
		if(this.action==0){
			this.action=1;
			this.iid++;
			if(this.iid==this.urls.length) this.iid=0;
			this.preload();
			$('#jsgalleryimgsrc'+this.divid).attr('src',this.img[this.iid].src);
			$('#jsgallerydesc'+this.divid).text(this.desc[this.iid]);
		}
		
	},
	
	prev:function(){
		
		if(this.action==0){
			this.action=1;
			this.iid--;
			if(this.iid==-1) this.iid=(this.urls.length-1);
			this.preload();
			$('#jsgalleryimgsrc'+this.divid).attr('src',this.img[this.iid].src);
			$('#jsgallerydesc'+this.divid).text(this.desc[this.iid]);
		}
		
	}
	
}
