// Adam Burmister, 2006
var ThumbnailPager = {
	/* Constants */
	COLUMN_WIDTH : 348,
	PHOTO_WIDTH : 520,
	PHOTO_HEIGHT : 390,
	PHOTO_WIDTH_PORTRAIT : 293,
	PORTRAIT_INDENT : 100,
	FADE_TIME : 0.5,
	/* Class vars */
	currentPage : 0,
	totalColumns : 0,
	thumbsPerColumn : 3,
	maxPages : 0,
	loadStatus : null,
	currPhoto : null,
	/* Methods */
	init : function () {
		var container = $("thumbnails");
		var soldSticker=$("SoldSticker");	
		if(container) {
			var ULs = container.getElementsByTagName('ul');
			this.totalColumns = ULs.length;
			this.maxPages = Math.round(this.totalColumns / 2);
			this.setAnchorSelected(1, false);
			this.currentPage = 0;
			this.currPhoto = $(initialThumb);
			if(this.currPhoto) this.currPhoto.className = 'selected';
			loadStatus = document.createElement('div');
			loadStatus.id = "loadStatus";
			loadStatus.className = "loading";
			loadStatus.style.display = "none";
			if(soldSticker) soldSticker.style.left = '0px';
			var indicator = document.createElement('img');
			indicator.src = '/images/loading.gif';
			indicator.alt = 'Loading image...';
			loadStatus.appendChild(indicator);
			$("photo").appendChild(loadStatus);
			ULs = container = null;
		}
	},
	nextPage : function () {
		if((this.currentPage + 2) <= this.maxPages ){
			var d = (arguments.length == 0) ? 1 : arguments[0];
			new Effect.MoveBy('thumbnails', 0, d * -this.COLUMN_WIDTH, { duration: .5, queue: {position:'end',scope:'thumbs'} })
			this.setAnchorSelected(this.currentPage + 1 + d, false);
			this.currentPage += d;
		}
	},
	previousPage : function () {
		if(this.currentPage > 0){
			var d = (arguments.length == 0) ? 1 : arguments[0];
			new Effect.MoveBy('thumbnails', 0, d * +this.COLUMN_WIDTH, { duration: .5, queue: {position:'end',scope:'thumbs'} })
			this.setAnchorSelected(this.currentPage + 1 - d, false);
			this.currentPage -= d;
		}
	},
	setAnchorSelected : function (i, move) {
		if(move) {
			var old = this.currentPage;
			var d = (old + 1) - i;
			if( d >= 0 ){
				this.previousPage(d);
			} else {
				this.nextPage(d * -1);
			}
			return;
		}
		if($("PagerPage" + (this.currentPage + 1))) {
			$("PagerPage" + (this.currentPage + 1)).className = "";
			$("PagerPage" + i).className = "selected";
		}
	},
	nextPhoto : function () {
		if(!this.currPhoto) return;
		var id = this.currPhoto.id.substring(5,7);
		var alpha = "*ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		var col = id.substring(0,1);
		var num = id.substring(1,2) * 1;
		num++;
		// Last of column
		if(num > this.currPhoto.parentNode.parentNode.getElementsByTagName('li').length) {
			num = 1;
			col = alpha.charAt(alpha.indexOf(col) + 1);
		}
		col = alpha.charAt(alpha.indexOf(col));
		if(alpha.indexOf(col) > this.totalColumns) {
			col = 'A';
			this.previousPage(this.maxPages-1);
		} else {
			if(((this.currentPage * 2) - alpha.indexOf(col)) < -2) 
				this.nextPage();
		}
		this.performThumbClick('thumb' + col + num);
	},
	previousPhoto : function () {
		if(!this.currPhoto) return;
		var id = this.currPhoto.id.substring(5,7);
		var alpha = "*ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		var col = id.substring(0,1);
		var num = id.substring(1,2) * 1;
		num--;
		// Last of column
		if(num == 0) {
			num = this.thumbsPerColumn;
			col = alpha.charAt(alpha.indexOf(col) - 1);
		}
		if(col == '*') {
			col = alpha.charAt(this.totalColumns);
			//num = number of images in last column
			num = $("column" + col).getElementsByTagName('li').length;
			this.nextPage(this.maxPages - 1);
		} else {
			if(((this.currentPage * 2) - alpha.indexOf(col)) == 0) 
				this.previousPage();
		}
		this.performThumbClick('thumb' + col + num);	
	},
	performThumbClick : function (id) {
		var onclick = $(id).onclick + "";
		var s = onclick.indexOf("ThumbnailPager.swapPhoto");
		var e = onclick.indexOf(");");
		var toDo = onclick.substring(s,e+2);
		toDo = toDo.replace('this','document.getElementById(\'' + id + '\')');
		eval(toDo);
	},
	swapPhoto : function (a, imgName, landscape) {
	    var href = document.getElementById("lb_largeView").href;
	    document.getElementById("lb_largeView").href = href.substring(0, href.indexOf("&image=")) + "&image="+escape(imgName);
		if(this.currPhoto == a) return;
		var photo=$('hero');
		var soldSticker=$('SoldSticker');
		if(photo)
		{
		
			var isLandscape = (landscape=='True');
		
			//Select thumbnail
			this.currPhoto.className = ''; // unselect
			this.currPhoto = a;
			this.currPhoto.className = 'selected';
			this.currPhoto.blur();
					
			// Setup fade for images
			var photoContainer = $('photo');
			var loadStatus = $('loadStatus');

			loadStatus.style.display = 'block';
				
			toFade =$('toFade');
			if(!toFade)
				toFade = document.createElement('img');
			toFade.style.display = 'none';
			toFade.height = photo.height;
			toFade.width = photo.width;
			toFade.style.left = photo.style.left;
			toFade.src = photo.src;
			toFade.id = 'toFade';
			toFade.style.display = 'block';
			
			photoContainer.appendChild(toFade);
	
			var indent = (isLandscape ? '0' : this.PORTRAIT_INDENT);
			var photoWidth = (isLandscape ? this.PHOTO_WIDTH : this.PHOTO_WIDTH_PORTRAIT);
			
			photo.onload = function(){
				this.width = photoWidth;
				this.style.left = indent + 'px';
				
				if(loadStatus) loadStatus.style.display = 'none';
										if(soldSticker) {
					if(isLandscape){
						if(soldSticker.style.left != '0px') 
							new Effect.MoveBy(soldSticker, 0, -ThumbnailPager.PORTRAIT_INDENT, { duration: 0.2, queue: {position:'end',scope:'sticker'} });
					} else {
						if(soldSticker.style.left != '100px') 
							new Effect.MoveBy(soldSticker, 0, +ThumbnailPager.PORTRAIT_INDENT, { duration: 0.2, queue: {position:'end',scope:'sticker'} });
					}
				}
			
				new Effect.Fade(toFade, { afterFinish : 
							function(){ 
								if(toFade) {  photoContainer.removeChild(toFade); toFade = null; } 
							}, 
							duration: ThumbnailPager.FADE_TIME, 
							transition: Effect.Transitions.sinoidal,
							queue: { position: 'end', scope: 'transition', limit: 2 }
							}
						);
			}
			photo.src = '/images/list_img/' + BPKey + '/' + imgName;
			
			if(soldSticker) {
				soldSticker.className = 'soldimg_after';
			}

			var l=$('printlink');
			if(l) {
				l.href = '/templates/propertyprint.aspx?bpkey=' + BPKey + '&mainimage='+imgName;
			}
		}
	}
}

