function EnBac_Widget()
{
    this.host = 'http://static.enbac.com/qcdantri';
    this.script_folder = this.host + '/';
	this.rand_limit = 4;

	this.Shuffle = function(v){
		for(var j, x, i = v.length; i; j = parseInt(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x);
		return v;
	}
    this.toArray = function (o){
	var a = new Array();
	for (x in o){
	    a.push(o[x]);
	}
	return a;
    }
    this.CreateStyleSheet = function()
    {
       var css = '.qcst_content_css {color:#3b3b3b; font-size:12px;font-family:Arial, Helvetica, sans-serif;} .qcst_content_css a, .qcst_content_css a:link, .qcst_content_css a:visited{font-weight:bold;color:#3374b4;text-decoration:underline; font-size:12px;padding-bottom:7px;font-family:Arial, Helvetica, sans-serif;} .qcst_content_css a:hover{color:#ffcc00}.qcst_content_css strong{color:#dc0c0c; font-size:12px}';

		css += '.qcsantao_content a,.qcsantao_content a:link,.qcsantao_content a:visited {color:#3b3b3b;text-decoration:none;font-weight:normal}';
		css += '.qcsantao_content a:hover {text-decoration:underline;}';

		css += '.qcdt_bottom {height:18px; width:300px; padding-top:3px;font-family:Arial, Helvetica, sans-serif;} .qcdt_bottom a, .qcdt_bottom a:link, .qcdt_bottom a:visited {text-decoration:none;color:#d3e9ff; font-family:Arial, Helvetica, sans-serif; font-size:11px}.qcdt_bottom a:hover {text-decoration:none; color:#fcff00;font-family:Arial, Helvetica, sans-serif; font-size:11px}';
        var style = document.createElement('style');
        style.type = 'text/css';
        var head = document.getElementsByTagName('head')[0];
        head.appendChild(style);
        if(style.styleSheet)  // IE
            style.styleSheet.cssText = css;
        else  // other browserss
            style.appendChild(document.createTextNode(css));
    }

    this.InitScript = function()
    {
        this.CreateStyleSheet();
    }

    this.OnLoaded = function(data, methodName)
    {
        var json = eval(data);
        var output = '';
        var image_url = '';
	var item_per_page = 5;
	var url = window.location.href.toLowerCase();
	var allItems = new Array();
	
	if(json['VIP']){
	    allItems['VIP'] = this.toArray(json['VIP']);
	}
	if(json['NORMAL']){
	    allItems.NORMAL = this.toArray(json['NORMAL']);
	}
    if ( url.indexOf('static.enbac.com/') <= 0){
		if (allItems.NORMAL){
			allItems.NORMAL = this.Shuffle(allItems.NORMAL);
		}
		if (allItems.VIP){
			allItems.VIP = this.Shuffle(allItems.VIP);
		}
	}
	if (allItems.VIP){
	    allItems.Items = allItems.VIP.concat(allItems.NORMAL);
	}
	else{
	     allItems.Items = allItems.NORMAL;
	}
	allItems.Items.length = (this.rand_limit > allItems.Items.length  ) ? allItems.Items.length : this.rand_limit;
	allItems.Items = this.Shuffle(allItems.Items);
	for (var i = 0; i < allItems.Items.length; i++)
        {
	    var image_url = '';
	    var price_img = '';
            if (allItems.Items[i].images != '')
            {
		var temp_arr = new Array();
		var k = 0;
		for (var j in allItems.Items[i].images)
		{
		    temp_arr[k] = allItems.Items[i].images[j];
		    k++;
		}
		var ranNumImg= Math.floor(Math.random()*(temp_arr.length));
		image_url = this.script_folder+temp_arr[ranNumImg].src;
		price_img = temp_arr[ranNumImg].price;
		//alert(allItems.Items[i].images);
            }
            else
            {
		image_url = this.script_folder+'images/no-images-69x53.jpg';
            }
	    output += '<div style="height:15px"><span></span></div><div id="enbac_item_'+allItems.Items[i].id+'" style="background:#fff;margin:0;  padding:0;height:110px;overflow:hidden" align="left" class="qcst_content_css"><a href="'+this.host+'/click.php?id='+allItems.Items[i].id+'" target="_blank" style="border:none;margin:0;padding:0"><img src="'+image_url+'" width="110" height="80" border="0" alt="'+allItems.Items[i].Title+'" title="'+allItems.Items[i].Title+'" style="border:none;margin:0;padding:0; margin-left:5px;" align="right" /></a><div style="height:32px;overflow:hidden"><a href="'+this.host+'/click.php?id='+allItems.Items[i].id+'" target="_blank" style="border:none;margin:0;padding:0">'+allItems.Items[i].Title+'</a></div></br><div style="margin:8px 0"><strong>'+price_img+'</strong></div><div class="qcsantao_content"><a href="'+this.host+'/click.php?id='+allItems.Items[i].id+'" target="_blank" style="border:none;margin:0;padding:0">'+allItems.Items[i].Description+'</a></div></div></div><div style="height:15px"><span></span></div>';


	    try{
		document.getElementById('EnBac_QCsangtao_Oto_300_600_Item').innerHTML = output;
	    }
	    catch (e){
		    //Do nothing?
	    }
        }

    }

}
output = '<div align="center" style="width:300px; height:600px; border: 1px solid #3273b5;background-color:#ffffff"><div style="height:16px;"><a href="http://admicro.vn" target="_blank"><img src="http://static.enbac.com/qcdantri/images/top_ad_dantri_oto.gif" width="298" height="15" border="0" style="border:none;margin:0;padding:0" /></a></div>';

output += '<div align="center"><div id="EnBac_QCsangtao_Oto_300_600_Item" style="width:279px;margin:0;padding:0" align="center"></div></div>'

output += '<div class="qcdt_bottom" style="background:#2886e6;margin-top:3px" align="center"><a href="http://enbac.com/Ad_content.html" target="_blank"><img src="http://static.enbac.com/qcdantri/images/dantri_oto_160_ico.gif"  border="none" align="absbottom" style="border:none" /> Liên hệ quảng cáo</a></div></div>';



try{
	var url = window.location.href.toLowerCase();

	if ( url.indexOf('static.enbac.com/') > 0)
	{
		document.write('<div id="EnBac_QCsangtao_Oto_300_600_Content" style="width:300px; height:600px" align="left"></div>');
	}
    try{
        document.getElementById('EnBac_QCsangtao_Oto_300_600_Content').innerHTML = output;
    }
    catch (e){
				//Do nothing?
    }
}
catch (e){
				//Do nothing?
}

