

//
//	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";
	},
	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;
	}
});

//
// 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;
}


function showSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

// ---------------------------------------------------

function hideSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}

// ---------------------------------------------------

function showFlash(){
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i != flashObjects.length; i++) {
		flashObjects[i].style.visibility = "visible";
	}

	var flashEmbeds = document.getElementsByTagName("embeds");
	for (i = 0; i != flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = "visible";
	}
}

// ---------------------------------------------------

function hideFlash(){
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i != flashObjects.length; i++) {
		flashObjects[i].style.visibility = "hidden";
	}

	var flashEmbeds = document.getElementsByTagName("embeds");
	for (i = 0; i != flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = "hidden";
	}

}


function moveAlertScreen() {
	arrayPageSize = getPageSize();
	arrayPageScroll = getPageScroll();
	topPosition = arrayPageScroll[1] + (arrayPageSize[3] / 3); // arrayPageSize[3] / 10	
	
	new Effect.Move('alertContainer', { y: topPosition, mode: 'absolute' });
}

var AlertBox = Class.create({
	
	
	initialize: function(messageKey, description ) {
		this.messageKey = messageKey;
	  	this.description = description;
		this.messageBox = null;
		
		this.showButton = false;
		
		this.style = "alert-info";
		if( messageKey.startsWith('MSG_INFO') ) {
			this.style = "alert-info";
			this.showButton = false;
			
		} else if( messageKey.startsWith('MSG_OK') ) {
			this.style = "alert-ok";
			this.showButton = true;
			
		} else if( messageKey.startsWith('MSG_ERROR') ) {
			this.style = "alert-error";
			this.showButton = true;
		}
		this.box = null;
		this.createAlertBox();
	},
  
	createAlertBox: function() {
	 	var alertBox = this;
	 	
		var box = new Element('div', {'id':'SexyAlertBox-Box','style':'display: block; z-index: 65557; margin-left: auto; margin-right: auto; width: 500px;'} );
		var inBox = new Element('div', {'id':'SexyAlertBox-InBox'});
		
		var boxContent = new Element('div', {'id':'SexyAlertBox-BoxContent'});	
		var boxContenedor = new Element('div',{'id':'SexyAlertBox-BoxContenedor', 'class':''+alertBox.style } );
		
		//var messageArea = new Element('div', {'style': 'height: 200px'});
		var msgHeader = new Element('h1');
		msgHeader.update('ข้อความ :');
		
		boxContenedor.appendChild(msgHeader);
		var msgDesc = new Element('p',{'style':'margin:10px; font-family:Verdana; font-size:12px;'});
		
		if( alertBox.description == '' ) {
			msgDesc.update('\u0e01\u0e23\u0e38\u0e13\u0e32\u0e23\u0e2d\u0e2a\u0e31\u0e01\u0e04\u0e23\u0e39\u0e48...');
			//กรุณารอสักครู่
			var loadingImage = new Element( 'div', {'id':'loadingImage'} );
			msgDesc.appendChild(loadingImage);
		} else {
			msgDesc.update(alertBox.description);
		}
		
		boxContenedor.appendChild(msgHeader);
		boxContenedor.appendChild(msgDesc);
		
		
		//boxContenedor.appendChild(messageArea);
		
		//box.messageArea = messageArea;
		
		if( alertBox.showButton ) {
			var buttons = new Element('div',{'id':'SexyAlertBox-Buttons'});
				
			var okButton = new Element('input', {'id':'BoxPromptBtnOk', 'class':'button', 'type':'button', 'value':'OK', 'style':'width: 70px;'});
			alertBox.okButton = okButton;
			buttons.appendChild(okButton);
			
			//var cancelButton = new Element('input', {'id':'BoxPromptBtnCancel', 'class':'button', 'type':'button', 'value':'CANCEL', 'style':'width: 70px;'});
			//alertBox.cancelButton = cancelButton;
			//buttons.appendChild(cancelButton);
			
			boxContenedor.appendChild( buttons );
			
		}
		
		boxContent.appendChild(boxContenedor);
		inBox.appendChild(boxContent);  
		box.appendChild(inBox);
		
		alertBox.box = box;
	},
	
	setFocusOnBox: function() {
		try {
			$('SexyAlertBox-Box').focus();
		} catch( exception ) {
		}
	},
	
	getBoxElement: function() {
		return this.box;
	}
  
});

function hideAlertBox() {

	Event.stopObserving(window, 'scroll', moveAlertScreen );
	
	if( $('alertContainer') != null ) {
		Element.hide('alertContainer');
		$('alertContainer').update('');
	}
	
	if( $('alertOverlay') != null ) {
		new Effect.Appear('alertOverlay', { duration: 0.4, from: 0.3, to: 0.0 });
	}
	showSelectBoxes();
	showFlash();

}

function moveAlertScreen() {
	arrayPageSize = getPageSize();
	arrayPageScroll = getPageScroll();
	topPosition = arrayPageScroll[1] + (arrayPageSize[3] / 3); // arrayPageSize[3] / 10
	new Effect.Move('alertContainer', { y: topPosition, mode: 'absolute' });	
}


function showAlertBox(messageArray) {

	// hide loading screen if exist
	hideAlertBox();
	
	hideSelectBoxes();
	hideFlash();
	
	//set grey layer
	
	if( $('alertOverlay') == null ) {
		var body = document.getElementsByTagName("body").item(0);
		var alertOverlay = document.createElement("div");
		alertOverlay.setAttribute('id','alertOverlay');
		body.appendChild(alertOverlay);
		
	}
	$('alertOverlay').style.display = 'none';
	
	//set container of message and progress bar

	if( $('alertContainer') == null ) {
		var body = document.getElementsByTagName("body").item(0);
		var alertContainer = document.createElement("div");
		alertContainer.setAttribute('id','alertContainer');
		alertContainer.style.display = 'none';
		body.appendChild(alertContainer);		
	}

	var messageKey = '';
	var messageDesc = '';
	
	if( messageArray != null ) {
	
		messageKey = messageArray[0].split('|')[0];
		messageDesc = messageArray[0].split('|')[1];
	}
		
	var alertBox = new AlertBox(messageKey,messageDesc);
	
	if( alertBox.okButton != null ) {
		
		alertBox.okButton.onclick = function() {
			Event.stopObserving(window, 'scroll', moveAlertScreen );
			
			if( $('alertContainer') != null ) {
				Element.hide('alertContainer');
			}
			if( $('alertOverlay') != null ) {
				new Effect.Fade('alertOverlay', { duration: 0.4});
			}
			showSelectBoxes();
			showFlash();
		}
	}
	
	$('alertContainer').appendChild(alertBox.getBoxElement());
	
	// calculate top offset for the lightbox and display 
	var arrayPageSize = getPageSize();
	Element.setHeight('alertOverlay', arrayPageSize[1] );
	new Effect.Appear('alertOverlay', { duration: 0.4, from: 0.0, to: 0.3 });
	
	var arrayPageScroll = getPageScroll();
	var topPosition = arrayPageScroll[1] + (arrayPageSize[3] / 3); // arrayPageSize[3] / 10
		
	Element.setTop('alertContainer', topPosition );
	Element.show('alertContainer');	
	//alertBox.setFocusOnBox();
	Event.observe(window, 'scroll', moveAlertScreen );
	
}


