﻿AsyncLoadScripts = function (c, d) {
	for (var b = c.length, e = b, f = function (){
		if (!(this.readyState && this.readyState !== "complete" && this.readyState !== "loaded")){
			this.onload = this.onreadystatechange = null;
			--e || d()
		}
	}, 
	g = document.getElementsByTagName("head")[0], i = function (h) {
		var a = document.createElement("script");
		a.async = true;
		a.src = h;
		a.onload = a.onreadystatechange = f;
		g.appendChild(a)
	}; b;) i(c[--b])
};

var HBT = (function(w)
{
	var charset = document.charset || document.characterSet;
	var debug = typeof hbt_debug != "undefined" && typeof hbt_debug === true ? true : false;
	var pid = typeof hbt_pid != "undefined" && typeof hbt_pid == "number" ? hbt_pid : 0;
	//var charset = typeof hbt_charset != "undefined" && hbt_charset == "cp1251" ? "cp1251" : "utf8";	
	var AdsArResult = [];
	var AdsCount = 0;	
	var default_params = {cid:[], goods:[],	show_count:	3, img_width: 120, img_height: 120}
	
	function uid(params){
		var uid = "";
		uid +=AdsCount;
		uid +=params.show_count;		
		for (var key in params.cid)	uid +=params.cid[key];		
		for (var key in params.goods) uid +=params.goods[key];
		return uid;
	}	
	
	function go_debug(text){
		if(debug)
			alert(text);
	}
	
	function AdsOutput(ads_uid){
		var output = "";
		var title = 0; var url = 1; var image_src = 2; var description = 3; var price = 4;
		
		if(typeof AdsArResult[ads_uid] != "undefined")
		{
			var html_doc = document.getElementsByTagName('head')[0];
			css = document.createElement('style');
			css.setAttribute('type', 'text/css');
			css.innerHTML = ".hbt_adv_shop{width:240px;border:1px solid #E6E6E6;padding:6px}.hbt_adv_shop a:link,a:visited{color:#204A00}.hbt_adv_shop a:hover{color:#FF4500}.hbt_item{margin-bottom:15px;padding-bottom:15px;border-bottom:1px solid #E6E6E6}.hbt_item:last-child{margin-bottom:0;padding-bottom:0;border-bottom:0}.hbt_item > a:link{text-decoration:none}.hbt_title{display:block;text-align:center;margin-bottom:5px}.hbt_price{display:inline-block;margin-left:10px;font-size:18px;color:#DF8D08;font-weight:900}\n"			
			html_doc.appendChild(css);
			
			var arrData = AdsArResult[ads_uid];
			var images_counter = 0;
			
			for (var key in arrData)
			{
				var tempImg = new Image();
				tempImg.onload = function(e){			
					var itemBox = document.createElement('div');
					itemBox.setAttribute("class", "hbt_item");

					var itemTitleSpan = document.createElement('span');
					itemTitleSpan.setAttribute("class", "hbt_title");	
						
					var itemTitleA = document.createElement('a');
					itemTitleA.setAttribute("href", arrData[images_counter][url] + "&referer3=" + encodeURIComponent(w.location.href)); // + "&referer2=" + escape(window.location.hostname)
					itemTitleA.setAttribute("target", "_blank");
					itemTitleA.setAttribute("title", arrData[images_counter][title]);	
					itemTitleA.innerHTML = arrData[images_counter][title];
					itemTitleSpan.appendChild(itemTitleA);
					
					itemBox.appendChild(itemTitleSpan);
					
					var itemImgA = document.createElement('a');
					itemImgA.setAttribute("href", arrData[images_counter][url] + "&referer3=" + encodeURIComponent(w.location.href)); // + "&referer2=" + escape(window.top.location.hostname)
					itemImgA.setAttribute("target", "_blank");
					itemImgA.setAttribute("title", arrData[images_counter][title]);	
					itemImgA.appendChild(e.target);	
					
					itemBox.appendChild(itemImgA);
					
					var itemPriceSpan = document.createElement('div');
					itemPriceSpan.setAttribute("class", "hbt_price");
					itemPriceSpan.innerHTML = arrData[images_counter][price];	
					
					itemBox.appendChild(itemPriceSpan);
					
					document.getElementById("hbt_ads_item_" + ads_uid).appendChild(itemBox);
					this.style.height = "auto";

					images_counter++;
				}				
				tempImg.setAttribute("src", arrData[key][image_src]);	
				tempImg.setAttribute("alt", arrData[key][title]);
				tempImg.setAttribute("align", "middle");
				tempImg.setAttribute("width", "120");
			}	
		}else{
			go_debug("Получен пустой результат");
		}
	}
	
	return {
		ShowAds: function(cid, goods, show_count){ /*ShowAds: function(cid, goods, show_count)*/
			AdsCount++
			var params = {
				cid:		typeof cid != "undefined" && cid instanceof Array ? cid : default_params.cid,
				goods:		typeof goods != "undefined" && goods instanceof Array ? goods : default_params.goods,	
				show_count:	typeof show_count != "undefined" && typeof show_count == "number" ? show_count : default_params.show_count,
				/*
				image:		{
					width	:	typeof (img.width != "undefined" ? img.width : null) != "undefined" && typeof img.width == "number" ? img.width : default_params.img_width,
					height	:	typeof (img.height != "undefined" ? img.height : null) != "undefined" && typeof img.height == "number" ? img.height : default_params.img_height,
				}
				*/
			}			
			var ads_uid = uid(params);			
			document.write("<div class='hbt_adv_shop' id='hbt_ads_item_" + ads_uid+ "'></div>");
			w.AsyncLoadScripts(["http://www.housebit.ru/outads/js/p" + pid + ".js?ads_uid=" + ads_uid + "&cid=" + params.cid + "&goods=" + params.goods + "&show_count=" + params.show_count + "&charset=" + charset],
			function () {
				AdsOutput(ads_uid);
			});
		},
		SetResult: function(uid, data){
			AdsArResult[uid] = data;
		}
	}
})(window)
