$(document).ready(function(){

    //Set up generic functions
    $('a').click(function(){
        if ($(this).attr('rel')=='external') {
            window.open($(this).attr("href"));
            return false;
        }
    });
    $('.autoclear').autoClear();
    $('.friendlyForm').formSetup();
    $('.calendar .event').setUpCalendar();
    $('.draggable').draggable();

	$('#alertNote').draggable({
		stop: function(event, ui) {
			var pos = $(this).position();
			document.cookie = 'alertTop=' + pos.top;
			document.cookie = 'alertLeft=' + pos.left;
		}
	});
	$('#alertNote .close').click(function() {
		$('#alertNote').fadeOut('fast');
		document.cookie = 'alertHide=1; path=/';
		return false; 
	});
	
    $('#smallCalendar .calendarControl .control').live('click', function() {
       var href = $(this).attr('href');
       var qs = href.substring(href.indexOf('?')+1, href.length);
	   $.get('index.php?c=calendar&a=calendar-ajax&' + qs, function(data) {
          $('#smallCalendar').html(data);
          $('.calendar .event').setUpCalendar();
        });
       return false;
    });
});

(function($){
    $.fn.extend({
        autoClear: function() {
            //Clear any selected field on focus
            $(this).focus(function() {
                $(this).val("");
            });
            //Reset value on blur
            $(this).blur(function() {
				if (! this.value.length) {
	               $(this).val($(this).attr("alt"));
				}
            });
        },
        setUpCalendar: function() {
            //small calendar highlighting
            $(this).each(function() {
               $(this).parent().addClass('highlight-day');
            });
        },
        formSetup: function () {
            //Find each form field in selected Form
            $('.friendlyForm .inputField').each(function () {
                $(this).blur().focus(function () {
                    $(this).addClass('focus');
                    $(this).closest('li').find('label').addClass('focus');
                    $(this).parent().siblings('.helper').fadeIn();
                }).blur(function () {
                    $(this).removeClass('focus');
                    $(this).closest('li').find('label').removeClass('focus');
                    $(this).parent().siblings('.helper').fadeOut();
                });
            });
        }
    });
})(jQuery);
