﻿function toogleEventTabs(tab){
    //tab är den flik man klickat på.
    if(tab.exists()){
        var tabCollection = getEventTabs();

        if(tabCollection.length !== 0){
            //Vi loopar över samtliga flikar och sätter dem till inaktiva.
            for(var i=0; i<tabCollection.length; i++){                
                tabCollection[i].removeClass("activetab").addClass("tab"); 
            }
        }
        
        
        //Nu sätter vi om så att den flik som just klickats blir aktiv.
        var clickedTab = tab.parent().parent();
        if(clickedTab.exists()){
            //plocka ut css-class för den klickade fliken
            var currentStyle = clickedTab.attr("class");
            var changeToStyle = changeEventCssTo(currentStyle);
            
            clickedTab.removeClass(currentStyle).addClass(changeToStyle);   
            toogleEventContent(clickedEventTabIndex(clickedTab, tabCollection)); 
        }
        
    }
}

function changeEventCssTo(css){
    if(css === "tab"){
        return "activetab";
    }else{
        return "tab";
    }
}

function getEventTabs(){
    var collection = new Array();
    
    $('.eventlisting .boxshadow .tabs').children().each(function(index) {
        if($(this).attr("class") !== "clear"){
           collection.push($(this));
        }
    });
    
    return collection;
}

function clickedEventTabIndex(tab, allTabs){
    var index = -1;
    for(var i=0; i<allTabs.length; i++){
        if (tab[0] === (allTabs[i])[0]){
            return i;
        }
    }
    return index;
}

function toogleEventContent(index){
    if(index === null || index === -1)return;
    var contentCollection = getEventTabContent();
    
    if(contentCollection !== null && contentCollection.length > 0){
        for(var i=0; i<contentCollection.length; i++){
            //Kör reset på allt först!
            contentCollection[i].removeClass("visible").addClass("invisible");
        }
        contentCollection[index].removeClass("invisible").addClass("visible");
    }
}

//Plocka upp en array med innehållet för samtliga flikar.
function getEventTabContent(){
    var collection = new Array();
    $('.eventlisting .boxshadow .body .wrapper .container .frame .content').children().each(function(index) {
           collection.push($(this));
    });

    return collection;
}


$.fn.exists = function () {
    return $(this).length !== 0;    
}

$(window).load(function() {
      
    $('.eventlinktab').click(function() {
        toogleEventTabs($(this));
    });
});

