/**
 * TwitCard
 * jQuery-1.4.1
 *
 * @author T.Yasui
 * @since 2009/1/29
 * @copyright Twit Card
 */

$(function(){
	$("#user_id").keydown(function(e){
		if(e.keyCode==13){
			enterId();
			return false;
		}
	});
	$("#user_id").blur(function(){
		enterId();
	});
	$("#design_box img").each(function(){
		$(this).click(function(){
			var design = $(this).attr("alt");
			$("#design").val(design);
			$("#memory_design").val(design);
			getCardImg();
		});
	});
	$("#new_design img").each(function(){
		$(this).click(function(){
			var design = $(this).attr("alt");
			$("#design").val(design);
			$("#memory_design").val(design);
			getCardImg();
		});
	});
	$("#open_design").click(function(){
		$("#new_design").toggle("middle");
		$(this).fadeOut();
	});
	
	//sendForm
	$("#send_to_id").bind("blur",function(){
		var user_id = $("#send_to_id").val();
		var dataList = {cmd:'get_data', id:user_id}
		ajaxGetData(dataList,"to");
	});
	$("#send_form").hide();
	$("#message").charCount({
			allowed: 45,		
			warning: 5
	});
	
	
	//button
	$("#create_pdf").click(function(){
		$("#memory_form").attr("action","createPdf.php");
		$("#memory_form").submit();
	});
	$("#send_img").click(function(){
		showCardForm();
	});
	$("#close_button").click(function(){
		hideCardForm();
	});
	$("#send_button").click(function(){
		var limit = $("#send_box .send_message .box .counter").html();
		var from = $("#memory_user_id").val();
		var to = $("#memory_to_id").val();
		var p = $("#pass").val();
		if(from == ""){
			alert("Enter your Twitter ID !");
		}else if(to == ""){
			alert("Enter your friend's Twitter ID !");
		}else if(limit < 0){
			alert("Please enter Messsage no more than 45 characters.!");
		}else if(p == ""){
			alert("Enter your Twitter Pass !");
		}else{
			$("#memory_form").attr("action","sendImage.php");
			$("#memory_form").submit();
			hideCardForm();
		}
	});
	
	//jCarouselLite
	$(".main .jCarouselLite").jCarouselLite({
		btnNext: ".next",
		btnPrev: ".prev"
	});
	
	//free_print
	$("#free_print").click(function(){
		$("#memory_form").attr("action","sendPrintForm.php");
		$("#memory_form").submit();
	});
	
	//BackImage
	$("#backImage").change(function(){
		uploadBackImage();
	});
	$("#upButton").click(function(){
		uploadBackImage();
	});
	
	
});


/* upload BackImage*/
function uploadBackImage(){
	var user_name = $("#memory_user_name").val();
	var user_id = $("#user_id").val();
	var now_id = getNow();
	var img_id = user_id +"_"+ now_id;
	var dir = "original/"+img_id;
	if(user_name){
		$("#twit_form").attr("action","uploadBackImage.php?now_id="+now_id);
		$("#twit_form").submit();
		$("#design").val(dir);
		$("#memory_design").val(dir);
		
		setTimeout(function(){
			getCardImg();
			},1000
		);
	}else{
		alert("Error:Enter Twitter ID");
	}
}

/* enterId */
function enterId(){
	var user_id = $("#user_id").val();
	var pre_id = $("#memory_user_id").val().toLowerCase();
	
	$("#user_id").val(user_id.replace("@", "").toLowerCase());
	
	var wait = 0;
	if(user_id != pre_id || $("#memory_user_name").val() == ""){
		//data setting
		$("#memory_user_id").val("");
		$("#memory_user_name").val("");
		$("#memory_user_img").val("");
		$("#memory_user_location").val("");
		$("#memory_user_lang").val("");
		$("#memory_user_bio").val("");
		$("#memory_user_url").val("");
		$("#memory_user_s_url").val("");
		$("#memory_user_qr").val("");
		$("#memory_user_qr_big").val("");
		$("#memory_user_timezone").val("");
		var dataList = {cmd:'get_data', id:user_id}
		ajaxGetData(dataList,"user");
		wait = 2000;
	}
	$("#cardimg").attr("src","img/loading.gif");
	setTimeout(function(){
		getCardImg();
		},wait
	);
	
}

/* getCardImage */
function getCardImg(){
	var design = $("#design").val();
	var user_id = $("#memory_user_id").val();
	var user_name = $("#memory_user_name").val();
	var user_img = $("#memory_user_img").val();
	var user_location = $("#memory_user_location").val();
	var user_lang = $("#memory_user_lang").val();
	var user_bio = $("#memory_user_bio").val();
	var user_url = $("#memory_user_url").val();
	var s_url = $("#memory_user_s_url").val();
	
	if(user_name){
		var img = "createCard.php?user_id="+user_id+"&design="+design+"&name="+encodeURIComponent(user_name)+"&img="+user_img+"&location="+encodeURIComponent(user_location)+"&lang="+user_lang+"&bio="+user_bio+"&url="+user_url+"&s_url="+s_url;
	}else{
		//idが適切で無い場合
		var img = "img/card_img/enter.png";
	}
	$("#cardimg").attr("src","img/loading.gif");
	setTimeout(function(){
		$("#cardimg").attr("src",img);
		},100
	);
}

/* overlay */
function showCardForm(){
	$("#send_from_img").attr("src",$("#memory_user_img").val());
	$("#send_from_id").html($("#memory_user_id").val());
	$("#send_from_name").html($("#memory_user_name").val());
	
	$("#send_to_img").attr("src",$("#memory_to_img").val());
	$("#send_to_id").html($("#memory_to_id").val());
	$("#send_to_name").html($("#memory_to_name").val());
	
	$("#send_card_img").attr("src",$("#cardimg").attr("src"));
	
	
	$("#send_form #send_box").hide();
	var overlayHeight = $("body").height();
	$("#send_form").css("height",overlayHeight);
	$("#send_form").fadeIn('fast');
	$("#send_form #send_box").animate(
		{height: "toggle", opacity: "toggle"},"slow"
	);
}
function hideCardForm(){
	$("#send_form #send_box").toggle();
	$("#send_form").fadeOut('fast');
}


/* ajax */
function ajaxGetData(dataList, dataTarget){
	var url = "ajaxRequest.php";
	$.ajax({
		url: url,
		type: "POST",
		data: dataList,
		dataType: "json",
		success: function(res){
			$("#memory_"+dataTarget+"_id").val(res['id']);
			$("#memory_"+dataTarget+"_name").val(res['name']);
			$("#memory_"+dataTarget+"_img").val(res['img']);
			if(dataTarget=="user"){
				$("#memory_"+dataTarget+"_location").val(res['location']);
				$("#memory_"+dataTarget+"_lang").val(res['lang']);
				$("#memory_"+dataTarget+"_bio").val(res['bio']);
				$("#memory_"+dataTarget+"_url").val(res['url']);
				$("#memory_"+dataTarget+"_s_url").val(res['s_url']);
				$("#memory_"+dataTarget+"_qr").val(res['qr']);
				$("#memory_"+dataTarget+"_qr_big").val(res['qr_big']);
				$("#memory_"+dataTarget+"_timezone").val(res['timezone']);
			}else if(dataTarget=="to"){
				$("#send_to_img").attr("src",res['img']);
				$("#send_to_name").html(res['name']);
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			alert("Error： Twitter API Limit Over !");
		}
	});
}

/* getNow */
function getNow(){
	var now = new Date();
	var year = now.getFullYear(); // 年 
	var mon  = now.getMonth() + 1; // 月 
	var date = now.getDate(); // 日 
	var week = now.getDay(); // 曜日 
	var hour = now.getHours(); // 時 
	var min  = now.getMinutes(); // 分 
	var sec  = now.getSeconds(); // 秒 
	
	if (mon < 10) { mon = "0" + mon; }
	if (date < 10) { date  = "0" + date; }
	if (hour < 10) { hour = "0" + hour; }
	if (min< 10) { min = "0" + min; }
	if (sec < 10) { sec = "0" + sec; }
	
	var date_id = String(year)+String(mon)+String(date)+String(hour)+String(min)+String(sec);
	return date_id;
}
