// contest.js
// version 1.0.1
// last update: February 9th 2010



// Form Validator

function handleValidation(element,message) {
	// we check if it already has the error class to prevent multiple messages appearing on the same element
	if (!element.hasClassName('errorField')) {
	
		// the name of the element
		var name = message.substr(0,message.indexOf(' '));
		
		// the actually error message
		var originalMessage = message;
		var message = message.substr(message.indexOf(' '));
		
		// if its a checkbox, don't highlight the element but highlight the text beside it
		if (element.type == "checkbox") {
			var label = $(element).next();
		} else {
			var label = $(element).up(0).previousSiblings()[0].down(0);
		}
		
		// the title we're going to use
		var title = element.title;
		
		// without *
		var titlesub = title.substr(0,title.indexOf('*'))
		
		// the output
		var out = '';
		
		// check the message and output the right title and message combo
		if (name == element.name && title) {
			if (message != "The email address supplied is already registered. Did you forget your login information?" && message.substr(1,8) != "username" && message.substr(0,11) != "The captcha") {
				if (title.indexOf('*') > -1) {
					out = titlesub+" "+message;
				} else {
					out = title+message;
				}
			} else if (message == "The email address supplied is already registered. Did you forget your login information?") {
				out = "The email address supplied is already registered.";
			} else {
				out = titlesub+" "+message;
			}
		} else {
		
			if (message.substr(1,8) === "username" && message.substr(14,6) !== "picked") {
				out = titlesub+" "+message.substr(10);
			} else	if (message.substr(1) === "passwords did not match") {
				out = "P"+message.substr(2);
			} else if (element.type == "checkbox") {
				out = titlesub+message;
			} else {
				out = originalMessage;
			}
		}
		
		// get all the labels in this from
		var labels = element.form.getElementsByTagName('label');
		for(var i=0;i<labels.length;i++) {
			// check all the labels for the one that matches the failed form element
			if (labels[i].htmlFor==element.id) {
				// put the message inside the label
				//if (element.type != "checkbox") {
					labels[i].innerHTML = out;
				//}
				// make the label stand out
				labels[i].addClassName('error');
				// remove the old border-color
				// this fixes an issue when fading out the border
				element.setStyle({'borderColor':''});
				// make the input stand out
				element.addClassName('errorField');
				// scroll the browser to that element.
				labels[i].scrollTo();

			}
		}
	
		// remove the styles from the failed form element and the label
		function removeStyles(t) {
			// search though all the labels again
			for(var i=0;i<labels.length;i++) {
				// find the label that matches the failed form element
				if (labels[i].htmlFor==element.id) {
					// return everything to normal
					labels[i].innerHTML = t;
					labels[i].removeClassName('error');
					element.removeClassName('errorField');
				}
			}
		}

	}

	// we want the element and label to retrun to normal when the user relizes the error.
	element.observe('focus', function(event) {
		removeStyles(title);
	});
}



// Search form

function searchFormClick(e,x) {
	// check to see if the text input value is equal to the default value
	if (e.value == x) {
		// if they are the same, remove the default value and the grey text colour
		e.value = '';
		e.removeClassName('searchgrey');
	}
}

function searchFormBlur(e,x) {
	// check if the text input doesn't have a value
	if (e.value == '' || !e.value || e.value == null) {
		// if theres no value, add the default value and the grey text colour
		e.value = x;
		e.addClassName('searchgrey');
	}
}



// Vote

function rateFile(element,id,uid) {
	// check if the user has already voted
	if ($(element).disabled == true) {
		// this should be replaced with something more user friendly
		alert("You can only vote once.");
	} else {
		// disable the button to prevent multiple votes
		$(element).disabled = true;
		// set the parameters
		var params = {
			"id" :          id,
			"rating" :      10,
			"uid" :         uid,
			"returnData" :  false
		}
		// send the rating
		jsonRequest('media.rateFile', params,
			function(result)     { rateFileResult(result, element); },
			function(exception)  { rateFileException(exception); }
		);
	}
}

// voting was successful
function rateFileResult(result, element) {
	if (result.id) {
		$(element).addClassName("disabled");
		$(element).innerHTML = $('voteResult').value;
	}
}
// oops, something went wrong
function exception(exception) {
	alert(exception);
}


// Like comment

function fmLike(mid, uid) {
	// if the user isn't logged in display the error message asking them to log in
	if (uid == 1) {
		alert("Please login to link this comment.");
	} else {	
		if (!$('vote'+mid).next().hasClassName("disabled")) {	
			$('vote'+mid).next().addClassName("disabled");
			$('vote'+mid).innerHTML = parseInt($('vote'+mid).innerHTML)+1;
			jsonRequest('media.rateFile', {"id":mid,"rating":10,"returnData":true},
	    		function(result) {
	    			// change the number after the action has been successful
	    			// $('#vote'+mid).innerHTML = result.votecount;
	    		},
	    		function(exception) { console.log(exception); }
	  		);
  		}
	}
}


// Ajax commenting

function ajaxComments(element, obj) {
	
	url = "/ajax_comments";
	vars = obj;
	
	console.log(vars);

	new Ajax.Request(url, {
		method: 'get',
		parameters: vars,
		onSuccess: function(transport) {
			$(element).innerHTML = transport.responseText;
		}
	});

}

// Ajax form for Send to a Friend

function ajaxform(x) {
	// set the vars
	//var x = $$('#lightwindow_contents #emailform')[0];
	//x = $(x).up(y);
//$(x).getElements()
	//alert(x.name);
	var vars  = Form.serializeElements($(x).getInputs('text'));
	// get the action url
	//var url = $(x).action;
	var url = '/action/tellafriend';
	// send the form using ajax
	new Ajax.Request(url, {
		method: 'post',
		postBody: vars,
		onSuccess: function(transport) {
			// hide the form and show the message that the email was sent
			x.hide();
			x.next(0).show();
			//$('thanksforemail').show();
		}
	});

}



// Tabbed gallery class

var Gallery = Class.create();

Gallery.prototype = {
	initialize: function(e) {
		this.element = $(e);
		this.createTabs();
	},
	createTabs: function() {
		this.element.childElements().each(function(e,i) {
			if (i > 1) {
				e.hide();
			}
		});
		this.tabs = this.element.down(1).childElements();
		this.tabs.each(function(e,i) {
			e.observe('click', function(event) {
				this.siblings().each(function(f,j) {
					f.removeClassName('active');
				});
				this.addClassName('active');
				this.up(2).childElements().each(function(f,j) {
					if (j > 0) {
						f.hide();
					}
				});
				this.up(2).childElements()[i+1].show();
			});
		});
	}
}
