/**
 * All In One JavaScript file
 * includes the following js files:
 * - cookie.js
 * - textsize.js
 * - trust.js
 * - enlarge.js
 * - iglossary.js
 * - navigation.js
 */

/*******************************************************************************
 * cookie.js 
 */
var Cookie = {
    set : function(name, value, seconds) {
        if (seconds) {
            d = new Date();
            d.setTime(d.getTime() + (seconds * 1000));
            expiry = '; expires=' + d.toGMTString();
        } else
            expiry = '';
        document.cookie = name + "=" + value + expiry + "; path=/";
    },
    get : function(name) {
        nameEQ = name + "=";
        ca = document.cookie.split(';');
        for (i = 0; i < ca.length; i++) {
            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;
    },
    unset : function(name) {
        Cookie.set(name, '', -1);
    }
};

/*******************************************************************************
 * textsize.js
 */
function changeTextSize(className) {
    $('access').getElementsByClassName(className)[0].addClassName('accessOn');
    if (className == 'small') {
        document.getElementsByTagName('body')[0].style.fontSize = 100 + '%';
        $('access').getElementsByClassName('medium')[0]
                .removeClassName('accessOn');
        $('access').getElementsByClassName('large')[0]
                .removeClassName('accessOn');
        Cookie.set('textSize', className, 31556926);
    } else if (className == 'medium') {
        document.getElementsByTagName('body')[0].style.fontSize = 115 + '%';
        $('access').getElementsByClassName('small')[0]
                .removeClassName('accessOn');
        $('access').getElementsByClassName('large')[0]
                .removeClassName('accessOn');
        Cookie.set('textSize', className, 31556926);
    } else if (className == 'large') {
        document.getElementsByTagName('body')[0].style.fontSize = 130 + '%';
        $('access').getElementsByClassName('small')[0]
                .removeClassName('accessOn');
        $('access').getElementsByClassName('medium')[0]
                .removeClassName('accessOn');
        Cookie.set('textSize', className, 31556926);
    }
}
function textSizeLinks() {
    var textControls = ['<div id="access">', snippet('misctextsize'),
                        '<a class="small" href="#" onclick="changeTextSize(\'small\');return false;">A</a>',
                        '<a class="medium" href="#" onclick="changeTextSize(\'medium\');return false;">A</a>',
                        '<a class="large" href="#" onclick="changeTextSize(\'large\');return false;">A</a>',
                    '</div>'];
    $('textSize').innerHTML = textControls.join('');
}

document.observe('dom:loaded', function() {
    // write out the text size links if the textSize div is present
    if ($('textSize')) {
        textSizeLinks();
        if (!Cookie.get('textSize')) {
            changeTextSize('small');
            Cookie.set('textSize', 'small', 31556926);
        } else {
            changeTextSize(Cookie.get('textSize'));
        }
    }
});

/*******************************************************************************
 * trust.js
 */
function trustBoxOK() {

    new Effect.Fade($('trust'));
    new Effect.Appear($('audience'));

    Cookie.set('trust', 'dismissed', 86400);

    return false;
}
function audienceOK() {

    new Effect.Fade($('trustFilterNoise'));
    new Effect.Fade($('trustFilter'));
    new Effect.Fade($('audience'));

    // set cookie here if javascript is enabled
    // set cookie so window isn't displayed again for 1 day
    Cookie.set('audience', 'dismissed', 86400);

    return false;
}

//added by eqeee - jleino - 110601
function toHome() {
    var home = '/'; //window.location.protocol + '//' + window.location.host + '/scripts/pages/en/index.php';
    debug(home);
    window.location.href = home;
    return false;
}


/*******************************************************************************
 * enlarge.js
 */
function closeEnlarge() {
    $('filter').hide();
    $('enlarge').hide();
}

var enlargeHTML = '';

function enlarge(imgSrc, imgWidth, imgHeight, imgTitle, ImgTxt, ImgCategory,
        ImgID) {
    dcsMultiTrack('DCS.dcsuri', imgSrc, 'WT.cusImageCategory', ImgCategory,
            'WT.cusImage', imgTitle.replace("'", ''), 'WT.cusLogOn',
            loggedIn ? '1' : '0', 'WT.cusEngage', 'Engagement', 'WT.cusEPoint',
            '2');
    enlargeHTML = '<div id="imageInfo" style="width:' + imgWidth
            + 'px;margin-top:'
            + Math.max(document.documentElement.scrollTop, 20) + 'px">';
    enlargeHTML += '<a href="#"  class="close" onclick="closeEnlarge(); return false;"><img src="/html/images/close.gif" alt="'
            + snippet('miscpopupclose') + '" /></a>';
    enlargeHTML += '<img src="' + imgSrc + '" width="' + imgWidth
            + '" height="' + imgHeight + '" /><br />';
    enlargeHTML += '<span>';
    /*
     * if (loggedIn != true) { enlargeHTML += '<input class="register" name=""
     * type="button" value="'+snippet('imageregister')+'"
     * onclick="dcsMultiTrack(\'DCS.dcsuri\', \''+imgSrc+'\',
     * \'WT.cusImageCategory\', \''+ImgCategory+'\', \'WT.cusImage\',
     * \''+imgTitle.replace("'", '')+'\');
     * location=\'/scripts/pages/'+snippet('LangID')+'/_registration/login.php\';"
     * />'; } else {
     */
    enlargeHTML += '<input class="register" name="" type="button" value="'
            + snippet('imagedownload')
            + '" onclick="dcsMultiTrack(\'DCS.dcsuri\', \''
            + imgSrc
            + '\', \'WT.cusImageCategory\', \''
            + ImgCategory
            + '\', \'WT.cusImage\', \''
            + imgTitle.replace("'", '')
            + '\', \'WT.cusEngage\',\'Engagement\',\'WT.cusEPoint\',\'3\'); downloadImage(\''
            + ImgCategory + '\', \'' + ImgID + '\');" />';
    /* } */
    enlargeHTML += '</span>';
    enlargeHTML += '<h3>' + imgTitle + '</h3>';
    enlargeHTML += '<p>' + ImgTxt + '</p>';
    enlargeHTML += '</div>';
    $('filter').show();
    $('enlarge').innerHTML = enlargeHTML;
    $('enlarge').show();
}

function downloadImage(ImgCategory, ImgID) {
    window.open('/scripts/pages/' + snippet('LangID')
            + '/download_image.php?c=' + ImgCategory + '&i=' + ImgID + '');
}

document.observe('dom:loaded', function() {
    $('contain').insert( {
        before : '<div id="filter"></div><div id="enlarge"></div>'
    });
    $('filter').hide();
    $('enlarge').hide();
});

/*******************************************************************************
 * iglossary.js
 */
document.observe('dom:loaded', function() {
    $$('a.iglossary').each(
            function(element) {
                new Tip(element, $(element.href.substring((element.href
                        .indexOf('#') + 1))).innerHTML, {
                    duration : 0.3,
                    effect : "appear",
                    title : element.innerHTML
                });
            });
});

/*******************************************************************************
 * navigation.js
 */
document.observe('dom:loaded', function(){
    // attach event listeners to child LIs of element with id parentId
    function suckerFish(target) {
        if ($(target) == null) return;
        $$('#' + target + '>li').each( function (item,index) {
            item.observe('mouseover', function (event) {
                this.addClassName('over');
            });
            item.observe('mouseout', function (event) {
                this.removeClassName('over');
            });
        });
    }
    function initSpotLightVideo( data, index ) {
        // console.log('movie[' + index + ']', data);
        new SpotLightVideo(data);
    }
    
    suckerFish('nav');
    suckerFish('nav2');
    // set the focus on the search query field
    $$('#nav2 li.last ul')[0].observe('mouseover', function(){
        $('q').focus();
    });
    window.spotLightMovies.each(initSpotLightVideo);
    debug('spotlightMovies initialized');
});

// used to get snippets of text for use in javascript files.
function snippet (term) {
    return snippetArr[term];
}

//document
//        .write('<script type="text/javascript" src="/html/json/general.json"><\/script>');
try {
    document.execCommand("BackgroundImageCache", false, true);
} catch (err) {
}

/**
 * add search tracking parameter
 */
function addWToss(form) {
    dcsMultiTrack("WT.oss", form.q.value);
    return true;
}


