var currentVideoUrl;

function scroller(scrollerId) {
	this.x = 1;
	this.id = scrollerId;
	this.numberOfImages = 0;
	this.imageWidth = 0;
	this.imageHeight = 0;
	this.imageThrobWidth = 0;
	this.imageThrobHeight = 0;
	this.containerWidth = 0;
	this.numberOfScreens = 0;
}

scroller.prototype.init = function() {
	var colPrev = YAHOO.util.Dom.getElementsByClassName("prev", "a", this.id);
	for (var i=0;i<colPrev.length;i++) {
		YAHOO.util.Event.addListener(colPrev[i], 'click', this.move, this, true);
	}
	var colNext = YAHOO.util.Dom.getElementsByClassName("next", "a", this.id);
	for (var i=0;i<colNext.length;i++) {
		YAHOO.util.Event.addListener(colNext[i], 'click', this.move, this, true);
	}
	var colThumbs = YAHOO.util.Dom.getElementsByClassName("thumb", "img", this.id);
	this.numberOfImages = colThumbs.length;
	if (this.numberOfImages > 0) {
		this.imageHeight = colThumbs[0].height;
		this.imageWidth = colThumbs[0].width;
		this.imageThrobHeight = colThumbs[0].height*1.08;
		this.imageThrobWidth = colThumbs[0].width*1.08;
	}	      
	for (var i=0;i<this.numberOfImages;i++) {
		YAHOO.util.Event.addListener(colThumbs[i], 'click', this.handleClick);
		YAHOO.util.Event.addListener(colThumbs[i], 'mouseover', this.handleMouseover, this, true);
		YAHOO.util.Event.addListener(colThumbs[i], 'mouseout', this.handleMouseout, this, true);
	}
}

scroller.prototype.handleMouseover = function(e) {
	if (e.srcElement && !e.target) {
		e.target = e.srcElement;
	}
	//YAHOO.util.Dom.addClass(e.target.parentNode, "selected");
	var attributes = {
		width: {to:this.imageThrobWidth},
		height: {to:this.imageThrobHeight}
	};
	var anim = new YAHOO.util.Motion(e.target, attributes, 0.3, YAHOO.util.Easing.easeOut);
	anim.animate();	
}

scroller.prototype.handleMouseout = function(e) {
	if (e.srcElement && !e.target) {
		e.target = e.srcElement;
	}
	//YAHOO.util.Dom.removeClass(e.target.parentNode, "selected");
	var attributes = {
		width: {to:this.imageWidth},
		height: {to:this.imageHeight}
	};
	var anim = new YAHOO.util.Motion(e.target, attributes, 0.3, YAHOO.util.Easing.easeOut);
	anim.animate();	
}

scroller.prototype.handleClick = function(e) {
	YAHOO.util.Event.stopEvent(e);
	var titles = YAHOO.util.Dom.getElementsByClassName("movie-title", "p", this.parentNode.parentNode);
	for (var i=0;i<titles.length;i++) {
	    document.getElementById("current-movie-title").innerHTML = titles[i].innerHTML;
	}
    loadPlayer(this.parentNode.rel, true, this.parentNode.id, this.parentNode.getAttribute('silo'));
	initStarRating();
}
		
scroller.prototype.move = function(e) {
	if (e.srcElement && !e.target) {
		e.target = e.srcElement;
	}
	YAHOO.util.Event.stopEvent(e);
	//console.log("starting to move...");
	// get the width of the element that contains the scrolling images
	this.containerWidth = document.getElementById(e.target.rel).parentNode.offsetWidth;
	var screens = (this.containerWidth / this.imageWidth);
	this.numberOfScreens = this.numberOfImages / new String(screens).charAt(0);
	this.numberOfScreens = new Number(new String(this.numberOfScreens).charAt(0));
	//this.numberOfScreens = Math.ceil(this.numberOfScreens);
	//console.log("image width: " + this.imageWidth + ", container width: " + this.containerWidth + ", number of screens: " + this.numberOfScreens);
	//console.log(this.x + " " + this.numberOfScreens);
	if (YAHOO.util.Dom.hasClass(e.target,"prev")) {
		//console.log("it's a prev...");
		if ( this.x === 1 ) {
			//console.log("value is 1 so not doing anything...");	
			return;
		}		
		var attributes = {
			points : {
				by : [this.containerWidth, 0]
			}
		};
		this.x--;
		//console.log("value of x is " + this.x);
	} else {
		//console.log(this.x + " = " + this.numberOfScreens);
		if (this.x === this.numberOfScreens) {
			//console.log("value is " + this.x + " so not doing anything...");	
			return;
		}
		var attributes = {
			points : {
				by : [-this.containerWidth, 0]
			}
		};
		this.x++;
		//console.log("value of x is " + this.x);
	}
	var anim = new YAHOO.util.Motion(e.target.rel, attributes, 0.5, YAHOO.util.Easing.easeOut);
	anim.animate();
}

var initCarousels = function() {
	var carouselElements = YAHOO.util.Dom.getElementsByClassName("carousel", "div");
	var carouselObjects = new Array();
	for (var i = 0;i<carouselElements.length;i++) {
		carouselObjects[i] = new scroller(carouselElements[i].id);
		YAHOO.util.Event.onAvailable(carouselElements[i].id, carouselObjects[i].init, carouselObjects[i], true);
	}
}

YAHOO.util.Event.onDOMReady(initCarousels);