
jQuery.fn.autohide = function () {
    return this.each(function () {
        var input = $(this), startValue = input.val();

        input.focus(function () {
            if (input.val() === startValue) {
                input.val('');
            }
        });

        input.blur(function () {
            if (input.val() === '') {
                input.val(startValue);
            }
        });

    });
};


jQuery(window).load(function(){
    jQuery("form#ClientLoginForm input[type!=submit]").autohide();

    jQuery("body:not(.noAjax) div#Nav a:not(div#Nav a.noAjax), div#Sidebar h1 a, a.ajax").each(
        function(i) {

            var link = jQuery(this);
            jQuery(this).click(function(event){

                var $link = $(this);
                $link.addClass('attached');

                event.preventDefault();

                jQuery("div#Nav a, div#Sidebar h1 a").each(function(j){
                    if(i==j)
                        jQuery(this).addClass('current');
                    else
                    if(jQuery(this).hasClass('current')) jQuery(this).removeClass('current');
                });
                jQuery('#Content div.liner div.holder').addClass('loading');
                jQuery('#Intro div').fadeOut();
                jQuery('#Content div.liner div.holder div.content').fadeOut( function() {
                    jQuery.getJSON(link.attr('href')+'ajax', "", function(data) {
                        document.title = data['Title'];
                        jQuery('#Intro div').html(data['Sidebar']).fadeIn();
                        jQuery('#Content div.liner div.holder div.content').html(data['Main']).fadeIn(function(){
                            if(jQuery('#LocationMap').length) initialize();
                        });
                        if(data['SubNavID']) {
                            jQuery('#' + data['SubNavID']).replaceWith(data['SubNav']);
                        }
                        jQuery('#Content div.liner div.holder').removeClass('loading');
                        if (!jQuery('#Overlay').length) {
                            box.buildOverlay();
                        }
                        box.addJboxHandler();
                        box.addWindowHandler();


                        if (jQuery('#CustomAjaxForm_VideoAccessForm').length) {
                            jQuery('#CustomAjaxForm_VideoAccessForm').ajaxForm({
                                beforeSubmit: function () {
                                    jQuery('#Form_VideoAccessForm div.Actions').addClass('loading');
                                },
                                type: "POST",
                                dataType: 'json',
                                success : function(data) {
                                    var $field;
                                    if (data.errors) {
                                        for (i in data.errors) {
                                            if (data.errors.hasOwnProperty(i)) {
                                                $field = jQuery('#CustomAjaxForm_VideoAccessForm *[name="' + i + '"]');
                                                $field.val(data.errors[i]);
                                                $field.parent().addClass('error');
                                                $field.autohide();
                                            }
                                        }
                                    } else {
                                        //location.href = $link.attr('href');
                                        $link.click();
                                    }
                                }
                            });
                        }

                        if (jQuery('#CustomAjaxForm_Form').length) {
                            jQuery('#CustomAjaxForm_Form').ajaxForm({
                                beforeSubmit: function () {
                                    jQuery('#CustomAjaxForm_Form div.Actions').addClass('loading');
                                },
                                type: "POST",
                                dataType: 'json',
                                success : function(data) {
                                    var $field;
                                    jQuery('#CustomAjaxForm_Form div.Actions').removeClass('loading');
                                    if (data.errors) {
                                        for (i in data.errors) {
                                            if (data.errors.hasOwnProperty(i)) {
                                                $field = jQuery('#CustomAjaxForm_Form *[name="' + i + '"]');
                                                $field.val(data.errors[i]);
                                                $field.parent().addClass('error');
                                                $field.autohide();
                                            }
                                        }
                                    } else {
                                        $link.click();
                                    }
                                }
                            });
                        }

                        pageTracker._trackPageview($link.attr('href'));
                    });
                });
            });
        }
    );

});
