$(document).ready(function(){
	
	$("li.Example").hide();
	$("#ShowExample").click(function(){
		if($(this).text()=="ตัวอย่าง"){
			$(this).text("ซ่อน");
		}else{
			$(this).text("ตัวอย่าง");
		}
		$("li.Example").slideToggle("slow");
	});

	$('a[href=#top]').click(function(){
        $('html, body').animate({scrollTop:0}, 'slow');
        return false;
    });

	$("a[rel=showimage]").fancybox({
		'transitionIn' : 'none',
		'transitionOut' : 'none',
		'titlePosition' : 'over',
		'titleFormat' : function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Image ' +  (currentIndex + 1) + ' / ' + currentArray.length + ' ' + title + '</span>';
		}
	});

	$("p.searchoption").hide();
	$("#ShowMoreOption").click(function(){
		if($(this).text()=="+เพิ่มเติม"){
			$(this).text("-ซ่อน");
		}else{
			$(this).text("+เพิ่มเติม");
		}
		$("p.searchoption").slideToggle("slow");
	});

	$('select#province_id').change(function(){
		$.ajax({
		  url: root+"lib/amphur.php",
		  data: "province_id="+$('select#province_id').val(),
		  success: function(result){
			$('span#show_amphur').html(result);
		  }
		});
	});
	$('select#province').change(function(){
		$.ajax({
		  url: root+"lib/amphur2.php",
		  data: "province="+$('select#province').val(),
		  success: function(result){
			$('span#show_amphur').html(result);
		  }
		});
	});
	$('select#category_id').change(function(){
		$.ajax({
		  url: root+"lib/subcategory.php",
		  data: "category_id="+$('select#category_id').val(),
		  success: function(result){
			$('span#show_subcategory').html(result);
		  }
		});
	});
	$('select#category').change(function(){
		$.ajax({
		  url: root+"lib/subcategory2.php",
		  data: "category="+$('select#category').val(),
		  success: function(result){
			$('span#show_subcategory2').html(result);
		  }
		});
	});

	$('a#buttonSearch').click(function(){
		if($("select#type").val()=="" &&
			$("input#price").val()=="" && 
  			 $("select#province").val()=="" &&
			 $("select#amphur").val()==""){
		     alert("กรุณาเลือกอย่างน้อย 1 เงื่อนไข");
			$("select#type").focus();
		}else{
			$('form#formsearch').attr("action",root+"search.php");
			$('form#formsearch').submit();
		}
	});

	$('a#buttonSearchGoogleMap').click(function(){
		if($("select#type").val()=="" &&
			$("input#price").val()=="" && 
  			 $("select#province").val()=="" &&
			 $("select#amphur").val()==""){
		     alert("กรุณาเลือกอย่างน้อย 1 เงื่อนไข");
			$("select#type").focus();
		}else{
			$('form#formsearch').attr("action",root+"googlemap.php");
			$('form#formsearch').submit();
		}
	});
	
	// Search
	//$("form#formsearch").hide();
	$(".show_search").click(function(){
		if($(this).text()=="แสดง"){
			$(this).text("ซ่อน");
		}else{
			$(this).text("แสดง");
		}
		$("form#formsearch").slideToggle("slow");
	});

	// Calculator
	$("form#show_calculator").hide();
	$("#calculator").click(function(){
		if($(this).text()=="คำนวนราคา่"){
			$(this).text("ซ่อน");
		}else{
			$(this).text("คำนวนราคา่");
		}
		$("form#show_calculator").slideToggle("slow");
	});

	//ทุกจังหวัด
	$("li.hideprovince").hide();
 	$("#moreprovinceall").click(function(){
		if($(this).text()=="ดูทุกจังหวัด"){
			$(this).text("ซ่อนจังหวัด");
		}else{
			$(this).text("ดูทุกจังหวัด");
		}
		$("li.hideprovince").slideToggle("slow");
	});
	// show post
	$(".show_block").click(function(){
 		$("ul#showpost").removeClass("show_list");
		$("ul#showpost").addClass("show_block");
 		$(".show_block").addClass("selected");
		$(".show_list").removeClass("selected");
	});
	$("ul#showpost li:even").addClass("hilight");
 	$(".show_list").click(function(){
		$("ul#showpost li:even").addClass("hilight");
 		$("ul#showpost").addClass("show_list");
		$("ul#showpost").removeClass("show_block");
 		$(".show_block").removeClass("selected");
		$(".show_list").addClass("selected");
	});
	
	// showviewmore
	$("li.viewmore").hide();
	$("span.load").hide();
	$(".showviewmore").click(function(){
		if($(".showviewmore label").text()=="ดูเพิ่มเติม"){
			$("span.load").show();
			$("#load_list_index").load("load_list_index.php");
			$(".showviewmore a label").text("ยกเลิก");
			$("span.load").hide();
		}else{
			$(".showviewmore a label").text("ดูเพิ่มเติม");
 
		}
		$("li.viewmore").slideToggle("slow");
		$(".showviewmore img").attr("src","img/pageicon_up.png");
	});

	// register
	$(".register").fancybox({
		'hideOnOverlayClick' : false,
		'titleShow' : false,
		'type' : 'iframe',
		'width' : 600,
		'height' : 640,
		'href' : root+'form_register.php'
	});
	// profile
	$(".profile").fancybox({
		'hideOnOverlayClick' : false,
		'titleShow' : false,
		'type' : 'iframe',
		'width' : 600,
		'height' : 470,
		'href' : root+'form_profile.php'
	});
	// changepass
	$(".changepass").fancybox({
		'hideOnOverlayClick' : false,
		'titleShow' : false,
		'type' : 'iframe',
		'width' : 600,
		'height' : 270,
		'href' : root+'form_changepass.php'
	});
	// contact
	$(".content_ads, .calculator").fancybox({
		'hideOnOverlayClick' : false,
		'titleShow' : false,
		'type' : 'iframe',
		'width' : 500,
		'height' : 330,
	});
	
	// contact
	$(".contact").fancybox({
		'hideOnOverlayClick' : false,
		'titleShow' : false,
		'type' : 'iframe',
		'width' : 500,
		'height' : 330,
		'href' : root+'form_contact.php'
	});

	// survey
	$(".survey").fancybox({
		'hideOnOverlayClick' : false,
		'titleShow' : false,
		'type' : 'iframe',
		'width' : 500,
		'height' : 350,
		'href' : root+'form_survey.php'
	});
	// content_ads
	$(".content_ads").fancybox({
		'hideOnOverlayClick' : false,
		'titleShow' : false,
		'type' : 'iframe',
		'width' : 530,
		'height' : 580,
	});
	// login
	$(".login").fancybox({
		'hideOnOverlayClick' : false,
		'titleShow' : false,
		'type' : 'iframe',
		'width' : 480,
		'height' : 230,
		'href' : root+'form_login.php'
	});
	
	// forgot
	$(".forgot").fancybox({
		'hideOnOverlayClick' : false,
		'titleShow' : false,
		'type' : 'iframe',
		'width' : 480,
		'height' : 260,
		'href' : root+'form_forgot.php'
	});
	
	// uploadphoto
	$(".uploadphoto").fancybox({
		'hideOnOverlayClick' : false,
		'titleShow' : false,
		'type' : 'iframe',
		'width' : 530,
		'height' : 430,
	});
	// map
	$(".gmap, .map").fancybox({
		'hideOnOverlayClick' : false,
		'titleShow' : false,
		'type' : 'iframe',
		'width' : 590,
		'height' : 430,
	});
	// gmap update
	$(".gmapupdate").fancybox({
		'hideOnOverlayClick' : false,
		'titleShow' : false,
		'type' : 'iframe',
		'width' : 560,
		'height' : 540,
		'href' : root+'form_gmap.php'
	});
	// map upload
	$(".mapupdate").fancybox({
		'hideOnOverlayClick' : false,
		'titleShow' : false,
		'type' : 'iframe',
		'width' : 560,
		'height' : 600,
		'href' : root+'form_mapimg.php'
	});
	// photo
	$(".photo").fancybox({
		'hideOnOverlayClick' : false,
		'titleShow' : false,
		'type' : 'iframe',
		'width' : 815,
		'height' : 485,
	});
	
	// avatar
	$(".avatar").fancybox({
		'hideOnOverlayClick' : false,
		'titleShow' : false,
		'type' : 'iframe',
		'width' : 530,
		'height' : 280,
		'href' : root+'form_avatar.php'
	});
	// logo
	/*
	$(".logo").fancybox({
		'hideOnOverlayClick' : false,
		'titleShow' : false,
		'type' : 'iframe',
		'width' : 610,
		'height' : 420,
		'href' : root+'form_logo.php'
	});
	*/
	// detail
	//$("div.thumb ul").hide();
	$(".detail_photo").click(function(){
		if($(this).text()=="แสดง"){
			$(this).text("ซ่อน");
			$(this).attr("title","ซ่อนกรอบนี้");
		}else{
			$(this).text("แสดง");
			$(this).attr("title","แสดงกรอบนี้");
		}
		$("div.thumb ul").slideToggle("slow");
	});

	//$("div.info p").hide();
	$(".detail_info").click(function(){
		if($(this).text()=="แสดง"){
			$(this).text("ซ่อน");
			$(this).attr("title","ซ่อนกรอบนี้");
		}else{
			$(this).text("แสดง");
			$(this).attr("title","แสดงกรอบนี้");
		}
		$("div.info p").slideToggle("slow");
	});

	//$("div.prices p").hide();
	$(".detail_prices").click(function(){
		if($(this).text()=="แสดง"){
			$(this).text("ซ่อน");
			$(this).attr("title","ซ่อนกรอบนี้");
		}else{
			$(this).text("แสดง");
			$(this).attr("title","แสดงกรอบนี้");
		}
		$("div.prices p").slideToggle("slow");
	});

	//$("div.option p").hide();
	$(".detail_option").click(function(){
		if($(this).text()=="แสดง"){
			$(this).text("ซ่อน");
			$(this).attr("title","ซ่อนกรอบนี้");
		}else{
			$(this).text("แสดง");
			$(this).attr("title","แสดงกรอบนี้");
		}
		$("div.option p").slideToggle("slow");
	});
	$(".detail_nearby").click(function(){
		if($(this).text()=="แสดง"){
			$(this).text("ซ่อน");
			$(this).attr("title","ซ่อนกรอบนี้");
		}else{
			$(this).text("แสดง");
			$(this).attr("title","แสดงกรอบนี้");
		}
		$("div.nearby p").slideToggle("slow");
	});
	//$("div.update p").hide();
	$(".detail_update").click(function(){
		if($(this).text()=="แสดง"){
			$(this).text("ซ่อน");
			$(this).attr("title","ซ่อนกรอบนี้");
		}else{
			$(this).text("แสดง");
			$(this).attr("title","แสดงกรอบนี้");
		}
		$("div.update p").slideToggle("slow");
	});

	//$("div.user p").hide();
	$(".detail_user").click(function(){
		if($(this).text()=="แสดง"){
			$(this).text("ซ่อน");
			$(this).attr("title","ซ่อนกรอบนี้");
		}else{
			$(this).text("แสดง");
			$(this).attr("title","แสดงกรอบนี้");
		}
		$("div.user p").slideToggle("slow");
	});

	//$("div.tags p").hide();
	$(".detail_tags").click(function(){
		if($(this).text()=="แสดง"){
			$(this).text("ซ่อน");
			$(this).attr("title","ซ่อนกรอบนี้");
		}else{
			$(this).text("แสดง");
			$(this).attr("title","แสดงกรอบนี้");
		}
		$("div.tags p").slideToggle("slow");
	});

	$(".ShowRegister").click(function(){
		parent.$.fancybox({
			'hideOnOverlayClick' : false,
			'titleShow' : false,
			'type' : 'iframe',
			'width' : 600,
			'height' : 640,
			'href' : root+'form_register.php?post='+$("input#post").val()
		});
	});
	
	$(".ShowLogin").click(function(){
		parent.$.fancybox({
			'hideOnOverlayClick' : false,
			'titleShow' : false,
			'type' : 'iframe',
			'width' : 480,
			'height' : 230,
			'href' : root+'form_login.php?post='+$("input#post").val()
		});
	});

	// post
	$(".post").click(function(){
		$.ajax({
		  url: root+"lib/checklogin.php",
		  type: 'POST',
		  data: $('form#formname').serialize(),
		  success: function(result){
			  if(result==1){
				parent.location =  root+'user/post';
			  }else{
				parent.location =  root+'user/login';
			  }
		  }
		});
	});

	// Login Check
	$("span.check").hide();
	$('input#email').keyup(function(){
 		if($("input#email").val()){
			$("#showemail").hide();
		}
	});
	$('input#password').keyup(function(){
 		if($("input#password").val()){
			$("#showpassword").hide();
		}
	});
	$("#formname p.status").hide();
	$('a#buttonLogin').click(function(){
		if($("input#email").val()==""){
 			$("#showemail").addClass("false").text("กรุณาใส่อีเมล์ผู้ใช้").show();
			$("input#email").focus();
			return false;
		}else{
			$("#showemail").hide();
		}
		if(!isValidEmail($("input#email").val())){
 			$("#showemail").addClass("false").text("กรุณาใส่อีเมล์ให้ถูกต้อง").show();
			$("input#email").focus();
			return false;
		}else{
			$("#showemail").hide();
		}
		if($("input#password").val()==""){
			$("#showpassword").addClass("false").text("กรุณาใส่รหัสผ่าน").show();
			$("input#password").focus();
			return false;
		}else{
			$("#showpassword").hide();
		}
		if($("input#email").val() && $("input#password").val()){
			$.ajax({
			  url: root+"lib/login.php",
			  type: 'POST',
			  data: $('form#formname').serialize(),
			  success: function(result){
				if(result>0){
					window.location=root+"register/profile";
					return false;
				}
				else if(result==-1){
 					$("#formname p.status").show();
					$("#showsuccess").addClass("fail").text("ไม่สามารถเข้าระบบได้ กรุณาตรวจสอบข้อมูลอีกครั้ง").show();
					$("input#username").focus();
					return false;
				}
				else{
					alert("ระบบขัดข้อง กรุณาแจ้งผู้ดูแลระบบ \n"+result);
				}
			  }
			});
		}
	});
	// more subcategory
	$("li.showmoresubcategory").hide();
 	$("#moresubcategory").click(function(){
		if($(this).text()=="ดูทุกรุ่น"){
			$(this).text("ซ่อน");
		}else{
			$(this).text("ดูทุกรุ่น");
		}
		$("li.showmoresubcategory").slideToggle("slow");
	});
	
	$("#buttonSearchOption").click(function(){
		var self = window.location;
		var url = '';
			url += "/gear-"+$("select#gear").val();
			url += "/color-"+$("select#color").val();
			url += "/province-"+$("select#province").val();
			url += "/fuel-"+$("select#fuel").val();
			if($("input#mapimg").attr("checked")==true){
			url += "/mapimg-1";
			}
			if($("input#mapimg").attr("checked")==false){
			url += "/mapimg-0";
			}
		window.location = $("input#uri").val()+url;
		// gear=1/color=1/province=1/fuel=1/mapimg=1/
	});

	$("#basketItemsWrap li:first").hide();

	$(".compare").click(function() {
		var productIDValSplitter 	= (this.id).split("_");
		var productIDVal 			= productIDValSplitter[1];
		
		var productX 		= $("#productImageWrapID_" + productIDVal).offset().left;
		var productY 		= $("#productImageWrapID_" + productIDVal).offset().top;
		
		if( $("#productID_" + productIDVal).length > 0){
			var basketX 		= $("#productID_" + productIDVal).offset().left;
			var basketY 		= $("#productID_" + productIDVal).offset().top;			
		} else {
			var basketX 		= $("#basketTitleWrap").offset().left;
			var basketY 		= $("#basketTitleWrap").offset().top;
		}
		
		var gotoX 			= basketX - productX;
		var gotoY 			= basketY - productY;
		
		var newImageWidth 	= $("#productImageWrapID_" + productIDVal).width() / 3;
		var newImageHeight	= $("#productImageWrapID_" + productIDVal).height() / 3;
		
		$("#productImageWrapID_" + productIDVal + " img")
		.clone()
		.prependTo("#productImageWrapID_" + productIDVal)
		.css({'position' : 'absolute'})
		.animate({opacity: 0.4}, 100 )
		.animate({opacity: 0.1, marginLeft: gotoX, marginTop: gotoY, width: newImageWidth, height: newImageHeight}, 1200, function() {	
			//$("#notificationsLoader").html('<img src="'+root+'img/ajax_loading.gif" style="border:0px;">');
			$.ajax({  
				type: "POST",  
				url: root+"lib/addtobasket.php",  
				data: { productID: productIDVal, action: "addToBasket"},  
				success: function(theResponse) {			
					if( $("#productID_" + productIDVal).length > 0){
						$("#productID_" + productIDVal).animate({ opacity: 0 }, 500);
						$("#productID_" + productIDVal).before(theResponse).remove();
						$("#productID_" + productIDVal).animate({ opacity: 0 }, 500);
						$("#productID_" + productIDVal).animate({ opacity: 1 }, 500);
						$("#notificationsLoader").empty();
					} else {
						$("#basketItemsWrap li:first").before(theResponse);
						$("#basketItemsWrap li:first").hide();
						$("#basketItemsWrap li:first").show("slow");  
						$("#notificationsLoader").empty();			
					}
					$("#Product_"+productIDVal).contents().unwrap();
				}  
			});  
		});
	});
	
	$("#basketItemsWrap li img").live("click", function(event) { 
		var productIDValSplitter 	= (this.id).split("_");
		var productIDVal 			= productIDValSplitter[1];	
		//$("#notificationsLoader").html('<img src="'+root+'img/ajax_loading.gif" style="border:0px;">');
		$.ajax({  
			type: "POST",  
			url: root+"lib/addtobasket.php",  
			data: { productID: productIDVal, action: "deleteFromBasket"},  
			success: function(theResponse) {
				$("#productID_" + productIDVal).hide("slow",  function() {$(this).remove();});
				$("#notificationsLoader").empty();
			}  
		});  
	});

	$("#checkCompare").click(function(){
		$.ajax({
		  url: root+"lib/checkbasket.php",
		  success: function(result){
 			if(result>0 && result<6){
				window.location = root+'home/compare';
			}else if(result>5){
				alert("เปรียบเทียบได้ครั้งละ 5 หลัง เท่านั้นค่ะ");
				return false;
			}else{
				alert("ยังไม่ได้เลือกรถ");
				return false;
			}
		  }
		});
	});

	$("a#buttonEmailAlert").click(function(){
		if($("input#youemail").val()==""){
 			$("#showemailalert").addClass("false").text("กรุณาใส่ชื่ออีเมล์").show();
			$("input#youemail").focus();
			return false;
		}else{
			$("#showemailalert").hide();
		}
		if(!isValidEmail($("input#youemail").val())){
			$("#showemailalert").addClass("false").text("กรุณาใส่อีเมล์ให้ถูกต้อง").show();
 			$("input#youemail").focus();
			return false;
		}else{
			$("#showemail").hide();
		}
		$("#showemailalert").addClass("true").text("กำลังบันทึก....").show();
		$.ajax({
		  type: "POST",
		  url: root+"lib/emailalert.php",
		  data: $('form#formemailalert').serialize(),
		  success: function(result){
  			if(result==-1){
 				$("#showemailalert").removeClass("true").addClass("false").text("อีเมล์นี้เคยสมัครไว้แล้ว").show();
			}else if(result==1){
 				$("#showemailalert").addClass("true").text("บันทึกข้อมูลเีัรียบร้อย").show();
				$("input#youemail").val('');
			}else{
 				$("#showemailalert").removeClass("true").addClass("false").text("ไม่สามารถบันทึกข้อมูลได้").show();
				return false;
			}
		  }
		});
	});

	$("#ClearCache").click(function(){
		if(confirm("Confirm? ")){
			$("#ClearCache").html("กำลังลบข้อมูล.....");
			$.ajax({
			  url: root+"lib/clearcache.php",
			  success: function(result){
				if(result==0){
					alert("ลบข้อมูลเรียบร้อยแล้ว");
					$("#ClearCache").hide();
					$("#showitem").html(0);
					return false;
				}
				else{
					alert("ไม่สามารถลบข้อมูลได้");
					$("#ClearCache").html('<span class="trash icon"></span> Clear cache now');
					return false;
				}
			  }
			});	
		}
	});	

//end ready
});

// contact_user
function ContactUser(id,toemail){
	$.fancybox({
		'hideOnOverlayClick' : false,
		'titleShow' : false,
		'type' : 'iframe',
		'width' : 510,
		'height' : 365,
		'href' : root+'form_contact_user.php?id='+id+'&toemail='+toemail
	});
}

var Loading = '<div style="text-align:center;margin:100px;"><img src="'+root+'/img/ajax_loading1.gif"></div>';

function LoadMemberPost(id,sort){
	$("a.sort").removeClass("selected");
	$("a."+sort).addClass("selected");
	$("span#showPostList").html(Loading);
	$("span#showPostList").load(root+"/lib/post_list_member.php?id="+id+"&sort="+sort);
}


function LoadPostList(sort,param,page){
	$("a.sort").removeClass("selected");
	$("a."+sort).addClass("selected");
 	$("span#LoadPostList").html(Loading);
	$("span#LoadPostList").load(root+"/lib/post_list.php?orderby="+sort+param+"&page="+page);
}

function showphoto(id,file,title){
	$("p#showphoto_img img").attr("src",file);
	$("p#showphoto_desc").html(title);
}
function category(id){
	$("ul.subcategory_"+id).slideToggle("slow");
	$("#a_"+id).toggleClass("selected");
}
function subcategory(id){
	$("li.moresubcategory_"+id).slideToggle("slow");
	if($("a#more_"+id).text()=="ดูทุกหมวด"){
		$("a#more_"+id).text("ซ่อนหมวด");
	}else{
		$("a#more_"+id).text("ดูทุกหมวด");
	}
}

function Logout(){
	//if(confirm("ยืนยัน?")){
		$.ajax({
		  url: root+"lib/logout.php",
		  success: function(result){
				//alert("ออกจากระบบเรียบร้อยแล้ว");
				//parent.$("div#formlogin").show();
				//parent.$("div#usermenu").hide();
				window.location=root;
				return false;
		  }
		});
	//}
}

function Sendtofriend(url,photo,price){
	// sendtofriend
	var title = $("title").text();
	$.fancybox({
		'hideOnOverlayClick' : false,
		'titleShow' : false,
		'type' : 'iframe',
		'width' : 600,
		'height' : 350,
		'href' : root+'form_sendtofriend.php?title='+title+'&url='+url+'&photo='+photo+'&price='+price
	});
	//$("title").text()
}
/*
function LoadGallery(id,title,filename){
 	$('span#showgallery').html('<p style="margin-top:200px;"><img src="'+root+'img/ajax_loading1.gif" style="border:0px;"></p>');
	$('span#showgallery').load(root+"lib/loadgallery.php?id="+id+"&title="+title+"&filename="+filename);
}
*/
function PhotoBig(id,title,filename){
	// photo1
	$.fancybox({
		'hideOnOverlayClick' : false,
		'titleShow' : false,
		'cyclic' : true,
 		'type' : 'iframe',
		'width' : 585,
		'height' : 450,
		'href' : root+'content_photo.php?id='+id+'&title='+title+'&filename='+filename
	});
}


// admin

function ItemDelete(mod,id){
	var a = "";
	if(mod=="member"){ a = "ประกาศทั้งหมดจะถูกลบไปด้วย";}
	if(confirm("Confirm? "+a)){
		$.ajax({
		  url: root+"lib/admin_delete.php",
		  data: "mod="+mod+"&id="+id,
		  success: function(result){
			if(result>0){
				//alert("ลบข้อมูลเรียบร้อยแล้ว");
				$('tr.row_'+id).hide();
				return false;
			}
			else{
				alert("ไม่สามารถลบข้อมูลได้");
				return false;
			}
		  }
		});	
	}
}
