var ajax_loader = '<img src="/_style/images/ajax-loader-small.gif" style="border: none;" />';




// helper function for displaying a standard error message dialog
function display_error_dialog(message, height){
	$("body").append('<div id="uidialog_error"></div>');
	$("#uidialog_error").html(message).dialog({
		resizable: false, overlay: { opacity: 0.5, background: "#EE7777" }, modal: true, show: "fade", hide: "fade",
	   	height: height,
	   	title: "Es ist ein Fehler aufgetreten",
		buttons: {
	  		"OK": function(){
	 			$(this).dialog("close");
	 			$("#uidialog_error").remove();
	 		}
	   	}
	});
	setTimeout('$("#uidialog_error").dialog("destroy").remove();', 5000);
}




// helper function for displaying a standard success message dialog
function display_success_dialog(message, height){
	$("body").append('<div id="uidialog_success"></div>');
	$("#uidialog_success").html('<p class="success_message">'+message+'</p>').dialog({
		resizable: false, overlay: { opacity: 0.5, background: "#77EE77" }, modal: true, show: "fade", hide: "fade",
	   	height: height,
	   	title: "Aktion erfolgreich",
		buttons: {
	  		"OK": function(){
	 			$(this).dialog("close");
	 			$("#uidialog_success").remove();
	 		}
	   	}
	});
	setTimeout('$("#uidialog_success").dialog("destroy").remove();', 2000);
}




// helper for counting characters in textbox
function cnt_chars(id){
	var text = $("#"+id+" textarea").val();
	var len = (text) ? text.length : '0';
	var max = $("#"+id+" .statusbox span:last").html();

	if (len > max){
		st = this.scrollTop;
		$("#"+id+" textarea").val(text.substr(0, max));
		this.scrollTop = st;
		len = max;
	}

	$("#"+id+" .statusbox span:first").html(len);
}




// helper to activate star rating wherever it is
$("document").ready(function(){
	init_star_rating();
});



function init_star_rating(){
	$("div.star_rating").each(function(){
		rating_element = this;
		values = $(this).html();
		$(this).html("");

		rv = values.split('#');
		rid = $(this).attr("id");

		$('#'+rid).rater("", {
			star_size	: 16,
			curvalue	: (rv[2]) || 0,
			rating_id	: rv[0],
			rating_t    : rv[1],
			rater_id	: rid,
			click       : $('#'+rid).is(".noop") ? false : function(rating_value, rid, cid, tid){
			           		$('#'+rid).html(ajax_loader);
			           		$.post('/_ajax/ajax.common.php', { content_id : cid, type_id : tid, rating : rating_value }, function(ret){
								$('#'+rid).fadeOut('slow', function(){
				                    $('#'+rid).html(ret).fadeIn();
				                });
							});
			              }
		});
	});
}




/*
 * jQuery plugin: fieldSelection - v0.1.0 - last change: 2006-12-16
 * (c) 2006 Alex Brem <alex@0xab.cd> - http://blog.0xab.cd
 */

(function() {
	var fieldSelection = {
		getSelection: function() {
			var e = this.jquery ? this[0] : this;
			return (
				/* mozilla / dom 3.0 */
				('selectionStart' in e && function() {
					var l = e.selectionEnd - e.selectionStart;
					return { start: e.selectionStart, end: e.selectionEnd, length: l, text: e.value.substr(e.selectionStart, l) };
				}) ||

				/* exploder */
				(document.selection && function() {

					e.focus();

					var r = document.selection.createRange();
					if (r == null) {
						return { start: 0, end: e.value.length, length: 0 }
					}

					var re = e.createTextRange();
					var rc = re.duplicate();
					re.moveToBookmark(r.getBookmark());
					rc.setEndPoint('EndToStart', re);

					return { start: rc.text.length, end: rc.text.length + r.text.length, length: r.text.length, text: r.text };
				}) ||

				/* browser not supported */
				function() {
					return { start: 0, end: e.value.length, length: 0 };
				}

			)();
		},

		replaceSelection: function() {
			var e = this.jquery ? this[0] : this;
			var text = arguments[0] || '';

			return (

				/* mozilla / dom 3.0 */
				('selectionStart' in e && function() {
					e.value = e.value.substr(0, e.selectionStart) + text + e.value.substr(e.selectionEnd, e.value.length);
					return this;
				}) ||

				/* exploder */
				(document.selection && function() {
					e.focus();
					document.selection.createRange().text = text;
					return this;
				}) ||

				/* browser not supported */
				function() {
					e.value += text;
					return this;
				}

			)();
		}
	};

	jQuery.each(fieldSelection, function(i) { jQuery.fn[i] = this; });

})();
