﻿//===============================================================================
//								Server Action
//===============================================================================

var viewAction = new ViewAction();

//===============================================================================
//								Exchange Rate
//===============================================================================

var ToolbarControl =
{
	titleFaceBook: "",

	setParaForFB: function(title) {
		this.titleFaceBook = title;
	},

	gotoFaceBook: function() {
		if (this.titleFaceBook == "") {
			this.titleFaceBook = window.document.title;
		}
		window.open("http://www.facebook.com/share.php?u="
			+ location.href + "&t=" + encodeURI(this.titleFaceBook));
	}
};

//===============================================================================
//								Exchange Rate
//===============================================================================

var ExchangeRate =
{
	idEGoldBuy: "eGoldBuy",
	idEGoldSell: "eGoldSell",
	idEForexRate: "eForexRate",
	xmlhttp: null,

	getXmlHttpObject: function() {
		if (window.XMLHttpRequest) {
			// code for IE7+, Firefox, Chrome, Opera, Safari
			return new XMLHttpRequest();
		}
		if (window.ActiveXObject) {
			// code for IE6, IE5
			return new ActiveXObject("Microsoft.XMLHTTP");
		}
		return null;
	},

	stateChanged: function() {
		if (this.xmlhttp.readyState == 4) {
			var xmlDoc = this.xmlhttp.responseXML.documentElement;
			var path = "../Themes/Default/Images/BoxExchange/Weather/";

			document.getElementById("idImageCloudy").src = path +
				xmlDoc.getElementsByTagName("AdImg")[0].childNodes[0].nodeValue;

			document.getElementById("idImageTemp0").src = path +
				xmlDoc.getElementsByTagName("AdImg1")[0].childNodes[0].nodeValue;
			document.getElementById("idImageTemp1").src = path +
				xmlDoc.getElementsByTagName("AdImg2")[0].childNodes[0].nodeValue;
		}
	},

	init: function(e) {
		this.xmlhttp = this.getXmlHttpObject();
		if (this.xmlhttp == null) {
			return;
		}
		this.xmlhttp.onreadystatechange = function(ev) {
			ExchangeRate.stateChanged();
		};
		this.changeWeather(document.getElementById("idComboWeather").value);
	},

	changeWeather: function(region) {
		if (this.xmlhttp == null) {
			return;
		}
		var url = "Handlers/GetWeather.ashx?Region=" + region;
		this.xmlhttp.open("GET", url, true);
		this.xmlhttp.send(null);
	},

	showPopup: function(url, w, h) {
		var mywindow = window.open(url, "mywindow",
			"location=0,status=0,scrollbars=0,resizable=0,width=" + w + ",height=" + h);
		mywindow.moveTo(0, 0);
		mywindow.focus();
	}
};

//===============================================================================
//								Search Service
//===============================================================================

var SearchService =
{
	elCategory : null,
	elRegion : null,
	elProvince : null,
	elDetailService : null,
	listProvince : new Array(),
	provinceID : 0,
	pageSubmit : "",

	init : function(idCboCategory, idCboRegion, idCboProvince, idDetailService, pageSubmit)
	{
		this.elCategory = utilObj.getElById(idCboCategory);
		this.elRegion = utilObj.getElById(idCboRegion);
		this.elProvince = utilObj.getElById(idCboProvince);
		this.elDetailService = utilObj.getElById(idDetailService);
		this.pageSubmit = pageSubmit;
		this.changeRegion();
	},
	
	changeRegion : function()
	{
		this.removeAllItemsCboProvince();
		
		for (var i = 0; i < this.listProvince.length; i++)
		{
			if (this.elRegion.value == this.listProvince[i].regionID)
			{
				this.addItemCboProvince(this.listProvince[i]);
			}
		}
	},
	
	addItemCboProvince : function(province)
	{
		var op = utilObj.createEl("OPTION");
		op.value = province.provinceID;
		op.text = province.name;
		op.selected = this.provinceID == province.provinceID;
		try
		{
			this.elProvince.add(op, null);
		}
		catch (e)
		{
			this.elProvince.add(op);
		}
	},
	
	addProvince : function(province)
	{
		this.listProvince.push(province);
	},
	
	removeAllItemsCboProvince : function()
	{
		while (this.elProvince.options.length > 0)
		{
			this.elProvince.remove(0);
		}
	},

	formSubmit : function()
	{
		location.href = this.pageSubmit + "?CategoryID=" + this.elCategory.value
			+ "&RegionID=" + this.elRegion.value
			+ "&ProvinceID=" + this.elProvince.value
			+ "&DetailServiceID=" + this.elDetailService.value;
	}
};

//===============================================================================
//								Quick Search
//===============================================================================

var QuickSearch =
{
	isKeydown : false,
	
	resetKeydown : function()
	{
		this.isKeydown = false;
	},

	formSubmit : function()
	{
		if (utilObj.getElById("tbxQuickSearch").value.trim() == "")
		{
			//alert(GetTextLang(30));
			utilObj.getElById("tbxQuickSearch").select();
			utilObj.getElById("tbxQuickSearch").focus();
			return false;
		}
		this.goToSearchPage(utilObj.getElById("tbxQuickSearch").value);
	},
	
	goToSearchPage : function(textSearch)
	{
		location.href = utilObj.getElById("pageSubmit").value + "?CategoryID=" 
			+ utilObj.getElById("searchCategoryID").value
			+ "&Keyword=" + encodeURIComponent(textSearch);
	},
	
	keydown : function(e)
	{
		if (e.keyCode == 13)
		{
			this.isKeydown = true;
			
			this.formSubmit();
		}
	}
};

//===============================================================================
//								All Page Submit
//===============================================================================

function allPageSubmit()
{
	return !QuickSearch.isKeydown;
};

//===============================================================================
//								Hot News
//===============================================================================

var HotNewsBlock =
{
	listHotNewsID: new Array(),
	timeDelay: 10000,
	index: 0,
	elCurrentBlock: null,
	timer: null,

	init: function() {
		if (utilObj.getElById("listHotNewsID") == null) return;
		this.timeDelay = utilObj.getElById("timeDelayHotNews").value;
		var listNewsID = utilObj.getElById("listHotNewsID").value.trim();
		if (listNewsID != "") {
			this.listHotNewsID = utilObj.getElById("listHotNewsID").value.split(",");
		}
		this.showTitle();
		this.changeHotNews();
		this.autoStart();
	},

	showTitle: function() {
		var html = "";
		for (var i = 0; i < this.listHotNewsID.length; i++) {
			html += "<div class='blockTitle' onclick='location.href = \"/DetailNews.aspx?NewsID=" +
				this.listHotNewsID[i] + "\";'>";
			html += utilObj.getElById("containerHotNewsTitle" + this.listHotNewsID[i]).innerHTML;
			html += "</div>";
		}
		utilObj.getElById("containerHotNewsTitle").innerHTML = html;
	},

	showHot: function(curIndex) {
		window.clearInterval(this.timer);
		this.index = curIndex - 1;
		this.changeHotNews(true);
		this.timer = window.setInterval("HotNewsBlock.changeHotNews(true)", this.timeDelay);
	},

	next: function() {
		window.clearInterval(this.timer);
		this.changeHotNews(true);
		this.timer = window.setInterval("HotNewsBlock.changeHotNews(true)", this.timeDelay);
	},

	back: function() {
		window.clearInterval(this.timer);
		this.changeHotNews(false);
		this.timer = window.setInterval("HotNewsBlock.changeHotNews(true)", this.timeDelay);
	},

	changeHotNews: function(isNext) {
		var self = HotNewsBlock;
		if (self.listHotNewsID.length <= 1) return;
		if (isNext) {
			self.index++;
			self.index = (self.index <= self.listHotNewsID.length - 1) ? self.index : 0;
		}
		else {
			self.index--;
			self.index = (self.index < 0) ? self.listHotNewsID.length - 1 : self.index;
		}
		utilObj.getElById("containerImageHot").innerHTML = utilObj.getElById("containerImageHot" + self.listHotNewsID[self.index]).innerHTML;
		// utilObj.getElById("containerDescriptionHot").innerHTML = utilObj.getElById("containerDescriptionHot" + self.listHotNewsID[self.index]).innerHTML;
	},

	autoStart: function() {
		if (this.listHotNewsID.length <= 1) return;
		this.timer = window.setInterval("HotNewsBlock.changeHotNews(true)", this.timeDelay);
	}
};

//===============================================================================
//								Horizontal Menu
//===============================================================================

var HorizontalMenu =
{
	position:
	{
		top: "top",
		bottom: "bottom"
	},

	curPosition: "",
	elHorizontalMenu: null,
	sizeElHorizontalMenu: null,
	elContainerTop: null,
	elContainerBottom: null,
	keyCookie: "VNWays.HorizontalMenuPosition",

	changePosition: function(e, pos) {
		this.curPosition = pos;
		if (this.position.bottom == pos) {
			this.elContainerTop.style.height = "0px";
			this.elContainerBottom.style.height = "41px";
		}
		else {
			this.elContainerTop.style.height = "41px";
			this.elContainerBottom.style.height = "0px";
		}

		if (utilObj.isIE && utilObj.appVersion < 7) {
			this.changePositionForIE6(e);
		}
		else {
			this.changePositionForFixed(e);
		}
		utilObj.setCookie(this.keyCookie, pos);
	},

	changePositionForFixed: function(e) {
		if (this.elHorizontalMenu == null || this.sizeElHorizontalMenu == null) return;
		var de = utilObj.getDocument();
		var top = 0;
		var left = 0;
		if (this.curPosition == this.position.bottom) {
			top = de.clientHeight - this.sizeElHorizontalMenu.height;
		}
		this.elHorizontalMenu.style.top = top + "px";
		if (de.clientWidth > this.sizeElHorizontalMenu.width) {
			left = (de.clientWidth - this.sizeElHorizontalMenu.width) / 2;
		}
		this.elHorizontalMenu.style.left = left + "px";
	},

	changePositionForIE6: function(e) {
		var oScroll = utilObj.getDocumentScroll();
		var de = utilObj.getDocument();
		var top = 0;
		var left = 0;
		if (this.curPosition == this.position.bottom) {
			top = (oScroll.scrollTop + de.clientHeight - this.sizeElHorizontalMenu.height);
		}
		else {
			top = oScroll.scrollTop;
		}
		if (de.clientWidth > this.sizeElHorizontalMenu.width) {
			left = (de.clientWidth - this.sizeElHorizontalMenu.width) / 2;
		}
		this.elHorizontalMenu.style.top = top + "px";
		this.elHorizontalMenu.style.left = left + "px";
	},

	init: function(e) {
		this.elContainerTop = utilObj.getElById("idContainerHorizontalMenuTop");
		this.elContainerBottom = utilObj.getElById("idContainerHorizontalMenuBottom");
		this.elHorizontalMenu = utilObj.getElById("idHorizontalMenu");

		this.sizeElHorizontalMenu = utilObj.getElementSize(this.elHorizontalMenu);
		this.curPosition = this.position.top;
		var posCookie = utilObj.getCookie(this.keyCookie);
		if (posCookie != null) {
			this.curPosition = posCookie;
		}

		var me = this;
		if (utilObj.isIE && utilObj.appVersion < 7) {
			this.elHorizontalMenu.style.position = "absolute";
			utilObj.addEvent(utilObj.wd, "resize", function(e) { me.changePositionForIE6(e); });
			utilObj.addEvent(document, "scroll", function(e) { me.changePositionForIE6(e); });
		}
		else {
			this.elHorizontalMenu.style.position = "fixed";
			utilObj.addEvent(utilObj.wd, "resize", function(e) { me.changePositionForFixed(e); });
		}
		this.changePosition(e, this.curPosition);
	}
};

HorizontalMenu.todayNews =
{
	isFirst: true,
	elBtn: null,
	elBtnSize: null,
	elContainer: null,
	elContainerSize: null,

	init: function(ev) {
		this.createContainer();
		if (this.elContainer == null) return;
		this.elContainerSize = utilObj.getElementSize(this.elContainer);
		var me = this;
		utilObj.addEvent(document, "mousedown", function(e) { me.hide(e); });
	},

	createContainer: function() {
		var me = this;
		if (this.elContainer != null) return;
		this.elContainer = utilObj.createEl("DIV");
		this.elContainer.className = "containerTodayNews";
		if (utilObj.isIE && utilObj.appVersion < 7) {
			this.elContainer.style.position = "absolute";
			utilObj.addEvent(utilObj.wd, "resize", function(e) { me.changePositionForIE6(e); });
			utilObj.addEvent(document, "scroll", function(e) { me.changePositionForIE6(e); });
		}
		else {
			this.elContainer.style.position = "fixed";
		}
		utilObj.addChildToBody(this.elContainer);
		this.hide();
	},

	createSepItem: function() {
		var elSep = utilObj.createEl("DIV");
		elSep.className = "sep";
		return elSep;
	},

	createItem: function(num, item) {
		var elContainerItem = utilObj.createEl("DIV");
		elContainerItem.className = "item";
		utilObj.addEvent(elContainerItem, "click", function(e) {
			location.href = "DetailNews.aspx?NewsID=" + item.newsID;
		});

		var d = Date.parseDate(item.lastUpdate, "xs:dateTime");
		var elTitle = utilObj.createEl("DIV");
		elTitle.className = "title";
		elTitle.innerHTML = num + ". " + item.title + " (" + d.getHours() + ":" + d.getMinutes() + ")";
		elContainerItem.appendChild(elTitle);

		var elDescription = utilObj.createEl("DIV");
		elDescription.className = "description";
		elDescription.innerHTML = '<img class="borderImg" align="left" style="border-width: 0px;" src="' + item.image + '"/>' + item.description;
		elContainerItem.appendChild(elDescription);

		return elContainerItem;
	},

	createList: function(list) {
		for (var i = 0; i < list.length; i++) {
			if (i != 0) {
				this.elContainer.appendChild(this.createSepItem());
			}
			this.elContainer.appendChild(this.createItem(i + 1, list[i]));
		}
	},

	changePositionForIE6: function(e) {
		if (this.elContainer == null && this.elContainer.style.display != "" || this.elBtn == null || this.elContainerSize == null) return;
		var top = 0;
		var elBtnPos = utilObj.getElementPosition(this.elBtn);

		if (HorizontalMenu.curPosition == HorizontalMenu.position.bottom) {
			top = elBtnPos.Y - this.elContainerSize.height;
		}
		else {
			top = elBtnPos.Y + this.elBtnSize.height;
		}
		this.elContainer.style.top = top + "px";
	},

	show: function(elBtn, e) {
		if (this.elContainer == null) return;
		this.elContainer.style.display = "";
		this.elBtn = elBtn;
		this.elBtnSize = utilObj.getElementSize(elBtn);
		var elBtnPos = utilObj.getElementPosition(elBtn);
		if (HorizontalMenu.curPosition == HorizontalMenu.position.top) {
			this.elContainer.style.top = (elBtnPos.Y + this.elBtnSize.height) + "px";
			this.elContainer.style.left = (elBtnPos.X + this.elBtnSize.width - this.elContainerSize.width - 2) + "px";
		}
		else {
			this.elContainer.style.top = (elBtnPos.Y - this.elContainerSize.height) + "px";
			this.elContainer.style.left = (elBtnPos.X + this.elBtnSize.width - this.elContainerSize.width - 2) + "px";
		}

		if (this.isFirst) {
			var me = this;
			viewAction.getListTodayNews(function(response) {
				if (response.result != null) {
					if (response.result.hasErrors) {
						me.elContainer.innerHTML = response.result.errorsAlert;
					}
					else {
						me.createList(response.result.data);
					}
				}
				else {
					me.elContainer.innerHTML = "Server Error.";
				}
			});
			this.isFirst = false;
		}
	},

	hide: function(e) {
		if (this.elContainer == null) return;
		var elTarget = utilObj.getTargetElement();
		while (elTarget != null) {
			if (elTarget == this.elContainer) return;
			elTarget = elTarget.parentNode;
		}

		this.elContainer.style.display = "none";
		this.elContainer.style.top = "-1000px";
		this.elContainer.style.left = "-1000px";
	}
};

utilObj.addEvent(window, "load", function(e) {
	HorizontalMenu.init(e);
	HorizontalMenu.todayNews.init(e);
});