/**
 * Executed when document is finished loading elements into DOM
 */
$(document).ready(function() {

	//$("#levelmenu ul").hide();

	/**
	 * Display confirmation dialog whenever link
	 * with className "confirm" is clicked
	 */
	$(".confirm").click(function(){
		return confirm("Vai tiešām vēlies veikt šo darbību?");
	});

	if (typeof(category_id) != "undefined") {
		toggleCategory(category_id);
	}
	$("#fullform input[alt]").focus(function(){
		if ($(this).attr("alt").length > 0) {
			$("body").append('<div id="helpbox">'+$(this).attr("alt")+'</div>');
			var parent = $(this).parent();
			$("#helpbox").css("left", ($(parent).offset().left + $(parent).width()+ 4)+"px");
			$("#helpbox").css("top", ($(parent).offset().top - 1)+"px");
		}
	});

	$("#fullform input[alt]").blur(function(){
		$("#helpbox").remove();
	});

	$("input[tabindex=1]").focus();

	if ($("#flash-message").length > 0) {
		$("#flash-message").hide();
		setTimeout('$("#flash-message").slideDown("fast")', 500);
		var fm = setTimeout('$("#flash-message").slideUp("fast")', 10500);
		$("#flash-message").click(function(){
			clearTimeout(fm);
			$(this).slideUp("fast");
		});
	}

	$(".iconized .title a").click(function(){
		var parent = $(this).parent().parent();

		$(parent).find(".title a").removeClass("active");
		$(this).addClass("active");

		$(parent).find(".tab-content").hide();
		$("#tab-content-"+$(this).attr("rel")).show();

		$(parent).find(".title a").each(function(){
			$(parent).find(".page-icon").removeClass($(this).attr("rel"));
		});

		$(parent).find(".page-icon").addClass($(this).attr("rel"));
	});

	$(".email").each(function(i) {
		$(this).replaceWith(makeEmailLink($(this).text()));
	});
});

/**
 * Reply to comment
 *
 * todo: move text cursor to the end
 *
 * @param int id Comment ID
 */
function replyComment(id) {
	$("#CommentText").val("[q]"+$("#quote-"+id).text()+"[/q]").focus();
}

/**
 * Reply to message
 *
 * @param int user_id User ID
 * @return boolean false
 */
function replyMessage(user_id) {
	$("#MessageReceiverId").val(user_id);
	$("#MessageText").focus();
	return false;
}

/**
 * todo: description
 * todo: convert to jquery
 * todo: ja iezīmēts teksts, tad nenodzēšam to bet iekļaujam iekš tagiem
 */
function insertText(val, target_id) {
  var el = document.getElementById(target_id);

  if (document.selection) // IE
  {
    el.focus();
    sel = document.selection.createRange();
    sel.text = val;
  }
  else if ((el.selectionStart) || (el.selectionStart == 0)) // MOZILLA/NETSCAPE
  {
    var startPos = el.selectionStart;
    var endPos = el.selectionEnd;
    var i=startPos + val.length/2;
    el.value = el.value.substring(0, startPos) + val + el.value.substring(endPos, el.value.length);
    el.selectionStart = i;
    el.selectionEnd = i;
  }
  else // other
  {
    el.value += val;
  }

  el.focus();
}

/**
 * Toggle category in level menu
 *
 * @param int id Category ID
 */
function toggleCategory(id) {
	var visible = $("#category-"+id).is(":visible");

	$("#levelmenu ul").hide();

	if (!visible) {
		$("#category-"+id).show();
	}
}

/**
 * Print email link (protection from spambots)
 * If javascript is disabled, nothing is visible
 *
 * @param string p1 Part 1 - this is username
 * @param string p2 Part 2 - this is domain and TLD
 * @param string name (optional) Display value
 */
function writemail(p1, p2, name) {
	if (typeof(name) == "undefined") {
		name = p1+"@"+p2;
	}
	document.write("<a href='mailto:"+p1+"@"+p2+"'>"+name+"</a>");
}

/**
 * Make email link
 *
 * @param string email Email
 * @return string
 */
function makeEmailLink(email) {
	return "<a href='mailto:"+email+"'>"+email+"</a>";
}