// ajaxSend is a global event that is broadcast before every Ajax request.
// This code adds a handler for that event, that in turn adds the authenticity
// (see this page's layout) token to the request data.
$(document).ajaxSend(function(event, request, settings) {
    if (typeof(AUTH_TOKEN) == "undefined") return;

    // settings.data is a serialized string like "foo=bar&baz=boink" (or null)
    settings.data = settings.data || "";
    settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(AUTH_TOKEN);
});

jQuery.extend(jQuery, {
    init_button_hover : function(){
      $('a.button').each(function(){
        $(this).hover(
          function(){$(this).addClass('ui-state-hover cursor_pointer');},
          function(){$(this).removeClass('ui-state-hover cursor_pointer');}
        );
      });
    },

    init_nav_links : function(){
        $('a.nav_link').each(function(){
            // Attach content retrieval
            var link = new LinkHandler({
                use_storage : true,
                type    : 'html',
                anchor  : this,
                success : function(html){
                    $.update_main_frame_content(this,html);
                }
            });
        });
    },

    init_faq : function(list, show_all) {
        $('li', $(list)).each(function(){
            var li = $(this);
            $('h2.question', li).click(function(){
                $._toggle_faq_answer(li);
            });
        });

        //var anchor = $(show_all);
        //anchor.click(function(){
        //    $('li', $(list)).each(function(){ $._toggle_faq_answer(this); });
        //});
    },

    _toggle_faq_answer : function(li) {
        var q = $('h2.question', li);
        var a = $('.answer', li);
                     
        if ( a.hasClass('no_display') ) {
            a.show('blind');
            a.removeClass('no_display');
        } else {
            a.hide('blind');
            a.addClass('no_display');
        }
    },

    // It took you until IE8 to get compliant?!
    pre_ie8_corners : function (){
        if ( $.browser.msie && $.browser.version < 8 ) {
            $('.ui-corner-all').corner('5px'); // this will hose ui-widget-content borders
            $('.ui-corner-top').corner("top 5px");
            $('.corner_5').corner("5px");
            $('.corner').corner("10px");
            $('.corner_top').corner("top 10px");
            $('.corner_bottom').corner("bottom 10px");
        }
    },

    update_main_frame_content : function(anchor, html) {
        $('a.nav_link').each(function(){
            (anchor._action == $(this).attr('href')) ? $(this).addClass('selected') : $(this).removeClass('selected');
        });

        // Because we take this out of the normal flow (positioning), IE
        // does not understand that it is suppose to fade it out. 
        if ( $.browser.msie ) $('#splash_imageC').fadeOut();
        
        $('div#content').fadeOut(function(){
            $(this).html(html);
            if ( $.browser.msie ) $('#splash_imageC').fadeIn(2000);

            $(this).fadeIn(1500,function(){
                // http://blog.bmn.name/2008/03/jquery-fadeinfadeout-ie-cleartype-glitch/
	        if(jQuery.browser.msie)
		    $(this).get(0).style.removeAttribute('filter');


                // IE fadeIn/opacity stuff sucks so there are a couple of things
                // we need to not fadeIn with the rest of it and rather just show
                // it when we have already faded in our main content. This browser sucks!
                $('.no_fade', $(this)).removeClass('hidden');

                //$.pre_ie8_corners();
                $.init_button_hover();
                $.init_nav_links();
            });
        });
        
    },

    log : function(log_msg) {
        if ( !DEBUG ) return;
        $.browser.msie ? alert(log_msg) : console.log(log_msg);
    },

    warn : function(log_msg) {
        if ( !DEBUG ) return;
        // IE: alert(log_msg.name +': '+ log_msg.description +'; msg = '+ log_msg.message);
        $.browser.msie ? alert(log_msg) : console.warn(log_msg);
    },

    is_empty : function(something) {
        // Remember, typeof null will return object, so check for null and
        // empty strings first
        if ( null == something || "" == something ) return true;

        // for jquery objects, check size
        if ( typeof something == "object" )
            return !(something.size() > 0 );

        return (jQuery.trim(something).length == 0 ? true : false);
    }
});