/**
 * @author Joz
 */
/***********************************

Common Javascripts
Author: Jos Deldime

***********************************/

var main = {
	
	form_initialized: false, // flag to show whether form on this page have been initialized (to avoid double calls if 2 forms on page)
	loginAttempts: 0, // used in login page
	
	// for error messages
	showErrors: function(response) {
			// show all fields
			// class .error or class .normal
			$("input").removeClass("error");
			$("textarea").removeClass("error");
			for (var i=0; i<response.errorFields.length; i++) {
				
					var fieldname = "[name='"+response.errorFields[i]+"']";
					
					$(fieldname).addClass("error");
					
					// bind to instead of taking over event:
					$(fieldname).bind("focus", function() {
								$(this).removeClass("error");
						});					
			}

			main.topOfPage(); // jump to top to show this msgbox
			$('#messagebox').html(response.message);			
			$('#messagebox').addClass("error");
			$("#messagebox").fadeIn("normal");
			
		},
	
	topOfPage: function() {
		scrollTo(0,0); // much nicer than using anchors (they appear in url #TOP)
	},
		
	hideErrors: function(response) {
			$("#messagebox").fadeOut("normal");
		},
	
	// for normal messages
	showMessage: function(response) {
			main.topOfPage(); // jump to top to show this msgbox
			$('#messagebox').html(response.message);
			$("#messagebox").fadeIn("normal");
		},

	hideMessage: function(response) {
			$("#messagebox").fadeOut("normal");
		},
	
	// generic way of handling ajax form responses:
	handleResponse: function(response) {
		
		if (response.errors) {
			if (response.redirect) {
				main.redirect(response.redirect);
			} else {
				main.enable(".submit");
				main.showErrors(response);	
			}
			
						
		} else { // saved
			if (response.redirect) main.redirect(response.redirect);
			else main.hideErrors();
			
		} // end if
				
	},
	
	redirect: function(url) {
		top.location.href=url; // TODO: could be speeded up
	},

	watch_input: function(elementID, callback) {
		$(elementID).change( function() {
			if(typeof(callback)!='undefined') callback(); // function to show that something has been changed
			//else alert("callback function is not defined");
		});

	},

	disable: function(elementID) {
		$(elementID).attr("disabled","disabled");
	},

	enable: function(elementID) {
		$(elementID).removeAttr("disabled");
	},
	
	confirmthis: function(msg) {
		if (typeof(msg)=='undefined') msg = "Are you sure?";
		return confirm(msg);
	}
	
	
}; // end main

$(document).ready(function(){
	
	// init form behaviour
	$("form:not([name='no_ajax_form'])").ajaxForm({
			success:  main.handleResponse,
			beforeSubmit: function() { main.disable(".submit"); main.hideMessage(); },
			dataType:  'json'
	});
	
	// allow iframe links (used for contest rules etc)
	$("a.iframe").fancybox({ 
		frameWidth: 500,
		frameHeight:400
	}); 	
	
});


