	


    YAHOO.moviezen.QuizFormWidget = function () { 
        this.init();
    };
    YAHOO.moviezen.QuizFormWidget.prototype = {
        init: function() {
            YAHOO.util.Event.addListener("myoqForm", "submit", this.handleSubmit, this);
            YAHOO.util.Event.addListener("quizCategory", "change", this.handleChangeType, this);
        },
	    handleSubmit : function(e) {
		    if (getEl('quizTitle').value == '')) {
			    alert("Don't forget to give your quiz a name!");
    			YAHOO.util.Event.stopEvent(e);
	    		return false;
		    } else if ( getEl('quizTitle').value == 'Enter a quiz name' ) {
    			alert("Don't forget to give your quiz a name!");
	    		YAHOO.util.Event.stopEvent(e);
		    	return false;
    		}
		
    		var association = false;
	    	if (getEl('quizCategory').value == 'M') {
		    	var movies = getEl('associationM').getElementsByTagName('input');
			    for (i=0; i<movies.length; i++) {
    				var m = movies[i];
	    			if (m.checked) {
		    			association = true;
			    		break;
				    }
    			}
		    	if (!association) {
	    			alert("You mentioned this quiz is about a single movie, but forgot to tell us which");
			    	YAHOO.util.Event.stopEvent(e);
				    return false;
    			}
	    	} else if (getEl('quizCategory').value == 'A') {
		    	var actors = getEl('associationA').getElementsByTagName('input');
			    for (i=0; i<actors.length; i++) {
				    var a = actors[i];
    				if (a.checked) {
	    				association = true;
		    			break;
    				}
	    		}
		    	if (!association) {
			    	alert("You mentioned this quiz is about a single actor, but forgot to tell us which");
				    YAHOO.util.Event.stopEvent(e);
    				return false;
	    		}
		    } 
    		return true;
	    },
    	handleChangeType : function() {
	    	switch(getEl('quizCategory').value) {
		    	case "G":
			    	getEl('associationM').hide();
				    getEl('associationA').hide();
    				break;
	    		case "M":
				    getEl('associationA').hide();
		    		getEl('associationM').show();
			    	break;
    			case "A":
	    			getEl('associationM').hide();
		    		getEl('associationA').show();
			    	break;
    			case "O":
	    			getEl('associationM').hide();
		    		getEl('associationA').hide();
			    	break;
    		}
    	}
    };



var SearchWidget = Class.create();
SearchWidget.prototype = {
	STATES : { READY : 0, SEARCHING : 1 },
	setOptions : function(options) { 
		this.options = {
			parent:'search',
			action:'searchMovies',
			url:'/search/movie'
		}
		Object.extend(this.options, options || {});
	},
	initialize : function(options) {
		this.setOptions(options);
		this.search = getEl('search').getElementsByClassName('search') ).first();
		
		YAHOO.util.Event.addListener(this.search, 'focus', this.handleFocus.bind(this));
		YAHOO.util.Event.addListener(this.search, 'blur', this.handleBlur.bind(this));
		YAHOO.util.Event.addListener(this.search, 'keypress', this.handleEnter.bind(this));
		this.text = this.search.value;
		this.button = $A( $(this.options.parent).getElementsByClassName('jbutton') ).first();
		Event.observe(this.button, 'click', this.handleSearch.bind(this));
		this.results = $A( $(this.options.parent).getElementsByClassName('results') ).first();
		this.loading = $A( $(this.options.parent).getElementsByClassName('loading') ).first();
	},
	handleFocus : function() {
		if ($F(this.search) == this.text) {
			this.search.value = "";
		}
	},
	handleBlur : function() {
		if (!TextUtils.hasText($F(this.search))) {
			this.search.value = this.text;
		}
	},
	handleEnter : function(e) {
		if (e.keyCode == Event.KEY_RETURN) {
			this.handleSearch();
			YAHOO.util.Event.stopEvent(e);
			return false;
		} else {
			return true;
		}
	},
	handleSearch : function() {
		var params = "quizAction=" + this.options.action + "&format=quiz&" + Form.Element.serialize(this.search);
		new Ajax.Request(this.options.url, {parameters:params, onComplete:this.processSearch.bind(this)});
		this.state = this.STATES.SEARCHING;
		this.paint();
	},
	processSearch : function(response) {
		$(this.results).update( response.responseText );
		this.state = this.STATES.READY;
		this.paint();
	},
	paint : function() {
		switch (this.state) {
			case this.STATES.READY:
				$(this.loading).hide();
				$(this.results).show();
				break;
			case this.STATES.SEARCHING:
				$(this.results).hide();
				$(this.loading).show();
				break;
		}
	}
};

var QuizRatingWidget = Class.create();
QuizRatingWidget.prototype = {
	STATES : { READY : 0, RATED : 1 },
	setOptions : function(options) { 
		this.options = {
			form:'ratings',
			results:'averages'
		}
		Object.extend(this.options, options || {});
	},
	handleRate : function() {
		var params = Form.serialize( this.options.form );
		new Ajax.Request('/quiz/rate', {parameters:params, onComplete:this.processRate.bind(this)});
	},
	processRate : function() {
		this.state = this.STATES.RATED;
		this.paint();
	},
	initialize : function(options) {
		this.setOptions(options);
		this.table = $A( $(this.options.form).getElementsByTagName('table') ).first();
		this.ratings = $A( $(this.options.form).getElementsByTagName('input') );
		this.ratings.each( function(value, index) {
			if ($(value).type == "radio") {
			Event.observe($(value), 'click', this.handleRate.bind(this));
			}
		}.bind(this) );
		this.state = this.STATES.READY;
		//this.paint();
	},
	paint : function() {
		switch(this.state) {
			case this.STATES.READY:
				$(this.options.results).hide();
				$(this.table).show();
				break;
			case this.STATES.RATED:
				$(this.table).hide();
				$(this.options.results).show();
				break;
		}
	}
};

var QuizRemoveMovieWidget = {
	handleRemove : function(link, quizId) {
		var params = "quizAction=removeMovie&quizId="+quizId;
		new Ajax.Request('/myoq.sv', {parameters:params});
		$(link).update("Removed Movie!");
	}
};

var QuizRemoveActorWidget = {
	handleRemove : function(link, quizId) {
		var params = "quizAction=removeActor&quizId="+quizId;
		new Ajax.Request('/myoq.sv', {parameters:params});
		$(link).update("Removed Actor!");
	}
};

var reorderWidget = {
	items : new Array(),
	setupList : function() {
		Sortable.create('questions', {scroll:window,constraint:false});
	},
	handleSubmit : function() {
		$('saveOrderBtn1').disabled = true;
		$('saveOrderBtn2').disabled = true;
		
		var query = Sortable.serialize($('questions')).toQueryParams("&");
		var ids = query["questions[]"];
		
		if (ids == null) {
			this.items = new Array();
		} else if (ids.indexOf(",") < 0) {
			this.items = new Array(ids);
		} else {
			this.items = ids.toArray();
		}
	
		$('questionIds').value = this.items;
		return true;
	}
};

var QuestionContentTypeWidget = Class.create();
QuestionContentTypeWidget.prototype = {
	initialize : function() {
		Event.observe($('contentType'), "change", this.handleChange.bind(this));
	},
	handleChange: function() {
		$('contentI').hide();
		$('contentA').hide();
		$('contentV').hide();
		switch ($('contentType').value) {
			case 'I':
				$('contentI').show();
				break;
			case 'A':
				$('contentA').show();
				break;
			case 'V':
				$('contentV').show();
				break;
		}
	}
};

var QuestionAnswerTypeWidget = Class.create();
QuestionAnswerTypeWidget.prototype = {
	initialize : function() {
		Event.observe($('answerType'), "change", this.handleChange.bind(this));
	},
	handleChange: function() {
		getEl('answerC').hide();
		getEl('answerM').hide();
		getEl('answerP').hide();
		switch (getEl('answerType').value) {
			case 'C':
				getEl('answerC').show();
				break;
			case 'M':
				getEl('answerM').show();
				break;
			case 'P':
				getEl('answerP').show();
				break;
		}
	}
};


var quizActivateWidget = {
	handleConfirm : function() {
		if ( parseInt( $('questionCount').value ) < 3 ) {
			alert("You need at least 3 questions to activate your quiz.  Add more questions!");
			return false;
		} else if (confirm("Once activated, you will no longer be able to edit your quiz.  Are you sure you are done?")) {
			return true;
		} else {
			return false;
		}
	}
};

var createQuestionWidget = {
	handleValidate : function() {
		if (!TextUtils.hasText($('question').value)) {
			alert("Whoops, you forgot to type in a question");
			return false;
		}
		$('questionSubmitBtn').disabled = true;
		return true;
	}
};

var quizWidget = {
	handleSubmit : function() {
		var forms = $A( document.getElementsByClassName('questionForm') );
		var questions = new Array();
		forms.each( function(value, index) { 
			questions.push( Form.serialize(value) );
		} );
		$('quizAnswers').value = questions;
		$('quizForm').submit();
	}
};

var FormUtils = {
	checkAll : function(field) {
		field.checked = true;
		for (i = 0; i < field.length; i++) {
			field[i].checked = true ;
		}
	}
};

