function isEmail(str){
       var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
       return reg.test(str);
}

jQuery.fn.typing_blink = function(){
	
	var target = $(this);
	var timer = setInterval(function(){
	target.addClass("text_transparent");
	var timer = setTimeout(function(){
		target.removeClass("text_transparent");},500);
	},1000);
	
	return timer;
};
jQuery.fn.blink = function(){
	var target = $(this);
	var timer = setInterval(function(){
	console.log("now adding red");
	target.addClass("blink_red");
	var timer = setTimeout(function(){
		console.log("now removing red");
		target.removeClass("blink_red");},100);
	},200);
	setTimeout(function(){clearInterval(timer);},1000);
	return target;
};

jQuery.fn.typing_out = function(speed,callback){
	console.log("typing out");
	var target = $(this);
	var chars = $(this).attr('data-chars').split("");
	console.log(chars);
	var temp = '';
	
	var i=0;
	var timer = setInterval(function(){
		temp += chars[i++];
		target.text(temp);
		if(i == (chars.length)){
			console.log("clearing interval");
			clearInterval(timer);
			callback();
			return;
		}
		
	},speed);
}


$(document).ready(function(){
	var current_stage = 0;
	$("#link_btn_invite").click(function(){
		$(this).hide().next().show().focus();
		return false;
	});

	//alert('ss');
	$("#link_about").click(function(){
			console.log("clicking about:chosen"+typeof $(this).attr("chosen")+":"+$(this).attr("chosen"));
			if($(this).attr("chosen")=="true"){
				$(this).attr("chosen","false");
				$(this).removeClass("nav_link_btn_sub_chosen");
				$("#div_promote").show();
				$("#div_about").hide();
			}
			else{
				$(this).attr("chosen","true");
				$(this).addClass("nav_link_btn_sub_chosen");
				$("#div_promote").hide();
				$("#div_about").show();
			}
	});
	
	$("#link_thatime").click(function(){
		$(this).attr("chosen","false");
				$(this).removeClass("nav_link_btn_sub_chosen");
				$("#div_promote").show();
				$("#div_about").hide();
	})
	
	var get_code_handler = function(){
		$(this).unbind("click");
		$(this).hide();
		$(this).next().show();
	
		var email = $(this).prev().val();
		if(!isEmail(email)){
			alert("you know you enter a not-email address,right?");
			$(this).bind("click",get_code_handler);
			$(this).show();
			$(this).next().hide();
		}else{
			$.post("http://thatime.com/index.php/ajax_plain/apply_code",{"email":email},function(txt){
				if(txt.result == true){
					alert("OK!We got your email,you may get key later");
					$("#btn_get_code").prev().hide();
					$("#btn_get_code").hide().next().hide();
				}
				else{
					alert(txt.error_msg);
					$("#btn_get_code").bind("click",get_code_handler);
					$("#btn_get_code").show();
					$("#btn_get_code").next().hide();
				}
				
			},"json");
			
		}
	};
	
	$("#btn_get_code").bind("click",get_code_handler);
	
	$(".input_text_login").click(function(){
		if($(this).val() == $(this).attr("name")){
			$(this).val("");
		}
	});
	$(".input_text_login").blur(function(){
		if($(this).val() == ""){
			$(this).val($(this).attr("name"));
		}
	});
	
	var current_typing_blink = $(".blink_text").typing_blink();
	
	function start_stage(stage_id){
		
		console.log("starting stage "+stage_id);
		var curr_stage = $("#stage_"+stage_id);
		curr_stage.prev().remove();
		curr_stage.after("<div class='div_bond'></div>");
		curr_stage.show();
		switch (stage_id){
			case 1:
				curr_stage.find(".text_script").typing_out(200,function(){
				$("#stage_1 > img").fadeIn(500);
				$("#blink_text_1").hide();
				setTimeout(function(){
					$("#stage_1").addClass("yellow_outline");
					
					$.scrollTo($("#stage_1").next(),1000);
					
					setTimeout(function(){move_to_stage(2);},1100);	
					},1000);
				});
				break;
			case 2:
				curr_stage.find(".text_script").typing_out(200,function(){
					curr_stage.find("img").fadeIn();
					$("#blink_text_2").hide();
					setTimeout(function(){
						$("#stage_2").addClass("yellow_solid");
						setTimeout(function(){move_to_stage(3);},1500);	
						},1000);
				});
				break;
			case 3:
				curr_stage.find(".text_script").typing_out(200,function(){
					curr_stage.find("img").fadeIn();
					$("#blink_text_3").hide();
					setTimeout(function(){
						$("#stage_3").addClass("yellow_gradient");
						setTimeout(function(){move_to_stage(4);},2000);	
						},1000);
				});
				break;
			case 4:
				curr_stage.find(".text_script").typing_out(200,function(){
					//curr_stage.find("img").fadeIn();
					$("#blink_text_4").hide();
					curr_stage.find(".cube_blue").fadeIn();
					curr_stage.find(".cube_line").show();
					curr_stage.find(".cube_yellow").fadeIn();
					
					setTimeout(function(){
						curr_stage.find(".line").addClass("line_full");
						setTimeout(function(){curr_stage.find(".cube_blue").addClass("cube_yellow");},1000);
						setTimeout(function(){move_to_stage(5);},2500);	
					},1000);
				});
				break;
			
			case 5:
					curr_stage.find(".text_script").typing_out(200,function(){
					//curr_stage.find("img").fadeIn();
					$("#blink_text_5").hide();
					var length  = curr_stage.find(".moment_cube").size();
					
					var i=0;
					var timer = setInterval(function(){
						curr_stage.find(".moment_cube").eq(i++).fadeIn();
						if(i == (length)){
							console.log("clearing interval");
							clearInterval(timer);
						}
					},300);
					
					setTimeout(function(){
						setTimeout(function(){move_to_stage(6);},2500);	
					},2000);
				});
				break;
			case 6:
				curr_stage.find(".text_script").typing_out(200,function(){
					//curr_stage.find("img").fadeIn();
					$("#blink_text_5").hide();
					var length  = curr_stage.find(".moment_cube").size();
					
					setTimeout(function(){
						setTimeout(function(){close_film();},2500);	
					},2000);
				});
			
			break;	
		
		}
	
	
	
	
	}
	function close_film(){
		//return;
		$(".div_bond").remove();
		$("#impress").fadeOut();
		//init the film
		$(".text_script").text("");
		$(".blink_text").show();
		$("#impress").children().hide();
		$("#impress").find("img").hide();
		$("#stage_1").removeClass("yellow_outline");
		$("#stage_2").removeClass("yellow_solid");
		$("#stage_3").removeClass("yellow_gradient");
		$(".moment_cube").hide();
		$(".cube_blue").hide().removeClass("cube_yellow").find(".event_cube_avatar").show();
		$(".cube_yellow").hide().find(".event_cube_avatar").show();
		$(".cube_line").hide();
		$(".line").removeClass("line_full");
		$(".p_dot").remove();
		
		$(".nav_bar").show();
	}
	function move_to_stage(stage_id){
		
		var adding_dots = setInterval(function(){
			$("#stage_"+(stage_id-1)).after("<div align=center class='p_dot'><div class='dot'></div></div>");
			$.scrollTo(10000000);
		},60);
		
		setTimeout(function(){
		
		clearInterval(adding_dots);
		start_stage(stage_id);
		$.scrollTo($("#stage_"+stage_id));
		},2000);
		
	}
	
	/*
	$(".text_script").typing_out(200,function(){
		$("#stage_1 > img").show();
		$("#blink_text_1").hide();
		setTimeout(function(){
			$("#stage_1").addClass("yellow_outline");
			move_to_stage(2);		
			},1000);
	});
	*/
	
	$("#btn_login").click(function(){
		if($.trim($(":text[name='username']").val()) == ""){
			$(":text[name='username']").blink().focus();
			return false;
		}
		
		if($.trim($(":password[name='password']").val()) == ""){
			$(":password[name='password']").blink().focus();
			return false;
		}
	
	});
	
	$("#film_trigger").click(function(){
		$(".nav_bar").fadeOut();
		$("#impress").show();
		start_stage(1);
	
	});
	
	$("#invite_box").hover(function(){
		$("#input_email").show();
		setTimeout(function(){$("#input_email").addClass("width_100")},50);
	
	},function(){
		$("#input_email").removeClass("width_100");
		setTimeout(function(){$("#input_email").hide();},200);
	});
	
	
});
