﻿//===============================================================================
//								Detail Product Page
//===============================================================================

var ProductPage =
{
	isMapLink: true,
	popupPlaceBuy: null,
	isClickDetailProduct: false,

	run: function(e) {
		try {
			this.createPlaceBuy();
			this.ratingStar.createRate();
			this.showTabInProPage();
			this.createGallery.reload();
			this.relateProducts.ini();
			this.createMapLink(e);

		}
		catch (ex) {

		}
	},

	/* ***************************** Create Map Link *************************************/
	createMapLink: function(e) {
		var item = listItemsCatAll[utilObj.getElById("proCatID").innerHTML.trim()];
		// Show Tree
		if (item != null && item.id != "" && !this.isClickDetailProduct) {
			if (typeof (nameCat) != "undefined") {
				BKC.listObjPage[nameCat].findTargetChildEl(item.id, e);
			}
		}
		if (this.isMapLink) {
			MapLink.resetList();
			while (item != null) {
				MapLink.addNode(item.content, item.url);
				item = item.parent;
			}
			MapLink.addNode("Sản phẩm", "callDefaultProductsPage_Click();");
			MapLink.addNode("Trang chủ", "otherLink_Click('#Home', false);");
			MapLink.buildMapLink();
		}
		else {
			MapLink.hide();
		}
	},
	createPlaceBuy: function() {
		var me = this;
		builderPlaceBuy = function() {
			var div = document.createElement("DIV");
			div.id = "divPlaceBuy_DetailProduct";
			div.innerHTML = "Sản phẩm này hiện chưa có thông tin này";
			utilObj.addChildToBody(div);
			me.popupPlaceBuy = new ModalPopup(div.id, "divPlaceBuy_DetailProduct_Popup");
			me.popupPlaceBuy.div = div;
		};
		if (this.popupPlaceBuy == null) {
			builderPlaceBuy();
		}
		var popupProductPlaceBuy = utilObj.getElById("popupProductPlaceBuy");
		var displayProductPlaceBuy_Content = utilObj.getElById("displayProductPlaceBuy_Content");
		var popupProductPlaceBuy_Content = utilObj.getElById("popupProductPlaceBuy_Content");
		if (popupProductPlaceBuy == null) {
			this.popupPlaceBuy.div.innerHTML = displayProductPlaceBuy_Content.innerHTML = "Sản phẩm này hiện chưa có thông tin này";
			return;
		}
		this.popupPlaceBuy.div.innerHTML = popupProductPlaceBuy.innerHTML;
		if ((displayProductPlaceBuy_Content != null) && (popupProductPlaceBuy_Content != null)) {
			displayProductPlaceBuy_Content.innerHTML = popupProductPlaceBuy_Content.innerHTML;
		}

	},
	showPlaceBuy: function() {
		if (this.popupPlaceBuy != null) {
			this.popupPlaceBuy.display();
		}
	},
	hidePlaceBuy: function() {
		if (this.popupPlaceBuy != null) {
			this.popupPlaceBuy.hide();
		}
	},

	/* ***************************** Create Map Link *************************************/

	showTabInProPage: function() {
		if (utilObj.getElById("displayTabProduct") == null
			|| utilObj.getElById("displayProductInfo") == null
			|| utilObj.getElById("displayProductTechnical") == null
			|| utilObj.getElById("displayProductContent") == null
			|| utilObj.getElById("displayProductReviews") == null) return;
		var nameTabPro = "nameTabPro";
		var listTabItems = [];
		listTabItems[nameTabPro + "_1"] = new TabItem("Thông tin sản phẩm", 1, true, utilObj.getElById("displayProductInfo"), null);
		listTabItems[nameTabPro + "_2"] = new TabItem("Nơi bán sản phẩm", 2, false, utilObj.getElById("displayProductPlaceBuy"), null);
		listTabItems[nameTabPro + "_3"] = new TabItem("Bài viết sản phẩm", 3, false, utilObj.getElById("displayProductContent"), null);
		listTabItems[nameTabPro + "_4"] = new TabItem("Nhận xét sản phẩm", 4, false, utilObj.getElById("displayProductReviews"), null);
		var tblTabPro = new TabAjax$BTD(nameTabPro, BKC.pathImgSpacer, listTabItems, "_3", utilObj);
		utilObj.getElById("displayTabProduct").appendChild(tblTabPro.createMain());
		tblTabPro = null;
	}
};

ProductPage.relateProducts =
{
	oImgTop : null,
	oImgBottom : null,
	counter : 1,
	isMouseOver : false,
	isDown : false,

	ini : function()
	{
		var oImgTop = utilObj.getElById("imgRelateScrollTop");
		utilObj.addEvent(oImgTop, "click", function(e) { ProductPage.relateProducts.showListUp(); });
		utilObj.addEvent(oImgTop, "mouseover", function(e) { oImgTop.className = "box_btn_6_T_over"; });
		utilObj.addEvent(oImgTop, "mouseout", function(e) { oImgTop.className = "box_btn_6_T_out"; });
		this.oImgTop = oImgTop;
		var oImgBottom = utilObj.getElById("imgRelateScrollBottom");
		utilObj.addEvent(oImgBottom, "click", function(e) { ProductPage.relateProducts.showListDown(); });
		utilObj.addEvent(oImgBottom, "mouseover", function(e) { oImgBottom.className = "box_btn_6_B_over"; });
		utilObj.addEvent(oImgBottom, "mouseout", function(e) { oImgBottom.className = "box_btn_6_B_out"; });
		oImgBottom.style.visibility = "hidden";
		this.oImgBottom = oImgBottom;
		this.counter = 1;
		if (utilObj.getElById("imgItemRelate" + (this.counter + 1)) == null)
			oImgTop.style.visibility = "hidden";
	},
	
	mouseMoveProduct : function(id, e)
	{
		this.isMouseOver = true;
		var el = utilObj.getElById(id);
		var pos = utilObj.mouseCoordsAndPosEl(e, el);
		el.style.zIndex = 10000;
		el.style.visibility = "visible";
		el.style.top = pos.Y + "px";
		el.style.left = pos.X + "px";
	},
	
	mouseOutProduct : function(id)
	{
		this.isMouseOver = false;
		var el = utilObj.getElById(id);
		el.style.visibility = "hidden";
		el.style.top = "-1000px";
		el.style.left = "-1000px";
	},
	
	autoUpDown : function()
	{
		if (this.isMouseOver) return;
		if (this.isDown)
		{
			this.showListDown();
			this.isDown = (utilObj.getElById("imgItemRelate" + (this.counter - 1)) != null);
		}
		else
		{
			this.showListUp();
			this.isDown = (utilObj.getElById("imgItemRelate" + (this.counter + 1)) == null);
		}
	},
	
	showListUp : function()
	{
		var id = "imgItemRelate" + this.counter;
		if (utilObj.getElById("imgItemRelate" + (this.counter + 1)) == null)
		{
			return;
		}
		this.oImgBottom.style.visibility = "visible";
		new Effect.BlindUp(id);
		this.counter++;
		if (utilObj.getElById("imgItemRelate" + (this.counter + 1)) == null)
		{
			this.oImgTop.style.visibility = "hidden";
		}
	},
	
	showListDown : function()
	{
		var id = "imgItemRelate" + (this.counter - 1);
		if (utilObj.getElById(id) == null)
		{
			return;
		}
		this.oImgTop.style.visibility = "visible";
		new Effect.BlindDown(id);
		this.counter--;
		if (this.counter == 1)
		{
			this.oImgBottom.style.visibility = "hidden";
		}
	}
};

ProductPage.ratingStar = 
{
	oStar_RateDesign : null,
	oStar_RateSpecific : null,
	oStar_RateEffective : null,
	isChoose : false,
	
	createRate : function()
	{
		var t = utilObj.getElById("TotalTimeRate");
		this.createRateDesign(t.innerHTML);
		this.createRateSpecific(t.innerHTML);
		this.createRateEffective(t.innerHTML);
	},
	
	ratingNow : function()
	{
		if (!this.isChoose)
		{
			alert(GetTextLang(6));
			return;
		}
		if (this.oStar_RateDesign != null && this.oStar_RateSpecific != null && this.oStar_RateEffective != null)
		{
			var oTTR = utilObj.getElById("TotalTimeRate");
			oTTR.innerHTML = parseInt(oTTR.innerHTML) + 1;
			var a = this.oStar_RateDesign.getTotalMark();
			var b = this.oStar_RateSpecific.getTotalMark();
			var c = this.oStar_RateEffective.getTotalMark();
			try { oAjax.updateRP( a, b, c, oTTR.title ); } catch (e) { }
			utilObj.getElById("BtnRate").style.display = "none";
		}
	},
	
	createRateDesign : function(t)
	{
		var o = utilObj.getElById("RateDesign");
		this.oStar_RateDesign = new RatingStar$BTD(10, o.title, t, "oStar_RateDesign", "Themes/Default/Images/stars.gif");
		o.appendChild(this.oStar_RateDesign.createMain());
	},
	
	createRateSpecific : function(t)
	{
		var o = utilObj.getElById("RateSpecific");
		this.oStar_RateSpecific = new RatingStar$BTD(10, o.title, t, "oStar_RateSpecific", "Themes/Default/Images/stars.gif");
		o.appendChild(this.oStar_RateSpecific.createMain());
	},
	
	createRateEffective : function(t)
	{
		var o = utilObj.getElById("RateEffective");
		this.oStar_RateEffective = new RatingStar$BTD(10, o.title, t, "oStar_RateEffective", "Themes/Default/Images/stars.gif");
		o.appendChild(this.oStar_RateEffective.createMain());
	}
};

ProductPage.createGallery =
{
	images : Array,
	gallery : Object,
	nav : Object,
	path : BKC.pathNormalGallery,
	
	reload : function()
	{
		this.nav = $("infoGallery");
		if( this.nav == null || !document.getElementsByTagName)
			return false;
		
		this.images = new Array();
		
		this.createImageArray( 
			this.nav.getElementsByTagName("A"),
			this.nav.getElementsByTagName("IMG"), 
			this.images.length );
		this.gallery = new ImageGallery("gallery-image", this.images, 5);
		this.gallery.bEffects = true;
		this.gallery.bAddAnchor = false;
	},
	
	createImageArray : function( _l, _i, _b)
	{
		for( var i = 0; i < _l.length; i++ )
		{
			if (_i[i] == null) continue;
			_l[i].href = this.path + _i[i].src.substring( _i[i].src.lastIndexOf("/") + 1 );
			_l[i].title = _i[i].title;
			_l[i].id = (i+_b) + "link";
			_l[i].onclick = function()
			{
				ProductPage.createGallery.gallery.goTo( parseInt( this.id , 10 ) + 1 );
				return false;
			};
			
			var _m = new Array();
			_m.push(_l[i].href);
			_m.push("");
			_m.push(_l[i].title);
			
			this.images.push(_m);
		}
	}
};