var OBJAVY_AJAX_URL = "/as/modules/objavy/";

jQuery(document).ready(function($) {
	$("#objavy_category1_select, #objavy_category2_select").change(function() {
		_objavy_category_select_click($, this);
	});
	
	$(".objavy_remove_attach").click(function() {
		var $this = $(this);
		var parts = $this.attr('id').split('_');

		$.getJSON(OBJAVY_AJAX_URL + '?a=remove_attach&id='+parts[1], function(json) {
			if (json && json.status) {
				if (json.status==-1) {
					window.location.href = window.location.href;
					return;
				}
				$this.parent().remove();
			}
		});
		return false;
	});
});

function _objavy_category_select_click($, obj, section) {
	var $this = $(obj);
	if ($this.attr('id')=='objavy_category1_select') {
		var level = 2;
		$('#arrow2').hide();
		$('#objavy_category3_select').hide();
		_select_clear(document.getElementById('objavy_category3_select'));
	} else if ($this.attr('id')=='objavy_category2_select') {
		var level = 3;
	}
	
	var target_select = document.getElementById('objavy_category'+level+'_select');
		$(target_select).hide();
	$('#arrow'+(level-1)).hide();
		
	_select_clear(target_select);
	if ($this.val()!=0) {
		$.getJSON(OBJAVY_AJAX_URL + '?a=get_categories&section='+section+'&parent=' + $this.val(), function(json) {
			if (json) {
				_select_set_options(target_select, json);
				$(target_select).show();
				$('#arrow'+(level-1)).show();
			}
		});
	}
}

function objavy_add_attach() {
	var container = document.getElementById('attaches');
	var attaches = container.getElementsByTagName('DIV');
	var max = 0;
	for(var i=0; i<attaches.length; i++) {
		 parts = attaches[i].id.split('_');
		 if (parts[0]=='attach')
			max = parseInt(parts[1]);
	}

	var new_attach = document.createElement('DIV');
	new_attach.setAttribute('id', 'attach_'+(max+1));
	new_attach.setAttribute('class', 'attach');
	
	var new_file = document.createElement('INPUT');
	new_file.setAttribute('type', 'file');
	new_file.setAttribute('name', 'file_'+(max+1));
	
	new_attach.appendChild(new_file);
	container.appendChild(new_attach);
}

function objavy_video_hideshow() {
	var el_pm = document.getElementById("objavy-video-plusminus");
	var el_div = document.getElementById("objavy-video-div");
	if (el_div.style.display=="none") {
		el_div.style.display = "block";
		el_pm.innerHTML = "-";
	} else {
		el_div.style.display = "none";
		el_pm.innerHTML = "+";
	}
}

function objavy_video_delete() {
	if (!jQuery) return false;
	var $ = jQuery;
	$("#objavy-video-delete").hide();
	$("#objavy-video-container").html("");
	$("#as_Video").val("");
}

function objavy_video_load() {
	if (!jQuery) return false;
	var $ = jQuery;
	var code = document.getElementById("as_Video").value;
	$.ajax({
		url: OBJAVY_AJAX_URL,
		dataType: "json",
		data: {a: "load_video", code: code},
		type: "POST",
		success : function(json) {
			if (json && json.status) {
				jQuery("#objavy-video-delete").show();
			} else {
				jQuery("#objavy-video-delete").hide();
			}
			jQuery("#objavy-video-container").html(json.code);
		}
	});
}

function objavy_add_quote(post_id, username) {
	var message_name = 'posttext_' + post_id;
	var theSelection = '';
	var divarea = false;
	divarea = document.getElementById(message_name);

	// Get text selection - not only the post content :(
	if (window.getSelection)
	{
		theSelection = window.getSelection().toString();
	}
	else if (document.getSelection)
	{
		theSelection = document.getSelection();
	}
	else if (document.selection)
	{
		theSelection = document.selection.createRange().text;
	}

	if (theSelection == '' || typeof theSelection == 'undefined' || theSelection == null)
	{
		theSelection = divarea.innerHTML.replace(/<br>/ig, '\n');
		theSelection = theSelection.replace(/<br\/>/ig, '\n');
		theSelection = theSelection.replace(/&lt\;/ig, '<');
		theSelection = theSelection.replace(/&gt\;/ig, '>');
		theSelection = theSelection.replace(/&amp\;/ig, '&');
	}

	if (theSelection)
	{
		insert_text('[quote="' + username + '"]' + theSelection + '[/quote]');
	}

	return;
}
