

CardGalleryManager = function(nbImages, maxNbImages, photos, pagerId, instanceName, imgIdPrefix, featureId, blankImagePath, required) {
	this._nbImages = nbImages;
	this._maxNbImages = maxNbImages;
	this._nbPages = this.getNbPages(this._nbImages, this._maxNbImages);
	this._photos = photos;
	this._pagerId = pagerId;
	this._photoPages = [];
	this._instanceName = instanceName;
	this._imgIdPrefix = imgIdPrefix;
	this._featureId = featureId;
	this._blankImage = blankImagePath;
	this._currentPage = 0;
	this._required = required;
} 

CardGalleryManager.prototype.execute = function(){
    this.initiateGallery();
}

CardGalleryManager.prototype.getNbPages = function(nbImages, maxNbImages) {
	var division = nbImages / maxNbImages;
	var nb = 0;
	for(var cpt = 0 ; cpt < division ; cpt++){nb++;}
	return nb;
}

CardGalleryManager.prototype.outputPager = function() {
	var str = "";
	if(this._nbPages > 1){
		str += '<a onclick="'+this._instanceName+'.previousPage()"><img src="/luxurylisting/images/pager/pager-arrow-left-white.gif" /></a>&nbsp;';
		for(var i = 0 ; i < this._nbPages ; i++) {
			if(i == 0) {
				str += '<a style="text-decoration: underline;" id="pageLink'+i+'" onclick="'+this._instanceName+'.gotoPage('+i+')">'+(i+1)+'</a>&nbsp;';
			}else{
				str += '<a id="pageLink'+i+'" onclick="'+this._instanceName+'.gotoPage('+i+')">'+(i+1)+'</a>&nbsp;';
			}
		}
		str += '<a onclick="'+this._instanceName+'.nextPage()"><img src="/luxurylisting/images/pager/pager-arrow-right-white.gif" /></a>';
		document.getElementById(this._pagerId).innerHTML = str;	
	}
}

CardGalleryManager.prototype.initiateGallery = function() {
	this.createPhotoPages();
	this.fillImageSources();
	this.outputPager();
}

CardGalleryManager.prototype.fillImageSources = function() {
	try{
		var len = this._photoPages[0].length;
		for(var i = 0 ; i < len ; i++){
			try{
				var node = document.getElementById("thb"+i);
				node.setAttribute("src", this._photoPages[0][i].urlThumb);
				var alt = this._photoPages[0][i].alt;
				if(alt.substring("/luxury/") != -1){
					var strLen = "/luxury/".length;
					var totalLen = alt.length;
					var startIndex = alt.indexOf("/luxury/") + strLen;
					alt = alt.substring(startIndex, totalLen);
				}				
				node.setAttribute("alt", alt);
				node.setAttribute("title", alt);
				
				document.getElementById("hidthb"+i).setAttribute("value", this._photoPages[0][i].url);
			}catch(ex){}
		}
	
            if(this._nbImages < this._maxNbImages){

                    var len = this._photoPages[0].length;
                    var preLastIndex = (len - 2);
                    var lastIndex = (len - 1);

                    for(var ii = len ; ii < this._maxNbImages ; ii++) {
                            var img = document.getElementById("thb"+ii);
                            img.style.display = "none";			
                    }

                    var str = document.getElementById("thb"+preLastIndex).className;
                    document.getElementById("thb"+preLastIndex).className = str + " bottomBorder";

                    var strLast = document.getElementById("thb"+lastIndex).className;
                    document.getElementById("thb"+lastIndex).className = strLast + " bottomBorder";

                    if((len % 2) != 0){
                            document.getElementById("thb"+lastIndex).className += " rightBorder";
                    }

            }
        }catch(ex){}
}

CardGalleryManager.prototype.createPhotoPages = function() {
	var cpt = 0;
	for(var i = 0 ; i < this._nbPages ; i++) {
		this._photoPages[i] = [];
		
		for(var ii = 0 ; ii < this._maxNbImages ; ii++) {
			if(cpt < this._nbImages){				
				this._photoPages[i][ii] = this._photos[cpt++];				
			}
		}		
	}
}

CardGalleryManager.prototype.nextPage = function() {
	var maxPage = this._photoPages.length - 1;
	var nextPage = this._currentPage + 1;
	if(nextPage <= maxPage){
		this._currentPage = nextPage;
		this.gotoPage(this._currentPage);
	}
	var len = this._photoPages.length;
	for(var i = 0 ; i < len ; i++) {
		document.getElementById("pageLink"+i).style.textDecoration = "none";
	}
	document.getElementById("pageLink"+this._currentPage).style.textDecoration = "underline";
}

CardGalleryManager.prototype.previousPage = function() {
	var minPage = 0;
	var prevPage = this._currentPage -1;
	if(prevPage >= 0){
		this._currentPage = prevPage;
		this.gotoPage(this._currentPage);
	}
	var len = this._photoPages.length;
	for(var i = 0 ; i < len ; i++) {
		document.getElementById("pageLink"+i).style.textDecoration = "none";
	}
	document.getElementById("pageLink"+this._currentPage).style.textDecoration = "underline";
}

CardGalleryManager.prototype.gotoPage = function(page) {
	var prevPage = this._currentPage;
	this._currentPage = page;
	for(var cpt = 0 ; cpt < this._maxNbImages ; cpt++){              
		var node = document.getElementById("thb"+cpt);
		node.setAttribute("src", "/luxury/images/spacer.gif");
		node.setAttribute("alt", "");
		node.setAttribute("title", "");
		node.style.display = "block";
		if(cpt == 0 || (cpt % 2) == 0 ){
			node.className = "";
		}else{
			node.className = "rightBorder";
		}
		if(cpt == (this._maxNbImages - 2) || cpt == (this._maxNbImages - 1)){
			node.className += " bottomBorder";
		}
		document.getElementById("hidthb"+cpt).setAttribute("value", "");
	}
	var len = this._photoPages[page].length;
	for(var i = 0 ; i < len ; i++) {
		var src = this._photoPages[page][i].urlThumb;
		document.getElementById("thb"+i).setAttribute("src", src);
		var alt = this._photoPages[page][i].alt;
		if(alt.substring("/luxury/") != -1){
			var strLen = "/luxury/".length;
			var totalLen = alt.length;
			var startIndex = alt.indexOf("/luxury/") + strLen;
			alt = alt.substring(startIndex, totalLen);
		}
		document.getElementById("thb"+i).setAttribute("alt", alt);
		document.getElementById("thb"+i).setAttribute("title", alt);
		var url = this._photoPages[page][i].url;
		document.getElementById("hidthb"+i).setAttribute("value", url);
	}
	document.getElementById("pageLink"+prevPage).style.textDecoration = "none";
	document.getElementById("pageLink"+this._currentPage).style.textDecoration = "underline";
	
	if(len < this._maxNbImages){		
	
		var preLastIndex = (len - 2);
		var lastIndex = (len - 1);
		
		for(var ii = len ; ii < this._maxNbImages ; ii++) {
			var img = document.getElementById("thb"+ii);
			img.style.display = "none";			
		}
		
		var str = document.getElementById("thb"+preLastIndex).className;
		document.getElementById("thb"+preLastIndex).className = str + " bottomBorder";
		
		var strLast = document.getElementById("thb"+lastIndex).className;
		document.getElementById("thb"+lastIndex).className = strLast + " bottomBorder";
		
		if((len % 2) != 0){
			document.getElementById("thb"+lastIndex).className += " rightBorder";
		}
		
	}
	
}






























