$(function(){
 	var toggle = function(direction, display) {
	    return function() {
	      var self = this;
	      var ul = $(".sub", this);
	      if( ul.css("display") == display && !self["block" + direction] ) {
	        self["block" + direction] = true;
	        ul["slide" + direction](200, function() {
	          self["block" + direction] = false;
	        });
	      }
	    };
	  }
	  $("#nav li").hover(toggle("Down", "none"), toggle("Up", "block"));
	  $("#nav li .sub").hide();

	/*$("#nav > li .sub").hide(); 
	$("#nav > li").hover(
        function () {
			$(this).children(".sub").slideDown(200);
			
        },function(){
			$(this).children(".sub").slideUp(200);
	});//hover*/

	  $(".mainimg, #top, #footer, .maintext, .textoff").bind("contextmenu", function(e) {
	      //e.preventDefault();
		  return false;
	  });
	 
	 
	   /*1. $(document).bind("contextmenu",function(e){  
		   2.     //you can enter your code here, e.g a menu list  
		   3.           
		   4.     //cancel the default context menu  
		   5.     return false;  
		   6. });
		*/
	  
	$('.mainimg div').cycle({ 
	    fx:     'fade', 
	    speed:  'slow', 
	    timeout: 5000, 
	    next:   '.next', 
	    prev:   '.back' 
	});
	/*$('.homenews ul').cycle({ 
	    fx:     'scrollUp', 
	    speed:  'slow', 
	    timeout: 5000, 
	    continuous:	   3
	});*/
	if($(".newsticker-jcarousellite li").length > 2){
		$(".newsticker-jcarousellite").jCarouselLite({
			vertical: true,
			hoverPause:true,
			visible: 2,
			auto: 2000,
			speed: 2000
		});
	}
	
	$('.newsgallerythumb .img').cycle({ 
	    fx:     'scrollLeft', 
	    speed:  'slow', 
	    timeout: 0, 
	    pager:  '.newselimg' 
	});
	
	$(".newsgallerythumb li").click(function(){
		var img = $(this).find("a").attr('rel');
		
		$(".imgfull img").attr('src','/images/ajax-loader.gif').hide();
		$(".imgfull img").load(function () {
			$(this).hide();
            $(this).fadeIn();
        }).error(function () {
            // notify the user that the image could not be loaded
        }).attr('src', img);
		//$(".imgfullfix a").attr("href",img);
		//$(".imgfull img").fadeTo(200, 0,function(){$(this).attr('src',img);}).fadeTo("slow", 1);
		//alert('dd');
	});
	
	$(".imgshop, .thumbimg li a").fancybox();
	
	$(".menueffect, .mPretzel li").hover(
		function(){
			$(this).animate({"top": "+=15px"},"normal");
		},
		function(){
			$(this).animate({"top": "-=15px"},"normal");
		}
	);
	$(".gamelist li a, .gamelistleft a").click(function(){
		window.open($(this).attr("href"),'',$(this).attr("rel"));
		return false;
	});
	
	$(".homeabout li a img, .homefun a img").hover(
			function() {
			$(this).animate({"opacity": ".7"}, "normal");
			},
			function() {
			$(this).animate({"opacity": "1"}, "normal");
	});
	
	//contact form
	$("#contactform").submit(function(){
		var txtHeadtext = $("#txtHeadtext").val();
		var txtDetail = $("#txtDetail").val();
		var txtName = $("#txtName").val();
		var txtAddress = $("#txtAddress").val();
		var txtEmail = $("#txtEmail").val();
		var txtTel = $("#txtTel").val();
		var chkimgkey = $("#chkimgkey").val();
		
		if(jQuery.trim(txtHeadtext) == ''){
			alert('กรุณากรอกชื่อเรื่อง');
			return false;
		}
		if(jQuery.trim(txtDetail) == ''){
			alert('กรุณากรอกข้อความ');
			return false;
		}
		if(jQuery.trim(txtName) == ''){
			alert('กรุณากรอกชื่อ - นามสกุล');
			return false;
		}
		if(jQuery.trim(txtEmail) == ''){
			alert('กรุณากรอกอีเมล');
			return false;
		}else{
			if(isValidEmail(txtEmail)){
				
			}else{
				alert('กรุณากรอกอีเมลล์ให้ถูกรูปแบบ');
				return false;
			}
		}
		if(Query.trim(chkimgkey) == ''){
			alert('กรุณากรอก code');
			return false;
		}
		return true;
	});
	
	$("#formtofriend").submit(function(){
		var txtNameFriend = $("#txtNameFriend").val();
		var txtEmailFriend = $("#txtEmailFriend").val();
		var txtNameUser = $("#txtNameUser").val();
		var txtEmailUser = $("#txtEmailUser").val();
		var txtMsg = $("#txtMsg").val();
		if(jQuery.trim(txtNameFriend) == ''){
			alert('กรุณากรอกชื่อผู้ส่ง');
			return false;
		}
		if(jQuery.trim(txtEmailFriend) == ''){
			alert('กรุณากรอกอีเมล์ผู้ส่ง');
			return false;
		}
		if(jQuery.trim(txtNameUser) == ''){
			alert('กรุณากรอกชื่อผู้รับ');
			return false;
		}
		if(jQuery.trim(txtEmailUser) == ''){
			alert('กรุณากรอกอีเมล์ผู้รับ ');
			return false;
		}

		return true;
	});
	
});

function isValidEmail(str) {
	   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

