$(document).ready(function(){
	
	function nextClick(handler){
		$("#next").colorbox({width:"890px", height:"680px", href:"/company/add", form:"#form_add", overlayClose:false},function(){
			typeof handler == "function" ? handler() : "";
		});
	}
	
	function saveSelected(){
		$("#activity_save").bind("click",function(){
			$("#activities").empty();
			$(".selected_cats > p").each(function(){
				var id = $(this).attr("id");
				var title = $("#title_"+id).text();
				var appends = '<p style="margin:15px 0px;"><a href="/companies/'+id+'" class="acts_href">'+title+'</a></p>';
				appends += '<input type="hidden" name="activity['+id+']" value="'+id+'" />';
				$("#activities").append(appends);
			});
			$("#cboxClose").click();
		});
	}
	
	function step2(end){
		var disabled = false;
		var checkCounts = function(counts){
			$("#counts").text(counts);
			if (counts <= 0){
				$(".child_get").attr("disabled",true);
				$(".child_get").filter(":checked").attr("disabled",false);
				disabled = true;
			}
			else if (disabled){
				disabled = false;
				$(".child_get").attr("disabled",false);
			}
		};
		
		var selectedClick = function(){
			$(".selected_cats > p").unbind().bind("click",function(){
				var id = $(this).attr("id");
				$(this).remove();
				$("#check_"+id).attr("checked", false);
				checkCounts(++count);
			});
		};
		$(".parent").bind("click",function(){
			if ($(this).next().is(":visible")){
				$(this).next().hide();
				$(this).children("img").attr("src","/img/catalog/arrow_close.png");
			}
			else{
				$(this).next().show();
				$(this).children("img").attr("src","/img/catalog/arrow_open.png");
			}
		});
		$(".child_get").bind("click",function(){
			var val = $(this).val();
			if ($(this).attr("checked")){
				var text = $(this).next().text();
				var hidden = "<input type='hidden' name=sel["+val+"] value='"+val+"' />";
				$(".selected_cats").append("<p id='"+val+"'>"+text+hidden+"</p>");
				selectedClick();
				checkCounts(--count);
			}
			else{
				$(".selected_cats > #"+val).remove();
				checkCounts(++count);
			}
		});
		checkCounts(count);
		selectedClick();
		if (end && typeof end == "function"){
			end();
		}
		else{
			nextClick();
		}
	}
	
	$(".addCompany").colorbox({width:"890px", height:"680px", overlayClose:false},function(){
		nextClick(step2);
	});
	
	$(".delCompany").bind("click",function(){
		var id = $(this).attr("id").split("_")[1];
		var removeHandler = function(response){
			$("#tr_"+response.id).remove();
		};
		sendRequest("/ajax/deleteCompany", {id:id}, removeHandler);
	});
	
	$("#activity_change").colorbox({},function(){
		step2(saveSelected);
	});
	
	$("#add_phone").bind("click",function(){
		var id = Math.round(Math.random() * 100);
		var app_phone = "<p><input type='text' name='phone["+id+"]' value='' />&nbsp;&nbsp;&nbsp;&nbsp;";
		app_phone += "<span class='del_phone pointer' id='delPhone_"+id+"'>X</span></p>";
		$(this).before(app_phone);
		delPhone();
	});
	
	var delPhone = function(){
		$(".del_phone").unbind().bind("click",function(){
			var id = Number(0);
			var current = this;
			try{
				id = parseInt($(this).attr("id").split("_")[1]);
			}
			catch(e){
				id = 0;
			}
			if (id){
				$.post("/ajax/deletePhone",{"phone":id}, function(){
					alert("ok");
				});
			}
			$(current).parent().remove();
		});
	};
	delPhone();
});
