// JScript File

// Implements the ImgRibbon object.  We have 5 images at any given time.
function ImgRibbon(catalogId){
    // Constructor.
    var theCatalogId = catalogId;
    var prodImagesObj = null;
    var theStartIndex = null;
    var theNumOfImages = 5
    var imgElArray = new Array();  
    // Put all the image elements into an array so we can set them quickly.
	for(var i = 1; i<=theNumOfImages; i++){
		imgElArray.push(window.ImgRibbonIF.document.getElementById('imgRibbon' + i));
	}
    //var prodIdArray = new Array(5);
    
    
    // Load the product image URLs for the category.
	this.initCategory = function(categoryId){
		var url = "/getcatprodimages";	
 		if(categoryId==theCatalogId) url = "/getglblprodimages"; 		
 		var parameters = "catid=" + categoryId;
 		theRequest.post(url, this.initCategoryOnComplete, parameters,this.showError);
 	}
 	this.initCategoryOnComplete = function(jsonObj){  	
 		// prodImagesObj has product_id,label,imagepath data members
		prodImagesObj = jsonObj
		theStartIndex = 0;
		showImages();	
		if(prodImagesObj[0]!=null) theProductData.show(prodImagesObj[0].product_id);
  	}   
  	
	function showImages(){
		for(var i=theStartIndex; i< theStartIndex+theNumOfImages; i++){
			if(i < prodImagesObj.length){
				imgElArray[i-theStartIndex].src = prodImagesObj[i].imagepath;
				imgElArray[i-theStartIndex].className="clickableImage";
				imgElArray[i-theStartIndex].title = prodImagesObj[i].label;
				//prodIdArray[i-theStartIndex] = prodImagesObj[i].product_id;
				var fn = "theProductData.show('" + prodImagesObj[i].product_id + "')";
				imgElArray[i-theStartIndex].onclick = new Function(fn);    
				
				
			}else{
				imgElArray[i-theStartIndex].src = "/assets/images/prod/blank.jpg";
				imgElArray[i-theStartIndex].className="";
				imgElArray[i-theStartIndex].title = "";
				//prodIdArray[i-theStartIndex] = 0;
				imgElArray[i-theStartIndex].onclick = null;
			}
		}
	
	}
	
	this.doNext = function(){
		// Since the images are not the same width we could lose the last one.
		if(theStartIndex+theNumOfImages - 1 < prodImagesObj.length)
			theStartIndex+=theNumOfImages  - 1;
		showImages();
	}
	
	this.doPrev = function(){
		theStartIndex-=theNumOfImages - 1;
		if(theStartIndex<0) theStartIndex=0;
		showImages();
	} 
	
	//for debugging
 	this.showError = function(jsonObj,txt){
 	
 alert(txt);	
 	
 	
 	}
	
}
