function fbCheckLogin(callbackFunc){
	var uid = FB.Connect.get_loggedInUser();
		
		FB.Connect.requireSession(function(){
		
			// it will prompt a permission form
			FB.ensureInit(function () {
			
				// auto run you function
				callbackFunc();				
				
			});
		});
		
		

	return false;
}





function fbLogout(callbackFunc){

	if(window.confirm("Sure?")){
		FB.Connect.logout(callbackFunc());
	}
	return false;
}

function fbGetInfo(){

		var uid = FB.Connect.get_loggedInUser();
		var friends;
		var info;


			/* get friend */
			 FB.Facebook.apiClient.friends_get(null, function(friends_ids) {
				if (friends_ids) {
					friends=friends_ids.join(',')
				} else {
					friends='';
				}
			});
        
        
				var all="";
			
				/* get info */
				FB.Facebook.apiClient.users_getInfo(uid,['first_name','last_name','name','birthday','sex','pic_square','profile_url'], function(result, ex){
					/*window.alert("Exception :"+ex);*/
					
					var str="";
					var myArray=result[0];
					for ( var key in  myArray) {
						str+="&"+key+"="+encodeURIComponent(myArray[key]);
						
					
						/* sample code */
						if(key=='name'){

							$('#name').val(myArray[key]);
						}
						if(key=='profile_url'){

							$('#facebook').val(myArray[key]);
						}
						
						if(key=='uid'){

							$('#uid').val(myArray[key]);
						}
						if(key=='pic_square'){

							$('#logo').val(myArray[key]);
						}
						
					}
					info=str;
					
			
			
			
					 
					 all+=info;
					 all+="&friends="+friends;
					 
					 var all2="";
					 
					 all2=all.replace(/&/ig,"<BR>");
					 $("#debug").html(all2);
					 
					 
					 /* pass to server */
					//alert(all);
					
					
						/* pass to server */
						
						$.ajax({
							type: "POST",
							url: "ajax_reg2db.php",
							data: all,
							success: function(msg){
								
								
								//alert(msg);
								
						
							}
							});
							

				});
	return false;					
}







$(function(){
/*
$("#facebook").bind("click",function(){
	return fbCheckLogin(fbGetInfo);
})*/


   var name = "#floatMenu";
    var menuYloc = null;
	
    $(document).ready(function(){
    
    
		menuYloc = parseInt($(name).css("top").substring(0,$(name).css("top").indexOf("px")));
		$(window).scroll(function () { 
			var offset = menuYloc+$(document).scrollTop()+"px";
			$(name).animate({top:offset},{duration:100,queue:false});
		});
	
		$(".gallery a").attr("rel","group");
			$(".gallery a").lightbox({
				displayTitle:true,
				fitToScreen:true
				});			
	
        
        
		if($(name).length>0){
			menuYloc = parseInt($(name).css("top").substring(0,$(name).css("top").indexOf("px")));
		
				var offset = menuYloc+$(document).scrollTop()+"px";
				$(name).animate({top:offset},{duration:100,queue:false});
		
        }
        
        /*$("#floatMenu a").bind("click",function(){
			alert($(this).href());
			$(".item:2>h2").each(function(){
				alert($(this).html());
			});
        });*/
    }); 



	$("#enquiry").validate({
		rules:{
			
			name: "required",
			phone: "required",
			type: "required",
			product: "required",
			buy_method: {
				required:  function(element) {
				
					return $("input[@name='type']:checked").val()>=1;
				}

			},
			
			buy_arexxa: {
				required:  function(element) {
					return $("input[@name='type']:checked").val()>=1;
				}
			}
	
		},
		submitHandler: function(form) {
			if (window.confirm('確定送出？')){
				
				


				$.ajax({
						
					type: "POST",
					url: "order2db.php",
					data: $("#enquiry").serialize(),
					success: function(msg){
						
					
			
						
						if(msg=="ok"){
								
							$("#enquiry").html("<div class='infobox'><strong>您的留言已送出，批核後便會顯示出來。</strong></div>");
							alert("您的留言已送出，批核後便會顯示出來。");
							
							var targetOffset = $("#enquiry").offset().top;
		$('html,body').animate({scrollTop: targetOffset}, 1000);

						}
						
						if(msg=="error"){
							alert("Sorry.");
						}
					}
				});
				
				return false;
				
				

			}else{
			
			}
		} 
	});
	
	
	$(".content").append('<div class="back"><a href="#">按此回到最上</a></div>');
	$("h2").bind("mouseover",function(){
		$(this).css({
		background:"#3A4966",
		cursor:"pointer"
		});
		
	});
	$("h2").bind("mouseout",function(){
		$(this).css({
		background:"#2A3958"
		});
		
	});
	
	
	$("h2").bind("click",function(){
		/*$("h2").next().hide();*/
		$(this).next().show();
		
		var targetOffset = $(this).offset().top;
		$('html,body').animate({scrollTop: targetOffset}, 1000);
		return false;
	});
	
	$(".back a").bind("click",function(){
		/*$("h2").next().hide();	*/	
		var targetOffset = $(".main").offset().top;
		$('html,body').animate({scrollTop: targetOffset}, 1000);
		return false;
	});	
	
	$("#url").bind("click",function(){
		$(this).select();
		return false;

	});
	$(".youtube_url input").bind("click",function(){
		$(this).select();
		return false;

	});	
	
	$(".openClose").bind("click",function(){
		if ($("h2:first").next().is(":hidden")){
			$("h2").next().show();
		}else{
			$("h2").next().hide();
		}

		return false;

	});
});


   
   
  

function youtubeIDextract(url){ 

  var youtube_id; 

  youtube_id = url.replace(/^[^v]+v.(.{11}).*/,"$1"); 

  return youtube_id; 
}


function funToolTips(obj,msg){
var target=$(document.createElement('div'));

 $(obj).after(target);
	/*var target=obj.find("+ div");*/
	target.css({
		"position" : "absolute"
	});
 
	var obj_pos=$(obj).position();

   var left=obj_pos.left;
	var top=obj_pos.top;

	
	top=top+$(obj).attr("offSetHeight");
	left=left+0;
	target.css({
		"color" : "#333333",
		"padding" : "2px 5px 2px 5px",
		"background" : "#fff9d7",
		"border" : "1px solid #e2c822",
		"top" : top+"px",
		"left" : left+"px"
	});
		
	/*target.html(msg);*/
	/*target.html('<div stle="white-space: nowrap;">'+msg+'</div>');*/
	target.html('<table cellpadding="0" cellspacing="0" border="0"><tr><td nowrap="nowrap">'+msg+'</td></tr></table>');
	/*target.append('<div class"clear"></div>');*/
	window.setTimeout(function(){
		$(target).hide();
		
		window.setTimeout(function(){
			$(target).remove();
		},2000);
	 },3000);
}


$(document).ready(function(){

	
	$(".content a[href*='youtube.com/watch']").each(function(){
		var link=$(this).attr("href");
		//alert(link);
		var u_div=$(document.createElement('div'));
		var u_img=$(document.createElement('img'));
		var u_vid=youtubeIDextract(link);
		u_img.attr("src",'http://img.youtube.com/vi/'+u_vid+'/0.jpg');
		u_img.css("width",'320px');
		u_div.addClass("u_div");
		u_div.append(u_img);

		u_img.attr("title","[按此播放]");
		u_img.css({
			cursor:"pointer"
		});
		u_img.bind("click",function(){
			$(this).parent().prev().click();
		});
		$(this).after(u_div);
		
		
		
		var btn=$(document.createElement('input'));
		btn.addClass("button");
		
		btn.css({
		"width":"40px",
		"text-align":"center",
		"margin-left":"8px"
		
		});

		btn.val("播放");
		btn.attr("title","[按此播放]");
		$(this).after(btn);
		
		
		
		var playerWidth=320;
		var playerHeight=240;
		
		btn.bind("click",function(){
		
			if($(this).next().hasClass("playerDiv")){
				// remove
				$(this).next().remove();
				$(this).val("播放");
				var u_div=($(this).next());
				u_div.show();
			}else{
				var u_div=($(this).next());
				u_div.hide();
				
				$(this).val("關閉");
				// create
				var playerDiv=$(document.createElement('div'));
				playerDiv.css({
				"border":"0px solid #ff0000",
				"width":"500px",
				"text-align":"left",
				"margin-left":"0px"
				});
				playerDiv.addClass("playerDiv");
				var vid=youtubeIDextract(link);
				
				/* =================================== */
				var videoObj = document.createElement("ojbect");
				var videoParam1 = document.createElement("param");
				var videoParam2 = document.createElement("param");
				var videoParam3 = document.createElement("param");
				var videoEmbed = document.createElement("embed");
				/*var videoLink = "http://www.youtube.com/v/" + playBtn.getAttribute("id") + "&hl=en&fs=1&rel=0" + (GM_getValue("isAutoplay") ? "&autoplay=1" : "&autoplay=0");*/
				var videoLink = "http://www.youtube.com/v/"+vid+"&hl=en&fs=1&rel=0&autoplay=1";
				
				videoObj.setAttribute("width", playerWidth);
				videoObj.setAttribute("height", playerHeight);
				videoParam1.setAttribute("name", "movie");
				videoParam1.setAttribute("value", videoLink);
				
				videoParam2.setAttribute("name", "allowFullScreen");
				videoParam2.setAttribute("value", "true");
				
				videoParam3.setAttribute("name", "allowscriptaccess");
				videoParam3.setAttribute("value", "always");
				
				videoEmbed.setAttribute("src", videoLink);
				videoEmbed.setAttribute("type", "application/x-shockwave-flash");
				videoEmbed.setAttribute("allowfullscreen", "true");
				videoEmbed.setAttribute("allowscriptaccess", "always");
				videoEmbed.setAttribute("width", playerWidth);
				videoEmbed.setAttribute("height", playerHeight);
				
				videoObj.appendChild(videoParam1);
				videoObj.appendChild(videoParam2);
				videoObj.appendChild(videoParam3);
				videoObj.appendChild(videoEmbed);
				
				playerDiv.append(videoObj);
				$(this).after(playerDiv);
				/* =================================== */
			}
			
			
			
			
		});
		
		/*
			var regexVideoId = link.match(/(v=|v\/)([a-zA-Z0-9_-]+)&?/i)*/
	});
	



		
	
 // end ready 
});

