goodsimages = new cImages();
imagesInit();

function imagesInit(){
	try{
		var it = document.getElementById('product_pic');
		var aLinks = it.getElementsByTagName('A');
		if(aLinks.length){
			for(var i = 0; i < aLinks.length; i++){
				aLinks[i].onclick = (function(obj){
											return function(){
												return showPic(obj, goodsimages);
									}})(aLinks[i]);
			}
		}
		
		if(aLinks.length >= 5){
			goodsimages.Init();
			addImagesIntoGoods();
		}
	}catch(e){
		setTimeout("imagesInit()", 100);
	}
}

function addImagesIntoGoods(){
	try{ // in case when object created after attempt to add images into
		addImagesIntoGoodsJS();
		goodsimages.Show(0);
	}catch(e){
		setTimeout("addImagesIntoGoods()", 100);
	}
}

function cImages(){
	this.images = new Array();
	this.images_tn = new Array();
	this.thumbs = new Array();
	this.pos = 0;
	this.buttonBack = false;
	this.buttonForward = false;
	this.current = 0;
	this.progressbar = false;
	this.progressbarwidth = 151;
	
	this.init = false;
	
	this.Init = cImages_Init;
	this.showProgressBar = cImages_showProgressBar;
	this.isInit = cImages_isInit;
	this.addImage = cImages_addImage;
	this.Show = cImages_Show;
	this.statusNaviButtons = cImages_statusNaviButtons;
	this.goForward = cImages_goForward;
	this.goBack = cImages_goBack;
	this.getImageIdByThumb = cImages_getImageIdByThumb;
	this.setCurrent = cImages_setCurrent;
}

function cImages_Init(){
	var imagePlaces = document.getElementById('product_pic');
	if(this.buttonBack == false && this.buttonForward == false){
		this.thumbs = imagePlaces.getElementsByTagName('A');
	}
	this.init = true;

	this.showProgressBar();
}

function cImages_addImage(img, img_tm){
	this.images.push(img);
	this.images_tn.push(img_tm);
	this.showProgressBar();
}

function cImages_showProgressBar(){
	//alert(this.progressbar + ' == false && ' + this.thumbs.length + ' && ' + this.images_tn.length + '  > ' + this.thumbs.length);
	if(this.progressbar == false && this.thumbs.length && this.images_tn.length > this.thumbs.length){
		this.thumbs[0].parentNode.parentNode.removeChild(this.thumbs[this.thumbs.length - 1].parentNode);
		
		var oDiv = document.createElement('DIV');
		this.progressbar = document.createElement('P');
		var oSpan = document.createElement('SPAN');
		oSpan.appendChild(document.createTextNode('\xA0'));
		oDiv.className = 'progressline';
		this.progressbar.appendChild(oSpan);		
		oDiv.appendChild(this.progressbar);

		// creating arrows	
		var nodeBack = document.createElement('SPAN');
		var nodeForward = document.createElement('SPAN');
		
		var nodeBackNBSP = document.createTextNode('\xA0');
		var nodeForwardNBSP = document.createTextNode('\xA0');
		
		nodeBack.appendChild(nodeBackNBSP);
		nodeForward.appendChild(nodeForwardNBSP);
		
		this.buttonBack = document.createElement('LI');
		this.buttonForward = document.createElement('LI');
		
		this.buttonBack.appendChild(nodeBack);
		this.buttonForward.appendChild(nodeForward);
		
		this.buttonBack.className = 'arrowleft';
		this.buttonForward.className = 'arrowright';
	
		this.thumbs[0].parentNode.parentNode.insertBefore(this.buttonBack, this.thumbs[0].parentNode);
		this.thumbs[0].parentNode.parentNode.appendChild(this.buttonForward);
		this.thumbs[0].parentNode.parentNode.parentNode.appendChild(oDiv);
		
		this.buttonBack.onmouseup = (function(obj){
											return function(){
												obj.goBack();
												return false;
									}})(this);
		
		this.buttonForward.onmouseup = (function(obj){
											return function(){
												obj.goForward();
												return false;
									}})(this);
					
		this.thumbs[0].parentNode.parentNode.className += " pb";
	}
}

function cImages_Show(pos){
	if(this.isInit()
		&& this.images_tn.length > this.thumbs.length
		&& ((pos != this.pos && pos < this.images.length) || !pos)){
		if(pos > 0 && this.images.length - pos < this.thumbs.length){
			pos = this.images.length - this.thumbs.length;
		}
		for(var i = 0; i < this.thumbs.length; i++){
			var img = this.thumbs[i].getElementsByTagName('IMG')[0];
			img.src = this.images_tn[pos + i];
			img.parentNode.href = this.images[pos + i];
			
			//alert(this.current + ' == ' + (pos + i));
			
			if(this.current == pos + i){
				img.className = 'current';
			}else{
				img.className = '';
			}
		}
		
		this.pos = pos;
		try{
			this.progressbar.style.width = Math.ceil(this.progressbarwidth * (this.pos + this.thumbs.length) / this.images_tn.length) + "px";
			//alert(this.progressbar.style);
		}catch(e){}
		this.statusNaviButtons();
	}
}

function cImages_goForward(){
	if(this.isInit() && this.images.length >= this.pos + this.thumbs.length){
		this.Show(this.pos + this.thumbs.length);
	}
}

function cImages_goBack(){
	if(this.isInit() && this.pos){
		var pos = this.pos  - this.thumbs.length;
		if(pos < 0){
			pos = 0;
		}
		this.Show(pos);
	}
}

function cImages_statusNaviButtons(){
	if(this.isInit()){
		// back		
		if(!this.pos){
			this.buttonBack.className = 'arrowleftdis';
			this.buttonForward.className = 'arrowright';
		}else{
			this.buttonBack.className = 'arrowleft';
			if(this.pos + this.thumbs.length >= this.images.length){
				this.buttonForward.className = 'arrowrightdis';
			}else{
				this.buttonForward.className = 'arrowright';
			}
		}
	}
}

function cImages_isInit(){
	return this.init;
}

function cImages_getImageIdByThumb(thumbName){
	ret = -1;
	if(thumbName.length){
		for(var i = 0; i < this.images_tn.length; i++){
			if(thumbName == this.images_tn[i]){
				ret = i;
				i = this.images_tn.length;
			}
		}
	}
	return ret;
}

function cImages_setCurrent(current){
	this.current = current;
}

function showPic(curpic){
	var ret = false;
	var bigimg = false;
	try{bigimg = document.getElementById("bigimg");}
	catch(e){}
	if(curpic && bigimg)
	{
		bigimg.src = curpic.href;
		thumbs = document.getElementById("product_pic").getElementsByTagName("img");
		if(arguments.length > 1){
			var images = arguments[1];
			imgIndex = images.getImageIdByThumb(curpic.firstChild.src);
			if(imgIndex != -1){
				images.setCurrent(imgIndex);
			}
		}

		for(i=0;i<thumbs.length;i++) {
			thumbs[i].className = "";
		}
		curpic.firstChild.className = "current";
	}
	return ret;
}
