var url = "";
var share_timer;
var room_no;

$(function(){
	
	setSrc();
	initBind();
	getTimeline();
	
});



function initBind(){
	
	$("#register_form").hide();
	$("#member").hide();
	$("#caster").hide();
	
	$("#url_button").bind("click",function(){
		httpGo();
	});
	
	$("#share_button").bind("click",function(){
		if(share_timer != undefined){
			clearTimeout(share_timer);
		}
		room_no = $("#room_input").val();
		$("#state").text("ルーム"+room_no+"に在籍中");
		$("#caster").show();
		$("#caster_button").show();
		$("#member_link").show();
		share();
	});
	
	$("#cancel_button").bind("click",function(){
		if(share_timer != undefined){
			clearTimeout(share_timer);
		}
		$("#state").text("入室していません");
		room_no = "";
		$("#caster").hide();
		$("#caster_button").hide();
		$("#member_link").hide();
	});
	
	$("#caster_button").bind("click",function(){
		if(id == 0){
			alert("ログインする必要があります");
		}else{
			postCaster();
		}
	}).hide();
	
	$("#member_link").bind("click",function(){
		getMember();
		$("#member").show();
		return false;
	}).hide();
	
	$("#chat_button").bind("click",function(){
		postChat();
	});
	
	$("#register").bind("click",function(){
		$("#register_form").show();
		return false;
	});
	
	$("#reg_button").bind("click",function(){
		reg();
	});
	
	$("#close_button").bind("click",function(){
		$("#register_form").hide();
	});
	
	$("#close_member_button").bind("click",function(){
		$("#member").hide();
	});
	
	$("#close_shortcut_button").bind("click",function(){
		$("#shortcut").hide();
	});
	
	$("#google_icon").bind("click",function(){
		$("#tv").attr("src","http://itoak.net/parse.php?url=http://www.google.co.jp/");
	});
	
	$("#search_icon").bind("click",function(){
		$("#tv").attr("src","http://itoak.net/parse.php?url=http://bing.com/");
	});
	
	$("#youtube_icon").bind("click",function(){
		$("#tv").attr("src","http://itoak.net/parse.php?url=http://www.youtube.com/");
	});
	
	$("#yahoo_icon").bind("click",function(){
		$("#tv").attr("src","http://itoak.net/parse.php?url=http://search.yahoo.co.jp/");
	});
	
	$("#amazon_icon").bind("click",function(){
		$("#tv").attr("src","http://itoak.net/parse.php?url=http://www.amazon.co.jp/");
	});
	
	$("#hatena_icon").bind("click",function(){
		$("#tv").attr("src","http://itoak.net/parse.php?url=http://b.hatena.ne.jp/");
	});
	
}


function httpGo(){
	url = "./parse.php/?url="+$("#url_input").val();
	$("#tv").attr("src",url);
	var para = {"url":url};
	$.post("./set_url.php",para,function(){});
}

function setSrc(){
	setTimeout("setSrc()",1000);
	if(url != document.getElementById("tv").contentWindow.document.location.href){
		url = document.getElementById("tv").contentWindow.document.location.href;
		
		var para = {"url":url};
		
		$.post("./set_url.php",para,function(){});
	}
}

function share(){
	
	share_timer = setTimeout("share()",5000);
	
	var dt = new Date();
	var avoid_cache = "&date="+dt.getDate()+dt.getHours()+dt.getMinutes()+dt.getSeconds();
		
	var json_url = "./get_url.php?room_no="+room_no+avoid_cache;
	$.getJSON(json_url,function(data){
		
		$("#caster").text("現在の放送者:"+data.user_name);
		
		if(data.id != id){
			if(data.url != document.getElementById("tv").contentWindow.document.location.href){
				$("#tv").attr("src",data.url);
			}
		}
	});
}

function postChat(){
	
	var body = $("#chat_input").val();
	if($("#private_flag").attr("checked")){
		var flag = 1;
	}else{
		var flag = 0;
	}
	var data = {"body":body, "flag":flag, "room_no":room_no};
	$.post("./post_chat.php",data,function(){
		$("#chat_input").val("");
		});
}

function postCaster(){
	var para = {
		"room_no" : room_no 
	}
	$.post("./update_caster.php",para,function(){
		
	});
}


function getTimeline(){
	
	chat_timer = setTimeout("getTimeline()",1000);
	
	var dt = new Date();
	var avoid_cache = "date="+dt.getDate()+dt.getHours()+dt.getMinutes()+dt.getSeconds();
	
	if($("#share_chat_flag").attr("checked")){
		var json_url = "./get_timeline.php?room_no="+room_no+"&"+avoid_cache; 
	}else{
	var json_url = "./get_timeline.php"+"?"+avoid_cache;
	}
	$.getJSON(json_url,function(data){
		$("#time_line table").remove();
		$("<table/>").appendTo($("#time_line"));
		for (var i=0;i<5;i++){
			$("#time_line table").append("<tr><td><span class=\"user_name\">"+data[i].user_name+"</span><br /><span class=\"body\">"+data[i].body+"</span><br /><span class=\"date\">"+data[i].date+"</span></td></tr>");
		}
		
	});
	
}

function reg(){
	
	var reg_name = $("#reg_name").val();
	var reg_pass = $("#reg_pass").val();
	var para = {
		"user_name": reg_name,
		"pass": reg_pass,
		"mode": "reg"
		}
	$.post("./register.php",para,function(response){
			if(response.match(/err">(.+)<\/span/ig)){
				var err_mes = RegExp.$1
				$("#reg_message").html(err_mes);
			}else{
				alert(response);
				$("#reg_name").val("");
			    $("#reg_pass").val("");
			}
		});
}


function getMember(){
	
	var dt = new Date();
	var avoid_cache = "&date="+dt.getDate()+dt.getHours()+dt.getMinutes()+dt.getSeconds();
	
	var json_url = "./get_member.php?room="+room_no+avoid_cache;
	
	
	$.getJSON(json_url,function(data){
		
		var now = new Date();
		
		
		$("#member table tr").remove();
		for(var i=0; i<data.length; i++){
			var login_data = data[i];
			var last_login = new Date(login_data.date);
			var time = Math.floor((now - last_login)/1000);
			if(time < 60){
				$("#member table").append("<tr><td>"+login_data.user_name+"</td></tr>");
			}
		}
	});
}


