	


var StoryThumbsWidget = Class.create();
StoryThumbsWidget.prototype = {
	VOTES : { THUMBS_UP : 1, THUMBS_DOWN : 2, THUMBS_NONE : 3 },
	vote : 3,
	setOptions : function(options, stats) { 
		this.options = {
			widget:'thumbs'
		};
		this.stats = {
			score : 0,
			votes : 0
		};
		Object.extend(this.options, options || {});
		Object.extend(this.stats, stats || {});
	},
	initialize : function(id, options, stats) {
		this.id = id;
		this.setOptions(options, stats);
		this.up = $A( $(this.options.widget).getElementsByClassName('up') ).first();
		this.down = $A( $(this.options.widget).getElementsByClassName('down') ).first();		
		this.score = $A( $(this.options.widget).getElementsByClassName('score') ).first();
		this.votes = $A( $(this.options.widget).getElementsByClassName('votes') ).first();
		Event.observe(this.up, 'click', this.handleUp.bind(this));
		Event.observe(this.down, 'click', this.handleDown.bind(this));
	},
	handleUp : function() {
		if (!currentUser.isLoggedIn()) {
			alert("Login or register to vote!");
			return;
		}
		switch (this.vote) {
			case this.VOTES.THUMBS_UP:
				this.vote = this.VOTES.THUMBS_NONE;
				this.stats.score -= 1;
				this.stats.votes -= 1;
				break;
			case this.VOTES.THUMBS_DOWN:
				this.vote = this.VOTES.THUMBS_UP;
				this.stats.score += 1;
				break;
			case this.VOTES.THUMBS_NONE:
				this.vote = this.VOTES.THUMBS_UP;
				this.stats.score += 1;
				this.stats.votes += 1;
				break;
		}
		this.paint();
		var params = "newsid="+this.id+"&vote="+this.vote;
		new Ajax.Request('/news/vote', {parameters:params});
	},
	handleDown : function() {
		if (!currentUser.isLoggedIn()) {
			alert("Login or register to vote!");
			return;
		}
		switch (this.vote) {
			case this.VOTES.THUMBS_UP:
				this.vote = this.VOTES.THUMBS_DOWN;
				this.stats.score -= 1;
				break;
			case this.VOTES.THUMBS_DOWN:
				this.vote = this.VOTES.THUMBS_NONE;
				this.stats.votes -= 1;
				break;
			case this.VOTES.THUMBS_NONE:
				this.vote = this.VOTES.THUMBS_DOWN;
				this.stats.votes += 1;
				break;
		}
		this.paint();		
		var params = "newsid="+this.id+"&vote="+this.vote;
		new Ajax.Request('/news/vote', {parameters:params});
	},
	paint : function() {
		Element.removeClassName(this.up, "on");
		Element.removeClassName(this.down, "on");
		Element.removeClassName(this.up, "off");
		Element.removeClassName(this.down, "off");
		switch (this.vote) {
			case this.VOTES.THUMBS_UP:
				Element.removeClassName(this.up, "off");
				Element.removeClassName(this.down, "on");
				Element.addClassName(this.up, "on");
				Element.addClassName(this.down, "off");
				break;
			case this.VOTES.THUMBS_DOWN:
				Element.removeClassName(this.up, "on");
				Element.removeClassName(this.down, "off");
				Element.addClassName(this.up, "off");
				Element.addClassName(this.down, "on");
				break;
			case this.VOTES.THUMBS_NONE:
				Element.removeClassName(this.up, "on");
				Element.removeClassName(this.down, "on");
				Element.addClassName(this.up, "off");
				Element.addClassName(this.down, "off");
				break;
		}
		this.score.update("+"+this.stats.score);
		this.votes.update("("+this.stats.votes+" votes)");
	}
};

var StoryBookmarkWidget = Class.create();
StoryBookmarkWidget.prototype = {
	STATES : { READY : 0, SAVING : 1, SAVED : 2, FAILED : 3, TIMED_OUT : 4 },
	initialize : function(link, storyId) {
		this.state = this.STATES.READY;
		this.storyId = storyId;
		this.link = link;
		Event.observe(this.link, 'click', this.onClick.bind(this));
	},
	onClick : function() {
		if (this.state != this.STATES.READY) { return; }
		this.state = this.STATES.SAVING;
		$(this.link).update("Saving...");
		var params = "newsAction=bookmark&newsid="+this.storyId;
		new Ajax.Request('/news/addfavorite', {parameters:params, onSuccess:this.onSuccess.bind(this), onFailure:this.onFailure.bind(this), onTimeout:this.onTimeout.bind(this)});
	},
	onSuccess : function(response) {
		this.state = this.STATES.SAVED;
		$(this.link).update("<a href=\"/news/savedstories\">Saved!</a>");
	},
	onFailure : function() {
		this.state = this.STATES.FAILED;
		$(this.link).update("Failed - Please Refresh/Try Again");
	},
	onTimeout : function() {
		this.state = this.STATES.TIMED_OUT;
		$(this.link).update("Timed Out - Please Refresh/Try Again");		
	}
};

var StoryUnboookmarkWidget = Class.create();
StoryUnboookmarkWidget.prototype = {
	STATES : { READY : 0, REMOVING : 1, REMOVED : 2 },
	initialize : function(link, storyId) {
		this.state = this.STATES.READY;
		this.storyId = storyId;
		this.link = link;
		Event.observe(this.link, 'click', this.handleUnbookmark.bind(this));
	},
	handleUnbookmark : function() {
		if (this.state == this.STATES.REMOVED) { return; }
		this.state = this.STATES.REMOVING;
		this.paint();
		var params = "newsAction=unbookmark&newsid="+this.storyId;
		new Ajax.Request('/news/removefavorite', {parameters:params, onComplete:this.processUnbookmark.bind(this)});
	},
	processUnbookmark : function(response) {
		this.state = this.STATES.REMOVED;
		this.paint();
	},
	paint : function() {
		switch (this.state) {
			case this.STATES.READY:
				break;
			case this.STATES.REMOVING:
				$(this.link).update("Removing...");
				break;
			case this.STATES.REMOVED:
				$(this.link).update("<a href=\"/news/savedstories\">Removed</a>");
				break;
		}
	}
};

var headlineWidget = {
	sampleHeadline : 'Add a simple headline',
	handleFocus : function(field) {
		if (field.value == this.sampleHeadline) {
			field.value = '';
		}
	},
	handleBlur : function(field) {
		if (field.value == '') {
			field.value = this.sampleHeadline;
		}
	}
};

var descriptionWidget = {
	sampleDescription : 'Briefly, describe the story',
	handleFocus : function(field) {
		if (field.value == this.sampleDescription) {
			field.value = '';
		}
	},
	handleCount : function(textarea, count, limit) {
		if ($(textarea).value.length <= limit) {
			$(count).update( limit-$(textarea).value.length );
			return true;
		} else {
			alert("Whoops, please keep your comments under "+limit+" characters.  Thanks.");
			$(textarea).value = $(textarea).value.substring(0, limit);
			return false;
		}
	},
	handleBlur : function(field) {
		if (field.value == '') {
			field.value = this.sampleDescription;
		}
	}
};

var actorSearchWidget = {
	handleFocus : function(field) {
		if (field.value == 'Type in an actor name, and press SEARCH') {
			field.value = '';
		}
	},
	search : function() {
		var params = "action=searchActors&"+Form.Element.serialize($("asearch"));
		new Ajax.Updater('actorSearchResults', '/search/actor', {parameters:params, asynchronous:true, evalScripts:true});
	},
	captureEnterOnSearch : function(e) {
		if (this.pressedEnterKey(e)) {
			this.search();
			return false;
		}
		return true;
	},
	pressedEnterKey : function(e) {
		var keynum;
		if(window.event) { // handle IE enter
			keynum = e.keyCode;
		} else if(e.which) { // Netscape/Firefox/Opera
			keynum = e.which;
		}
		if (keynum == 13) {
			return true;
		} else {
			return false;
		}
	}
};

var movieSearchWidget = {
	handleFocus : function(field) {
		if (field.value == 'Type in a movie name, and press SEARCH') {
			field.value = '';
		}
	},
	search : function() {
		var params = "action=searchMovies&"+Form.Element.serialize($("msearch"));
		new Ajax.Updater('movieSearchResults', '/search/movie', {parameters:params, asynchronous:true, evalScripts:true});			
	},
	captureEnterOnSearch : function(e) {
		if (this.pressedEnterKey(e)) {
			this.search();
			return false;
		}
		return true;
	},
	pressedEnterKey : function(e) {
		var keynum;
		if(window.event) { // handle IE enter
			keynum = e.keyCode;
		} else if(e.which) { // Netscape/Firefox/Opera
			keynum = e.which;
		}
		if (keynum == 13) {
			return true;
		} else {
			return false;
		}
	}
};

var newsFormWidget = {
	validate : function() {

		var headlineRegex = new RegExp("[a-z0-9\?\!]+");
		var urlRegex = /^(http[s]?:\/\/)?(www\.)?([\w-]+)(\.[\w-]+)+[\/[\w-]+]*[\S]*$/;
		
		if ($F('newsAction') == 'addStory' && ( $F('headline').match(headlineRegex) == null  || $F('headline') == headlineWidget.sampleHeadline ) ) {

			alert("Whoops, don't forget a story headline!");
			new Effect.Highlight('headlineLabel');
			return false;
			
		} else if ( urlRegex.test( $F('url') ) != true ) {
				
			alert("This doesn't appear to be a valid url.  Please correct it.");
			new Effect.Highlight('urlLabel');
			return false;	
							
		} else {
		
			return Clicker.guard();
		
		}
	}
};