/*----------------------------------------------------------
Clear inputs on focus (finds all inputs with a title tag) 
------------------------------------------------------------*/

(function ($) {

$.fn.hint = function (blurClass) {
    if (!blurClass) blurClass = 'blur';

    return this.each(function () {
        // get jQuery instance of 'this'
        var $$ = $(this); 

        // get it once since it won't change
        var title = $$.attr('title'); 

        // only apply logic if the element has the attribute
        if (title) { 

            // Note this is a one liner

            // on blur, set value to title attr if text is blank
            $$.blur(function () {
                if ($$.val() == '') {
                    $$.val(title).addClass(blurClass);
                }
            })

            // on focus, set value to blank if current value matches title attr
            .focus(function () {
                if ($$.val() == title) {
                    $$.val('').removeClass(blurClass);
                }
            })

            // clear the pre-defined text when form is submitted
            .parents('form:first').submit(function () {
                if ($$.val() == title) {
                    $$.val('').removeClass(blurClass);
                }
            }).end()

            // now change all inputs to title
            .blur();

            // counteracts the effect of Firefox's autocomplete stripping the blur effect
            if ($.browser.mozilla && !$$.attr('autocomplete')) {
                setTimeout(function () {
                    if ($$.val() == title) $$.val('');
                    $$.blur();
                }, 10);
            }
        }
    });
};

})(jQuery);

$(function(){ 
	// find all the input elements with title attributes
	$('input[title!=""]').hint();
	$('textarea[title!=""]').hint();
});

/*----------------------------------------------------------
	ADDS THE CLASS LAST TO THE LAST LI IN THE UL
------------------------------------------------------------*/

$(document).ready(function(){
	$("div.form_container li:last").addClass("last");
});

/*----------------------------------------------------------
	ERROR TIPS
------------------------------------------------------------*/

$(document).ready(function(){

	$("div.form_container form ul li input, div.form_container form ul li textarea").focus(function() {
		$(this).next("p").show();
		$(this).addClass("focus");
	});
	$("div.form_container form ul li input, div.form_container form ul li textarea").blur(function() {
		$(this).next("p").hide();
		$(this).removeClass("focus");
	});

});

/*----------------------------------------------------------
	CHECKBOX
------------------------------------------------------------*/

$(document).ready(function(){

	/* Applies class that makes things fancy */

	$("li.checkbox ul").addClass("fancy_checkbox");
	$("li.radio ul").addClass("fancy_radio");
	
	/* Finds any inputs that are already checked and applies the appropriate class to the label */
	
	$("li input:checked").siblings("label").addClass("checked");

/*--------------------------
	CHECKBOX
---------------------------*/

	$("ul.fancy_checkbox li label").click(function(){
		$(this).toggleClass("checked");
	});
	
	$("ul.fancy_checkbox li label").mouseover(function(){
		$(this).addClass("hover");
	});
	
	$("ul.fancy_checkbox li label").mouseout(function(){
		$(this).removeClass("hover");
	});
	
	$("ul.fancy_checkbox li label").mousedown(function(){
		$(this).addClass("pressed");
	});
	
	$("ul.fancy_checkbox li label").mouseup(function(){
		$(this).removeClass("pressed");
	});

/*--------------------------
	RADIO
---------------------------*/

	$("ul.fancy_radio li label").click(function(){
		$(this).parent().siblings().children("label").removeClass("checked");
		$(this).addClass("checked");
	});
	
	$("ul.fancy_radio li label").mouseover(function(){
		$(this).addClass("hover");
	});
	
	$("ul.fancy_radio li label").mouseout(function(){
		$(this).removeClass("hover");
	});
	
	$("ul.fancy_radio li label").mousedown(function(){
		$(this).addClass("pressed");
	});
	
	$("ul.fancy_radio li label").mouseup(function(){
		$(this).removeClass("pressed");
	});

});