var taleOldOnload = window.onload;

window.onload = function()
{
    if (typeof taleOldOnload == 'function')
    {
         taleOldOnload();
    }

    window.taleInstance = new tale();
    taleInstance.init();
}

tale = function ()
{

}


tale.prototype.init = function()
{
    if (this.isPlayerPopup())
    {
        this.resizePlayerPopup();
    }
    else
    {
        this.initFontSizeSwitches();
        this.initPlayerPopupLink();
    }
}


tale.prototype.initFontSizeSwitches = function()
{
    var switchBox = document.getElementById('fontSizeBox');
    this.fontSizeSwitchesBox = switchBox;
    this.pageTextBox = document.getElementById('pageText');

    if ((!switchBox) || (!this.pageTextBox))
    {
        return;
    }

    var switches = switchBox.getElementsByTagName('a');

    var script = this;
    for (var i=0; i<switches.length; i++)
    {
        var link = switches[i];

        link.blockUrl = link.href + '&block=fontSizeSwitches&random=' + Math.random();

        link.onclick = function()
        {
            var el = this;
            script.setFontSize( el );
            loadXmlHttp(el.blockUrl, script.processFontSizeSwitchResponse, script);
            el = null;
            return false;
        }
    }
}

tale.prototype.setFontSize = function ( link )
{
    var rel = link.getAttribute('rel');
    if (!rel)
    {
        return;
    }
    this.pageTextBox.className = rel;

}
tale.prototype.isPlayerPopup = function()
{
    var box = document.getElementById('popupPlayerBox');
    if (box)
    {
        return true;
    }
    return false;
}
tale.prototype.processFontSizeSwitchResponse = function (xmlhttp, script)
{
    if (xmlhttp.readyState != 4)
    {
        return;
    }

    script.fontSizeSwitchesBox.innerHTML = xmlhttp.responseText;
    script.initFontSizeSwitches();
}

tale.prototype.initPlayerPopupLink = function()
{
    var link = document.getElementById('playerPopupLink');
    if (!link)
    {
        return;
    }
    link.onclick = function()
    {
        var url = link.href;

        var winName = 'player_popup';
    	var W = 250;
    	var H = 150;

    	var left = 0;
    	var top  = 0;

       	var popupPlayerWin = window.open(url, winName, "scrollbars=0,resizable=1,width=" + W + ",height=" + H + " , top=" + top + ", left=" + left);

        return false;
    }

}
tale.prototype.resizePlayerPopup = function()
{
    var initW = 250;
    var initH = 150;

    var contentWidth = 225;
    var contentHeight = 106;

    window.resizeTo(initW,initH);
    var size = this.getPopupInnerSize();

    var xToAdd = initW - size[0];
    var yToAdd = initH - size[1];

    var newWidth = contentWidth + xToAdd;
    var newHeight = contentHeight + yToAdd;

    window.resizeTo(newWidth, newHeight);
    var left = Math.round((screen.width-newWidth)/2);
    var top = Math.round((screen.height-newHeight)/2);
    if (top < 0)
    {
        top = 0;
    }
    if (left < 0)
    {
        left = 0;
    }
    window.moveTo(left, top);

}

tale.prototype.getPopupInnerSize = function()
{
    var x,y;
    if (window.innerHeight) // all except Explorer
     {
       x = window.innerWidth;
       y = window.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
    {  // Explorer 6 Strict Mode
       x = document.documentElement.clientWidth;
       y = document.documentElement.clientHeight;
    }
    else if (document.body) // other Explorers
    {
       x = document.body.clientWidth;
       y = document.body.clientHeight;
    }
    var size = new Array(x, y);

    return size;
}

