// event observe OnLoad
Event.observe(window, 'load', page_loaded, false);  

// function page load
function page_loaded(evt) {
	if( $('lightbox') ) { Event.observe('lightbox', 'click', item_clicked, false); }
	if( $('submitContactMail') ) { Event.observe('submitContactMail', 'click', contactUsSubmit, false); }
	if( $('advSearch') ) { Event.observe('advSearch', 'click', item_clicked, false); }
}

// contact us form OnSubmit // ++++++++++++++++ // start
function contactUsSubmit() {
	// check mail
	var i_length = $('contactus').length ;

	//
	for( var i=0; i<i_length; i ++ ) {
		if ( $('contactus').elements[i].value == '' && ( $('contactus').elements[i].name=='contactname' || $('contactus').elements[i].name=='contactemail' || $('contactus').elements[i].name=='contactmessage') ) {
			var tmpID = $('contactus').elements[i].id ;
			alert( str_pleasefill + $( 'L_'+ tmpID ).innerHTML );  //$('contactus').elements[i].name
			$('contactus').elements[i].focus() ;
			return false ;
		}
	}

	// process mail
	showLightBox() ;

	// collect data
	var str_arg = '';
	var obj_form = eval('document.contactus') ;
	var i_length = obj_form.length ;
		for( var i=0; i<i_length; i ++ ) {
			if( str_arg != '' ) { str_arg += '&'; }
			str_arg += obj_form[i].id +'='+ encodeURI(obj_form[i].value) ;
		}

	str_arg += '&langsel='+ str_lang  ;

	//
	var myReturn = f_ajax( 'http_mailContact.php', str_arg , 'text' );

	// clear data
	obj_form.reset() ;

	// set href
	location.href = '#lightBoxContent';

}
// contact us form OnSubmit // ++++++++++++++++ // end

// all click // +++++++++++++++++ // start
	function item_clicked(evt){
		var child = Event.element(evt);

		if( child.id == 'closebutton' || child.id == 'lightbox_closebutton' || child.id == 'img_lightbox_closebutton'  ) {
			hideLightBox() ;
			// set href
			location.href = '#top';
		/*
		} else if( child.id.substr(0,4) == 'faq_' ) {
			showLightBox() ;
			//
			var myID = child.id.substring( child.id.indexOf( '_' ) +1, child.id.length ) ;
			var myReturn = f_ajax( 'http_faq.php', 'id='+ myID +'&langsel='+ str_lang , 'text' );
		} else if( child.id.substr(0,5) == 'news_' ) {
			showLightBox() ;
			//
			var myID = child.id.substring( child.id.indexOf( '_' ) +1, child.id.lastIndexOf( '_' ) ) ;
			var myReturn = f_ajax( 'http_newsDetail.php', 'id='+ myID +'&langsel='+ str_lang , 'text' );
		} else if( child.id.substr(0,4) == 'mas_' ) {
			showLightBox() ;
			//
			var projID = child.id.substring( child.id.indexOf( '_' ) +1, child.id.lastIndexOf( '_' ) ) ;
			var myID= child.id.substring( child.id.lastIndexOf( '_' ) +1, child.id.length ) ;
			var myReturn = f_ajax( 'http_layoutDetail.php', 'id='+ myID +'&projid='+ projID +'&langsel='+ str_lang , 'xml' );
		*/
		} else if( child.id.substr(0, 4) == 'faq_' ) {
			// get id 
			var faqID = child.id.substr( 4, child.id.length );
			if( $( 'faq_ans_'+ faqID ).style.visibility == 'visible' ) {
				$( 'faq_ans_'+ faqID ).style.visibility = 'hidden' ;
				$( 'faq_ans_'+ faqID ).style.display = 'none';
			} else {
				$( 'faq_ans_'+ faqID ).style.visibility = 'visible' ;
				$( 'faq_ans_'+ faqID ).style.display = 'block';
			}
		} else if( child.id.type = 'a' ) {
			return ;
		} else if( child.id == 'login' ) {
		} else if( child.id == 'searchSwitch' ) {
		}

		Event.stop(evt); //avoid another call related to 'parent_node' itself
	}
// all click // +++++++++++++++++ // end

// AJAX // ++++++++++++++++++++++++++ // start
	function f_ajax( http_file, str_arg, returnType ) {
		// returnType : responseXML || responseText
		var myAjax = new Ajax.Request( http_file, { 
			method:'post' ,
			parameters: str_arg ,
			onSuccess: function(transport){
				if( returnType == 'text' ) {
					 var response = transport.responseText || "no response text";
					 $('lightBoxContent').innerHTML = response ;
				} else if( returnType == 'xml' ) {
					 var response = transport.responseXML || "no response XML";
					 // check output
					 switch( http_file ) {
						 case 'http_layoutDetail.php' :
							 var myStatus = response.getElementsByTagName('unitStatus') ;
							var myDisplay = response.getElementsByTagName('displayText') ;
							var mySQL = response.getElementsByTagName('sql') ;
							$('lightBoxContent').innerHTML = myDisplay[0].childNodes[0].nodeValue ;
							// $('lightBoxContent').innerHTML += '<br />'+ mySQL[0].childNodes[0].nodeValue ;
							// className
							// if( myStatus[0] > 0 ) { $('lightbox').setAttribute('class', 'sold'); } else { $('lightbox').setAttribute('class', ''); }
							//if( myStatus[0] > 0 ) { $('lightbox').className = 'sold'; } else { $('lightbox').className = '' ; }
						 break ;
					 }
				}
			},
			onFailure: function(){ alert('Something went wrong...') }
		});
	}
// AJAX // ++++++++++++++++++++++++++ // end

// open & close lightBox // ++++++++++++++++++++++ // start

		// showLightBox // ++++++++++++++++++++++++++++++++++ // start
		function showLightBox() {
			var obj_frame = $('lightboxbg') ;
			var obj_detail = $('lightbox') ;

			if( obj_frame ) {
				obj_frame.style.visibility = 'visible';
				obj_frame.style.display = 'block';

				obj_detail.style.visibility = 'visible';
				obj_detail.style.display = 'block';

				if( !document.all ) { 
					obj_frame.style.position = 'fixed'; 
				} else {
					obj_detail.style.position = 'absolute'; 
				}
				//
				////
				bod = document.getElementsByTagName('body')[0];
				bod.style.height = '100%' ;
				bod.style.overflow = 'hidden' ;
		  
				htm = document.getElementsByTagName('html')[0];
				htm.style.height = '100%' ;
				htm.style.overflow = 'hidden' ; 
				////
			}
		}
		// showLightBox // ++++++++++++++++++++++++++++++++++ // end


		// hideLightBox // ++++++++++++++++++++++++++++++++++ // start
		function hideLightBox() {
		
			var obj_frame = $('lightboxbg') ;
			obj_frame.style.visibility = 'hidden';
			obj_frame.style.display = 'none';

			var obj_detail = $('lightbox') ;
			obj_detail.style.visibility = 'hidden';
			obj_detail.style.display = 'none';

			////
			bod = document.getElementsByTagName('body')[0];
			bod.style.height = '100%' ;
			bod.style.overflow = 'auto' ;
		  
			htm = document.getElementsByTagName('html')[0];
			htm.style.height = '100%' ;
			htm.style.overflow = 'auto' ; 
			////
			$('lightBoxContent').innerHTML = 'Processing ...';
			$('lightbox').setAttribute('class', '');
		}
		// hideLightBox // ++++++++++++++++++++++++++++++++++ // end


// open & close lightBox // ++++++++++++++++++++++ // end