
/**
 * functions used for chartsarchive (pulldowns)
 *
*/

function getFirstYear(linkId)
{
    for (var key in aSl[linkId])
    {
        firstYear = key;
        break;
    }

    return firstYear;
}

function don(datum) {
    var Do= new Date();
    Do.setTime(datum.getTime()+(3-((datum.getDay()+6) % 7))*86400000);

    return Do;
}

function archiveBuildPullDown(linkId, year)
{
    formName                = 'archiveSelector_' + linkId;
    selName                 = 'aSW_' + linkId;
    formElement             = document.forms[formName].elements[selName];

    if( formElement ) {

        formElement.innerHTML   = "";

        i = 0;
        for (var key in aSl[linkId][year])
        {
            if (key == 'w')
            {
                for(n=0; n < aSl[linkId][year]['w'].length; n++)
                {
                    weekKey = aSl[linkId][year]['w'][n];
                    if (weekKey != '')
                    {
                        day         = weekKey.substring(4,2);
                        month       = weekKey.substring(0,2);
                        //dateStr   = day + '.' + month + '.' + year;
                        dateStr     = day + '.' + month + '.';
                        date1       = new Date(year,month-1,day);
                        date2Ms     = 604800000 + Date.parse(date1);
                        DoDat       = don(date1);
                        kwj         = DoDat.getFullYear();
                        DoKW1       = don(new Date(kwj,0,4));
                        kwNr        = Math.floor(1.5+(DoDat.getTime()-DoKW1.getTime())/86400000/7)
                        date2       = new Date(date2Ms);
                        month2      = (parseInt(date2.getMonth())+1).toString();
                        if (month2.length < 2)
                        {
                            month2 = '0' + month2;
                        }
                        day2 = date2.getUTCDate().toString();
                        if (day2.length < 2)
                        {
                        	day2 = '0' + day2;
                        }
                        //dateStr2  =  day2 + '.' + month2 + '.' + date2.getUTCFullYear();
                        dateStr2    =  day2 + '.' + month2 + '.';
                        //optTitle    = 'KW ' + kwNr + ' (' + dateStr + '-' + dateStr2 + ')';
                        optTitle    = dateStr + '-' + dateStr2;
                        formElement.options[i] = new Option(optTitle, weekKey);
                        i++;
                    }
                }
            }
            else
            {
                optTitle = aSl[linkId][year][key];
                dateStr = year + '-' + key;
                formElement.options[i] = new Option(optTitle, key);
                i++;
            }
        }
    }
}

function archiveGoToCharts(linkId,channelUrl, seoText, timeSteps)
{
    if (channelUrl == '//charts/')
    {
        channelUrl = '/charts/';
    }

    //seoText = "-";

    channelUrl = channelUrl + seoText + '/';

    formName    = 'archiveSelector_' + linkId;

    selName     = 'aSY_' + linkId;
    formElement = document.forms[formName].elements[selName];
    aDYear      = formElement.options[formElement.selectedIndex].value;

    selName     = 'aSW_' + linkId;
    formElement = document.forms[formName].elements[selName];

    if( formElement ) {

        aDMonthDay  = formElement.options[formElement.selectedIndex].value;
        day         = aDMonthDay.substring(4,2);
        month       = aDMonthDay.substring(0,2);
    } else {

        day   = '';
        month = '';
    }

    if (day == '')
    {
        day = '01';
    }

    if(timeSteps == 'week')
    {
        archiveDate = aDYear + '-' + month + '-' + day;
    }
    else if(timeSteps == 'month')
    {
        archiveDate = aDYear + '-' + month + '-' + day;
    }
    if(timeSteps == 'year')
    {
        archiveDate = aDYear;
    }

    url = channelUrl + linkId + '/' + archiveDate;
    window.location.href= url;

    return false;
}

function fixInfoBoxTop(obj) {

    chartsUl = document.getElementById('charts');
    var tmpmainy = findTop(chartsUl);
    tmpmainy = (parseInt(tmpmainy) + parseInt(chartsUl.offsetHeight));
    liObject = document.getElementById(obj);
    var children = liObject.childNodes;
    for(var i = 0; i < children.length; i++) {

        if(children.item(i).className == 'info_box_block') {

            oChild = children.item(i);
            var tmpy = findTop(liObject);
            tmp = tmpmainy - tmpy;
            if (tmp < oChild.offsetHeight) {

                newtop = (oChild.offsetHeight-tmp) ;
                oChild.style.top = '-' + newtop + 'px';
            } else {

                //Default is -10
                oChild.style.top = '-10px';
            }
            return;
        }
    }
}

function findTop(obj) {
    var curleft = curtop = 0;

    if (obj.offsetParent) {

        do {

            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;

        } while (obj = obj.offsetParent);
    }
    return curtop;
}




