// -----------------------------------------------------------------------------------
//
//	phongthuy v2.02
//	by Lokesh Dhakar - http://www.huddletogether.com
//	3/31/06
//
//	For more information on this script, visit:
//	http://huddletogether.com/projects/lightbox2/
//
//	Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
//	
//	Credit also due to those who have helped, inspired, and made their code available to the public.
//	Including: Scott Upton(uptonic.com), Peter-Paul Koch(quirksmode.org), Thomas Fuchs(mir.aculo.us), and others.
//
//
// -----------------------------------------------------------------------------------
/*

	Table of Contents
	-----------------
	Configuration
	Global Variables

	Extending Built-in Objects	
	- Object.extend(Element)
	- Array.prototype.removeDuplicates()
	- Array.prototype.empty()

	phongthuy Class Declaration
	- initialize()
	- startPhongthuy()
	- changePhongThuy()
	- resizePhongThuyContainer()
	- showImage()
	- updateDetails()
	- updateNav()
	- enableKeyboardNav()
	- disableKeyboardNav()
	- keyboardAction()
	- preloadNeighborPhongThuy()
	- end()
	
	Miscellaneous Functions
	- getPageScroll()
	- getPageSize()
	- getKey()
	- listenKey()
	- showSelectBoxes()
	- hideSelectBoxes()
	- pause()
	- initLightbox()
	
	Function Calls
	- addLoadEvent(initLightbox)
	
*/
// -----------------------------------------------------------------------------------

//
//	Configuration
//
//var fileLoadingImage = "images/loading.gif";		
//var fileBottomNavCloseImage = "images/closelabel.gif";

var resizeSpeed = 7;	// controls the speed of the image resizing (1=slowest and 10=fastest)

var borderSize = 10;	//if you adjust the padding in the CSS, you will need to update this variable
var fileLoadingImage = pathClient+"images/loading.gif";		
var fileBottomNavCloseImage = pathClient+"images/close_map.png";

// -----------------------------------------------------------------------------------

//
//	Global Variables
//
var phongthuyArray = new Array;
var activePhongThuy;

if(resizeSpeed > 10){ resizeSpeed = 10;}
if(resizeSpeed < 1){ resizeSpeed = 1;}
resizeDuration = (11 - resizeSpeed) * 0.15;

// -----------------------------------------------------------------------------------

//
//	Additional methods for Element added by SU, Couloir
//	- further additions by Lokesh Dhakar (huddletogether.com)
//
Object.extend(Element, {
	getWidth: function(element) {
	   	element = $(element);
	   	return element.offsetWidth; 
	},
	setWidth: function(element,w) {
	   	element = $(element);
    	element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   		element = $(element);
    	element.style.height = h +"px";
	},
	setTop: function(element,t) {
	   	element = $(element);
    	element.style.top = t +"px";
	},
	setLeft: function(element,l) {
	   	element = $(element);
    	element.style.left = l +"px";
	},
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src; 
	},
	setHref: function(element,href) {
    	element = $(element);
    	element.href = href; 
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	}
});

// -----------------------------------------------------------------------------------

//
//	Extending built-in Array object
//	- array.removeDuplicates()
//	- array.empty()
//
Array.prototype.removeDuplicates = function () {
	for(i = 1; i < this.length; i++){
		if(this[i][0] == this[i-1][0]){
			this.splice(i,1);
		}
	}
}

// -----------------------------------------------------------------------------------

Array.prototype.empty = function () {
	for(i = 0; i <= this.length; i++){
		this.shift();
	}
}

// -----------------------------------------------------------------------------------

//
//	phongthuy Class Declaration
//	- initialize()
//	- startPhongthuy()
//	- changePhongThuy()
//	- resizePhongThuyContainer()
//	- showImage()
//	- updateDetails()
//	- updateNav()
//	- enableKeyboardNav()
//	- disableKeyboardNav()
//	- keyboardNavAction()
//	- preloadNeighborPhongThuy()
//	- end()
//
//	Structuring of code inspired by Scott Upton (http://www.uptonic.com/)
//
var phongthuy = Class.create();

phongthuy.prototype = {
	
	// initialize()
	// Constructor runs on completion of the DOM loading. Loops through anchorphongthuy tags looking for 
	// 'phongthuy' references and applies onclick events to appropriate links. The 2nd section of
	// the function inserts html at the bottom of the page which is used to display the shadow 
	// overlayPhongThuy and the image container.
	//
	initialize: function() {
		if(document.getElementById('phongthuy')!=null||document.getElementById('overlayPhongThuy')!=null) return;
		//if (!document.getElementsByTagName){ return; }
		//var anchorsphongthuy = document.getElementsByTagName('a');

		// loop through all anchorphongthuy tags
		/*for (var i=0; i<anchorsphongthuy.length; i++){
			var anchorphongthuy = anchorsphongthuy[i];
			
			var relAttributePhongthuy = String(anchorphongthuy.getAttribute('rel'));
			
			// use the string.match() method to catch 'phongthuy' references in the rel attribute
			if ((relAttributePhongthuy.toLowerCase().match('phongthuy'))){
				anchorphongthuy.onclick = function () {myLightboxPhongthuy.startPhongthuy(this); return false;}
			}
		}*/
		var objBodyPhongthuy = document.getElementsByTagName("body").item(0);
		var objOverlayPhongthuy = document.createElement("div");
		objOverlayPhongthuy.setAttribute('id','overlayPhongThuy');
		objOverlayPhongthuy.onclick = function (){Element.hide('overlayPhongThuy');Element.hide('phongthuy'); return false; };
		objOverlayPhongthuy.style.display = 'none';
		//objOverlayPhongthuy.onclick = function() { myLightboxPhongthuy.end(); return false; }
		objBodyPhongthuy.appendChild(objOverlayPhongthuy);
		
		var objLightboxPhongthuy = document.createElement("div");
		objLightboxPhongthuy.setAttribute('id','phongthuy');
		objLightboxPhongthuy.style.display = 'none';
		objBodyPhongthuy.appendChild(objLightboxPhongthuy);				
	
		var objOuterPhongThuyContainer = document.createElement("div");
		objOuterPhongThuyContainer.setAttribute('id','outerPhongThuyContainer');
		objLightboxPhongthuy.appendChild(objOuterPhongThuyContainer);
		
		//top
		var objBottomNav = document.createElement("div");
 		objBottomNav.setAttribute('id','bottomNav');		
 		
 		var objBottomNavCloseLink = document.createElement("a");
 		objBottomNavCloseLink.setAttribute('id','bottomNavClose');
 		objBottomNavCloseLink.setAttribute('href','#');
 		objBottomNavCloseLink.onclick = function() {Element.hide('overlayPhongThuy');Element.hide('phongthuy'); return false; }
 		objBottomNav.appendChild(objBottomNavCloseLink);
 	
 		var objBottomNavCloseImage = document.createElement("img");
 		objBottomNavCloseImage.setAttribute('src', fileBottomNavCloseImage);
 		objBottomNavCloseImage.setAttribute('border', '0px');
 		objBottomNavCloseLink.appendChild(objBottomNavCloseImage);
 		
 		objOuterPhongThuyContainer.appendChild(objBottomNav);	
 		//end top 

		var objPhongThuyContainer = document.createElement("div");
		objPhongThuyContainer.setAttribute('id','PhongThuyContainer');
		objOuterPhongThuyContainer.appendChild(objPhongThuyContainer);						
	},
	
	//
	//	startPhongthuy()
	//	Display overlayPhongThuy and phongthuy. If image is part of a set, add siblings to phongthuyArray.
	//
	startPhongthuy: function(phongthuyLink) {	

		//hideSelectBoxes();

		// stretch overlayPhongThuy to fill page and fade in
		var arrayPageSize = getPageSize();
		Element.setHeight('overlayPhongThuy', arrayPageSize[1]);
		
		new Effect.Appear('overlayPhongThuy', { duration: 0.2, from: 0.0, to: 0.8 });

		phongthuyArray = [];
		phongthuyNum = 0;		

		if (!document.getElementsByTagName){ return; }
		var anchorsphongthuy = document.getElementsByTagName('a');

		// calculate top offset for the phongthuy and display 
		//var arrayPageSize = getPageSize();
		var arrayPageScroll = getPageScroll();
		var lightboxphongthuyTop = arrayPageScroll[1] + (arrayPageSize[3] / 15);

		Element.setTop('phongthuy', lightboxphongthuyTop);
		//Element.setLeft('map_box', '10');
		//
		//new Effect.Appear('outerPhongThuyContainer', { duration: 1.7, from: 0.0, to: 0.99 });
		//Element.show('outerPhongThuyContainer');
		//document.getElementById('outerPhongThuyContainer').style.height='200px';
		
		this.changePhongThuy(phongthuyNum);
	},

	//
	//	changePhongThuy()
	//	Hide most elements and preload image in preparation for resizing image container.
	//
	changePhongThuy: function(phongthuyNum) {	
		
		activePhongThuy = phongthuyNum;	// update global var
		Element.setInnerHTML('PhongThuyContainer',$('divSearchDefault').innerHTML);
		Element.show('phongthuy');
// 		Element.setInnerHTML('PhongThuyContainer','<div  id="t"><table style="margin: 0px auto;" width="100%" border="0" cellpadding="5"><tbody><tr><td colspan="2" style="background-color:#CC0000; color:#fff; font-size:12px"><div id="phongthuy_close"><A onclick="document.getElementById(\'overlayPhongThuy\').style.display = \'none\';document.getElementById(\'outerPhongThuyContainer\').style.display = \'none\'" href="javascript:void(0)"><IMG alt="Ðóng" src="images/close.gif"></A></div><div style="padding-top:8px;"><strong><asp:Label ID="Label1" runat="server">Xem phong thủy</asp:Label></strong></div></td></tr><tr><td valign="top"><table style="padding-left: 4px" cellPadding="3" width="100%" align="center" border="0"><tr><td style="line-height: 29px;padding-left: 10px" width="60"><asp:label id="Label9" runat="server">Năm sinh</asp:label></td><td style="width: 96px; line-height: 29px"><input width="80px" id="txt_Namsinh" type="text" size="14" name="txt_Namsinh"></td><td style="padding-left: 20px;line-height: 29px" width="60"><asp:label id="Label10" runat="server">Giới tính</asp:label></td><td style="line-height: 29px">&nbsp;<select id="select_Gioitinh" name="a" runat="server" ><OPTION value="1" >Nữ</OPTION><OPTION value="0" selected>Nam</OPTION></select></td></tr><tr><td style="line-height: 29px;padding-left: 10px" width="60"><asp:label id="Label11" runat="server">Hướng nhà</asp:label></td><td style="width: 96px; line-height: 29px"><select id="select_huong" name="a" runat="server"><OPTION value="0" >Đông</OPTION><OPTION value="1" >Tây</OPTION><OPTION value="2" >Nam</OPTION><OPTION value="3" >Bắc</OPTION><OPTION value="4" >Đông Bắc</OPTION><OPTION value="6" >Đông Nam</OPTION><OPTION value="5" >Tây Nam</OPTION><OPTION value="7" >Tây Bắc</OPTION></select></td><td style="padding-left: 20px;line-height: 29px" width="60"></td><td style="line-height: 29px"></td></tr><tr><td align="center" colSpan="4">&nbsp; <input id="btnSearch" onclick="ViewDetail()" type="button" value="Xem" name="btnSearch" runat="server"></td></tr></table></td></tr></tbody></table><table id="searchTable" cellSpacing="0" cellPadding="0" width="100%" align="center" border="0"><tr ><td vAlign="top"><div class="border" id="txtHint" style="z-index:10000"></div></td></tr></table> </div>')			
// 		document.getElementById("txt_Namsinh").focus();
// 		if(document.getElementById('hdpathHuong').value!='')
// 			document.getElementById("select_huong").value=document.getElementById('hdpathHuong').value;
	},

	//
	//	resizePhongThuyContainer()
	//
	resizePhongThuyContainer: function( imgWidth, imgHeight) {

		// get current height and width
		this.wCur = Element.getWidth('outerPhongThuyContainer');
		this.hCur = Element.getHeight('outerPhongThuyContainer');

		// scalars based on change from old to new
		this.xScale = ((imgWidth  + (borderSize * 2)) / this.wCur) * 100;
		this.yScale = ((imgHeight  + (borderSize * 2)) / this.hCur) * 100;

		// calculate size difference between new and old image, and resize if necessary
		wDiff = (this.wCur - borderSize * 2) - imgWidth;
		hDiff = (this.hCur - borderSize * 2) - imgHeight;

		if(!( hDiff == 0)){ new Effect.Scale('outerPhongThuyContainer', this.yScale, {scaleX: false, duration: resizeDuration, queue: 'front'}); }
		if(!( wDiff == 0)){ new Effect.Scale('outerPhongThuyContainer', this.xScale, {scaleY: false, delay: resizeDuration, duration: resizeDuration}); }

		// if new and old image are same size and no scaling transition is necessary, 
		// do a quick pause to prevent image flicker.
		if((hDiff == 0) && (wDiff == 0)){
			if (navigator.appVersion.indexOf("MSIE")!=-1){ pause(250); } else { pause(100);} 
		}
	},
	//
	//	enableKeyboardNav()
	//
	enableKeyboardNav: function() {
		document.onkeydown = this.keyboardAction; 
	},

	//
	//	disableKeyboardNav()
	//
	disableKeyboardNav: function() {
		document.onkeydown = '';
	},

	//
	//	keyboardAction()
	//
	keyboardAction: function(e) {
		if (e == null) { // ie
			keycode = event.keyCode;
		} else { // mozilla
			keycode = e.which;
		}

		key = String.fromCharCode(keycode).toLowerCase();
		
		if((key == 'x') || (key == 'o') || (key == 'c')){	// close phongthuy
			myLightboxPhongthuy.end();
		} else if(key == 'p'){	// display previous image
			if(activePhongThuy != 0){
				myLightboxPhongthuy.disableKeyboardNav();
				myLightboxPhongthuy.changePhongThuy(activePhongThuy - 1);
			}
		} else if(key == 'n'){	// display next image
			if(activePhongThuy != (phongthuyArray.length - 1)){
				myLightboxPhongthuy.disableKeyboardNav();
				myLightboxPhongthuy.changePhongThuy(activePhongThuy + 1);
			}
		}


	},

	end: function() {
		this.disableKeyboardNav();
		//Element.hide('phongthuy');
		new Effect.Fade('overlayPhongThuy', { duration: 0.2});
		//showSelectBoxes();
	}
}

// -----------------------------------------------------------------------------------

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

// -----------------------------------------------------------------------------------

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

// -----------------------------------------------------------------------------------

//
// getKey(key)
// Gets keycode. If 'x' is pressed then it hides the phongthuy.
//
function getKey(e){
	if (e == null) { // ie
		keycode = event.keyCode;
	} else { // mozilla
		keycode = e.which;
	}
	key = String.fromCharCode(keycode).toLowerCase();
	
	if(key == 'x'){
	}
}

// -----------------------------------------------------------------------------------

//
// listenKey()
//
function listenKey () {	document.onkeypress = getKey; }
	
// ---------------------------------------------------

function showSelectBoxes(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

// ---------------------------------------------------

function hideSelectBoxes(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}

// ---------------------------------------------------

//
// pause(numberMillis)
// Pauses code execution for specified time. Uses busy code, not good.
// Code from http://www.faqts.com/knowledge_base/view.phtml/aid/1602
//
function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}

// ---------------------------------------------------
function initLightboxphongthuy() { myLightboxPhongthuy = new phongthuy();}
//Event.observe(window, 'load', initLightboxphongthuy, false);
