var GET_IMAGE_URL = ""; 
var VERSION = "";
var CHECK_UPDATES_INTERVAL = 5000;
var current_movie_id = 0;

function Slider(data, container, width, height, previousPlaceholder, nextPlaceholder) {
    if (data == null) return;
	this.currentLiveMovie = -1;

	GET_IMAGE_URL = data.getAttribute("imagesServer");
	VERSION = data.getAttribute("v");
	var list = new Array();
	if (data.nodeName) {
		var lis = data.getElementsByTagName("li");
		for (var i=0;i<lis.length;i++) {
			list.push({id:lis[i].getAttribute("id"), views:lis[i].getAttribute("views"), live:lis[i].getAttribute("live"), thumb:lis[i].getAttribute("thumb")});
			if (this.currentLiveMovie == -1 && lis[i].getAttribute("live") && eval(lis[i].getAttribute("live"))) {
				this.currentLiveMovie = lis[i].getAttribute("id");
				if (this.currentLiveMovie != Common.GetMovieId()) flix.showLiveIndication(this.currentLiveMovie);
			}
		}
		if (this.currentLiveMovie == -1 || this.currentLiveMovie == Common.GetMovieId()) flix.hideLiveIndication();
	} else {
		list = data;
	}
	this.container = container;
	
    this.data = list;
	this.width = width;
	this.height = height;

	this.margin = 10;
	this.debug = false;

	this.previousPlaceholder = previousPlaceholder;
	this.nextPlaceholder = nextPlaceholder;

	var t = this;
	if (this.previousPlaceholder) {
		this.previousPlaceholder.onmouseup = function() { t.slide(1); }
		this.previousPlaceholder.onselectstart = function() { event.returnValue = false; return false; }
	}
	if (this.nextPlaceholder) {
		this.nextPlaceholder.onmouseup = function() { t.slide(-1); }
		this.nextPlaceholder.onselectstart = function() { event.returnValue = false; return false; }
	}

	this.firstOnScreen = 0;
	this.firstLoaded = -1;

	this.thumbnailsPerPage = Math.floor((parseInt(this.container.offsetWidth, 10)+this.margin)/(this.width+this.margin));

	this.lastOnScreen = this.firstOnScreen+this.thumbnailsPerPage-1;
	this.lastLoaded = -1;
	//window.setTimeout(this.CheckForUpdates, CHECK_UPDATES_INTERVAL);
	
	this.currentMovieInFocus = 0;
}
Slider.prototype.setFocus = function(movieId) {
	if (this.currentMovieInFocus > 0) {
		document.getElementById("__"+this.currentMovieInFocus).style.border = "0px";
		document.getElementById("__"+this.currentMovieInFocus).style.left = parseInt(document.getElementById("__"+this.currentMovieInFocus).style.left, 10)+2;
		document.getElementById("__"+this.currentMovieInFocus).style.top = 0;
	}
	if (!document.getElementById("__"+movieId)) {
		currentMovieInFocus = 0;
		return;
	}

	this.currentMovieInFocus = movieId;
	document.getElementById("__"+movieId).style.border = "2px solid #cccccc";
	document.getElementById("__"+movieId).style.left = parseInt(document.getElementById("__"+movieId).style.left, 10)-2;
	document.getElementById("__"+movieId).style.top = -2;
}
Slider.prototype.pushThumbnail = function(thumbnail, leftPosition) {
	var divs = this.div.getElementsByTagName("div");
	for (var i=0;i<divs.length;i++) {
		divs[i].style.left = (parseInt(divs[i].style.left, 10)+(this.width+this.margin))+"px";
	}
	var imgs = this.div.getElementsByTagName("img");
	for (var i=0;i<imgs.length;i++) {
		imgs[i].style.left = (parseInt(imgs[i].style.left, 10)+(this.width+this.margin))+"px";
	}
	this.data.splice(0, 0, thumbnail);
	this.drawSingle(thumbnail, 90);
}
Slider.prototype.findMovie = function(movieId) {
	for (var i=0;i<this.data.length;i++) {
		if (this.data[i].id == movieId) return this.data[i];
	}
	return null;
}
Slider.prototype.CheckForUpdates = function() {
	var t = this;
	var poster = new Ajax("/movie/PollForNewMovies?v="+VERSION+"&broadcasterId="+Common.GetBroadcasterId(), {method:'get', onComplete:function() {
		var slider = document.slider;
		
		var liveMovie = this.transport.responseXML.firstChild;
		var liveMovieId = liveMovie.getAttribute("id");
		
		if (liveMovieId != slider.currentLiveMovie) {
			if (slider.currentLiveMovie != -1) {
				try {
					if (document.getElementById("__"+slider.currentLiveMovie)) {
						document.getElementById("__live"+slider.currentLiveMovie).parentNode.removeChild(document.getElementById("__live"+slider.currentLiveMovie));
					}
				} catch (e) {
				}
				var lastLiveMovie = slider.findMovie(slider.currentLiveMovie);
				if (lastLiveMovie != null) lastLiveMovie.live = "false";
			}
			slider.currentLiveMovie = liveMovieId;
			
			if (liveMovieId != -1 && slider.findMovie(liveMovieId) == null) {
				document.slider.pushThumbnail({id:liveMovie.getAttribute("id"), views:liveMovie.getAttribute("views"), thumb:liveMovie.getAttribute("thumb"), live:"true"});
				flix.showLiveIndication();
			} else {
				flix.hideLiveIndication();
			}
		}
/*		VERSION = this.transport.responseXML.firstChild.getAttribute("v");
		var lis = this.transport.responseXML.firstChild.getElementsByTagName("li");

		for (var i=0;i<lis.length;i++) {
			document.slider.pushThumbnail({id:lis[i].getAttribute("id"), views:lis[i].getAttribute("views"), live:lis[i].getAttribute("live")});
		}
*/
		window.setTimeout(document.slider.CheckForUpdates, CHECK_UPDATES_INTERVAL);
	}}).request();
}
Slider.prototype.status = function() {
	alert("firstOnScreen:"+this.firstOnScreen+"\nfirst loaded:"+this.firstLoaded+"\nlast loaded:"+this.lastLoaded);
}
Slider.prototype.slide = function(value) {
	if (value > 0) {
		if (this.firstOnScreen-value < 0) value = Math.abs(0-this.firstOnScreen);
	}
	this.div.style.left = (parseInt(this.div.style.left, 10)+value*(this.width+this.margin))+"px";
	this.firstOnScreen -= value;
	this.lastOnScreen -= value;
	var currentLeft = -2;
	for (var i=this.firstOnScreen-1;i<this.firstLoaded;i++) {
		currentLeft += 1;
		if (this.data[i]) {
			this.drawThumbnail(this.data[i], (this.width+this.margin)*((document.all) ? currentLeft : currentLeft));
		}
	}
	this.firstLoaded = this.firstOnScreen-1;

	currentLeft = this.lastLoaded-this.firstOnScreen+1;

	for (var i=this.lastLoaded;i<=this.lastOnScreen+1;i++) {
		if (this.data[i]) {
			this.drawThumbnail(this.data[i], (this.width+this.margin)*((document.all) ? currentLeft : currentLeft));
		}
	}
	this.lastLoaded = (this.lastLoaded < this.lastOnScreen+1) ? this.lastOnScreen+1 : this.lastLoaded;

	this.nextPlaceholder.style.visibility = (this.lastOnScreen == this.data.length-1) ? "hidden" : "visible";
	this.previousPlaceholder.style.visibility = (this.firstOnScreen == 0) ? "hidden" : "visible";

	this.hideOffscreenThumbnails();
}
Slider.prototype.hideOffscreenThumbnails = function() {
	var thumbnails = this.div.childNodes;
	for (var i=0;i<thumbnails.length;i++) {
		if (parseInt(thumbnails[i].style.left, 10) + parseInt(this.div.style.left, 10) >= -2 &&
			parseInt(thumbnails[i].style.left, 10) + parseInt(this.div.style.left, 10) < 630) {
			thumbnails[i].style.display = "";
		} else {
			thumbnails[i].style.display = "none";
		}
	}
}
Slider.prototype.clickLive = function() {
	document.slider.setFocus(this.currentLiveMovie);
	movie.playIt(this.currentLiveMovie);
	flix.hideLiveIndication();
}
Slider.prototype.clickThumbnail = function()
{
	//debugger;
    current_movie_id = this.movieId;
    document.slider.setFocus(this.movieId);
	movie.playIt(this.movieId);
}
Slider.prototype.draw = function() {
	//debugger;
    this.clippedDiv = document.createElement("div");
	this.clippedDiv.style.position = "absolute";
	this.clippedDiv.style.width = this.container.offsetWidth+(this.width+10)*2;
	this.clippedDiv.style.height = this.height+100;
	this.clippedDiv.style.backgroud = "green";
	this.container.appendChild(this.clippedDiv);

	this.div = document.createElement("div");
	this.div.style.position = "absolute";
	this.div.style.width = this.container.offsetWidth+(this.width+10)*2;
	this.div.style.height = this.height;
	this.div.style.left = (-(this.width+this.margin))+"px";
	if (!this.debug) {
		this.clippedDiv.style.clip = "rect(-2px "+eval(parseInt(this.container.offsetWidth, 10)+2)+"px "+(this.height+100)+"px -2px)";
	} else {
		var __left = document.createElement("div");
		__left.style.cssText = "border-right:1px solid black; z-index:1; position:absolute; left:"+eval(-(this.width+this.margin))+"; width:"+eval(this.width+this.margin)+"; height:100; background:white; -moz-opacity:.50;filter:alpha(opacity=50); ";
		this.container.appendChild(__left);

		var __right = document.createElement("div");
		__right.style.cssText = "border-left:1px solid black; z-index:1; position:absolute; left:"+eval(this.container.offsetWidth)+"; width:"+eval(this.width+this.margin)+"; height:100; background:white; -moz-opacity:.50;filter:alpha(opacity=50);";
		this.container.appendChild(__right);
	}

	this.clippedDiv.appendChild(this.div);

	var currentLeft = -1;
	for (var i=this.firstOnScreen-1;i<this.firstOnScreen+this.thumbnailsPerPage+1;i++) {
		currentLeft += 1;
		if (!this.data[i]) continue;

		if (this.firstLoaded == -1) this.firstLoaded = i;
		this.lastLoaded = i;
		this.drawSingle(this.data[i], (this.width+this.margin)*currentLeft);
	}

	this.nextPlaceholder.style.visibility = (this.lastOnScreen >= this.data.length-1) ? "hidden" : "visible";
	this.previousPlaceholder.style.visibility = (this.firstOnScreen == 0) ? "hidden" : "visible";
	
	this.setFocus(Common.GetMovieId());
}
Slider.prototype.drawSingle = function(thumbnail, leftPosition) {
	if (document.getElementById("__"+thumbnail.id)) return;
	var img = document.createElement("img");
	img.style.cursor = (document.all) ? "hand" : "pointer";
	img.alt = thumbnail.id;
	img.id = "__"+thumbnail.id;
	img.src = thumbnail.thumb;// GET_IMAGE_URL.replace(/\[SID\]/, thumbnail.id);;
	img.width = this.width;
	img.height = this.height;
	img.align = "left";
	img.style.position = "absolute";
	img.style.left = leftPosition+"px";
	img.movieId = thumbnail.id;
	img.onclick = this.clickThumbnail;
	img.style.cursor = "hand";
	this.div.appendChild(img);

	var viewsDiv = document.createElement("div");
	if(thumbnail.views == "0")
    {
      viewsDiv.innerHTML = "NO VIEWS";
    }
    else if(thumbnail.views == "1")
    {
      viewsDiv.innerHTML = "1 VIEW";
    
    }
    else
    {
      viewsDiv.innerHTML = thumbnail.views +" VIEWS";
    
    }
    //debugger;
	viewsDiv.style.position = "absolute";
	viewsDiv.style.left = leftPosition+"px";
	viewsDiv.style.top = "60px";
	viewsDiv.style.width = this.width+"px";
	viewsDiv.align = "center";
	viewsDiv.movieId = thumbnail.id;
	viewsDiv.onclick = this.clickThumbnail;
	viewsDiv.style.cursor = "hand";
	viewsDiv.className = "itemView";
	this.div.appendChild(viewsDiv);
	
	if (thumbnail.live == "true") {
		var liveDiv = document.createElement("img");
		liveDiv.id = "__live"+thumbnail.id;
		liveDiv.style.position = "absolute";
		liveDiv.style.left = eval(leftPosition+2)+"px";
		liveDiv.style.top = "45px";
		liveDiv.src = "/images/live.png";
		this.div.appendChild(liveDiv);
	}
	
}
Slider.prototype.drawThumbnail = function(thumbnail, left)
{	
	this.drawSingle(thumbnail, (left-parseInt(this.div.style.left, 10)));
}
