/*----------------------------------------------------------
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();
});

/*----------------------------------------------------------
	ADD CLASS TO URLS THAT LINK TO FILES
----------------------------------------------------------*/

$(document).ready(function(){

	$("a[@href$=jpg]").addClass("img");
	
	$("a[@href$=gif]").addClass("img");
	
	$("a[@href$=pdf]").addClass("pdf");

	$("a[@href$=zip]").addClass("zip");

	$("a[@href$=psd]").addClass("psd");
	
	$("a[@href$=docx]").addClass("doc");
	
	$("a[@href$=doc]").addClass("doc");


/*----------------------------------------------------------
	ADDS THE CLASS LAST TO VARIOUS ITEMS
------------------------------------------------------------*/

	$("div.form_container div:last-child").addClass("last");
	$("div.form_container li:last-child").addClass("last");

/*----------------------------------------------------------
	FILE MANAGER POPUP BUBBLES
------------------------------------------------------------*/

	$("div.form_container .file_manager ul li").mouseover(function(){
		$(this).children("a").addClass("hover");
		$(this).children("span").show();
	});
	$("div.form_container .file_manager ul li").mouseout(function(){
		$(this).children("a").removeClass("hover");
		$(this).children("span").hide();
	});

/*----------------------------------------------------------
	ACTIVATES THE FANCY BUTTONS
------------------------------------------------------------*/

	/* Applies class that makes things fancy */
	
	$('.form_button').addClass('fancy_button');

/*--------------------------
	MAKES IT WORK
---------------------------*/

	var buttonId = $(".form_button input").attr("id");
	var buttonValue = $(".form_button input").attr("value");

	$('.form_button label').after('<label class="button confirm" for=""><span></span></label>');
	$('.form_button label.button').attr('for', buttonId);
	$('.form_button label.button span').text(buttonValue);

/*----------------------------------------------------------
	CANCEL BUTTON
------------------------------------------------------------*/

	$("li.form_button a.cancel").click(function(){
		history.go(-1);
	});

/*----------------------------------------------------------
	ACTIVATES THE FANCY CHECKBOX
------------------------------------------------------------*/

	/* Applies class that makes things fancy */

	$(".checkbox ul").addClass("fancy_checkbox");
	$(".radio ul").addClass("fancy_radio");
	
	/* Adds in the span which will become the checkbox/radio */
	
	$("ul.fancy_checkbox li label").prepend('<span/>');
	$("ul.fancy_radio li label").prepend('<span/>');	
	
	/* Finds any inputs that are already checked and applies the appropriate class to the label */
	
	$("input:checked").siblings("label").addClass("checked");

/*--------------------------
	MAKES IT WORK
---------------------------*/

	$("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");
	});

/*--------------------------
	AND THE RADIOS
---------------------------*/

	$("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");
	});

/*----------------------------------------------------------
	SORTABLE LIST
------------------------------------------------------------*/

	$('div.form_container div ol li').append('<span></span>');

	function updateLast(){
		$('div.form_container div ol li:last-child').addClass('last').siblings('li').removeClass('last');
		$('div.form_container div ol li').css({opacity:1});
		//CODE COULD GO HERE TO SERIALIZE THE DATA
	}
	
	$('div.form_container div ol').sortable({
		axis: 'y',
		containment: 'parent',
		cursor: 'n-resize',
		delay: 50,
		handle: 'span',
		stop: updateLast
	});

	$('div.form_container div ol li span').mousedown(function(){
		$(this).parent('li').siblings('li').css({opacity:0.6});
	});
	$('div.form_container div ol li span').mouseup(function(){
		$(this).parent('li').siblings('li').css({opacity:1});
	});

});
