﻿var targetMenuCurrentTabIndex = 1;

function targetMenuSelect(tabIndex) {
    if (tabIndex != targetMenuCurrentTabIndex) {
            
        $('#targetMenu').css("background-image", "url(/media/design/tab_box_bg_" + tabIndex + ".gif)"); 
        
        //document.getElementById("targetContent" + tabIndex).style.display = "block";
        //document.getElementById("targetContent" + targetMenuCurrentTabIndex).style.display = "none";
        //document.getElementById("themeImage").src = themeImages[tabIndex - 1];
        //document.getElementById("themeTitle").src = themeTitles[tabIndex - 1];
        document.getElementById("themeLink").href = themeLinks[tabIndex - 1];                  
        
        $("#targetContent" +  + targetMenuCurrentTabIndex).fadeOut();
        $("#targetContent" +  + tabIndex).delay(300).fadeIn(500);
        
        $("#themeTitle").fadeOut('fast');
        $("#themeImage").fadeOut('fast',function() {
           $("#themeImage")[0].src = themeImages[tabIndex - 1];
           $("#themeImage").fadeIn(function() {
                $("#themeTitle")[0].src = themeTitles[tabIndex - 1];
                $("#themeTitle").fadeIn();           
           });
        });                        
        
        if (isIE6) pngfix();                   
        targetMenuCurrentTabIndex = tabIndex;
    }
}

var newsMenuCurrentTabIndex = 1;

function newsMenuSelect(tabIndex) {
    if (tabIndex != newsMenuCurrentTabIndex) {
        document.getElementById("newsMenu").style.backgroundImage = "url(/media/design/newsbox_bg_tabs_" + tabIndex + ".gif)";
        document.getElementById("newsContent" + tabIndex).style.display = "block";
        document.getElementById("newsContent" + newsMenuCurrentTabIndex).style.display = "none";
        newsMenuCurrentTabIndex = tabIndex;
    }
}

function themeItem(type, group, title, url, groupurl) {
    this.type = type;
    this.group = group;
    this.title = title;
    this.groupurl = groupurl;
    this.url = url;    
}

function initThemes() {
    var mode = readCookie("themeListMode");
    if (mode==null) changeThemes(0);
    else {
        //writeThemes(mode); 
        changeThemes(mode);   
        document.getElementById("themeModeSelect").selectedIndex = mode;
    }
}

function changeThemes(mode) {
    createCookie("themeListMode",mode);
    if (mode==0) {
        $("#themeCategory").show();
        $("#themeAlpha").hide();
    } else {
        $("#themeCategory").hide();
        $("#themeAlpha").show();    
    }
}

function writeThemes(mode) { 
    changeThemes(mode);
}

function writeThemesOld(mode) {
    createCookie("themeListMode",mode)
    var groups = new Array();
    var themeHtml = '<div class="themeColumn1">';
    var groupCount = 1;
    var groupItemsCount = 0;
    var groupDistribution = "14,16,12,10".split(',');

    // Create groups for theme groups              
    if (mode == 0) {
        for (i = 0; i < themItems.length; i++) {
            var data = themItems[i].split(',');
            var key = data[1];
            var title = data[0];
            var url = data[2];
            var groupurl = "#";
            if (data.length>3) groupurl = data[3];
            var item = new themeItem(0, key, title, url, groupurl);
            if (!groups[key]) groups[key] = new Array();
            groups[key][groups[key].length] = item;
        }
    }

    // Create groups for letters
    if (mode == 1) {
        themItems.sort();
        groupDistribution = "10,10,12,10".split(',');
        for (i = 0; i < themItems.length; i++) {
            var data = themItems[i].split(',');            
            if (data.length > 3) {
                // Add for group
                var key = data[1].substring(0, 1);
                if (!groups[key]) groups[key] = new Array();                                      
                var item = new themeItem(1, key, data[1], data[3], "");            
                groups[key][groups[key].length] = item;                          
            } else {
                // Add for items in group
                var key = data[0].substring(0, 1);
                if (!groups[key]) groups[key] = new Array();                
                var item = new themeItem(1, key, data[0], data[2], "");            
                groups[key][groups[key].length] = item;
            }
        }
    }
    
    // Sort keys
    var b = [];
    for (k in groups) b.push(k);
    b.sort();    

    // Write HTML for groups and items    
    for (var i = 0; i < b.length; ++i) 
    {
        var key = b[i];        
        if (groupItemsCount > groupDistribution[groupCount - 1] && groupCount < 4) {
            groupCount++;
            themeHtml += '</div><div class="themeColumn' + groupCount + '">';
            groupItemsCount = 0;
        }
        themeHtml += '<ul><li><h3>';
        if (groups[key][0].type == 0) themeHtml += '<a href="' + groups[key][0].groupurl + '">';
        themeHtml += key;
        if (groups[key][0].type == 0) themeHtml += '</a>';
        themeHtml += '</h3></li>';
        for (r = 0; r < groups[key].length; r++) {
            themeHtml += '<li><a href="' + groups[key][r].url + '">' + groups[key][r].title + '</a></li>';
            groupItemsCount++;
        }
        themeHtml += '</ul>';
    }

    themeHtml += '</div>'
    document.getElementById("themeData").innerHTML = themeHtml;
}

function preloadImages(images) {
    if (!typeof preload_image_object === "undefined") {
        preload_image_object = new Image();
        for(i=0; i<=images.length; i++) preload_image_object.src = images[i];    
    }
}


function createCookie(name,value) {    
    document.cookie = name+"="+value+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}


var xpos = 0;
function scrollLeft() {
    if (xpos != 203) {
        animateScroll(xpos, xpos + 203);
        xpos = xpos + 203;
    }
}

function scrollRight() {
    if (xpos >= (-203 * 2)) {
        animateScroll(xpos, xpos - 203);
        xpos = xpos - 203;
    }
}

function animateScroll(from, to) {                       
    var t = 0;        
    if (to > from) {
        for (i = from; i <= to; i = i+2) {
            t = t + 2;
            window.setTimeout("setScrollPosition(" + i + ")", t);
        }
    } else {
        for (i = from; i >= to; i = i-2) {
            t = t + 2;                             
            window.setTimeout("setScrollPosition(" + i + ")", t);
        }        
    }
}

function setScrollPosition(xpos) {
    var ta = document.getElementById("thumbsArea");        
    ta.style.left = xpos + "px";    
}
