var isIe =  /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
if(isIe)
{
    var flagBannerLoadList = [];
    var flagBannersOldOnload = window.onload;
    window.onload = function()
    {
        if (typeof flagBannersOldOnload == 'function')
        {
            flagBannersOldOnload();
        }
        for(var i in flagBannerLoadList)
        {
            if(typeof flagBannerLoadList[i].activate == 'function')
            {
                flagBannerLoadList[i].activate();
            }
        }
    }
}
var flagBanners = new flagBannerManagerScript();
/*
* flag banner manager
*/
function flagBannerManagerScript()
{
    this.bannerSizes = {
    '1_1': {'w':728, 'h': 90, 'w2':728, 'h2': 250, xOffset: 0}, 
    '1_2': {'w':728, 'h': 90, 'w2':980, 'h2': 280, xOffset: 0},
    '2_1': {'w':340, 'h': 55, 'w2':340, 'h2': 340, xOffset: 0},
    '2_2': {'w':340, 'h': 55, 'w2':480, 'h2': 400, xOffset: -140},
    '3_1': {'w':250, 'h': 250, 'w2':485, 'h2': 250, xOffset: -235},
    '3_2': {'w':250, 'h': 250, 'w2':485, 'h2': 360, xOffset: -235}
   };
   this.banners = {};
   this.closeBtnHeight = 17;
}
flagBannerManagerScript.prototype.initBanner = function(parentId, bannerType, openLimit, mouseLimit)
{
    var properties = {};
    properties.type = bannerType;
    properties.openLimit = openLimit;
    properties.mouseLimit = mouseLimit;
    var sizeProperties = this.bannerSizes[properties.type];
    // default properties
    for(var key in sizeProperties)
    {
        properties[key] = sizeProperties[key];
    }
    
    var banner = new flagBannerScript(properties, parentId);
    this.banners[properties.type] = banner;
}
flagBannerManagerScript.prototype.open = function(type)
{
    var banner = this.banners[type];
    if(banner)
    {
        banner.open(false);
    }
}
flagBannerManagerScript.prototype.opened = function(type)
{
    var banner = this.banners[type];
    if(banner)
    {
        banner.opened();
    }
}
flagBannerManagerScript.prototype.close = function(type)
{
    var banner = this.banners[type];
    if(banner)
    {
        banner.close();
    }
}
flagBannerManagerScript.prototype.viewOpenBtn = function(type)
{
    var banner = this.banners[type];
    if(banner)
    {
        banner.viewOpenBtn();
    }
}

/*
* flag banner
*/
function flagBannerScript(properties, parentId)
{
    var key;
    for(key in properties)
    {
        this[key] = properties[key];
    }

    this.parentDiv = document.getElementById(parentId);
    if(!this.parentDiv)
    {
        return;
    }
    this.parentDiv.innerHTML = '';
    this.parentDiv.style.height = this.h + 'px';
    this.parentDiv.style.position = "relative";
    
    this.id = ox_swf.getAttribute('id');
    this.swfObj = new FlashObject('x', this.id, '728', '290', '9');
    
    for(key in ox_swf.attributes)
    {
       this.swfObj.setAttribute(key, ox_swf.attributes[key]);
    }    
    for(key in ox_swf.variables)
    {
       this.swfObj.addVariable(key, ox_swf.variables[key]);
    }
    
    ox_swf = null;
    
    if(isIe)
    {
        flagBannerLoadList.push(this);
    }
    else
    {
        this.activate();
    }
}
flagBannerScript.prototype.activate = function()
{
    this.swfObj.setAttribute('width', this.w2);
    this.swfObj.setAttribute('height', this.h2);
    this.swfObj.addParam("scale", "noscale");
    this.swfObj.addParam('quality', 'high');
    this.swfObj.addParam('wmode', 'transparent');
    this.swfObj.addParam('allowScriptAccess', 'always');

    // check allowed load open
    this.openOnLoad = false;
    if(this.openLimit)
    {
        var count = this.getValue('opened');
        if(count < this.openLimit)
        {
            ++count;
            this.setValue('opened', count);
            this.swfObj.addVariable('openOnLoad', 1);
            this.openOnLoad = true;
        }
    }

    // check allowed mouse over
    this.mouseLimitLeft = this.getMouseLimitLeft();
    this.swfObj.addVariable('mouseLimit', this.mouseLimitLeft);
    this.swfObj.addVariable('bannerType', this.type);

    // create container
    this.container = document.createElement('div');
    this.container.id = 'flag' + this.type;
    this.container.style.position = "absolute";
    this.container.style.zIndex = "900";
    this.container.style.top = "0px";
    this.container.style.left = "0px";
    this.container.style.width = this.w + "px";
    this.container.style.height = this.h + "px";
    this.container.style.overflow = 'hidden';
    this.parentDiv.appendChild(this.container);
    
    this.swfObj.write(this.container);

    // set flash reference
    this.flash = document.getElementById(this.id);
    this.flash.style.position = 'relative';
    this.flash.style.top = '0px';
    if(this.xOffset != 0)
    {
        this.flash.style.left = this.xOffset + 'px';
    }

    // create close button
    this.closeBtn = document.createElement('a');
    this.closeBtn.style.height = "16px";
    this.closeBtn.style.backgroundColor = "#4f9ddc";
    this.closeBtn.style.cursor = "pointer";
    this.closeBtn.style.color = "#fff";
    this.closeBtn.style.display = "none";
    this.closeBtn.style.textDecoration = "none";
    this.closeBtn.href = "";
    this.closeBtn.style.position = "absolute";
    this.closeBtn.style.right = "0px";
    this.closeBtn.style.padding = "2px 5px 0 5px";
    this.closeBtn.style.bottom = "-1px";
    this.closeBtn.innerHTML = 'Aizvērt';
    var obj = this;
    this.closeBtn.onclick = function()
    {
        obj.close();
        return false;
    };
    this.container.appendChild(this.closeBtn);
    

    // create open button
    this.openBtn = document.createElement('a');
    this.openBtn.style.height = "16px";
    this.openBtn.style.backgroundColor = "#4f9ddc";
    this.openBtn.style.cursor = "pointer";
    this.openBtn.style.color = "#fff";
    this.openBtn.style.display = "none";
    this.openBtn.style.textDecoration = "none";
    this.openBtn.href = "";
    this.openBtn.style.position = "absolute";
    this.openBtn.style.right = "0px";
    this.openBtn.style.padding = "2px 5px 0px 5px";
    this.openBtn.style.bottom = "0px";
    this.openBtn.innerHTML = 'Atvērt';
    var obj = this;
    this.openBtn.onclick = function()
    {
        obj.playBanner();
        return false;
    };
    this.container.appendChild(this.openBtn);

    if(this.openOnLoad)
    {
        this.open(true);
    }
    else if(this.mouseLimitLeft < 1)
    {
        this.viewOpenBtn();
    }
}
flagBannerScript.prototype.viewOpenBtn = function()
{
    this.openBtn.style.display = "block";
}
flagBannerScript.prototype.opened = function()
{
    this.closeBtn.style.display = "block";
}
flagBannerScript.prototype.close = function()
{
    this.closeBtn.style.display = "none";
    if(!this.getMouseLimitLeft())
    {
        this.viewOpenBtn();
    }
    this.flash.Play();

    try
    {
        this.flash.sendToActionScript();
    }
    catch(err)
    {
    }    
    
    this.flash.style.left = this.xOffset + 'px';
    this.container.style.height = this.h + 'px';
    this.container.style.left = '0px';
    this.container.style.width = this.w + 'px';
}
flagBannerScript.prototype.playBanner = function()
{
    this.open(true);
    this.flash.Play();
    try
    {
        this.flash.sendToActionScript();
    }
    catch(err)
    {
    }
}
flagBannerScript.prototype.open = function(internalCall)
{
    if(!internalCall)
    {
        var count = this.getValue('mouse');
        if(count < this.mouseLimit)
        {
            ++count;
            this.setValue('mouse', count);
        }
    }
    
    
    this.openBtn.style.display = "none";
    this.container.style.left = this.xOffset + 'px';
    this.container.style.width = this.w2 + 'px';
    this.container.style.height = (this.h2 + flagBanners.closeBtnHeight) + 'px';
    this.flash.style.left = '0px';
}
flagBannerScript.prototype.getMouseLimitLeft = function()
{
    var mouseCount = this.getValue('mouse');
    var mouseLimitLeft = this.mouseLimit - mouseCount;
    return mouseLimitLeft;
}
flagBannerScript.prototype.setValue = function(name, value)
{
    var c_name = 'flag' + name + '_' + this.type + '_' + this.id;
    var expiredays = 1;
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}
flagBannerScript.prototype.getValue = function(name)
{
    var c_name = 'flag' + name + '_' + this.type + '_' + this.id;
    var defaultValue = 0;
    if (document.cookie.length > 0)
    {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1)
        { 
            c_start = c_start + c_name.length+1; 
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1)
            {
                c_end = document.cookie.length;
            }
            return unescape(document.cookie.substring(c_start, c_end));
        } 
    }
    return defaultValue;
}