	


var DvdGalleryLinks = Class.create();
DvdGalleryLinks.prototype = {
	STATES : { READY : 0, LOADING : 1 , TIMEOUT : 2, FAILURE : 3 },
	TEMPLATES : { 
		LOADING : "<p class=\"loading\">Loading movies...</p>",
		TIMEOUT : "<p class=\"error\">Hmmm, no response...Please refresh your browser and try again.</p>",
		FAILURE : "<p class=\"error\">Whoops, our servers coughed.  Please refresh your browser and try again.</p>"
	},
	initialize : function(language) {
		this.galleries = $A( $('browse').getElementsByTagName('A') );
		this.state = this.STATES.READY;
		this.language = language;
		this.bind();
	},
	bind : function() {
		this.galleries.each(
			function(g) { 
				Event.observe(g, "click", this.onClick.bindAsEventListener(this));
			}.bind(this)
		);
	},
	onClick : function(event) {
		Event.stop(event);
		if (this.state != this.STATES.LOADING) {
			this.link = Event.element(event);
			this.state = this.STATES.LOADING;
			this.paint();
			$('gallery').value = Element.readAttribute(this.link, "gallery");
			$('page').value = "1";
			var params = Form.serialize('filters');
			new Ajax.Request('/' + this.language + '/movies/browse', { parameters : params , onSuccess:this.onSuccess.bind(this), onFailure:this.onFailure.bind(this), onTimeout:this.onTimeout.bind(this) });
		}
	},
	onSuccess : function(response) {
		$('movies').update(response.responseText);
		new DvdPaginationLinks(this.language);
		new DvdSortingLinks(this.language);
		this.state = this.STATES.READY;
		this.paint();
	},
	onFailure : function() {
		this.state = this.STATES.FAILURE;
		this.paint();
	},
	onTimeout : function() {
		this.state = this.STATES.TIMEOUT;
		this.paint();
	},
	paint : function() {
		this.galleries.each(
			function(l) { 
				Element.removeClassName(l.parentNode, "selected"); 
			}
		);
		Element.addClassName(this.link.parentNode, "selected");		
		switch (this.state) {
			case this.STATES.READY: 
				break;
			case this.STATES.LOADING: 
				$('movies').update(this.TEMPLATES.LOADING);
				break;
			case this.STATS.TIMEOUT: 
				$('movies').update(this.TEMPLATES.TIMEOUT);
				break;
			case this.STATES.FAILURE:
				$('movies').update(this.TEMPLATES.FAILURE);
				break;
		}
	}
};

var descWidget = {
	showMore : function() {
		Element.hide('readMoreLink');
		Element.show('extraDesc');
		Element.show('hideMoreLink');
	},
	hideMore : function() {
		Element.show('readMoreLink');
		Element.hide('extraDesc');
		Element.hide('hideMoreLink');
	}
};


var DvdPaginationLinks = Class.create();
DvdPaginationLinks.prototype = {
	STATES : { READY : 0, LOADING : 1 , TIMEOUT : 2, FAILURE : 3 },
	TEMPLATES : { 
		LOADING : "<p class=\"loading\">Loading movies...</p>",
		TIMEOUT : "<p class=\"error\">Hmmm, no response...Please refresh your browser and try again.</p>",
		FAILURE : "<p class=\"error\">Whoops, our servers coughed.  Please refresh your browser and try again.</p>"
	},
	initialize : function(language) {
		if ($('pagination') != null) {
			this.links = $A( $('pagination').getElementsByTagName('A') );
			this.state = this.STATES.READY;
		  this.language = language;
			this.bind();
		}
	},
	bind : function() {
		this.links.each(
			function(g) { 
				Event.observe(g, "click", this.onClick.bindAsEventListener(this));
			}.bind(this)
		);
	},
	onClick : function(event) {
		Event.stop(event);
		if (this.state != this.STATES.LOADING) {
			this.link = Event.element(event);
			this.page = Element.readAttribute(this.link, "page");
			this.state = this.STATES.LOADING;
			this.paint();
			$('page').value = this.page;
			var params = Form.serialize('filters');
			new Ajax.Request('/' + this.language + '/movies/browse', { parameters : params , onSuccess:this.onSuccess.bind(this), onFailure:this.onFailure.bind(this), onTimeout:this.onTimeout.bind(this) });
		}
	},
	onSuccess : function(response) {
		$('movies').update(response.responseText);
		new DvdPaginationLinks(this.language);
		new DvdSortingLinks(this.language);
		this.state = this.STATES.READY;
		this.paint();
	},
	onFailure : function() {
		this.state = this.STATES.FAILURE;
		this.paint();
	},
	onTimeout : function() {
		this.state = this.STATES.TIMEOUT;
		this.paint();
	},
	paint : function() {
		this.links.each(
			function(l) { 
				Element.removeClassName(l, "selected"); 
			}
		);
		Element.addClassName(this.link, "selected");		
		switch (this.state) {
			case this.STATES.READY: 
				break;
			case this.STATES.LOADING: 
				$('movies').update(this.TEMPLATES.LOADING);
				break;
			case this.STATS.TIMEOUT: 
				$('movies').update(this.TEMPLATES.TIMEOUT);
				break;
			case this.STATES.FAILURE:
				$('movies').update(this.TEMPLATES.FAILURE);
				break;
		}
	}
};

var DvdSortingLinks = Class.create();
DvdSortingLinks.prototype = {
	STATES : { READY : 0, LOADING : 1 , TIMEOUT : 2, FAILURE : 3 },
	TEMPLATES : { 
		LOADING : "<p class=\"loading\">Loading movies...</p>",
		TIMEOUT : "<p class=\"error\">Hmmm, no response...Please refresh your browser and try again.</p>",
		FAILURE : "<p class=\"error\">Whoops, our servers coughed.  Please refresh your browser and try again.</p>"
	},
	initialize : function(language) {
		this.links = $A( $('sorting').getElementsByTagName('A') );
		this.state = this.STATES.READY;
		this.language = language;
		this.bind();
	},
	bind : function() {
		this.links.each(
			function(g) { 
				Event.observe(g, "click", this.onClick.bindAsEventListener(this));
			}.bind(this)
		);
	},
	onClick : function(event) {
		Event.stop(event);
		if (this.state != this.STATES.LOADING) {
			this.link = Event.element(event);
			this.sort = Element.readAttribute(this.link, "sort");
			this.state = this.STATES.LOADING;
			this.paint();
			$('sort').value = this.sort;
			$('page').value = "1";
			var params = Form.serialize('filters');
			new Ajax.Request('/' + this.language + '/movies/browse', { parameters : params , onSuccess:this.onSuccess.bind(this), onFailure:this.onFailure.bind(this), onTimeout:this.onTimeout.bind(this) });
		}
	},
	onSuccess : function(response) {
		$('movies').update(response.responseText);
		new DvdPaginationLinks(this.language);
		new DvdSortingLinks(this.language);
		this.state = this.STATES.READY;
		this.paint();
		urchinTracker('MoviesDVDBrowse');
	},
	onFailure : function() {
		this.state = this.STATES.FAILURE;
		this.paint();
	},
	onTimeout : function() {
		this.state = this.STATES.TIMEOUT;
		this.paint();
	},
	paint : function() {
		this.links.each(
			function(l) { 
				Element.removeClassName(l, "selected"); 
			}
		);
		Element.addClassName(this.link, "selected");		
		switch (this.state) {
			case this.STATES.READY: 
				break;
			case this.STATES.LOADING: 
				$('movies').update(this.TEMPLATES.LOADING);
				break;
			case this.STATS.TIMEOUT: 
				$('movies').update(this.TEMPLATES.TIMEOUT);
				break;
			case this.STATES.FAILURE:
				$('movies').update(this.TEMPLATES.FAILURE);
				break;
		}
	}
};

var DvdFilterLinks = Class.create();
DvdFilterLinks.prototype = {
	STATES : { READY : 0, LOADING : 1 , TIMEOUT : 2, FAILURE : 3 },
	TEMPLATES : { 
		LOADING : "<p class=\"loading\">Loading movies...</p>",
		TIMEOUT : "<p class=\"error\">Hmmm, no response...Please refresh your browser and try again.</p>",
		FAILURE : "<p class=\"error\">Whoops, our servers coughed.  Please refresh your browser and try again.</p>"
	},
	initialize : function() {
		Event.observe('filterMinRatings', "click", this.onClick.bindAsEventListener(this));
		if ($('filterAlreadyRated') != null) {
			Event.observe('filterAlreadyRated', "click", this.onClick.bindAsEventListener(this));
		}
		this.state = this.STATES.READY;
	},
	onClick : function(event) {
		if (this.state != this.STATES.LOADING) {
			this.state = this.STATES.LOADING;
			this.paint();
			$('page').value = "1";
			var params = Form.serialize('filters');
			new Ajax.Request('/movie.sv', { parameters : params , onSuccess:this.onSuccess.bind(this), onFailure:this.onFailure.bind(this), onTimeout:this.onTimeout.bind(this) });
		}
	},
	onSuccess : function(response) {
		$('movies').update(response.responseText);
		new DvdPaginationLinks(this.language);
		new DvdSortingLinks(this.language);
		this.state = this.STATES.READY;
		this.paint();
		urchinTracker('MoviesDVDBrowse');
	},
	onFailure : function() {
		this.state = this.STATES.FAILURE;
		this.paint();
	},
	onTimeout : function() {
		this.state = this.STATES.TIMEOUT;
		this.paint();
	},
	paint : function() {	
		switch (this.state) {
			case this.STATES.READY: 
				break;
			case this.STATES.LOADING: 
				$('movies').update(this.TEMPLATES.LOADING);
				break;
			case this.STATS.TIMEOUT: 
				$('movies').update(this.TEMPLATES.TIMEOUT);
				break;
			case this.STATES.FAILURE:
				$('movies').update(this.TEMPLATES.FAILURE);
				break;
		}
	}
};

var adWidget = {
	dirty : false,
	timer : null,
	handleRefresh : function() {
		if (this.dirty) {
			var params = "getAd=&size=300x250";
			new Ajax.Updater('ad1', '/ad.do', {parameters:params, evalScripts:false});			
			this.dirty = false;
			this.timer = setTimeout(function() { adWidget.dirty = true; }, 20000);
		} else {
			if (this.timer == null) {
				this.timer = setTimeout(function() { adWidget.dirty = true; }, 20000);
			}
		}
	}
};
adWidget.timer = setTimeout(function() { adWidget.dirty = true; }, 20000);

var quickRateWidget = {
	STATES : { READY : 0, LOADING_BATCH : 1, LOADING_MOVIE : 2, EMPTY : 3 },
	state : 0,
	movies : new Array(),
	language : 1,
	index : -1,
	position : 0,
	handleBatch : function(language) {
		this.language = language;
		this.state = this.STATES.LOADING_BATCH;
		var params = "quickRateBatch=&position="+this.position;
		new Ajax.Request('/' + language + '/movies/quickrate', {parameters:params, onComplete:this.processBatch.bind(this)});
	},
	processBatch : function(response) {
		this.state = this.STATES.READY;
		response.responseText.evalScripts();
		if (this.movies.length > this.index + 1) {
			this.handleNext(this.language);
		} else {
			this.handleEmpty(response);
		}
	},
	handleEmpty : function (response) {
		this.states = this.STATES.EMPTY;
		$('panel').update( response.responseText );
	},
	handleNext : function() {
		if (this.index < this.movies.length-1) {
			this.state = this.STATES.LOADING_MOVIE;
			this.index += 1;
			var nextParams = "nextQuickRate=&movieId="+this.movies[this.index];
			new Ajax.Request('/' + this.language + '/movies/quickrate', {parameters:nextParams, onComplete:this.processNext.bind(this)});
			
			if (this.index-1 >= 0) {
				var lastParams = "lastQuickRate=&movieId="+this.movies[this.index-1];
				new Ajax.Request('/' + this.language + '/movies/quickrate', {parameters:lastParams, onComplete:this.processPrevious.bind(this)});
			}
		} else {
			this.handleBatch();
		}
	},
	handlePrevious : function() {
		if (this.index > 0) {
			this.index -= 1;
			var nextParams = "nextQuickRate=&movieId="+this.movies[this.index];
			new Ajax.Request('/' + this.language + '/movies/quickrate', {parameters:nextParams, evalScripts:true, onComplete:this.processNext.bind(this)});
			
			var lastParams = "lastQuickRate=&movieId="+this.movies[this.index-1];
			new Ajax.Request('/' + this.language + '/movies/quickrate', {parameters:lastParams, evalScripts:true, onComplete:this.processPrevious.bind(this)});
		}
	},
	processNext : function(response) {
		this.state = this.STATES.READY;
		$('panel').update( response.responseText );
		if (this.index > 0) {
			
		}
		//adWidget.handleRefresh();;
	},
	processPrevious : function(response) {
		$('results').update( response.responseText );	
	},
	paint : function() {
		switch (this.state) {
			case this.STATES.READY:
				break;
			case this.STATES.LOADING:
				break;
		}
	}
};


var synopsisWidget = {
	showMore : function() {
		Element.hide('readMoreLink');
		Element.show('extraSynopsis');
	}
};

var textWidget = {
	showMore : function(link, text, less) {
		$(link).hide();
		$(text).show();
		$(less).show();
	},
	hideMore : function(link, text, less) {
		$(link).show();
		$(text).hide();
		$(less).hide();
	}
};

var browseWidget = {
	STATES : { READY : 0, LOADING : 1 },
	genreId : 'new',
	sort : 'byRelease',
	filterMinRatings : "off",
	filterAlreadyRated : "off",
	page : 1,
	initialize : function(genreId, sort, filterMinRatings, filterAlreadyRated, page) {
		this.genreId = genreId;
		this.sort = sort;
		this.filterMinRatings = filterMinRatings;
		this.filterAlreadyRated = filterAlreadyRated;
		this.page = page;
	},
	handleGenreClick : function(genreId) {
		if (this.state != null && this.state == this.STATES.LOADING) { alert("Please wait while we process your request"); return; }
		this.state = this.STATES.LOADING;
		this.genreId = genreId;
		this.page = 1;
		this.paint();
		var params = this.serialize();
		new Ajax.Request('/movie.sv', {parameters:params, onSuccess:this.processSearchRequest.bind(this), onFailure:this.processForward.bind(this), onTimeout:this.processForward.bind(this)});
	},
	handleSortClick : function(sort) {
		if (this.state != null && this.state == this.STATES.LOADING) { alert("Please wait while we process your request"); return; }
		this.state = this.STATES.LOADING;
		this.sort = sort;
		this.page = 1;
		this.paint();

		var params = this.serialize();
		new Ajax.Request('/movie.sv', {parameters:params, onSuccess:this.processSearchRequest.bind(this), onFailure:this.processForward.bind(this)});
	},
	handleFilterClick : function() {
		if (this.state != null && this.state == this.STATES.LOADING) { alert("Please wait while we process your request"); return false; }
		
		if ("on" == $F('filterMinRatings')) {
			this.filterMinRatings = "on";
		} else {
			this.filterMinRatings = "off";			
		}
		
		if ("on" == $F('filterAlreadyRated')) {
			this.filterAlreadyRated = "on";
		} else {
			this.filterAlreadyRated = "off";
		}
		this.page = 1;
		this.state = this.STATES.LOADING;
		this.paint();

		var params = this.serialize();
		new Ajax.Request('/movie.sv', {parameters:params, onComplete:this.processSearchRequest.bind(this), onFailure:this.processForward.bind(this), onTimeout:this.processForward.bind(this)});
	},
	handePageClick : function(page) {
		if (this.state != null && this.state == this.STATES.LOADING) { alert("Please wait while we process your request"); return; }
		this.state = this.STATES.LOADING;
		this.page = page;
		this.paint();

		var params = this.serialize();
		new Ajax.Request('/movie.sv', {parameters:params, onComplete:this.processSearchRequest.bind(this), onFailure:this.processForward.bind(this), onTimeout:this.processForward.bind(this)});
	},
	processSearchRequest : function(response) {
		response.responseText.evalScripts(); //to initialize rating widgets
		$('scaffold').update( response.responseText );
		this.state = this.STATES.READY;
		this.paint();
		urchinTracker('MoviesDVDBrowse');
	},
	processForward : function() {
		document.location = '/movies.do?browseDvds='+this.serialize();
	},
	paint : function() {
		switch(this.state) {
			case this.STATES.READY:
				$('loading').hide();
				$('scaffold').show();
				break;
			case this.STATES.LOADING:
				$('scaffold').hide();
				$('loading').show();
				break;
		}
		$A($('listTypes').getElementsByTagName("li")).each( function(value, index) { 
			if (value.id == "genre"+browseWidget.genreId) {
				Element.addClassName(value, "selected");
			} else if (Element.hasClassName(value, "selected")) {
				Element.removeClassName(value, "selected");
			}
		} );

		$A($('genreList').getElementsByTagName("li")).each( function(value, index) { 
			if (value.id == "genre"+browseWidget.genreId) {
				Element.addClassName(value, "selected");
			} else if (Element.hasClassName(value, "selected")) {
				Element.removeClassName(value, "selected");
			}
		} );
		
		$A($('sortList').getElementsByTagName("li")).each( function(value, index) { 
			if (value.id == "sort"+browseWidget.sort) {
				Element.addClassName(value, "selected");
			} else if (Element.hasClassName(value, "selected")) {
				Element.removeClassName(value, "selected");
			}
		} );
	},
	serialize : function() {
		var params = "movieAction=browseMovies&genreId="+this.genreId+"&sort="+this.sort+"&pageNav="+this.page;
		if (this.filterMinRatings == "on") { params += "&filterMinRatings=" + this.filterMinRatings; }
		if (this.filterAlreadyRated == "on") { params += "&filterAlreadyRated=" + this.filterAlreadyRated; }
		return params;
	}
};

var previewWidget = {
	STATES : { READY : 0, LOADING : 1 },
	handlePreview : function(movieId) {
		this.state = this.STATES.LOADING;
		this.paint();
		var params = "movieAction=previewMovie&movieId="+movieId;
		new Ajax.Request('/movie.sv', {parameters:params, onComplete:this.processPreview.bind(this)});
	},
	processPreview : function(response) {
		$('preview').update(response.responseText);
		this.state = this.STATES.READY;
		this.paint();
	},
	paint : function() {
		switch(this.state) {
			case this.STATES.READY:
				$('previewLoading').hide();
				$('preview').show();
				break;
			case this.STATES.LOADING:
				$('preview').hide();
				$('previewLoading').show();
				break;
		}
	}
};


/*******************************************************************************
 SUGGESTION (LINK) VOTING
 *******************************************************************************/
var LinkVoteWidget = Class.create();
LinkVoteWidget.prototype = {
	STATES : { READY : 0, SAVING : 1 },
	initialize : function(movieId, similarMovieId, thumbsUp, thumbsDown, savingText, thumbs, statPanel) {
		this.movieId = movieId;
		this.similarMovieId = similarMovieId;
		this.thumbsUp = thumbsUp;
		this.thumbsDown = thumbsDown;
		this.thumbsImagePanel = 'match'+similarMovieId;
		this.savingText = savingText;
		this.thumbs = thumbs == undefined ? Constants.THUMBS_NONE : thumbs;
		this.statPanel = statPanel;
	},
	
	handleVoteUp : function(event) {
		this.state = this.STATES.SAVING;
		switch(this.thumbs) {
			case Constants.THUMBS_NONE: this.thumbs = Constants.THUMBS_UP; break
			case Constants.THUMBS_UP: this.thumbs = Constants.THUMBS_NONE; break
			case Constants.THUMBS_DOWN: this.thumbs = Constants.THUMBS_UP; break
		}
		this.paint();
		this.saveVote();
	},
	
	handleVoteDown : function(event) {
		this.state = this.STATES.SAVING;
		switch(this.thumbs) {
			case Constants.THUMBS_NONE: this.thumbs = Constants.THUMBS_DOWN; break
			case Constants.THUMBS_UP: this.thumbs = Constants.THUMBS_DOWN; break
			case Constants.THUMBS_DOWN: this.thumbs = Constants.THUMBS_NONE; break
		}
		this.paint();
		this.saveVote();
	},
	
	saveVote : function() {
		var params = "rateAction=doSuggestVote&movieId="+this.movieId+"&similarMovieId="+this.similarMovieId+"&thumbsId="+this.thumbs;
		new Ajax.Request('/rate.sv', {parameters:params, asynchronous:true, onComplete:this.processVote.bind(this)});
	},
	
	processVote : function(response) {
		$(this.statPanel).innerHTML = response.responseText;
		this.state = this.STATES.READY;
		this.paint();
	},
	
	paint : function() {
		
		switch(this.thumbs) {
			case Constants.THUMBS_NONE: 
				$(this.thumbsUp).style.border="1px transparent";
				$(this.thumbsDown).style.border="1px transparent";
				break;
			case Constants.THUMBS_UP: 
				$(this.thumbsUp).style.border="1px solid";	
				$(this.thumbsDown).style.border="1px transparent";	
				break;
			case Constants.THUMBS_DOWN: 
				$(this.thumbsUp).style.border="1px transparent";	
				$(this.thumbsDown).style.border="1px solid";
				break;
		}
	}
};

