// JavaScript Document
$(document).ready(function(){ 

$('.popup').click(function(e){	
	var width = 700;
	var height = 520;
	var left = (screen.width - width)/2;
	var top = (screen.height - height)/2;
	var params = 'width='+width+', height='+height;
	       params += ', top='+top+', left='+left;
		   params += ', directories=no';
		   params += ', location=no';
		   params += ', menubar=no';
		   params += ', resizable=no';
		   params += ', scrollbars=no';
		   params += ', status=no';
		   params += ', toolbar=no';
		   newwindow=window.open($(this).attr('href'),'Chat', params);	
		   if (window.focus) {newwindow.focus()}	
				return false;
		   });
	


  //Move Image
	$("#featured").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 5000, true);   
		$("#featured").hover(   
			function() {   
				$("#featured").tabs("rotate",0,true);   
			},   
			function() {   
				$("#featured").tabs("rotate",5000,true);   
			}   
		); 
		
	//select all the a tag with name equal to modal
	$('a[name=modal]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
		
		//Get the A tag
		var id = $(this).attr('href');
	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		$('#mask').fadeIn(1000);	
		$('#mask').fadeTo("slow",0.8);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
              
		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);
	
		//transition effect
		$(id).fadeIn(2000); 
	
	});
	
	//if close button is clicked
	$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		
		$('#mask').hide();
		$('.window').hide();
	});		
	
	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});					
		
	
	$('#send').click(function(e){
		//statements to validate the form
		var filter =/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		var err_mail='อีเมล์ของท่านผิดรูปแบบ...กรุณาตรวจสอบด้วยครับ!';
		var err_subject='กรุณากรอกเรื่องที่ต้องการแจ้งด้วยครับ!';
		var err_name='กรุณากรอกชื่อของท่านด้วยครับ!';
		var err_feedback='กรุณาบันทึกข้อเสนอแนะของท่านด้วยครับ!';
		
		if (!filter.test(document.cform.email.value)) {
			document.cform.email.value=err_mail;
		} 
		
		if (document.cform.subject.value == "") {
			document.cform.subject.value=err_subject;
		}

		if (document.cform.fullname.value == "") {
			document.cform.fullname.value=err_name;
		}
		
		if (document.cform.feedback.value == "") {
			document.cform.feedback.value=err_feedback;
		}
		
		if ((document.cform.fullname.value == "") || (document.cform.subject.value == "") || (!filter.test(document.cform.email.value)) || (document.cform.feedback.value == "")){
			return false;
		} 

		if ((document.cform.fullname.value != "") && (document.cform.subject.value != "") && (filter.test(document.cform.email.value)) && (document.cform.feedback.value != "")) {
			//hide the form
			
			//$('.contactForm').hide();

			//show the loading bar

			$('.loader').append($('.bar'));
			$('.bar').css({display:'block'});
			//send the ajax request
			$.post('postfree_sendemail.php',{name:$('#fullname').val(),
							  email:$('#email').val(),
							  subject:$('#subject').val(),
							  message:$('#feedback').val()},

			//return the data
			function(data){
			  //hide the graphic
			  document.cform.feedback.value = "";
			  document.cform.email.value="";
			  document.cform.fullname.value="";
			  document.cform.subject.value="";
     		  $('.bar').css({display:'none'});
              $('.loader').append(data);
			  alert (data);
			  $('#mask').hide();
			  $('.window').hide();
			  //$('.loader').append(data);
			});

			//waits 2000, then closes the form and fades out
			//setTimeout('$("#backgroundPopup").fadeOut("slow"); $("#contactForm").slideUp("slow")', 2000);

			//stay on the page
			return false;
		}
  });


//Resize Image	
	$( ".resizeimg" ).aeImageResize({ height: 50, width: 80 });

	$( ".imgresize" ).aeImageResize({ height: 100, width: 70 });
		

	$("div#high_light").textAnimation({
    	mode:"highlight"
	});

});
