
var category = "전체보기";
var q = "";
var ajax_loading = 0;
(function($){
	$(function(){
		$(".vote").live("click", function(){ Event_setVote(this); });	
		$("#category a").bind("click", function() { 
			loadListByCategory(this); 
			$("#category a").css("font-weight","");			
			$(this).css("font-weight","bold");
			$(this).css("color","blue");
		});			
		
		$(".search .btn").bind("click", function() {
			var value = $("#goods").val();
			if (value == "경품선택" || value == "주최사" || value == "행사명"){
				Event_search();	
				return false;
			}						
		});

		$(".search .box").bind("keypress", Search_Keypress);
		hidden_resizing();
		movie_rate_init();
		$(".btn_approved").live("click",btn_approved_click);
		$(".btn_cancel").live("click",btn_cancel_click);
		$(".btn_delete").live("click",btn_delete_click);
		$(".btn_status_ok").live("click",btn_status_ok_click);		
		var i="blur focus change "+($.browser.mozilla?"paste input":"keyup");
		$(".div_status_form #status").live(i,status_keyup)
		$(".btn_send_tweet").live("click",btn_send_tweet_click);
	});

	btn_send_tweet_click = function(){
		var len = $(this).closest(".event1").find("#status").attr("value").length;
		if (len > 119){
			alert("트윗이 너무 깁니다");
			return false;
		}

		if (ajax_loading == 1){
			return false;
		}
		ajax_loading = 1;
		var thisEl = this;		

		var mode = "btn_send_tweet_click";
		var status = $(this).closest(".event1").find("#status").attr("value");
		var idx = $(this).closest(".event1").attr("id");
		var _url = "/event/process.html";
		var _data = {
			mode:mode,
			status:status,
			idx:idx
		}
		$.ajax({
			type:"post",
			data:_data,
			url:_url,
			success:function(msg){
				var msg_info = JSON.parse(msg);
				if (typeof(msg_info.error) == "undefined"){
					alert("트윗이 정상적으로 작성되었습니다");
					$(thisEl).closest(".div_status_form").hide();
					var html = "<div style='background-color:#f0f0f0; padding:5px;'><p> " + msg_info.status_text + " </p><p style='padding-top:2px;'><a style='color:#5379C2; font-weight:bold;' class='btn_status_ok' href=''>스폰서트윗 다시쓰기</a></p></div>";
					$(thisEl).closest(".div_status_form").closest("li").append(html);

				}else{					
					alert(convert_kr(msg_info.error));
				}
			},
			error:function(){ alert("error!!") },
			complete:function(){
				ajax_loading = 0;
			}
		});
	}

	Event_delete = function(idx, el){
		if (confirm('정말 삭제 하시겠습니까?'))
		{
			new Ajax.Request('/event/Event_delete.html',{
				parameters:{	
					idx:idx
				},
				onSuccess:function(res){
					if (res.responseText == "1"){
						$(el).parent().parent().parent().remove();
						alert("삭제되었습니다");
					}
					else{
						alert('Fail!!!!!!');				
					}
				},
				onFailure:function(res){				
								
				}
			});	
		}
	}

	Event_mydel = function(el){
		if (confirm('정말 삭제 하시겠습니까?'))
		{
			var idx = $(el).parent().parent().parent().attr("id");		
			new Ajax.Request('/event/Event_interest.html',{
				parameters:{	
					idx:idx,
					mode:"del"
				},
				onSuccess:function(res){
					$(el).parent().parent().parent().remove();
				},
				onFailure:function(res){				
								
				}
			});		
		}
	}

	btn_status_ok_click = function(){
		var div = $(this).closest(".event1");
		$(this).closest("div").remove();
		$(div).find(".div_status_form").show();
		$(div).find(".div_status_form #status").focus();
		status_keyup();
		return false;
	}

	status_keyup = function(){
		var len = $("#status").attr("value").length;
		$(".status_len").html(119 - len);		

		if (119 - len < 10){
			$(".status_len").css("color","#D40D12");			
		}
		else if (119 - len < 20){			
			$(".status_len").css("color","#5C0002");
		}
		else{
			$(".status_len").css("color","#CCCCCC");
		}
	}

	btn_approved_click = function(){
		if (confirm("승인하시겠습니까?")){
			var mode = $(this).attr("class");
			var idx = $(this).closest(".event1").attr("id");
			var _data = {
				mode:mode,
				idx:idx
			}
			var _url = "/event/process.html";
			$.ajax({
				type:"post",
				url:_url,
				data:_data,
				success:function(msg){
					if (msg == "success"){
						alert("승인하였습니다");
					}
				},
				error:function(){ alert("error!!!") }
			})
		}		
		return false;
	}
	
	btn_cancel_click = function(){
		if (confirm("취소하시겠습니까?")){
			var mode = $(this).attr("class");
			var idx = $(this).closest(".event1").attr("id");
			var _data = {
				mode:mode,
				idx:idx
			}
			var _data = {
				mode:mode,
				idx:idx
			}
			var _url = "/event/process.html";
			$.ajax({
				type:"post",
				url:_url,
				data:_data,
				success:function(msg){
					if (msg == "success"){
						alert("취소하였습니다");
					}
				},
				error:function(){ alert("error!!!") }
			})
		}		
		return false;
	}

	btn_delete_click = function(){
		if (confirm("삭제하시겠습니까?")){
			var mode = $(this).attr("class");
			var idx = $(this).closest(".event1").attr("id");
			var _data = {
				mode:mode,
				idx:idx
			}
			var _data = {
				mode:mode,
				idx:idx
			}
			var _url = "/event/process.html";
			$.ajax({
				type:"post",
				url:_url,
				data:_data,
				success:function(msg){
					if (msg == "success"){
						alert("삭제하였습니다");
					}
				},
				error:function(){ alert("error!!!") }
			})
		}			
		return false;
	}	

	giftboxcheck = function(){
		var flag = true;
		$("#giftbox input").each(function(){			
			if ($(this).attr("value") == "")
			{
				$(this).focus();
				flag = false;
				return false;
			}			
		});
		return flag;
	}		

	Event_setVote = function(el){
		var id = $(el).parent().parent().parent().attr("id");
		new Ajax.Request('/event/event_vote.html',{
			parameters:{
				id:id
			},
			onSuccess:function(res){
				if (res.responseText == "0")
				{
					alert('이미 추천하셨습니다.');
				}else if (res.responseText == "1")
				{
					var targetEl = $(el).parent().prev();					
					$(targetEl).html(parseInt(targetEl.html())+1);
				}
				
			},
			onFailure:function(res){
			}
		});
	}			

	Search_Keypress = function(event){
		if (event.which == 13)
		{
			var value = $("#goods").val();
			if (value == "경품선택" || value == "주최사" || value == "행사명")	{
				Event_search();					
				return false;
			}	
		}
	}

	checkCategory = function(){
		var flag = true;
		$(".category").each(function(){				
			if ($(this).attr("value") == "선택")
			{
				$(this).focus();
				flag = false;
				return false;
			}			
		});
		return flag;
	}	

	Event_Notice_Write = function(el){
		if (el == "remote") {
			el = $("#noticeform .box li:nth-child(2) a");
		}		
		var status = $(el).parent().prev().children(".textarea").attr("value");						
		var checkstatus = $('[name=select]:checked').next().html();
		if (status > 140) {
			//alert("140글자를 초과할 수 없습니다.");
			//return false;
		}
		if (status == "") {		
			alert('글 내용을 적어주세요');
			return false;
		}		
		if (confirm('등록하시겠습니까?'))
		{
			$("#rt").unbind();				
			
			status = checkstatus + " " + status;
			var parent = $(el).parent();
			$(parent).html("<img src='/img/search/loader4.gif' alt='updating' style='margin: 30px 0pt 0pt 25px;'>");			
			
			
			new Ajax.Request('/event/event_notice_write.html',{
				parameters:{						
					status:status
				},
				onSuccess:function(res){	
					$(parent).html("<a onclick='Event_Notice_Write(this)' href='javascript:void(0);'><img alt='글쓰기' src='/img/event/event_btn_write.gif' style='margin: 10px 0pt 0pt 5px;'/></a>");
					if (res.responseText == "1") {
						alert("등록되었습니다");
					}		
					$("#rt").bind("click",function(){ Event_Notice_Write('remote'); });
				},
				onFailure:function(res){				
									
				}
			});		
		}
	}

	Event_Follow = function(){
		new Ajax.Request('/event/event_follow.html',{
			parameters:{						
				
			},
			onSuccess:function(res){	
				alert(res.responseText);
			},
			onFailure:function(res){				
								
			}
		});		
	}

	Show_Popup = function(el){
		var top = $(el).position().top - 150;
		var left = $(el).position().left - 150;
		$('#write_popup').css("top", top);
		$('#write_popup').css("left", left);
		$('#write_popup').css("position", "absolute");
		$('#write_popup').fadeIn();		
	}

	Show_Popup2 = function(el){
		var top = $(el).position().top - 250;
		var left = $(el).position().left - 150;
		$('#write_popup2').css("top", top);
		$('#write_popup2').css("left", left);
		$('#write_popup2').css("position", "absolute");
		$('#write_popup2').fadeIn();		
	}	

	Event_MyInterest = function(el){
		var idx = $(el).parent().parent().parent().attr("id");		
		new Ajax.Request('/event/Event_interest.html',{
			parameters:{	
				idx:idx
			},
			onSuccess:function(res){
				if (res.responseText == "0"){
					alert('이미 등록하였습니다');
				}
				else{
					alert('등록 하였습니다.');				
				}
			},
			onFailure:function(res){				
							
			}
		});		
	}	
	
	checkform = function(){
		if ($("#title").attr("value") == ""){
			$("#title").focus();
			return false;
		}
		if ($("#company").attr("value") == ""){
			$("#company").focus();
			return false;
		}
		if (!checkCategory()){
			alert("카테고리를 선택하세요");
			return false;
		}
		if ($("#year").attr("value") == "" || $("#year").attr("value").length > 4){
			alert("날짜를 정확히 입력해주세요");
			$("#year").focus();
			return false;
		}
		if ($("#month").attr("value") == "--"){
			alert("날짜를 선택해주세요");
			$("#month").focus();
			return false;
		}	
		if ($("#day").attr("value") == "--"){
			alert("날짜를 선택해주세요");
			$("#day").focus();
			return false;
		}	
		if ($("#year2").attr("value") == "" || $("#year2").attr("value").length > 4){
			alert("날짜를 정확히 입력해주세요");
			$("#year2").focus();
			return false;
		}
		if ($("#month2").attr("value") == "--"){
			alert("날짜를 선택해주세요");
			$("#month2").focus();
			return false;
		}	
		if ($("#day2").attr("value") == "--"){
			alert("날짜를 선택해주세요");
			$("#day2").focus();
			return false;
		}	
		if ($("#year3").attr("value") == "" || $("#year3").attr("value").length > 4){
			alert("날짜를 정확히 입력해주세요");
			$("#year3").focus();
			return false;
		}
		if ($("#month3").attr("value") == "--"){
			alert("날짜를 선택해주세요");
			$("#month3").focus();
			return false;
		}	
		if ($("#day3").attr("value") == "--"){
			alert("날짜를 선택해주세요");
			$("#day3").focus();
			return false;
		}	
		if (!giftboxcheck()){	
			alert("경품정보를 입력해주세요");
			return false;
		}
		if ($("#target").attr("value") == ""){
			alert("대상을 입력해주세요");
			$("#target").focus();
			return false;
		}
		/*if ($("#place").attr("value") == ""){
			alert("장소를 입력해주세요");
			$("#place").focus();
			return false;
		}*/
		if ($("#city").attr("value") == "선택"){
			alert('지역을 선택하세요');
			$("#city").focus();
			$("#city").parent().children(".errMsg").html("지역을 선택하세요");
			return false;
		}/*
		if ($("#site").attr("value") == ""){
			alert("이벤트 url을 입력해주세요");
			$("#site").focus();
			return false;
		}*/
		if ($("#phone").attr("value") == ""){
			alert("연락처를 입력해주세요");
			$("#phone").focus();
			return false;
		}	
	}

	follow = function(screen_name, el){
		if (confirm("팔로우 하시겠습니까?")){
			var targetEl = $(el);
			$(targetEl).hide();
			$("<img id='progress' src='/img/mylink/loading_square.gif' style='width:45px;height:20px;vertical-align:middle' />").insertAfter($(targetEl));
			var mode = "follow";
			var _data = {
				mode:mode,
				screen_name:screen_name
			}
			var _url = "/appstore/appstore_process.html";
			$.ajax({
				type: "POST",
				url: _url,
				data: _data,
				success: function(msg){
					alert(msg);
					$("#progress").remove();
					$(targetEl).show();
				},
				error: function(){
					alert("error!!!");
				}
			});		
		}		
	}

	rate_btn_click = function(){
		$(".select_star").show();
	}

	select_star_li_click = function(){
		var background = $("p",this).css("background-image");
		background = background + " " + $("p",this).css("background-repeat");
		background = background + " " + $("p",this).css("background-attachment");
		background = background + " 6px 2px"// + $("p",this).css("background-position");
		background = background + " " + $("p",this).css("background-color");
		$(".rate_btn").html("").css("background",background);
		var movie_rate = $(this).attr("class").replace("star","");
		$("#movie_rate").val(movie_rate);
		$(".select_star").hide();
	}

	movie_rate_init = function(){
		$(".rate_btn").bind("click",rate_btn_click);
		$(".select_star li").bind("click",select_star_li_click);
	}

})(jQuery);
