
Object.prototype.method = function (method) {
	var context = this;
	return function () {
		method.apply(context, arguments);
		if (this.blur && this.tagName=="A") this.blur();
		return false;
	}
}

var photoIds;
var photoPaths;
var imagePath;
var photoIndex;
var scrolled ;
var VISIBLE_ITEMS = 7;
var SCROLL_MOVE_DISTANCE = 100;
var START_OFFSET_MAGIC_NUMBER = -88;
var hasScrolling;
var log;
var s;
var scrollor;
var isListingPhotoPageLoadedJs = false;

var rfScrollableSlideShowManager = {

	clickBack : function (theLink)
	{
		if (isListingPhotoPageLoadedJs && window.history && window.history.length && window.history.length > 1) {
			window.history.go (-1);
			return false;
		} 
		
		return true;
	},

	preload : function ()
	{
		for (var i = photoIndex; i < photoIds.length; i++) {
			var img = new Image();
			img.src = 	rfScrollableSlideShowManager.photoUrl (i);
		}
		for (var i = 0; i < photoIndex; i++) {
			var img = new Image();
			img.src = 	rfScrollableSlideShowManager.photoUrl (i);
		}
	},

	photoUrl : function (Id) {
		//return imagePath + (photoId % 100) + '/' + photoId + '_full.jpg';
		return photoPaths[Id];
	},

	addHandlers : function () {
		var ul = document.getElementById("ScrollableSlideShowThumbs");
		
		if (!ul)
			return;
			
		var as = ul.getElementsByTagName("a");
		
		scrollor = document.getElementById("ScrollableSlideShowInner");
			
		//	create new instance of Scrolley class. assign to scrollor object
		s = new Scrolley(
			scrollor.getElementsByTagName("div")[0]
			, scrollor.getElementsByTagName("ul")[0]
			, scrollor.getElementsByTagName("li")
			, VISIBLE_ITEMS
			, SCROLL_MOVE_DISTANCE
			, 10000
			, VISIBLE_ITEMS + photoIndex
		);
			
	},

	moveAbsoluteEasy : function (e)
	{
	
		if (photoIds.length == 1)
			return;
		
		var idToFind = e.className.replace('t_', '');
		var i;
		for (i = 0; i < photoIds.length; i++) {
			if (photoIds[i] == idToFind) {
				break;
			}
		}
		
		var eles = scrollor.getElementsByTagName("li");
		
		var j;
		for (j = 0; j < eles.length; j++) {
			if (eles[j].className == 'selectedThumb') {
				// this is our selected item so now we know the which item we need to find from here. 
				break;
			}
		}
		
		if (i < j) {
			rfScrollableSlideShowManager.moveBack(j - i);
		}
		
		if (i > j) {
			rfScrollableSlideShowManager.moveNext(i - j);
		}	
		
		return false;
	},

	moveAbsolute : function (e)
	{
		if (s.animating) 
			return false;
		
		var newIndex;
				
		var idToFind = e.className.replace('t_', '');
		for (var i = 0; i < photoIds.length; i++) {
			if (photoIds[i] == idToFind) {
				newIndex = i;
				break;
			}
		}
		
		var first, second, moveRight;
		var currentIndex = scrolled % s.totalItems;
		if (newIndex > currentIndex) {
			first = newIndex ;
			second = currentIndex;
			moveRight = true;
		} else {
			first = currentIndex ;
			second = newIndex;
			moveRight = false;
		}
		
		var distMove;
			//	HACK:
		if (first - second > parseInt(VISIBLE_ITEMS/2) + 1) {
			switch (first - second) {
				case photoIds.length - 1:
					distMove = 1
					break;
				case photoIds.length - 2:
					distMove = 2
					break;
				case photoIds.length - 3:
					distMove = 3
					break;
				default:
					distMove = 4
					break;
			}
			moveRight = !moveRight;
		} else {
			distMove = (first - second);
		}
		
		var x = s.index;
		var moved = 0;
		for (var i = 0; i < distMove; i++) {
	
			if (moveRight) {
				moved++;
				s.index++
			} else {
				moved--;
				s.index--;
			}
			
			if (s.index == 0)
				s.index = s.totalItems;				
			if (s.index == s.totalItems + s.visibleItems)
				s.index = s.visibleItems;
		}	
		
		if (moved != 0) {
			
			if (moved < 0) 
				moved *= -1;
				
			if (moved > 1)
				s.skip = moved - 1;
				
			s.manual();
			
			if (moveRight) {
				rfScrollableSlideShowManager.moveNext(moved);
					s.index--;
					s.forward();
			} else {
				rfScrollableSlideShowManager.moveBack(moved);
				s.index++;
				s.back();
			}
			
			if (moveRight) {
				rfScrollableSlideShowManager.moveNext(moved);
			} else {
				rfScrollableSlideShowManager.moveBack(moved);
			}
		}
		
		return false;
		
	},

	moveNext : function (num)
	{
		if (s.animating) 
			return;
		
		var ogPhotoIndex = photoIndex;
		
		if (!num) 
			num = 1;
		
		for (var i = 0 ; i < num; i++) {
			scrolled++;
			
			if (++photoIndex >= photoIds.length)
				photoIndex = 0;
		}
		
		rfScrollableSlideShowManager.changeImages ( photoIds[photoIndex], photoIds[ogPhotoIndex],photoIndex);		
		rfScrollableSlideShowManager.changeCounter();	
	},

	moveBack : function (num)
	{
		if (s.animating) 
			return;
			
		var ogPhotoIndex = photoIndex;
		
		if (!num) 
			num = 1;
			
		for (var i = 0 ; i < num; i++) {
			scrolled--;
			
			if (--photoIndex < 0)
				photoIndex = photoIds.length - 1;
		}
	
		rfScrollableSlideShowManager.changeImages ( photoIds[photoIndex], photoIds[ogPhotoIndex],photoIndex);		
		rfScrollableSlideShowManager.changeCounter();
	},

	changeImages : function (photoId, ogPhotoId,id)
	{
		var mainImg = document.getElementById("ScrollableSlideShowMainImage");
		
		if(mainImg)
			mainImg.src = rfScrollableSlideShowManager.photoUrl (id);
		 
		//	switch out the thumbnails 
		var thumbs = scrollor.getElementsByTagName("li");
		var links = scrollor.getElementsByTagName("a");
		
		for (var i = 0; i < thumbs.length; i++) {
			if (links[i].className == 't_' + photoId) {
				thumbs[i].className = 'selectedThumb';
			} else {
				thumbs[i].className = '';
			}			
		}		
	},

	changeCounter : function ()
	{
		//var counter = document.getElementById("pCounter");
	
		//counter.innerHTML = (photoIndex + 1) + ' of ' + photoIds.length;
	},
	
	positionFade : function ()
	{
		if (!hasScrolling || (!document.getElementById || !document.getElementsByName || !document.createElement) )
			return;
	
		var g = document.getElementById("gradForNonBranded");
		var g2 = document.getElementById("gradForNonBranded2");
			
		if (g)
		{
			var l = ((document.body.clientWidth - 670) / 2) + 9;
			
			g.style.left = parseInt(l) + "px";
			g.style.display ="inline";
		}
		
		if (g2)
		{
			var r = document.body.clientWidth - l + 10;
			g2.style.left = parseInt(r) + "px";
			g2.style.display ="inline";
		}
	},

	addEvent : function(elm, evType, fn, useCapture)
	{
		if (elm.addEventListener)
		{ 
			elm.addEventListener(evType, fn, useCapture); 
			return true; 
		}
		else if (elm.attachEvent)
		{ 
			var r = elm.attachEvent('on' + evType, fn); 
			return r; 
		}
		else
		{
		elm['on' + evType] = fn;
		}
	}
}


/*	TODO: Move into include?	*/
Scrolley = function (scrollableElement
	, itemContainer
	, items
	, visibleItems
	, scrollDistance
	, initialDelay
	, selectedIndex) {
	
	this.itemSize = scrollDistance;
	this.originalItemSize = scrollDistance;
	this.visibleItems = visibleItems;
	this.orientation = 0;
	this.skip = 0;

	this.scrollable = scrollableElement;
	this.items = items;
	this.totalItems = this.items.length;
	
	//	the list is left orientated by default. this value allows us to center it
	//	88 is a magic number 
	this.tweakValue = -2 * this.itemSize + START_OFFSET_MAGIC_NUMBER;
		
	var scrollor = this;
	
	if (this.totalItems <= this.visibleItems) {
	
		var leftbutton = document.getElementById("ScrollableSlideShowMoveLeft");	
		leftbutton.onclick = function () {
			this.blur();
			rfScrollableSlideShowManager.moveBack();
			return false
		}

		var rightbutton = document.getElementById("ScrollableSlideShowMoveRight");	
		rightbutton.onclick = function () {
			this.blur();
			rfScrollableSlideShowManager.moveNext();
			return false
		}
	
		return;
	}

	var leftbutton = document.getElementById("ScrollableSlideShowMoveLeft");	
	leftbutton.onclick = function () {
		this.blur();
		rfScrollableSlideShowManager.moveBack();
		scrollor.manual();
		scrollor.back();
		return false
	}

	var rightbutton = document.getElementById("ScrollableSlideShowMoveRight");	
	rightbutton.onclick = function () {
		this.blur();
		rfScrollableSlideShowManager.moveNext();
		scrollor.manual();
		scrollor.forward();
		return false
	}
	
	var bottomClones = new Array(this.visibleItems);
	var topClones = new Array(this.visibleItems);

	/* Duplicate head and tail of the item list */
	for (var i = 0; i < this.visibleItems; i++) {
	
		bottomClones[i] = this.items[this.totalItems - 1 - i].cloneNode(true);
		bottomClones[i].onclick = this.items[this.totalItems - 1 - i];

		topClones[i] = this.items[i].cloneNode(true);
		topClones[i].onclick = this.items[i];
	}

	/* Grow the list */
	for (var i = 0; i < this.visibleItems; i++) {
		itemContainer.insertBefore(bottomClones[i], this.items[0]);
		itemContainer.appendChild(topClones[i]);
	}
	
	this.index = selectedIndex;
	if (this.orientation)
		this.scrollable.scrollTop = this.index * this.itemSize;
	else
		this.scrollable.scrollLeft = this.index * this.itemSize + this.tweakValue;

	this.scrollForward = new Animator(
		0
		, this.itemSize
		, this.method(this.animateForward)
		, this.method(this.checkIndex)
	);
	
	this.scrollForward.setStep(3);
	this.scrollForward.setType(this.scrollForward.EASEINOUT);
	this.scrollForward.setRate(5);
	
	this.scrollBack = new Animator(
		0
		, this.itemSize
		, this.method(this.animateBack)
		, this.method(this.checkIndex)
	);
	this.scrollBack.setStep(3);
	this.scrollBack.setType(this.scrollForward.EASEINOUT);
	this.scrollBack.setRate(5);
	
	itemContainer.controller = this;
	
	//	uncomment to make scrolling pause when mouseover occurs
	//itemContainer.onmouseover = function (){this.controller.pause()};
	//itemContainer.onmouseout = function (){if (!isChild(itemContainer,this) || this == itemContainer) this.controller.resume()};

	//	uncomment to have phtoos scroll at random
	//this.auto = setTimeout(this.method(this.automatic), initialDelay);

}

Scrolley.prototype.setHorizontalMode = function () {this.orientation = 0}
Scrolley.prototype.setVerticalMode = function () {this.orientation = 1}

Scrolley.prototype.automatic = function () {
	if (this.isPaused) {
		this.restart = setTimeout(this.method(this.automatic), 5000);
		return;
	}
	this.forward();
	if (this.restart)
		clearInterval(this.restart);
	this.restart = null;
	this.auto = setInterval(this.method(this.forward), 14000);
}
Scrolley.prototype.manual = function () {
	if (!this.auto || this.isPaused)
		return;
	if (this.animating) {
		this.scrollForward.stop();
		this.checkIndex();
		this.animating = false;
	}
	if (this.auto)
		clearInterval(this.auto);
	this.auto = null;
	if (this.restart)
		clearInterval(this.restart);
	this.restart = setTimeout(this.method(this.automatic), 5000);
}
Scrolley.prototype.forward = function () {
	if (this.animating)
		return;
		
	this.animating = this.scrollForward;
	this.index++;

	this.scrollForward.start();

}
Scrolley.prototype.animateForward = function (value) {
	var move = ((this.index - 1) * this.itemSize + value) + this.tweakValue - this.tweak(this.itemSize - value);
	
	if (move < 0)
		move += (this.totalItems * this.itemSize);
		
	if (this.orientation)
		this.scrollable.scrollTop = move;
	else
		this.scrollable.scrollLeft = move;

}
Scrolley.prototype.back = function () {
	if (this.animating)
		return;
		
	this.animating = this.scrollBack;
	this.index--;

	this.scrollBack.start();
}
Scrolley.prototype.animateBack = function (value) {
	var move = ((this.index + 1) * this.itemSize - value) + this.tweakValue + this.tweak(this.itemSize - value);
		
	if (move < 0)
		move += (this.totalItems * this.itemSize);
	if (this.orientation)
		this.scrollable.scrollTop = move;
	else
		this.scrollable.scrollLeft = move;
		
}
Scrolley.prototype.checkIndex = function () {
	if (this.index <= 0  )
		this.index = this.totalItems;
	if (this.index >= this.totalItems + this.visibleItems)
		this.index = this.visibleItems;
		
	var move = (this.index*this.itemSize) + this.tweakValue;
	//	move += this.tweak(this.itemSize);
		
	// reset item size if needed
	this.itemSize = this.originalItemSize;
	this.skip = 0;
	

	if (move < 0) 
		move += (this.totalItems * this.itemSize);
		
	if (this.orientation)
		this.scrollable.scrollTop = move;
	else
		this.scrollable.scrollLeft = move;
//log.innerHTML += '<br />fin ' + move;

	this.animating = false;
}
Scrolley.prototype.tweak = function(value) {
	//	this value is similar to the method used to calculate the distance to move
	//	in the animator class. we can use it along with skip to move more than one
	//	item at a time.
	return (1 - Math.pow(1 - value / 100, 2)) * (this.itemSize * this.skip);

}
Scrolley.prototype.pause = function () {
	if (this.animating) {
		this.animating.pause();
	} else {
		this.manual();
	}
	this.isPaused = true;
}
Scrolley.prototype.resume = function () {
	if (this.animating) {
		this.animating.resume();
	}
	this.isPaused = false;
}



