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');
    });
    
    //has attribute
    jQuery.fn.hasAttr = function(name) {
        return (typeof this.attr(name) !== 'undefined') && (this.attr(name) !== false);
    }
    
    //element load
    jQuery.fn.elementLoad = function(href, func) {
        this.each(function() {
          jQuery(this).load(href.replace('#id_', ' #').replace('#class_', ' .'), func);
        });
    };
    
    //remote content
    jQuery.fn.remoteContent = function(href, effect, speed) {
        
        if (!effect) { effect = 'blind'; }
        if (!effect) { speed  = 'slow'; }
        
        this.each(function() {
          
            if ('none' == effect) {

                $(this).toggleClass('active').elementLoad(href, function() {
                    updateRows();
                    $(this).show();
                });
            } else {
                
                $(this).toggleClass('active').hide(effect, speed, function() {
                    $(this).elementLoad(href, function() {
                        updateRows();
                        $(this).show(effect, speed);
                    });
                });
            }
        });
    };
    
    //toggle content
    function toggleContent(jq_obj) {
        
        if (!jq_obj.hasAttr('id') || jq_obj.hasClass('no_id')) {
            
            jq_obj.toggleClass('active').next('.toggle_content').slideToggle('slow');
        } else {
            
            var id = jq_obj.attr('id').replace('-toggler', '');
            
            jq_obj.toggleClass('active');
            
            if (jq_obj.hasClass('load')) {
                
                jQuery('#' + id + '.toggle_content').elementLoad(jq_obj.attr('href'), function() {
                    updateRows();
                });
            }
            
            jQuery('#' + id + '.toggle_content').slideToggle('slow');
        }
    }
    jQuery('.toggle_content').hide();
    jQuery('.toggler').click(function() {
        toggleContent(jQuery(this));
        return false;
    });
    
    //load content
    function contentLoader(jq_obj) {

        if (!jq_obj.hasAttr('id') || jq_obj.hasClass('no_id')) {
            
            if (jq_obj.next('.remote_content').length == 0) {
                jq_obj.after('<div class="remote_content">Loading...</div>');
            }
            
            jq_obj.next('.remote_content').remoteContent(jq_obj.attr('href'));
        } else {
            
            var id = jq_obj.attr('id').replace('-loader', '');

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

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

        if (!jq_obj.hasAttr('id')) {
            return true;
        }
        
        var id = jq_obj.attr('id').replace('-trigger', '-dialog');
        
        jq_obj.toggleClass('active');

        if (jq_obj.hasClass('load')) {
            
            if (jQuery('#' + id + '.dialog').length == 0) {
                jq_obj.after('<div class="dialog hidden" id="' + id + '" title="' + jq_obj.attr('title') + '">Loading...</div>');
                jQuery('#' + id).dialog(dialog_conf)
            }
            
            jQuery('#' + id + '.dialog').elementLoad(jq_obj.attr('href'), function() {
                updateRows();
            });
        }

        jQuery('#' + id + '.dialog').dialog('open'); 
 
        return false;
    }
    jQuery('.dialog_trigger').not('.hover, .dblclick').click(function() {
        return dialogTrigger(jQuery(this));
    });
    jQuery('.dialog_trigger.hover').not('.click, .dblclick').hover(function() {
        return dialogTrigger(jQuery(this));
    });
    jQuery('.dialog_trigger.dblclick').not('.hover, .click').dblclick(function() {
        return dialogTrigger(jQuery(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;
    });

});
