//  JavaScript Document @Coretomic 2008 max@coretomic.com

function MoveRight(obj){
	//if(Gallery.lock)return;
	Gallery.lock = true;
	obj.onmouseout = function(){Gallery.lock = false};
	//Gallery.AddToRight();
	Gallery.MoveLeft(1);
	
}
function MoveLeft(obj){
	//if(Gallery.lock)return;
	Gallery.lock = true;
	//Gallery.AddToLeft();
	obj.onmouseout = function(){Gallery.lock = false};
	Gallery.MoveRight(1);
	
}

function compat_firstChild(obj){
				obj = obj.firstChild;
				do{
					if(obj.nodeType != 3)return obj;
					obj = obj.nextSibling;
					}while(obj)	
					return null;
}

var myWidth = 0;
var numPics = 0;

Gallery = {
	pic_width : 98,
	pic_height : 98,
	pic_amount : 8,
	pic_hspace : 0,	
	template : function(){return '<table cellpadding="0" cellspacing="0" border="0">'+
 ' <tr>'+
 '   <td class="nav right"><div class="move-left" id="lLeft" onmouseover="MoveLeft(this)"></div></td>'+
 '   <td valign="middle" id="lengthGal" class="move"><div class="visible" style="position: relative; overflow:hidden; white-space: nowrap;"><div id="move" style="position:absolute" class="items"></div></div></td>'+
 '   <td class="nav left"><div class="move-right" id="rRight" onmouseover="MoveRight(this)"></div></td>'+
 ' </tr>'+
'</table>';},
	move_obj : null,
	gal_debug: null,
	active_pics: [],
	next_left: 0,
	next_right: 0,
	lock: false,
	_self: this,
	callback : function(id){},
	CreateImageObject : function(pic){
		var img = document.createElement('IMG');
		img.src = pic.src;
		img.iasrc = pic.src;
		img.asrc = pic.asrc;
		//img.width = this.pic_width;
		//img.height = this.pic_height;
		img.width = pic.small_image_width;
		img.height = pic.small_image_height;
		img.hspace = this.pic_hspace;
		if(!pic.asrc){
			img.style.opacity = 0.5;
			img.style.filter = 'alpha(opacity=50)';
		}
		img.index = this.next_right;
		_this_gall = this;
		
		//alert(document.getElementById("lengthGal").clientWidth);
		
		img.onload = function(){
					
					if(numPics<pics.length)
					{	
						myWidth+=img.width+5;
						
						if(myWidth>document.getElementById("lengthGal").clientWidth)
						{
							document.getElementById("rRight").style.visibility = "visible";
							document.getElementById("lLeft").style.visibility = "visible";
						}
						else
						{
							document.getElementById("rRight").style.visibility = "hidden";
							document.getElementById("lLeft").style.visibility = "hidden";
							
						}
						numPics++;
						
					}
			}
		
		img.onclick =  function(){ 
				_this_gall.callback(pic.id)
		};
		
		img.onmouseover = function(){Gallery.BlinkActivePicture(img)};
		img.onmouseout = function(){Gallery.UnblinkActivePicture(img)};
		return img;
	},
	init : function (objname){
		if(pics.length < 1){
			alert('Too few pictures');
			return;  
		}	
		
		document.getElementById(objname).innerHTML = this.template();
		this.width = document.getElementById("lengthGal").clientWidth;
		this.next_right = 0;
		this.next_left = pics.length - 1;
		
		this.move_obj = document.getElementById('move');
		this.gal_debug = document.getElementById('gal_debug');
		
		for(var i = 0; i<pics.length; i++){

			this.active_pics[i] = this.CreateImageObject(pics[i]);
			//this.next_right++;	
			this.move_obj.appendChild(this.active_pics[i]);
		}	
		
	},
	UnblinkActivePicture : function (img){
				if(!img.asrc){
					img.style.opacity = 0.5;
					img.style.filter = 'alpha(opacity=50)';
				}else{
					img.src = img.iasrc;
				}
	},
	BlinkActivePicture : function (img){
		
		if(!img.asrc){
			img.style.opacity = 1;
			img.style.filter = 'alpha(opacity=100)';
		}else{
			img.src = img.asrc;
		}
		
	},
	MoveLeft : function (interrup){
		var self = this;
		
		if(this.move_obj.offsetWidth + this.move_obj.offsetLeft <= this.move_obj.parentNode.offsetWidth)this.AddToRight();
				
		this.move_obj.style.left = (this.move_obj.offsetLeft - 2) + 'px';		

		if(this.lock){	
		window.setTimeout(function(){self.MoveLeft(1);},1); }
		
	},
	AddToRight : function (){
		var n_img = this.active_pics[this.next_right];
		this.move_obj.appendChild(n_img);
		this.move_obj.style.left = (this.move_obj.offsetLeft + n_img.width) + 'px';
		this.next_right++;
		this.next_left++;
		if(this.next_right >= pics.length)this.next_right = 0;
		if(this.next_left >= pics.length)this.next_left = 0; 
	},
	RemoveFromLeft : function (){
		try{
			this.active_pics[0].removeNode(true);
		}catch(e){
			if (this.active_pics[0].parentNode)
				this.active_pics[0].parentNode.removeChild(this.active_pics[0]);			
		}
		for(var i = 0; i<this.pic_amount; i++){			
			this.active_pics[i] = this.active_pics[i+1];			
		}
		this.move_obj.style.left = (this.move_obj.offsetLeft + this.pic_width+this.pic_hspace*2) + 'px';
	},
	MoveRight : function (interrup){
		var self = this;
		if(this.move_obj.offsetLeft >= 0)this.AddToLeft();
		this.move_obj.style.left = (this.move_obj.offsetLeft + 2) + 'px';		
		if(this.lock){	
			window.setTimeout(function(){self.MoveRight(1);},1); }
	},
	RemoveFromRight : function (){
		try{
			this.active_pics[this.pic_amount-1].removeNode(true);
		}catch(e){
			if (this.active_pics[this.pic_amount-1].parentNode)
				this.active_pics[this.pic_amount-1].parentNode.removeChild(this.active_pics[this.pic_amount-1]);			
		}
		for(var i = this.pic_amount-1; i>-1; i--){			
			this.active_pics[i] = this.active_pics[i-1];			
		}
	},
	AddToLeft : function (){
		var n_img = this.active_pics[this.next_left];//.cloneNode(true);
		this.move_obj.insertBefore(n_img,this.move_obj.firstChild);
		this.move_obj.style.left = (this.move_obj.offsetLeft - n_img.width) + 'px';
		
		this.next_right--;
		this.next_left--;
		if(this.next_right < 0)this.next_right = pics.length - 1;
		if(this.next_left < 0)this.next_left = pics.length - 1;
	}
};
