	



var SortUser = Class.create();
SortUser.prototype = {
	initialize : function(id, username, avatar) {
		this.id = id;
		this.username = username;
		this.avatar = avatar;
	}
};

var buddySortWidget = {
	BUDDY_LIMIT : 30,
	initialize : function() {
		this.setupList();
	},
	setupItem : function(id) {
		var template = $('friendsTemplate_'+id).innerHTML;
		template = template.replace(/{/g, "<");
		template = template.replace(/}/g, ">");
		return "<li id=\"friends_"+id+"\" class=\"friend\">"+template+"</li>";
	},
	setupList : function() {
		Sortable.create('friends', {tag:'li',only:'friend',scroll:window,handle:'userImg',constraint:false});
	},
	items : new Array(),
	handleAdd : function (id) {

		var user = buddies[id];
		if (this.items.length >= this.BUDDY_LIMIT) {
			alert("Whoops, you can only select "+this.BUDDY_LIMIT+" buddies.  Please remove one before adding another.");
			return;
		}
		
		var dup = this.items.find( function(uid) { return (uid == id) } );
		if (dup != undefined) {
			alert("Whoops, you already have "+dup.username+" on your list.");
			return;
		}
		
		$('buddy'+id).addClassName( "selected" );
		$('addLink'+id).hide();
		$('removeLink'+id).show();
		
		this.items.push(id);
		var item = this.setupItem(id);

		if (this.items.length > 0) {
			new Insertion.Bottom($('friends'), item);
		} else {
			new Insertion.Top('friends', item);
		}
		
		new Effect.Highlight( 'friends_'+id );
		this.setupList();
	},
	handleRemove : function(id) {
		$('buddy'+id).removeClassName( "selected" );
		$('removeLink'+id).hide();
		$('addLink'+id).show();
		this.items = this.items.without(id);
		Element.remove('friends_'+id);
		this.setupList();
	},
	handleSubmit : function() {
	
		$('sortSubmitBtn1').disabled = true;
		$('sortSubmitBtn2').disabled = true;
	
		var query = Sortable.serialize($('friends')).toQueryParams("&");
		var ids = query["friends[]"];
		
		if (ids == null) {
			this.items = new Array();
		} else if (ids.indexOf(",") < 0) {
			this.items = new Array(ids);
		} else {
			this.items = ids.toArray();
		}
		
		$('friendsIds').value = this.items;
		return true;
	}
};



var QuickShout = Class.create();
QuickShout.prototype = {
	initialize : function() {
		this.defaults = new DefaultTextWidget($('quickShout'), "Type a quick comment");
		Event.observe($('quickShoutForm'), "submit", this.onSubmit.bindAsEventListener(this));
	},
	onSubmit : function(event) {
		if ($F('quickShout') == this.defaults.text) {
			Event.stop(event);
			alert("Whoops, you forgot to put in a comment!");
			return false;
		}
		return Clicker.guard();
	}
};

var DeleteComment = Class.create();
DeleteComment.prototype = {
	initialize : function(event, commentId) {
		Event.stop(event);
		if (confirm("Are you sure you want to delete this message?")) {
			var params = "commentId="+commentId;
			new Ajax.Request('/talk/deletecomment', {parameters:params});
			Try.these(
				function() { Effect.BlindUp("comment"+commentId); },
				function() { Element.hide("comment"+commentId); } 
			);
		}
	}
}
var dC = function(event, commentId) { new DeleteComment(event, commentId); }

/**
 * common.widget.js
 */

// Common vars needed
var ratingSubmitQueue = new Array();
var ratingSubmitQueueLength = 0;
var ajaxObj;
var d=document;
var curRatingIds = new Array(); // will be populated as we build pages w/ widgets
var curRatings = new Array(); // will be populated as we build pages w/ widgets
var origRatings = new Array();
var movieTitles = new Array();
var movieDbIds = new Array(); // also must be populated by pages
var rateSrc = new Array(); // also must be populated by pages

/** Handle rating actions **/
function rateScoreToImage(scoreID, type) {
	useNew = false;
	if (type == 'qck' || type == 'wiz' || type == 'mct') {
		useNew = true;
	}
	if (scoreID == 0) {
		return "/public/static/images/0stars." + (useNew ? "new." : "") + "gif";
	}
	if (scoreID == 1) {
		return "/public/static/images/1stars." + (useNew ? "new." : "") + "gif";
	}
	if (scoreID == 2) {
		return "/public/static/images/2stars." + (useNew ? "new." : "") + "gif";
	}
	if (scoreID == 3) {
		return "/public/static/images/3stars." + (useNew ? "new." : "") + "gif";
	}
	if (scoreID == 4) {
		return "/public/static/images/4stars." + (useNew ? "new." : "") + "gif";
	}
	if (scoreID == 5) {
		return "/public/static/images/5stars." + (useNew ? "new." : "") + "gif";
	}
	if (scoreID == 6) {
		return "/public/static/images/halfstars." + (useNew ? "new." : "") + "gif";
	}
	if (scoreID == 7) {
		return "/public/static/images/1_halfstars." + (useNew ? "new." : "") + "gif";
	}
	if (scoreID == 8) {
		return "/public/static/images/2_halfstars." + (useNew ? "new." : "") + "gif";
	}
	if (scoreID == 9) {
		return "/public/static/images/3_halfstars." + (useNew ? "new." : "") + "gif";
	}
	if (scoreID == 10) {
		return "/public/static/images/4_halfstars." + (useNew ? "new." : "") + "gif";
	}
	if (scoreID == _NOT_INTERESTED) {
		return "/public/static/images/0stars." + (useNew ? "new." : "") + "gif";
	}
	if (scoreID == _WANT_TO_SEE) {
		return "/public/static/images/0stars." + (useNew ? "new." : "") + "gif";
	}
	return "/public/static/images/0stars." + (useNew ? "new." : "") + "gif";
}
function flashSaveButton(){
	if (d.getElementById("widgetSaveBtn")) {
		d.images["widgetSaveBtn"].src="/public/static/images/bt_save_ani.gif";
	}
}

/** Handle Ajax stuff **/
function commonWgt_ajaxSubmit(mvID) {
	var params = commonWgt_getRateSubmitParams(mvID);
	new Ajax.Request('/rating/movie', {parameters:params});
}

/**
 * expand.widget.js
 */
// vars

var expWgt_openMovie="";



/** Handle expand/close **/

function expWgt_expand(mvID){

	expWgt_closeOpened();

	expWgt_rateSet(mvID, curRatings[mvID]); // make sure images ok

	d.getElementById("expWgtDisplay"+mvID).style.display="none";

	d.getElementById("expWgt_rateBox"+mvID).style.display="block";

	expWgt_openMovie=mvID;

}

function expWgt_collapse(mvID){

	d.getElementById("expWgt_rateBox"+mvID).style.display="none";

	d.getElementById("expWgtDisplay"+mvID).style.display="block";

}

function expWgt_closeOpened(){

	if(expWgt_openMovie != ""){

//		expWgt_collapse(expWgt_openMovie);

		expWgt_commentSet(expWgt_openMovie, d.getElementById("commonWgt_commentBox"+expWgt_openMovie).value);

		// don't need to call rateSet(), will already have been done

		expWgt_rateSave(expWgt_openMovie);

		expWgt_openMovie="";

	}

}



/** Handle rating actions **/

function expWgt_rateClear(mvID){

	expWgt_commentSet(mvID,""); // zero out comment

	expWgt_rateSet(mvID,0); // zero out rating

	expWgt_rateSave(mvID); // save it

}

function expWgt_rateDone(mvID) {

	expWgt_commentSet(mvID, d.getElementById("commonWgt_commentBox"+mvID).value);

	// don't need to rateSet(), will already have been done onclick

	expWgt_rateSave(mvID);

}

function expWgt_rateStarHover(mvID,starNum){

	d.images["expWgt_rateStarsBtn"+mvID].src=rateScoreToImage(starNum, 'exp');

}

function expWgt_rateStarsReset(mvID){

	rating=curRatings[mvID];

	if(rating == _NOT_INTERESTED || rating == _WANT_TO_SEE) {

		d.images["expWgt_rateStarsBtn"+mvID].src="/public/static/images/0stars.gif";

	}

	else {

		d.images["expWgt_rateStarsBtn"+mvID].src=rateScoreToImage(rating, 'exp');

	}

}

function expWgt_commentSet(mvID, newComment) {

	d.getElementById("commonWgt_commentBox"+mvID).value=newComment;



	// change comment image - if we have a comment, add the image, otherwise not

	if (d.getElementById("expWgt_comImg"+mvID)) {

		if (newComment == "") {

		  d.getElementById("expWgt_comImg"+mvID).style.display="none";

		}

		else {	

		  d.getElementById("expWgt_comImg"+mvID).style.display="inline";

		}

	}

}

function expWgt_rateSet(mvID, rating){

	curRatings[mvID]=rating;



	if(rating == _NOT_INTERESTED){

		d.images["expWgt_rateNiImg"+mvID].src="/public/static/images/ni_on.gif";

		d.images["expWgt_rateWsImg"+mvID].src="/public/static/images/ws.gif";

		d.images["expWgt_rateStarsBtn"+mvID].src="/public/static/images/0stars.gif";

		d.getElementById("expWgt_rateStarsBtn"+mvID).className="expWgt_rateStars";

		d.getElementById("expWgt_rateStarBar"+mvID).className="expWgt_rateStarBar";

		if (d.getElementById("expWgt_RerateBtn"+mvID)) {

				d.getElementById("expWgt_RerateBtn"+mvID).style.display="inline";

		}

		if (d.images["expWgt_RateImg"+mvID] != null) {

			d.images["expWgt_RateImg"+mvID].src="/public/static/images/ni_flat.gif";

		}

	}

	else if(rating == _WANT_TO_SEE){

		d.images["expWgt_rateNiImg"+mvID].src="/public/static/images/ni.gif";

		d.images["expWgt_rateWsImg"+mvID].src="/public/static/images/ws_on.gif";

		d.images["expWgt_rateStarsBtn"+mvID].src="/public/static/images/0stars.gif";

		d.getElementById("expWgt_rateStarsBtn"+mvID).className="expWgt_rateStars";

		d.getElementById("expWgt_rateStarBar"+mvID).className="expWgt_rateStarBar";

		if (d.getElementById("expWgt_RerateBtn"+mvID)) {

				d.getElementById("expWgt_RerateBtn"+mvID).style.display="inline";

		}

		if (d.images["expWgt_RateImg"+mvID] != null) {

			d.images["expWgt_RateImg"+mvID].src="/public/static/images/ws_flat.gif";

		}

	}

	else if(parseInt(rating) == 0){

		curRatings[mvID]=0;

		d.images["expWgt_rateWsImg"+mvID].src="/public/static/images/ws.gif";

		d.images["expWgt_rateNiImg"+mvID].src="/public/static/images/ni.gif";

		d.getElementById("expWgt_rateStarsBtn"+mvID).className="expWgt_rateStars";

		d.getElementById("expWgt_rateStarBar"+mvID).className="expWgt_rateStarBar";

		if (d.getElementById("expWgt_RerateBtn"+mvID)) {

				d.getElementById("expWgt_RerateBtn"+mvID).style.display="none";

		}

		if (d.images["expWgt_RateImg"+mvID] != null) {

			d.images["expWgt_RateImg"+mvID].src="/public/static/images/rateMe.gif";

		}

	}

	else{

		curRatings[mvID]=parseInt(rating);

		d.images["expWgt_rateWsImg"+mvID].src="/public/static/images/ws.gif";

		d.images["expWgt_rateNiImg"+mvID].src="/public/static/images/ni.gif";

		d.getElementById("expWgt_rateStarsBtn"+mvID).className="expWgt_rateStars expWgt_rateStars_mod_on";

		d.getElementById("expWgt_rateStarBar"+mvID).className="expWgt_rateStarBar expWgt_rateStarBar_mod_on";

		if (d.getElementById("expWgt_RerateBtn"+mvID)) {

				d.getElementById("expWgt_RerateBtn"+mvID).style.display="inline";

		}

		if (d.images["expWgt_RateImg"+mvID] != null) {

			d.images["expWgt_RateImg"+mvID].src=rateScoreToImage(curRatings[mvID], 'exp');

		}

	}

}

function expWgt_rateSave(mvID){

	expWgt_collapse(mvID);

	commonWgt_rateSave(mvID, true);

	expWgt_openMovie="";

}

/**
 * flat.widget.js
 */
// vars

var fltWgt_openMovie = "";



function fltWgt_rateStarHover(mvID, starNum, type){

 if (d.getElementById("fltWgt_rateStarsBtn"+mvID)) {
	d.images["fltWgt_rateStarsBtn"+mvID].src=rateScoreToImage(starNum, type);
 }

}

function fltWgt_rateStarsReset(mvID, type){

 rating=curRatings[mvID];

 if(rating == _NOT_INTERESTED || rating == _WANT_TO_SEE) {

   if (d.getElementById("fltWgt_rateStarsBtn"+mvID)) {

    	d.images["fltWgt_rateStarsBtn"+mvID].src="/public/static/images/0stars" + ((type == 'wiz' || type == 'mct') ? ".new" : "") + ".gif";

   }

 }

 else {

   if (d.getElementById("fltWgt_rateStarsBtn"+mvID)) { 

    	d.images["fltWgt_rateStarsBtn"+mvID].src=rateScoreToImage(rating, type);

   }

 }

}

function fltWgt_rateSet(mvID, rating, type, callback){

 if (curRatings[mvID]==rating && curRatings[mvID] != 0) {

   fltWgt_rateSet(mvID, 0, type); 

   if (callback != null) {

		var code = callback + "('" + mvID + "', '0')";

		eval(code);

   }

   return;

 }

 else {

   curRatings[mvID]=rating;
   prepend = "";

   if (type == 'small') {

		prepend = "bt_";

   }

   if (type == 'wiz' || type == 'mct') {

		prepend = "wiz_";

   }

   if(rating == _NOT_INTERESTED){

	if (d.getElementById("fltWgt_rateNiImg"+mvID)) { 

	 d.images["fltWgt_rateNiImg"+mvID].src="/public/static/images/" + prepend + "ni_on.gif";

	}

   } else {

	if (d.getElementById("fltWgt_rateNiImg"+mvID)) { 

	 d.images["fltWgt_rateNiImg"+mvID].src="/public/static/images/" + prepend + "ni_off.gif";

	}

   }

   if(rating == _WANT_TO_SEE) {

	if (d.getElementById("fltWgt_rateWsImg"+mvID)) { 

	 d.images["fltWgt_rateWsImg"+mvID].src="/public/static/images/" + prepend + "ws_on.gif";

	}

   } else {

	if (d.getElementById("fltWgt_rateWsImg"+mvID)) { 

	 d.images["fltWgt_rateWsImg"+mvID].src="/public/static/images/" + prepend + "ws_off.gif";

	}

   }

   if(rating == _NOT_INTERESTED || rating == _WANT_TO_SEE || rating == 0) {

	if (d.getElementById("fltWgt_rateStarsBtn"+mvID)) { 

	 d.images["fltWgt_rateStarsBtn"+mvID].src="/public/static/images/0stars" + ((type == 'wiz' || type == 'mct') ? ".new" : "") + ".gif";

	}

   }

   curRatings[mvID]=parseInt(rating);


   if (callback != null) {

		var code = callback + "('" + mvID + "', '" + rating + "')";

		eval(code);

	}

 }

 if (type != 'mct') {

		fltWgt_rateSave(mvID, true);

 } else {

		//no save for mct

		//fltWgt_rateSave(mvID, false);

 }

}



function fltWgt_expandComment(mvID) {

	// if it's already open, just close it

	if (fltWgt_openMovie == mvID) {

		fltWgt_collapse(fltWgt_openMovie);

	}

	else {

		fltWgt_closeOpened();

		d.getElementById("fltWgt_commentExpand"+mvID).style.display="block";

		fltWgt_openMovie=mvID;

	}

}

function fltWgt_collapse(mvID){

	d.getElementById("fltWgt_commentExpand"+mvID).style.display="none";

	fltWgt_rateSave(mvID, false);

	fltWgt_openMovie="";

}

function fltWgt_closeOpened(){

	if(fltWgt_openMovie != "") {

		fltWgt_collapse(fltWgt_openMovie);

	}

}

function fltWgt_rateSave(mvID, showMessage) {

	commonWgt_rateSave(mvID, showMessage);

}

function fltWgt_commentSet(mvID, newComment, callback) {

	d.getElementById("commonWgt_commentBox"+mvID).value=newComment;

	if (callback != null) {

		var code = callback + "('" + mvID + "')";

		eval(code);

   }

}

function fltWgt_rateClear(mvID, type){

	fltWgt_commentSet(mvID,""); // zero out comment

	fltWgt_rateSet(mvID,0,type); // zero out rating

// set calls save

//	fltWgt_rateSave(mvID, false); // save it

}

// Constants
_AJAX_PARAMS="rateAction=doRate&rateUser=" + currentUser.id + "&key=" + encodeURIComponent(currentUser.key);
_WANT_TO_SEE="12";
_NOT_INTERESTED="11";

function commonWgt_rateSave(mvID, showMessage){

	if (currentUser.isLoggedIn()) {
		commonWgt_ajaxSubmit(mvID);
	
		if (curRatings[mvID] == _WANT_TO_SEE && 
			origRatings[mvID] != _WANT_TO_SEE) {
	
			if (showMessage) {
					var title = movieTitles[mvID];
					if (title.length > 30) {
							title = title.substring(0, 30) + "...";
					}
					var msg = title + " has been added to your list.";
					//floatie.show(msg);
			}
		}

	} else {

		commonWgt_ajaxSubmit(mvID);
		if (showMessage) {
				var title = movieTitles[mvID];
				if (title.length > 30) {
						title = title.substring(0, 30) + "...";
				}
				msg = "Your ratings will not be saved until you register or sign in!";
				floatie.show(msg);
				alert("Your ratings will not be saved until you register or sign in!");
		}

	}
	
	if (d.getElementById("widgetSaveBtn")) {
		d.images["widgetSaveBtn"].src = "/public/static/images/bt_saved.gif";
	}
}

function commonWgt_getRateSubmitParams(mvID) {
	// seem to need this to get server responses on duplicate submission
	var ran_unrounded=Math.random()*1000000;
	var ran_number=Math.round(ran_unrounded); 

	if (movieDbIds && movieDbIds[mvID]) {
		return(_AJAX_PARAMS + "&movieid=" + movieDbIds[mvID] + "&rating=" + curRatings[mvID] + "&comment=" + encodeURIComponent( $('commonWgt_commentBox'+mvID).value ) + "&source=" + rateSrc[mvID] + "&rid=" + ran_number + "&ratingid=" + (curRatingIds && curRatingIds[mvID] ? curRatingIds[mvID] : -1));
	} else {
//		alert("Rating not saved...oops");
//		_uacct = "UA-191156-1";
//		urchinTracker('NoMovieDBID');
	}
}



