﻿ImageAdvert = function(regionID, title, url, imgName, type, width, height)
{
	this.regionID = regionID;
	this.title = title;
	this.url = url;
	this.imgName = imgName;
	this.type = type; // IMG; SWF
	this.width = width;
	this.height = height;
};

var ImageAdvertType =
{
	image : "IMG"
	,flash : "SWF"
};

ImageAdvertPosition = function()
{
	this.left = new Array();
	this.right = new Array();
	this.top = new Array();
	this.bottom = new Array();
	this.leftScroll = new Array();
	this.rightScroll = new Array();
	this.popup = new Array();
};

/*
var ArrayAdvert  = new Object();
ArrayAdvert["1"] = new ImageAdvertPosition();
ArrayAdvert["1"].left.push(new ImageAdvert("1", "", "", "", ImageAdvertType.image));
 */
 
LoadImageAdvert = function(myName, pathImg)
{
	this.myName = myName;
	this.pathImg = pathImg;
	this.oldPage = "";
	this.oldKey = "";
	AdvertScroll$BTD.ini();
	this.regionID = -1;
};

LoadImageAdvert.prototype.showAdvert = function(objUrl)
{
	var re = getRegionFromCookie();
	var value = "home";
	if (objUrl.page == "Products")
	{
		value = objUrl.paramater["CatID"];
	}
	else if (objUrl.page == "DetailProduct")
	{
		value = objUrl.catID;
	}
	
	if (this.oldKey == value && 
		this.oldPage == objUrl.page && 
		(re != null && this.regionID == re.id)) return;
	if (re != null)
	{
		this.regionID = re.id;
	}	
	this.oldPage = objUrl.page;
	this.oldKey = value;
	
	var me = this;
	setTimeout(function(e) { me.executeAdvert(value); }, 2000);
};

LoadImageAdvert.prototype.executeAdvert = function(key)
{
	this.createLeft(key);
	this.createRight(key);
	this.createTop(key);
	this.createBottom(key);
	this.createLeftScroll(key);
	this.createRightScroll(key);
	this.createPopup(key);
};

LoadImageAdvert.prototype.createLeft = function(key)
{
	//var el = this.createListAdvert(ArrayAdvert[key].left);
	
};

LoadImageAdvert.prototype.createRight = function(key)
{
	if (ArrayAdvert[key] != null && ArrayAdvert[key].right != null && ArrayAdvert[key].right.length > 0)
	{
		var el = this.createListAdvert(ArrayAdvert[key].right);
		if (tblInfoAdvert != null)
		{
			tblInfoAdvert.setContent(el.innerHTML);
		}
	}
	else
	{
		if (tblInfoAdvert != null)
		{
			tblInfoAdvert.setContent("");
		}
	}
};

LoadImageAdvert.prototype.createPopup = function(key)
{
	if (ArrayAdvert[key] != null && ArrayAdvert[key].popup != null && ArrayAdvert[key].popup.length > 0)
	{
		var el = this.createListAdvert(ArrayAdvert[key].popup);
		BKC.advert.setContentPopup(el.innerHTML);
	}
};

LoadImageAdvert.prototype.createTop = function(key)
{
	
};

LoadImageAdvert.prototype.createBottom = function(key)
{
	
};

LoadImageAdvert.prototype.createLeftScroll = function(key)
{
	if (ArrayAdvert[key] != null && ArrayAdvert[key].leftScroll != null && ArrayAdvert[key].leftScroll.length > 0)
	{
		var el = this.createListAdvert(ArrayAdvert[key].leftScroll);
		AdvertScroll$BTD.appendElementToLeft(el);
	}
	else
	{
		AdvertScroll$BTD.appendElementToLeft();
	}
};

LoadImageAdvert.prototype.createRightScroll = function(key)
{
	if (ArrayAdvert[key] != null && ArrayAdvert[key].rightScroll != null && ArrayAdvert[key].rightScroll.length > 0)
	{
		var el = this.createListAdvert(ArrayAdvert[key].rightScroll);
		AdvertScroll$BTD.appendElementToRight(el);
	}
	else
	{
		AdvertScroll$BTD.appendElementToRight();
	}
};

/*
 * Private Method
 */

LoadImageAdvert.prototype.createListAdvert = function(listAdv)
{
	var elContainer = utilObj.createEl("DIV");
	for (var i = 0; i < listAdv.length; i++)
	{
		if (this.regionID != listAdv[i].regionID) continue;
		var item = listAdv[i];
		if (item.type == ImageAdvertType.image)
		{
			elContainer.appendChild(this.createIMG(item));
		}
		else
		{
			elContainer.appendChild(this.createSWF(item));
		}
	}
	return elContainer;
};

LoadImageAdvert.prototype.createSWF = function(item)
{
	var elDIV = utilObj.createEl("DIV");
	elDIV.style.marginBottom = "5px";
	elDIV.style.cursor = "pointer";
	var html = '<span ' + this.getJSOnClick(item.url) + '><object id="' + this.myName + 'FlashObject" style="margin: 0px; padding: 0px;" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + item.width + '" height="' + item.height + '">';
	html += '<param name="movie" value="' + this.pathImg + item.imgName + '" />';
	html += '<param name="wmode" value="transparent" />';
	html += '<embed width="' + item.width + '" height="' + item.height + '" id="' + this.myName + 'FlashEmbed" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" wmode="transparent" type="application/x-shockwave-flash" scale="noscale" quality="high" menu="false" src="' + this.pathImg + item.imgName + '" />';
	html += "</object></span>";
	elDIV.innerHTML = html;
	return elDIV;
};

LoadImageAdvert.prototype.createIMG = function(item)
{
	var elDIV = utilObj.createEl("DIV");
	elDIV.style.marginBottom = "5px";
	elDIV.style.cursor = "pointer";
	var html = '<img src="' + this.pathImg + item.imgName 
		+ '" border="0" alt="' + item.title + '" width="' + item.width 
		+ '" height="' + item.height + '" ' + this.getJSOnClick(item.url) + ' />';
	elDIV.innerHTML = html;
	return elDIV;
};

LoadImageAdvert.prototype.getJSOnClick = function(url)
{
	if (url.trim() != "")
	{
		return ' onclick="utilObj.wd.open(\'' + url + '\');" ';
	}
	return "";
};
