jQuery(function() {
    
    var dialog_conf = {
        autoOpen: false,
        width: 1000,
        modal: true
    };
    
    jQuery('.accordion').not('ul').accordion();
    jQuery('.tabs').tabs();
    jQuery('.dialog_auto').dialog();
    jQuery('.dialog').dialog(dialog_conf);
    
    //menus
    jQuery('.v_menu ul.menu.tree').accordion({
        collapsible: true,
        active: false,
        header: 'li.header-root > a',
        autoHeight: false,
        icons: false
    });
    jQuery('.v_menu ul.menu.tree li.header-root > ul, \n\
            .h_menu ul.menu.tree li[class$="-root"] > ul').accordion({
        collapsible: true,
        active: false,
        header: 'li.header-1 > a',
        autoHeight: false,
        icons: false
    });
    jQuery('li.header > a.ui-accordion-header').each(function() {
        if (jQuery(this).next().is('ul')) {
            jQuery(this).addClass('expandable').append('<span class="icon toggle_plus float_right">Expand</span>');
        }
    });
    jQuery('li.header > a.ui-accordion-header').click(function() {
        jQuery('li.header > a.ui-accordion-header > span.icon').removeClass('toggle_minus').addClass('toggle_plus').text('Expand');
        jQuery('li.header > a.ui-accordion-header.ui-state-active > span.icon').removeClass('toggle_plus').addClass('toggle_minus').text('Collapse');
    });
    
    //remote content
    $.fn.remoteContent = function(href, effect, speed) {
        
        if (!effect) { effect = 'blind'; }
        if (!effect) { speed  = 'slow'; }
        
        this.each(function() {
          
            if ('none' == effect) {

                $(this).toggleClass('active').load(href.replace('#id_', ' #').replace('#class_', ' .'), function() {
                    updateRows();
                    $(this).show();
                });

            } else {
                $(this).toggleClass('active').hide(effect, speed, function() {
                    $(this).load(href.replace('#id_', ' #').replace('#class_', ' .'), function() {
                        updateRows();
                        $(this).show(effect, speed);
                    });
                });
            }

        });
    };
 
    
    //toggle content
    jQuery('.toggle_content').hide();
    jQuery('.toggler').click(function() {
        if (jQuery(this).hasClass('load')) {
            jQuery(this).toggleClass('active').next('.toggle_content').load(this.href.replace('#id_', ' #').replace('#class_', ' .'), function() {
                updateRows();
                jQuery(this).slideToggle('slow');
            });
        } else {
            jQuery(this).toggleClass('active').next('.toggle_content').slideToggle('slow');
        }
        
        return false;
    });
    
    //load content
    function contentLoader(jq_obj) {

        if (jq_obj.attr('id').length == 0 || jq_obj.hasClass('no_id')) {
            jQuery('.remote_content').remoteContent(jq_obj.attr('href'));
            return false;
        }

        if (jQuery('#' + jq_obj.attr('id') + '-content').length == 0) {
            jq_obj.after('<div class="remote_content" id="' + jq_obj.attr('id') + '-content">Loading...</div>');
        }
        jQuery('#' + jq_obj.attr('id') + '-content').remoteContent(jq_obj.attr('href'));

        return false;
    }
    jQuery('.content_loader').not('.hover, .dblclick').click(function() {
        return contentLoader(jQuery(this));
    });
    jQuery('.content_loader.hover').not('.click, .dblclick').hover(function() {
        return contentLoader(jQuery(this));
    });
    jQuery('.content_loader.dblclick').not('.hover, .click').dblclick(function() {
        return contentLoader(jQuery(this));
    });
    
    //dialogs
    function dialogTrigger(obj) {

        if (obj.id.length == 0) {
            return true;
        }

        if (jQuery(obj).hasClass('load')) {
            if (jQuery(obj).next('.dialog').length == 0 && jQuery('#' + obj.id + '-dialog').length == 0) {
                jQuery(obj).after('<div class="dialog hidden" id="' + obj.id + '-dialog" title="' + obj.title + '">Loading...</div>');
                jQuery('#' + obj.id + '-dialog').dialog(dialog_conf)
            }
            jQuery(obj).toggleClass('active');
            jQuery('#' + obj.id + '-dialog').load(obj.href.replace('#id_', ' #').replace('#class_', ' .'), function() {
                jQuery(this).dialog('open'); 
                updateRows();
            });
        } else {
            jQuery(obj).toggleClass('active');
            jQuery('#' + obj.id + '-dialog').dialog('open'); 
        }
 
        return false;
    }
    jQuery('.dialog_trigger').not('.hover, .dblclick').click(function() {
        return dialogTrigger(this);
    });
    jQuery('.dialog_trigger.hover').not('.click, .dblclick').hover(function() {
        return dialogTrigger(this);
    });
    jQuery('.dialog_trigger.dblclick').not('.hover, .click').dblclick(function() {
        return dialogTrigger(this);
    });
    
    //table row colours
    function updateRows() {
        jQuery('tbody tr:visible').removeClass('odd').filter(':odd').addClass('odd');
    }
    updateRows();
    
    //follow me sidebar
    jQuery('.follow_me').jScroll({top: 15});
    
    //scroll to top
    jQuery('a[href="#top"]').click(
        function (e) {
        jQuery('html, body').animate({scrollTop: '0px'}, 400);
        return false;
    });

});
