/**
 * Changes the HTML Season Overview in the Club Pages
 * to display icons instead of text. Displays Audio
 * and Video content if available. Barely does the same
 * as spieltag & spielplan
 */
 
/**
 * Checks for the existance of the overview
 * HTML Table, alters it
 */
function updateOverview() {
    if ($('table.ta01').length > 0) {
        //already been here?
        if ($('table.ta01 th.tn07').text() == "") {
            //"repair" header of the table
            tableUpdateHeader();
            //german version
            if (lang == "de") {
                tableAddBericht();
                tableAddVideo();
                tableAddAudio();
                tableAddAnalysis();
            } else {
                tableAddEnglishAnalysis();
            }
        }
    }
}

/**
 * Content was loaded, now display the icons
 */
function cb_contentLoaded(cb) {
    updateOverview();
}

/**
 * Alters the table header, depending on
 * the language
 */
function tableUpdateHeader() {
    if (lang == "de") {
        $('table.ta01 th.tn07').text("Bericht");
        //add other points: analyse, video, audio
        $('table.ta01 tr.head').append($('<th>').addClass("tn07Small").text("Analyse"));
        var league = document.location.href.match('/(liga2?)/')[1];
        if (league == "liga"){
          $('table.ta01 tr.head').append($('<th>').addClass("tn07Small").text("Video"));
        }
        $('table.ta01 tr.head').append($('<th>').addClass("tn07Small").text("Audio"));
    }
    if (lang == "en") {
        $('table.ta01 th.tn07').text("Details");    
    }
    //restyle Goal column
    $('.tn06').attr('style', 'padding-right: 30px; padding-left: 10px;');
    //remove titles from links 
    $("tr[class*='omi_']").each(function(el) {
        if ($(this).find('a').attr("class") == "") {
            //this is for ie, doesnt recognize utf8 (sometimes)
            var clazz = "hasNoResult";
            if ($(this).find('a').text() == "Spielbericht") {
                clazz = "hasResult";
            }
            $(this).find('a').attr("class", clazz);
        }
    });
}

/**
 * Adds a column to the table depending on
 * the existance of an article
 */
function tableAddBericht() {
    $('a.hasResult').each(function(el) {
        //remove text
        $(this).text("");
        //add icon
        $(this).append($('<img src="/pics/spieltag/bericht.gif" class="smallTip" title="Spielbericht" onmousemove="moveToolTip(event, this)" onmouseout="removeToolTips()" />'));
    });
    //those having no result, but maybe a prereport
    $('a.hasNoResult').each(function (el) {
        //check for content
        if ($(this).text().length > 1) {
            var title = $(this).text();
            var link = $(this).attr("href");
            //remove text
            $(this).text("");
            //add icon
            $(this).append($('<img src="/pics/spieltag/bericht.gif" class="smallTip" title="'+title+'" onmousemove="moveToolTip(event, this)" onmouseout="removeToolTips()" />'));
        }
    });
}

/**
 * Adds an analysis column. the link is
 * computed by looking at the original link.
 */
function tableAddAnalysis() {    
    $('a.hasResult').each(function(el) {
        //compute link
        var link = $(this).attr("href");
        link = link.replace("reiter=b", "reiter=a");
        var td = $('<td class="tn07Small"><a href="'+link+'"><img src="/pics/spieltag/analyse.gif" class="smallTip" title="Analyse" onmousemove="moveToolTip(event, this)" onmouseout="removeToolTips()" /></a></td>');
        $(this).parent().after(td);
    });
    //those without analysis
    $('a.hasNoResult').each(function(el) {
        $(this).parent().after($('<td></td>'));
    });
}

/**
 * Adds a video column by checking for the
 * video in the video array.
 */
function tableAddVideo() {     
    //append video table
    $("tr[class*='omi_']").each(function(el) {   
        //get omi
        var omi = $(this).attr('class');
        omi = omi.substring(omi.indexOf("omi_") + 4);
        //check for array existance
        var available = null;
        var filename = null;
        //check for some news
        if (typeof(video_cache) != "undefined") {
            if (video_cache[omi]){
                for (var obj in video_cache[omi]) {
                    available = video_cache[omi][obj]['deeplink'];
                    filename = video_cache[omi][obj]['video_type'];
                }
            }
        }
        if (available) {
            var bericht = "Spielbericht";
            //check news status
            if (filename.indexOf('v') > -1) {
                bericht = "Vorbericht";
            }
            //add link
            var link = "";
            var td = $('<td class="tn07Small"><a href="'+available+'"><img src="/pics/spieltag/video.gif" class="smallTip" title="'+bericht+'" onmousemove="moveToolTip(event, this)" onmouseout="removeToolTips()" /></a></td>');
            $(this).append(td);
        } else {
            //add empty
            $(this).append($('<td style="display:none;"></td>'));
        }
    });
}

/**
 * Adds a video column by checking for the
 * video in the video array.
 */
function tableAddAudio() {   
    //append audio table
    $("tr[class*='omi_']").each(function(el) {   
        //get omi
        var omi = $(this).attr('class');
        omi = omi.substring(omi.indexOf("omi_") + 4);
        //check for array existance
        var available = null;
        var filename = null;
        //check for some news
        if (typeof(availableAudionews) != "undefined") {
            if (availableAudionews[omi]){
                for (var obj in availableAudionews[omi]) {
                    available = availableAudionews[omi][obj]['fullurl'];
                    filename = availableAudionews[omi][obj]['filename'];
                }
            }
        }
        if (available) {
            var bericht = "Spielbericht";
            //check news status
            if (filename.indexOf('vb') > -1) {
                bericht = "Vorbericht";
            }
            
            //add link
            var link = "javascript:w=window.open('/de/bundesliga-audio/popup_audio_matchday.php?s="+available+"&i="+bericht+"','LIGARADIO','width=303,height=168');removeToolTips();";
            
            var td = $('<td class="tn07Small"><a href="'+link+'"><img src="/pics/spieltag/radio.gif" class="smallTip" title="'+bericht+'" onmousemove="moveToolTip(event, this)" onmouseout="removeToolTips()" /></a></td>');
            $(this).append(td);
        } else {
            //add empty
            $(this).append($('<td></td>'));
        }
    });
}

/**
 * Adds an analysis link to the result table
 * for the english version
 */
function tableAddEnglishAnalysis() {
    var counter = 0;
    $("tr[class*='omi_']").each(function(el) {
        counter++;
        //get omi
        var omi = $(this).attr('class');
        omi = omi.substring(omi.indexOf("omi_") + 4);
        //check result, without result, no analysis
        if ($(this).find("td.tn06").text() != "") {
            //add link to the last column
            var link = "/en/"+liga+"/matches/"+saison+"/index.php?omi="+omi+"&reiter=a&tag="+counter+"&activIndex=2";
            //console.log($(this).find("td.tn07"));
            $(this).find("td.tn07").html('<a href="'+link+'"><img src="/pics/spieltag/analyse.gif" class="smallTip" title="Details" onmousemove="moveToolTip(event, this)" onmouseout="removeToolTips()" /></a>');
        }
    });  
}