﻿var total_page=0;

//This example uses the javascript sdk to login before embedding the swf
	
	var APP_ID = "243803548981428";	
	var REDIRECT_URI = "http://apps.facebook.com/review-and-win/";
							
	var PERMS = ""; //comma separated list of extended permissions
	
	function init() {
		FB.init({appId:APP_ID, status: true, cookie: true, xfbml : true});
		//FB.getLoginStatus(handleLoginStatus);
		
	}
	$(init);

$(document).ready(function(){
	set_position();
	search_review("create_date");
	$("#top-10-table").load("top-10.php");
	$("#totel-review").load("total-review.php");
	//alert("สุดยอดรีวิวคือคุณ\n\n JAMNONG THUAIBUT");
	$("#winner-list").load("top-vote.php");
	$("#fade-bg").fadeTo(0,0.75);
	$("#fade-bg").show();
	$("#winner-cargo").show();
});

$(window).resize(function(){
	set_position();
});

function get_doc_size(){
	docHeight = $(window).height();
	docWidth = $(window).width();
}
function set_position(){
	get_doc_size();
	$("#submit-loading").css({"left": (docWidth/2) - 100 +"px"});
	$("#door-cargo-border").css({"left": (docWidth/2) - 490 +"px"});
	$("#tree-right-cargo").css({"left": (docWidth/2) + 337 +"px"});
	$("#tree-left-cargo").css({"left": (docWidth/2) - 680 +"px"});
	$("#tree-right-cargo").css({"width": (docWidth/2) - 337 +"px"});
	
	$("#btn-next-cargo").css({"left": (docWidth/2) + ((docWidth/2)-97) +"px"});
	
	$("#winner-cargo").css({"left": (docWidth/2) - 275 +"px"});
	$("#fade-bg").css({"width":docWidth +"px","height":docHeight +"px"});
}

function popup(num){
	stop();
	if(num == 0){
		$("#popup-cargo").animate({"top":"100px",opacity:0},"fast",function(){
			$("#popup-cargo").css('filter', 'none');
			$("#popup-cargo").hide();
		});
	}else{
		$("#popup-cargo").show();
		$("#popup-cargo").animate({"top":"148px",opacity:1},"fast",function(){
			$("#popup-cargo").css('filter', 'none');
			
		});
	}
}

function show_bubble(obj){
	$("#"+ obj).show();
	$("#"+ obj).animate({"top":"-76px",opacity:1,"filter":"none"},"fast",function(){
		//$("#"+ obj).css('filter', 'none');
		
	});
}
function hide_bubble(obj){
	$("#"+ obj).animate({"top":"-136px",opacity:0,"filter":"none"},"fast",function(){
		//$("#"+ obj).css('filter', 'none');
		$("#"+ obj).hide();
	});
}

function share_fb(num){

	u='http://www.pondsfacialfoam.com/share_fb.php?num='+ num +"&rand="+Math.random();
	if(num == 0){
		t="ถามพี่หนูนา รับ Pink iPod nano";
	}else{
		t="หนูนา Pink Me Up";
	}
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
		
}
function move_door(page){
	$("#btn-previous-cargo").html("");
	$("#btn-next-cargo").html("");
	$("#door-cargo").animate({"left": page * -960 +"px"},"slow",function(){
		if(page > 0){
			previous_page = page - 1;
			$("#btn-previous-cargo").html('<img src="images/btn-previous.png" onclick="move_door('+ previous_page +');" />');
		}
		if(page < total_page-1){
			next_page = page + 1;
			$("#btn-next-cargo").html('<img src="images/btn-next.png" onclick="move_door('+ next_page +');" />');
		}
	});
}

function search_review(orderby){
	$("#submit-loading").show();
	$("#btn-previous-cargo").html("");
	$("#btn-next-cargo").html("");
	$("#door-cargo-border").fadeTo('slow',0.5,function(){
		$.post("review-list.php", { "orderby": orderby },
		function(data){
			total_page = data.page;
			$("#door-cargo").css({"left":"0px","width": total_page*980 +"px"});
			$("#door-cargo").html(data.content);
			if(total_page > 1){
				$("#btn-next-cargo").html('<img src="images/btn-next.png" onclick="move_door(1);" />');
			}
			$("#door-cargo-border").fadeTo('slow',1);
			$("#submit-loading").hide();
		}, "json");
	});
}

function send_vote(id,mobile,email){
	
	alert("ปิดรับคะแนนโหวตแล้ว\nดูผล 10 รีวิวเข้ารอบด้านล่าง\nแล้วรอลุ้นว่า ใครจะได้รับคัดเลือกเป็น\nสุดยอดรีวิว 4 พ.ย.นี้");
	/*
	var error_msg = "";
	if((mobile == "") || (mobile == "แค่กรอกเบอร์มือถือทุกครั้ง")){
		error_msg = "- เบอร์มือถือ\n" + error_msg; 
	}else if(!isEmailAddr(email)){
		error_msg = "- email ไม่ถูกต้อง\n" + error_msg; 
	}else{
		mobile_len = mobile.length;
		ext = mobile.substr(0,2);
		if((mobile_len != 10) || (ext != "08") || (!isNumber(mobile))){
			error_msg = "- เบอร์มือถือไม่ถูกต้อง\n" + error_msg; 
		}
	}
	if(id == ""){
		error_msg = "- empty id\n" + error_msg; 
	}
	if(error_msg != ""){
		error_msg = "กรุณาตรวจสอบข้อมูลต่อไปนี้\n\n" + error_msg; 
		alert(error_msg);
	}else{
		$.post("vote.php", { "member_id": id, "mobile": mobile, "email": email },
		function(data){
			if(data == "empty"){
				alert("Data are empty");
			}else if(data == "error"){
				alert("ยังโหวตไม่ได้!\nรอครบ 1 ชั่วโมง ค่อยกลับมาโหวตให้รีวิวที่ชอบได้อีก");
			}else{
				alert("โหวตแล้ว!\nกลับมาโหวตให้รีวิวที่ชอบ...ได้อีกทุก 1 ชั่วโมง");
				$("#total-vote-cargo-"+ id).html("Vote&nbsp;&nbsp;"+ data);
			}
			hide_vote_form(id);
			hide_vote_form(id +"-1");
			hide_vote_form(id +"-2");
			document.getElementById("mobile_"+ id).value="แค่กรอกเบอร์มือถือทุกครั้ง";
			document.getElementById("email_"+ id).value="แค่กรอก email ทุกครั้ง";
			$("#top-10-table").load("top-10.php");
		});
	}*/
}
function click_vote(id){
	alert("ปิดรับคะแนนโหวตแล้ว\nดูผล 10 รีวิวเข้ารอบด้านล่าง\nแล้วรอลุ้นว่า ใครจะได้รับคัดเลือกเป็น\nสุดยอดรีวิว 4 พ.ย.นี้");
	//$("#vote-form-cargo-"+ id).show();
}
function hide_vote_form(id){
	$("#vote-form-cargo-"+ id).hide();
}


function isNumber(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}
function isEmailAddr(email)

{

  var result = false;

  var theStr = new String(email);

  var index = theStr.indexOf("@");

  if (index > 0)

  {

    var pindex = theStr.indexOf(".",index);

    if ((pindex > index+1) || (theStr.length > pindex+1))

	result = true;

  }

  return result;

}

function clickclear(thisfield, defaulttext) {
if (thisfield.value == defaulttext) {
thisfield.value = "";
}
}

function clickrecall(thisfield, defaulttext) {
if (thisfield.value == "") {
thisfield.value = defaulttext;
}
}

function shareFB(){
				
	FB.ui({method: 'feed', message:"",
		
		picture:"http://www.ibisland.com/images/share_fb.jpg", 
		name:"รีวิว ibis เมืองไทย พักฟรี ibis เกาหลี",
		link:"http://www.ibisland.com", 
		caption:"http://www.ibisland.com", 
		description:"สมมั้ย? บินไปพักฟรี..โรงแรมไอบิส เกาหลี 4 วัน 3 คืน รวมมูลค่า 150,000 บาท แค่ส่งรีวิวโรงแรมไอบิส เมืองไทย ที่ไหนก็ได้ แล้วชวนเพื่อนมาโหวต ภาพเด็ด คลิปโดน อารมณ์ไหนก็ได้ลุ้น มีรางวัลให้ทั้งคนโหวต คนรีวิว คลิกเลย", actions:{name:"Link", link:"http://www.ibisland.com"}
		
		});
	flashTracker("/#ShareFacebookPage");
}

function shareFBDetail(id,thumb){
				
	FB.ui({method: 'feed', message:"",
		
		picture:"http://www.ibisland.com/uploads/"+ thumb, 
		name:"น่าโหวตมั้ย? รีวิวนี้...ลุ้นพักฟรี ibis เกาหลี 4 วัน 3 คืน",
		link:"http://www.ibisland.com/review-detail.php?id="+ id, 
		caption:"http://www.ibisland.com/review-detail.php?id="+ id, 
		description:"ถ้าชอบ...ก็โหวตเลย!! แค่ส่งรีวิวโรงแรมไอบิส เมืองไทย ที่ไหนก็ได้ แล้วชวนเพื่อนมาโหวต ลุ้นบินไปพักฟรี..โรงแรมไอบิส เกาหลี 4 วัน 3 รวมมูลค่า 150,000 บาท มีรางวัลให้ทั้งคนโหวต คนรีวิว...ใครชอบเที่ยว คลิกเลย!!", actions:{name:"Link", link:"http://www.ibisland.com/review-detail.php?id="+ id}
		
		});
	flashTracker("/#ShareFacebookReview-"+ id);
}

function close_winner(){
	$("#winner-cargo").fadeTo(1000,0,function(){
			$("#fade-bg").fadeTo(1000,0,function(){
			$("#fade-bg").hide();
			$("#winner-cargo").hide();
		});
	});
	
}

function flashTracker(pagename){
	try {
		_gaq.push(['_setAccount', 'UA-18342664-1']);
  		_gaq.push(['_trackPageview']);
	} catch(err) {
		echo("error occur:"+ err);
	}
}


  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-18342664-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();



