/**
Custom tabber function
@author Darryl
**/

$.tabber = function(){   
    $.tabber.makeTabs();
    
    $('ul.subMenu').each(function(i){
        var checkWidth = 10;
        $(this).children('li').each(function(j){
            checkWidth = checkWidth + $(this).width();
        });
        
        // Check if we must make the sub tabs scrollable
        if(checkWidth > 900){
            $(this).parent().parent().parent().prepend('<div id="scrollLeftHolder'+i+'" class="scrollLeftHolder"><img src="' + baseURL + '/images/tango/16x16/actions/media-seek-backward.png" id="scrollLeftImage'+i+'" /></div>')
            $(this).parent().parent().after('<div id="scrollRightHolder'+i+'" class="scrollRightHolder"><img src="' + baseURL + '/images/tango/16x16/actions/media-seek-forward.png" id="scrollRightImage'+i+'" /></div>')
            $(this).parent().attr('id','scrollHolder'+i);

            $('#scrollRightImage'+i).hover(
                function(){
                    $.tabber.scroll('left',$(this).parent().parent().find('.tabScroller').find('.subMenu').attr('id'),checkWidth);
                    $.tabber.settings.scrollInterval = window.setInterval("$.tabber.scroll('left','"+$(this).parent().parent().find('.tabScroller').find('.subMenu').attr('id')+"')",50);
                },
                function(){
                    window.clearInterval($.tabber.settings.scrollInterval);
                    $("#subul_"+i).stop();
                }
            )
            $('#scrollLeftImage'+i).hover(
                function(){
                    $.tabber.scroll('right',$(this).parent().parent().find('.tabScroller').find('.subMenu').attr('id'),checkWidth);
                    $.tabber.settings.scrollInterval = window.setInterval("$.tabber.scroll('right','"+$(this).parent().parent().find('.tabScroller').find('.subMenu').attr('id')+"')",50);
                },
                function(){
                    window.clearInterval($.tabber.settings.scrollInterval);
                    $("#subul_"+i).stop();
                }
            ) 
        }
    })
    
}

$.extend($.tabber, {
    settings: {
        parentTabs: new Array,
        subTabs: new Array,
        haveSubTabs: false,
        className : 'tabber',
        subClass : 'subTabber',
        activeMainTab: '',        
        activeSubTab: new Array,
        scrollInterval: ''
    },
    
    scroll:function(dir,id){
        if(dir=='left')
            if($('#'+id).css('left').replace('px','')<=(($('#'+id).children('li').length * 30)*-1)+20)
                $('#'+id).animate({"left": (($('#'+id).children('li').length * 30)*-1)+20}, 50);
            else
                $('#'+id).animate({"left": "-=10px"}, 50);
        else if(dir=='right')
            if($('#'+id).css('left').replace('px','')>=0)
                $('#'+id).animate({"left": "0px"}, 50);
            else
                $('#'+id).animate({"left": "+=10px"}, 50);
    },
    
    
    makeTabs:function(){
        // Check we have anything to tab first
        if($('.' + $.tabber.settings.className).length == 0)
            return;            
        
        // Find all parent and sub tabs on the page, and hide them all
        $("."+ $.tabber.settings.className).each(function(i){
            // Attach a fake id in the mean time
            $(this).attr('id','tab_'+$(this).attr('href').replace('#',''));
            
            // Check if we have subtabs within this tab
            if($($(this).attr('href')+' .'+$.tabber.settings.subClass).length>0){
                
                // Give the containing ul an id
                $($(this).attr('href')+' .'+$.tabber.settings.subClass).parent().parent().attr('id','subul_'+i);
                
                $.tabber.settings.haveSubTabs = true;
                var tmp = new Array();
                // We definitely have some, so now let's go through them
                $($(this).attr('href')+' .'+$.tabber.settings.subClass).each(function (i){
                    
                    // Hide it
                    $($(this).attr('href')).css('display','none');
                    
                    // Tmp id it
                    $(this).attr('id','subtab_'+$(this).attr('href').replace('#',''));
                    
                    // Add it as a sub tab
                    tmp.push($(this).attr('href'));
                }).parent().addClass('ui-corner-tr').addClass('ui-corner-tl');

                $.tabber.settings.activeSubTab.push(new Array(tmp.length));
                
                // Add the sub tabs
                $.tabber.settings.subTabs.push(tmp);
            }else{
                $.tabber.settings.subTabs.push(null);
            }
            
            // Hide it
            $($(this).attr('href')).css('display','none');
            
            // Round it
            $(this).parent().addClass('ui-corner-tr').addClass('ui-corner-tl');
            
            // Add it as a parent
            $.tabber.settings.parentTabs.push($(this).attr('href'));
            
            // Attach the onClick to the parent
            $(this).bind('click',function(){
                // Hide the previous tab
                $($.tabber.settings.activeMainTab).css('display','none');
                $('#tab_'+$.tabber.settings.activeMainTab.replace('#','')).parent().removeClass('active');
                // Set active tab to me
                $.tabber.settings.activeMainTab = $(this).attr('href');
                $(this).parent().addClass('active');
                // Show active tab
                $($.tabber.settings.activeMainTab).css('display','block');
                // Return false to prevent page jumping       
                return false;
            });
                                        
        });
        
        // Add all subtabs onClicks
        if($.tabber.settings.haveSubTabs){
            for(i=0;i<$.tabber.settings.parentTabs.length;i++){
                if($.tabber.settings.subTabs[i]!=null){
                    for(j=0;j<$.tabber.settings.subTabs[i].length;j++){
                        $('#subtab_'+$.tabber.settings.subTabs[i][j].replace('#','')).bind("click", {ik: i, jk: j}, $.tabber.toggleSubTab);
                    }
                }
            }
            // Default active subtabs
            for(var i=0;i<$.tabber.settings.subTabs.length;i++){
                if($.tabber.settings.subTabs[i]!=null)
                    $.tabber.settings.activeSubTab[i] = $.tabber.settings.subTabs[i][0];
                else
                    $.tabber.settings.activeSubTab[i] = null;
            }
        }
        

        
        // Now decide what to display
        if(document.location.hash !=''){
            //$.tabber.settings.activeTab[$("a[href='"+document.location.hash+"']").attr('className')] = document.location.hash;
            // Is the active tab a parent or a subtab?
            var foundActive = false;
            $($.tabber.settings.parentTabs).each(function(i){
                if(document.location.hash==this){
                    $.tabber.settings.activeMainTab = document.location.hash;
                    foundActive = true;
                }
            });
            // Check for a subtab
            if(!foundActive && $.tabber.settings.subTabs.length > 0){
                for(i=0;i<$.tabber.settings.parentTabs.length;i++){
                    if($.tabber.settings.subTabs[i]!=null){
                        for(j=0;j<$.tabber.settings.subTabs[i].length;j++){
                            if(document.location.hash==$.tabber.settings.subTabs[i][j]){
                                $.tabber.settings.activeMainTab = $.tabber.settings.parentTabs[i];
                                $.tabber.settings.activeSubTab[i] = $.tabber.settings.subTabs[i][j];
                            }
                        }
                    }
                }
            }
        }else{
            // Nothing specifically selected, so do the default here
            $.tabber.settings.activeMainTab = $.tabber.settings.parentTabs[0];
        }
        
        // Check the width of the subtabs incase we need slide navigation
        
        $($.tabber.settings.activeMainTab).css('display','block');
        $('#tab_'+$.tabber.settings.activeMainTab.replace('#','')).parent().addClass('active');
        
        for(var i=0;i<$.tabber.settings.subTabs.length;i++){
            if($.tabber.settings.activeSubTab[i]!=null){
                $($.tabber.settings.activeSubTab[i]).css('display','block');
                $('#subtab_'+$.tabber.settings.activeSubTab[i].replace('#','')).parent().addClass('active');
            }
        }
    },
    
    toggleSubTab:function(e){
        // Hide the previous tab
        $($.tabber.settings.activeSubTab[e.data.ik]).css('display','none');
        $('#subtab_'+$.tabber.settings.activeSubTab[e.data.ik].replace('#','')).parent().removeClass('active');
        // Set active tab to me
        $.tabber.settings.activeSubTab[e.data.ik] = $(this).attr('href');
        $(this).parent().addClass('active');
        // Show active tab
        $($.tabber.settings.activeSubTab[e.data.ik]).css('display','block');
        // Return false to prevent page jumping 
        return false;
    }
})


    

jQuery(document).ready(function($) {
    $.tabber();
})
