$(function(){

	$(".productlist2 img, .productlist img").tooltip({ 
		effect: 'slide',
		 tip: '.popsample',
		 position: "top center",
		 onBeforeShow: function(event) { 
			/*var aa = this.getTrigger().find("img").attr("src"); 
			var url =  aa.replace("_promain", "");
			 $(".favpop img").attr("src",url);*/
			var chkimg = this.getTrigger().parents("a").attr("rel");
			if(chkimg !=''){
				$(".popsample img").attr('src',chkimg);
				return true;
			}else{
				return false;
			}
   		} 
		
	});

	$('.topimages').cycle({ 
		   fx: 'fade', 
	       speed:  'slow', 
	       timeout: 5000/*, 
	       pager:  '.homeselimg'*/
	});
	
	$('.imghead').cycle({ 
		   fx: 'fade', 
	       speed:  'slow', 
	       timeout: 4000
	});
	
	$('.thumbimg').cycle({ 
	    fx:     'scrollLeft', 
	    speed:  'slow', 
	    timeout: 0, 
	    next:   '.next', 
	    prev:   '.back' 
	});
	
	$(".imgsample img").animate({left:0},800);
	/*$(".quicknav a").click(function(){
		$(".quicknav a").removeClass("on");
		$(this).addClass("on");
		var num = $(this).attr("rel");
 te({left:-600},900);               ).animate({left:-1250},900);
		}
	});*/
	
	$(".tabnav li a").click(function(){
		$(".tabnav li a").removeClass("on");
		$(this).addClass("on");
		$(".productlist2").find(".samplepro").slideUp();
		$("#"+$(this).attr('rel')).slideDown();
	});
	
	$("#selectMachine").change(function(){
		$("#keysearch").submit();
	});
	
	
	$(".thumbimg img").click(function() {
		$(".thumbimg img").removeClass("on");
		$(this).addClass("on");
			
		var imgfullimg = $(this).parents("a").attr("href");
		var imgfullimgbig = $(this).parents("a").attr("rel");
	
       // var img = new Image();
		$(".imgprofull .imgfull").attr('src','/images/load.gif').hide();
			$(".imgprofull .imgfull").load(function () {
				$(this).hide();
	            //$('#loader').removeClass('loading').append(this);
	            $(this).fadeIn();
	        }).error(function () {
	            // notify the user that the image could not be loaded
	        }).attr('src', imgfullimg);
		$(".imgfullfix a").attr("href",imgfullimgbig);
		/*$(".imgprofull .imgfull").hide();
		$(".imgprofull .imgfull").stop().fadeTo(200, 0,function(){
			$(".imgprofull .imgfull").attr("src",imgfullimg).show();
			
			
		}).fadeTo("slow", 1);*/
        
		return false;

	});	
	
	$(".imgfullfix a").fancybox();
	$(".image-map a").fancybox({
		'imageScale':false,
		'centerOnScroll':false
	});

	$("#contactForm").submit(function(){
		var Company = $("input[name=Company]");
		var ContactPerson = $("input[name=ContactPerson]");
		var Email = $("input[name=E-mail]");
		var Telephone = $("input[name=Telephone]");
		var Howdid = $("select[name=Howdid]");
		var howcan = $("textarea[name=howcan]");
		
		if($('#hidelang').val()=='en'){
			var emptytxt = 'Please fill in';
			var erremail = 'Please check email format';
			var errselect = 'Please Select';
		}else{
			var emptytxt = 'กรุณากรอก';
			var erremail = 'อีเมลไม่ถูกต้อง';
			var errselect = 'กรุณาเลือก';	
		}
		
		$('#contactForm input').removeClass('error');
		if(jQuery.trim(Company.val()) == ''){
			alert(emptytxt+' '+$(Company).attr('title'));
			Company.addClass('error');
			return false;
		}
		if(jQuery.trim(ContactPerson.val()) == ''){
			alert(emptytxt+' '+$(ContactPerson).attr('title'));
			ContactPerson.addClass('error');
			return false;
		}
		if(jQuery.trim(Email.val()) == ''){
			alert(emptytxt+' '+$(Email).attr('title'));
			Email.addClass('error');
			return false;
		}else{
			if(isValidEmail(Email.val())){
				
			}else{
				alert(erremail);
				Email.addClass('error');
				return false;
			}
		}
		if(jQuery.trim(Telephone.val()) == ''){
			alert(emptytxt+' '+$(Telephone).attr('title'));
			Telephone.addClass('error');
			return false;
		}
		if(jQuery.trim(Howdid.val()) == ''){
			alert(errselect+' '+$(Howdid).attr('title'));
			Howdid.addClass('error');
			return false;
		}
		if(jQuery.trim(howcan.val()) == ''){
			alert(emptytxt+' '+$(howcan).attr('title'));
			howcan.addClass('error');
			return false;
		}
		return true;
	});

});
function isValidEmail(str) {
	   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}
