function openCompany() {
	$("#company-hd-open").fadeOut();
	$("#company-hd-close").fadeIn();
	$("#company-hd-content-bd").slideDown();
	return false;
}
function closeCompany() {
	$("#company-hd-open").fadeIn();
	$("#company-hd-close").fadeOut();
	$("#company-hd-content-bd").slideUp();
	return false;
}

function entry_box_tab_click(type) {
	$("#entry_box li").removeClass("selected");
	$("#entry_box_tab_"+type).addClass("selected");
	
	$("#entry_box_img").removeClass();
	$("#entry_box_img").addClass("entry_box_img_"+type);
	
	//$(".entry_box_tabs .info").hide();
	//$("#entry_box_form_info_" + type).fadeIn();
	
	
	$("#zoom_topic_type").val(type);

}

function zoom_new_entry() {
	$("#zoom_topic_text").focus();
	$("#zoom_topic_text").effect("highlight", {}, 1000);
}


function overview_tabs_click(type) {
	$("#overview_tabs li a").removeClass("current");
	$("#overview_tabs_"+type + " a").addClass("current");
	
	$("#topics_sort").val(type);
	
	topics(1);

}

function topics(page) {

	$("#loading_topics").show();
	
	var sort = $("#topics_sort").val();
	var company_id = $("#zoom_topic_company_id").val();
	$.ajax({
		url: ajax_url + "?action=zoom_topics&company_id="+company_id+"&sort="+sort+"&page="+page,
		cache: false,
		success: function(html){
			$("#loading_topics").hide();
			$("#topics").html(html);
		}
	});
	
	return false;
}

var zoom_topic = [["company_id","TXT"], ["text","TXA"], ["type","TXT"], ["tag","SEL"]];

function addTopic() {

	if(!control_login()) return false;
	
	var query = generateSingleQuery("zoom_topic");
	
	$("#entry_box_loading").show();
	
	$.ajax({
		type: 'POST',
		url: ajax_url + "?action=zoom_topic_add",
		data: query,
		cache: false,
		success: function(html){
			$("#entry_box_loading").hide();
			if(hasError(html)) {
				showErrorsAlert(html);
			} else {
				$("#zoom_topic_text").val("");
				showMessage(html);
				topics(1);
			}
		}
	});
	
	return false;
}


/********* SURVEY ****************/
var survey_current = 0;
function showSurvey(survey_id, company_id, title) {

	if(!control_login()) return false;
	
	Boxy.load(ajax_url + "?action=zoom_survey&survey_id="+survey_id+"&company_id="+company_id, {title: title, modal:false, closeable:true, unloadOnHide:true});
	survey_current = 0;
	return false;
}

function survey_pre() {
	if(survey_current <= 0) {
		return false;
	}
	survey_current--;
	survey_render();
}

function survey_next_question() {
	var survey_total = $("#survey_total").html();
	if(survey_current >= survey_total - 1) {
		return false;
	}
	survey_next();
}

function survey_next() {
	if(!survey_control()) {
		showMessage(zoom_survey_not_reply);
		return false;
	}
	survey_current++;
	survey_render();
}

function survey_finish() {

	if(!survey_control()) {
		showMessage(zoom_survey_not_reply);
		return false;
	}
	
	//@TODO
	//Datayi olustur
	var survey_total = $("#survey_total").html();
	
	var query = "";
	for(var i=0; i<survey_total; i++) {
		query += addParameter("q_" + i, $("#q_id_"+i).val());
		query += addParameter("c_" + i,  survey_get_data(i));
	}
	query += addParameter("len", survey_total);
	query += addParameter("survey_id", $("#survey_id").val());
	query += addParameter("company_id", $("#company_id").val());
	
	$("#survey_loading").show();
	
	$.ajax({
		type: 'POST',
		url: ajax_url + "?action=zoom_survey_add",
		data: query,
		cache: false,
		success: function(html){
			$("#survey_loading").hide();
			$("#slidebar-survey").hide();
			$("#survey").html(html);
			//Boxy.get("#survey").hide();
			//showMessage(html);
		}
	});
	
	return false;
	
	
	
}

function survey_control() {
	var data = survey_get_data(survey_current);	
	if(data == null || data == undefined || data == "undefined" || data == "null" || data == "") {
		return false;
	} else {
		return true;
	}
}

function survey_get_data(no) {
	if($("#t_" + no).val() == "SELECTIVE") {
		return $("#q_" + no + " input:checked").val();
	} else {
		return $("#q_" + no + " select").val();
	}
}

function survey_render() {
	var survey_total = $("#survey_total").html();
	
	if(survey_current >= survey_total - 1) {
		$("#survey_next").hide();
		$("#survey_finish").show();
	} else {
		$("#survey_next").show();
		$("#survey_finish").hide();
	}
	
	$("#survey_title").attr("src",project_url+"/images/zoom/q"+(survey_current+1)+".png");
	$("#survey_current").html(survey_current+1);
	$("#survey .content").hide();
	$("#q_" + survey_current).show();
}

/*********************************/

$(document).ready(function() {
	$("#zoom_topic_text").focus(function() { if(!control_login()) return false; } );
});



/********* COMMENTS **************/

function showComment(id) {
	if(!control_login()) return false;
	$("#comment_add_" + id).show();
}

function hideComment(id) {
	$("#comment_add_" + id).hide();
}

function showAllComments(id) {
	$("#comments_show_all_" + id).hide();
	
	$("#like_loading_" + id).show();
	
	$.ajax({
		url: ajax_url + "?action=zoom_comments&topic_id="+id,
		cache: false,
		success: function(html) {
			$("#like_loading_" + id).hide();
			$("#comments_"+id).html(html);
		}
	});
	
	return false;
}

function addComment(id) {
	$("#comment_loading_" + id).show();
	return false;
}

function addLike(id) {

	if(!control_login()) return false;
	
	$("#like_loading_" + id).show();
	
	$.ajax({
		url: ajax_url + "?action=zoom_topic_like_add&topic_id="+id,
		cache: false,
		success: function(html){
			$("#like_loading_" + id).hide();
			$("#like_" + id).html(general_thanks);

			if(html!=null && html!=""){
				$("#like_count_" + id).hide();
				$("#like_count_" + id).html(html);
				$("#like_count_" + id).fadeIn();
			}
			
			

		}
	});
	
	return false;
	
}

function addComment(id) {

	$("#comment_loading_" + id).show();
	var text = encodeURIComponent($("#zoom_comment_text_"+id).val());
	
	$.ajax({
		url: ajax_url + "?action=zoom_comment_add&topic_id="+id+"&text="+text,
		cache: false,
		success: function(html) {
			$("#comment_loading_" + id).hide();
			if(html!=null && html!=""){
				$("#zoom_comment_text_"+id).val("")
				hideComment(id);
				showAllComments(id);
			}
		}
	});
	
	return false;
	
}






/*******************************/ 

function companiesRight() {
	
	var total = Number($("#current_company_count").html());
	var page = Number($("#current_company_page").html());
	
	
	
	if(page >= total) return false;
	
	page++;
	return companies(page);
	
}

function companiesLeft() {
	var page = Number($("#current_company_page").html());
	
	if(page <= 1) return false;
	
	page--;
	return companies(page);
}


function companies(page) {

	$("#companies_loading").show();
	
	var sector = $("#zoom_sector").val();
	
	$.ajax({
		url: ajax_url + "?action=zoom_companies&page="+page+"&sector="+sector,
		cache: false,
		success: function(html){
			$("#companies_loading").hide();
			$("#current_company_page").html(page);
			$("#zoom_companies").html(html);
			$("#current_company_count").html($("#inner_companies_count").val());
		}
	});
	
	return false;
}

function zoom_companies_sector_change() {
	companies(1);
}

function zoom_company_search_change() {
	var company_id = $("#zoom_company_search_id").val();
	var company_name = $("#zoom_company_search").val();
	
	$.ajax({
		url: ajax_url + "?action=zoom_company_permalink&ID="+company_id+"&name="+company_name,
		cache: false,
		success: function(html){
			window.location = html;
		}
	});
	
}

function zoom_top_sector_change() {
	zoom_top();
}

function zoom_top() {

	$("#top_loading").show();
	
	var sector = $("#zoom_top_sector").val();
	
	$.ajax({
		url: ajax_url + "?action=zoom_top&sector="+sector,
		cache: false,
		success: function(html){
			$("#top_loading").hide();
			$("#zoom_top").html(html);
		}
	});
	
	return false;
}


/*********** hr *************/
function showAnswer(id) {
	$("#answer_add_" + id).show();
}

function hideAnswer(id) {
	$("#answer_add_" + id).hide();
}

function addAnswer(id) {
	$("#answer_loading_" + id).show();
	return false;
}

function addAnswer(id) {

	$("#answer_loading_" + id).show();
	var text = encodeURIComponent($("#zoom_topic_answer_text_"+id).val());

	$.ajax({
		url: ajax_url + "?action=hr_zoom_topic_answer_add&topic_id="+id+"&text="+text,
		cache: false,
		success: function(html) {
			$("#answer_loading_" + id).hide();
			if(html!=null && html!=""){
				var current_page = $("#pagination .current").html();
				current_page = (current_page == null) ? 1 : current_page;
				topics(current_page);
			}
		}
	});
	
	return false;
	
}

function rejectTopic(id) {

	if (!confirm(delete_confirmation)) return false;
	
	$.ajax({
		url: ajax_url + "?action=hr_rejectTopic&id="+id,
		cache: false,
		success: function(html){
			var current_page = $("#pagination .current").html();
			current_page = (current_page == null) ? 1 : current_page;
			topics(current_page);
		}
	});
	return false;
	
}


function rejectComment(topic_id, id) {

	if (!confirm(delete_confirmation)) return false;
	
	$.ajax({
		url: ajax_url + "?action=hr_rejectComment&id="+id,
		cache: false,
		success: function(html){
			showAllComments(topic_id);
		}
	});
	return false;
	
}


function rejectAnswer(id) {

	if (!confirm(delete_confirmation)) return false;
	
	$.ajax({
		url: ajax_url + "?action=hr_rejectAnswer&id="+id,
		cache: false,
		success: function(html){
			var current_page = $("#pagination .current").html();
			current_page = (current_page == null) ? 1 : current_page;
			topics(current_page);
		}
	});
	return false;
	
}
