//  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(5);
	
}
function MoveLeft(obj){
	//if(Gallery.lock)return;
	Gallery.lock = true;
	//Gallery.AddToLeft();
	obj.onmouseout = function(){Gallery.lock = false};
	Gallery.MoveRight(5);
	
}
var myWidth = 0;
var numPics = 0;

Gallery = {
	template : '<table border="0" cellpadding="0" cellspacing="0" class="show-gallery">'+
 ' <tr>'+
 '   <td class="nav left"><div class="move-left" id="lLeft" onmouseover="MoveLeft(this)"></div></td>'+
 '   <td class="move" id="lengthGal" width="100%"><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 right"><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,
	CreateImageObject : function(pic){
		var img = document.createElement('IMG');

		img.src = pic.src;
		img.iasrc = pic.src;
		img.asrc = pic.asrc;
	
		// img.width = 98;
		// img.height = 98;
		// img.hspace = 5;
	
		if(!pic.asrc){
			img.style.opacity = 0.5;
			img.style.filter = 'alpha(opacity=50)';
		}
		img.index = this.next_right;
		
				img.onload = function(){
					
					if(numPics<pics.length)
					{	
						myWidth+=img.width+5;
						
						if(myWidth>document.getElementById("lengthGal").clientWidth+25)
						{
							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(){
			window.location = pic.href;
			//window.location = window.location.href.substr(0, window.location.href.lastIndexOf(''))+"/doaction.php?action=GetHTML&id=62580&product_id="+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_left = pics.length - 1;
		this.next_right = 0;
		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<pics.length; i++){			
			this.active_pics[i] = this.active_pics[i+1];			
		}
		this.move_obj.style.left = (this.move_obj.offsetLeft + 120) + '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[pics.length-1].removeNode(true);
		}catch(e){
			if (this.active_pics[pics.length-1].parentNode)
				this.active_pics[pics.length-1].parentNode.removeChild(this.active_pics[pics.length-1]);			
		}
		for(var i = pics.length-1; i>-1; i--){			
			this.active_pics[i] = this.active_pics[i-1];			
		}
		//this.move_obj.style.left = (this.move_obj.offsetLeft - 120) + 'px';
	},
	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;
	}
	
};

if (window.navigator.userAgent.indexOf ("MSIE") >= 0)
{

		var old_onload = window.onload;
	
		window.onload = function(){
			
			old_onload();
		
			Gallery.init('gallery');			
		}
}			

