(function($){
	$(function(){
		$("#update").bind("click", update_click);
		$("#update_login").bind("click", login_click);
		$("#btn_update").bind("click", btnupdate_click);
		$("#btn_cancel").bind("click", btncancel_click);
		$("#btn_ok").bind("click", function(){ alert('이미 지우무비트윗 회원이십니다'); });
		$(".write_popup_box a").bind("click", Event_lotto);
		$(".write_popup_box a").prev().bind("keypress", Keypress);		
		$("#reply_submit").bind("click",reply_submit_click);
		var i="blur focus change "+($.browser.mozilla?"paste input":"keyup");
		$("#status").bind(i,status_keyup);
	});

	status_keyup = function(){
		var len = $("#status").attr("value").length;
		$("#text_count").html(len);
		if (len > 140){
			$(this).closest(".writebox").find("#text_count").css("color","red");			
		}
		else{
			$(this).closest(".writebox").find("#text_count").css("color","");
		}
	}

	Event_lotto = function(){ 
		var screenname = $("#popup_id").attr("value");
		var idx = $("#idx").attr("value");
		new Ajax.Request('/event/movie_event.html',{
			parameters:{
				screenname:screenname,
				mode:"lotto"
			},
			onSuccess:function(res){	
				$("#person").html(res.responseText)
			},
			onFailure:function(res){
				
			}
		});
	}

	Keypress = function(event){
		if (event.which == 13)
		{
			Event_lotto();
		}
	}

	btncancel_click = function(){
		if (confirm('정말로 탈퇴하시겠습니까?')){
			var mode = "signout";
			new Ajax.Request('/event/movie_event.html',{
				parameters:{
					mode:mode
				},
				onSuccess:function(res){
					if (res.responseText == "1"){
						alert('탈퇴되었습니다');
						document.location.reload();
					}
					else{
						alert('다시 시도해주십시요');
						document.location.reload();
					}
				},
				onFailure:function(res){
					document.location.reload();
				}
			});
		}
	}

	btnupdate_click = function(){
		if (confirm('열심히 활동하시겠습니까?')){
			var mode = "complete";
			new Ajax.Request('/event/movie_event.html',{
				parameters:{
					mode:mode
				},
				onSuccess:function(res){
					if (res.responseText == "1"){
						alert('승인되었습니다');
						document.location.reload();
					}
					else{
						alert('다시 시도해주십시요');
						document.location.reload();
					}
				},
				onFailure:function(res){
					alert('다시 시도해주십시요');
					document.location.reload();
				}
			});
		}
	}

	login_click = function(){
		var checked_value = "";
		var check_count = 0;
		$("[name=check]:checked").each(function(){
			checked_value = checked_value + $(this).next().html() + ",";
			check_count = check_count + 1;
		});
		if (check_count < 2){
			alert("2개 이상을 선택하셔야 합니다");
			return false;
		}
		oauthOpen('event_login' + encodeURIComponent(checked_value));
	}

	update_click = function(){		
		var mode = "update";
		var checked_value = "";
		var check_count = 0;
		$("[name=check]:checked").each(function(){
			checked_value = checked_value + $(this).next().html() + ",";
			check_count = check_count + 1;
		});

		if (check_count < 2){
			alert("2개 이상을 선택하셔야 합니다");
			return false;
		}

		jQuery("#update").css("opacity",0.2);		
		$("#update").unbind("click");
		new Ajax.Request('/event/movie_event.html',{
			parameters:{
				mode:mode,
				checked_value:checked_value
			},
			onSuccess:function(res){
				if (res.responseText == "1"){
					alert('신청하였습니다');
				}
				else if (res.responseText == "-1"){
					alert("신청대상이 아닙니다");
				}
				else if (res.responseText == "0"){
					alert('이미 신청되었습니다');
				}
				else if (res.responseText == "2"){
					alert('지우 트위트 회원가입을 하셔야 합니다');
					location.href = "/user/join_select.html";
				}
				else if (res.responseText == "3"){
					alert('지우 시사단카페 가입을 하신후 신청이 가능합니다.');
					location.href = "/menu_twittercafe/join.html?cafe_idx=1";
				}
				else{
					alert('다시 등록해주십시요');
				}
				$("#update").bind("click",update_click);
				jQuery("#update").css("opacity",1);		
			},
			onFailure:function(res){
			}
		});
	}

	loadMoreList = function(more){		
		var idx = $("#idx").attr("value");		
		//$(".event_list").fadeOut(function(){
			new Ajax.Request('/event/movie_event.html',{
				parameters:{
					more:more,
					mode:"more"
				},
				onSuccess:function(res){
					$(".event_list").html(res.responseText);								
					//$(".event_list").fadeIn();
				},
				onFailure:function(res){
				}
			});
		//});
	}

	reply_submit_click = function(){
		Event_twitterUpdate(this);
	}

	Event_twitterUpdate = function(el){
		var shorturl = $("#shortUrl").attr("value");
		var screen_name = document.getElementById("screen_name").value;
		var thumb = document.getElementById("thumb").value;
		var id = $("#num").attr("value");
		var status = $(el).closest("div").find("#status").attr("value");		
		var corp = $("#company").html();
		if (status > 140)
		{
			//alert("140글자를 초과할 수 없습니다.");
			//return false;
		}
		if (status == "")
		{			
			return false;
		}		
		
		var checked_value = "";
		var check_count = 0;
		$("[name=check]:checked").each(function(){
			checked_value = checked_value + $(this).next().html() + ",";
			check_count = check_count + 1;
		});

		if (check_count < 2){
			alert("위 사항을 읽어보시고 2개 이상을 선택해주세요");
			return false;
		}

		var mode = "3";
		
		var parent = $(el).parent();
		$(el).hide(); 
		$(parent).append("<img id='process' src='/img/search/loader3.gif' alt='updating' style='margin:6px 48px 4px 46px; vertical-align:middle;'>");			

		new Ajax.Request('/event/Event_Update.html',{
			parameters:{
				screen_name:screen_name,
				thumb:thumb,				
				status:status,
				url:shorturl,
				mode:mode,
				checked_value:checked_value
			},
			onSuccess:function(res){	
				$(el).show(); 
				$(parent).find("#process").remove();				
				$(res.responseText).insertAfter(".event_list .list");	
				$.alertTop("등록되었습니다");
				if ($(".event_list ul").size() == 1){
					$(".event_list #null").remove();
					$(".event_list ul:first").css("border","0");
				}				
			},
			onFailure:function(res){				
								
			}
		});				
	}

	Event_Deltwitter = function(idx, el){
		if (confirm('정말 삭제 하시겠습니까?'))
		{
			var target = $(el).parent().parent().parent().parent().parent();
			$(target).fadeOut();
			new Ajax.Request('/event/Event_twitter_delete.html',{
				parameters:{
					idx:idx,
					mode:"3"
				},
				onSuccess:function(res){	
					var id = $("#num").attr("value");
					target.remove();			
					$(".event_list ul:first").css("border-top","0");
				},
				onFailure:function(res){
					
				}
			}) ;
		}
	}

	Event_Retwit = function(el){			
		if (confirm("RT 하시겠습니까?")){
			var id = $(el).parent().parent().prev().children("a").html();
			var text = $(el).parent().parent().prev().text().split(":");		
			var status = "RT @" + id + text[1];		
			var shorturl = $("#shortUrl").attr("value");		
			var parent = $(el).parent();
			$(parent).append("<img id='progress' src='/img/mylink/loader2.gif' style='width:11px;height:11px;margin-left:10px;'>")
			$(el).remove();
			
			new Ajax.Request('/event/event_update.html',{
				parameters:{	
					status:status,
					url:shorturl,
					rt:"1"
				},
				onSuccess:function(res){		
					$("#progress").remove();
					$(parent).append("<a class='btn_rt' onclick='Event_Retwit(this);' href='javascript:void(0);'<b>RT</b></a>");
					alert('RT 하였습니다.');					
				},
				onFailure:function(res){				
								
				}
			});		
		}		
	}

	Event_search = function(){		
		var m = $("#goods").val();
		var q = $("#q2").attr("value");
		location.href = "/event/index.html?m=" + encodeURI(m) + "&q=" + encodeURI(q);
	}

	
})(jQuery)