var popupStatus = 0;
//centering popup
function centerPopup(){
//request data for centering
var windowWidth = document.documentElement.clientWidth;
var windowHeight = document.documentElement.clientHeight;
var popupHeight = $("#asciientryform").height();
var popupWidth = $("#asciientryform").width();
//centering
$("#asciientryform").css({
"position": "absolute",
"top": windowHeight/2-popupHeight/2,
"left": windowWidth/2-popupWidth/2
});
//only need force for IE6

$("#backgroundPopup").css({
"height": windowHeight
});

}
//loading popup with jQuery magic!
function loadPopup(){
//loads popup only if it is disabled
if(popupStatus==0){
$("#backgroundPopup").css({
"opacity": "0.7"
});
$("#backgroundPopup").fadeIn("slow");
$("#asciientryform").fadeIn("slow");
popupStatus = 1;
}
}


//disabling popup with jQuery magic!
function disablePopup(){
//disables popup only if it is enabled
if(popupStatus==1){
$("#backgroundPopup").fadeOut("fast");
$("#asciientryform").fadeOut("fast", function(){$("#postmenu").show();$("#ascii-quote").hide();});

popupStatus = 0;
}
}

function post_tags($this){
			var theinput = $this.parent().find("input");
			var tags = theinput.val();
			
			if (tags.length > 2) {
				$this.hide();
				$this.parent().find("input").attr("disabled","disabled");
				var thespan = $this.parent().parent().find("span");
				
				var id = $this.parent().parent().parent().find(".datum").attr("id");
			
				$.post("http://www.snart.cc/functions/ajax.php?f=addtags", { id: id, tags: tags },
					function(data){ 
						thespan.hide();
						if (thespan.text() == "Noch keine vorhanden" || thespan.text() == "None"){
							thespan.text(data.taglist);
						} else if (data.taglist) {
							thespan.append(", " + data.taglist);	
						}
						thespan.fadeIn();
						theinput.val("");
						theinput.removeAttr("disabled");
						theinput.parent().find("a").show();
					}, "json" );
			}
}
if (window.navigator.userAgent.indexOf("MSIE") > -1) { var _ie = true; } else { var _ie = false; }
$(document).ready(function(){
	if (!_ie){
		$(".asciibutton, .ascii-box, .pages > *").corner("5px");
	}
 	if ($("#l").val() == "en"){
		var l = {
					"thanks" : "Thank You",
					"notags" : "None"
				};
		var $rateexpl = ["horrible", "bad", "average", "good", "super"];
	} else {
		var l = {
					"thanks" : "Vielen Dank",
					"notags" : "Noch keine vorhanden"
				};
		var $rateexpl = ["lausig", "schwach", "mittelmäßig", "gut", "super"];
	}
	var skstd = $("#searchqry").val();
	
	$("#searchqry").not(".searching").click(
		function(){
			if($(this).val() == skstd) $(this).val("");
		}
	);
	
	$("#searchqry").not(".searching").blur(
		function(){
			if($(this).val() == "") $(this).val(skstd);
		}
	);
	
	$("#search a").click(
		function(){
			$("form#searchform").submit();
		}
	);
	
	$("textarea.theascii").autogrow();
	//LOADING POPUP
	//Click the button event!
	$("#entrybutton").click(function(){
	//centering with css
		centerPopup();
	//load popup
		loadPopup();
	});
	
	
	//CLOSING POPUP
	//Click the x event!
	$("#formclose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
	if (e.keyCode==27 && popupStatus==1){
		disablePopup();
	}
	});
	
	$("#postmenu a").click(
			function(){
				if ($(this).parent().hasClass("ascii")){
					var kind = "ascii";
				} else {
					var kind = "spruch";
				}
				if (!$(this).parent().hasClass("pic")){
					$(this).parent().parent().hide();
					$(this).parent().parent().next().show();
					$(this).parent().parent().next().find("input#kind").val(kind);
				}
				
			});
	
	var menu_item;;
	
	/*function specifickeeper_sd($this,mitem){
		if (menu_item == mitem) $this.find(".specifickeeper").slideDown(100);
	}
	function specifickeeper_su($this){
		$this.find(".specifickeeper").slideUp(100);
	}*/
	$("#categorymenu li").hover(
			function(){
				$(this).find(".specifickeeper").show();
				//menu_item = $("#categorymenu li").index(this);
				//setTimeout(specifickeeper_sd,100,$(this),menu_item);
				
			},
			function(){
				//$(this).find(".specifickeeper").slideUp(100);
				//setTimeout(specifickeeper_su,99,$(this));
				$(this).find(".specifickeeper").hide();
				
			});
	$("img.notworking").css({'opacity' : '0.2'});
	
	$("textarea.theascii").live("click",function(){$(this).select();});
	
	$(".bottom a.addtagslink").live("click",
		function(){
			$(this).hide();
			$(this).next().fadeIn("fast");
		});
	
	$(".bottom .addtags input").removeAttr("disabled").val("");
	
	
	$(".bottom .addtags a").live("click",
		function(){
			var $this = $(this);
			post_tags($this);
			
			
		});
	
	$('.bottom .addtags input').keypress( function(e) {
					var key = e.charCode || e.keyCode || 0
					if (key == 13) {
						var $this = $(this).parent().find("a");
						post_tags($this);
						
					};
				});
	
	var oldcaption;
	var rated = false;
	var over = false;
	var clicked = false;
	
	$('.datum .ratestar').live("mouseover",
		function(){
				
					if (!over) oldcaption = $(this).parent().html();
					over = true;
					if (!rated){
						var ratealt = $(this).parent().children(".ratestar").index(this);
						$(this).parent().children(".ratecount").hide();
						$(this).parent().children(".rateexpl").text($rateexpl[ratealt]);
						
						$(this).parent().children(".ratestar").attr("class","ratestar");
						$(this).addClass("on");
						$(this).prevAll().addClass("on");
					} else {
						$(this).parent().children(".rateexpl").text(l.thanks);
						
					}
					
					
	});
	
	$('.datum .ratestar').live("click",
		function(){
					
					if (!rated){
						var ratevalue = $(this).parent().children(".ratestar").index(this)+1;
						var id = $(this).parent().attr("id");
						$(this).parent().children(".rateexpl").text(l.thanks);
						
						$.post("http://www.snart.cc/functions/ajax.php?f=rate", { id: id, v: ratevalue } );
						clicked = true;
					} else {
						$(this).parent().children(".rateexpl").text(l.thanks);
						
						
					}
					
					
	});
	
	$('.datum').mouseleave(
		function(){
					if (!clicked) $(this).html(oldcaption);
					over=false;
					clicked = false;
					
	});
	
	/*
	 *
	 * SN Menü
	 *
	 */
	var overMenu = false;
	function menuSlideUp(){
		if (!overMenu) {
			$('.sndetails').slideUp();
			overMenu = false;
		}
	}
	$('li.sns').hover(
			function(){
				$(this).children('.sndetails').slideDown("fast");
			},
			function(){
				setTimeout(menuSlideUp, 300);
				
	});
	
	$('.sndetails').mouseover(
			function(){
				overMenu = true;
			}/*,
			function(){
				
				overMenu = false;
				setTimeout(menuSlideUp, 300);
				
	}*/);
	
	$('.sndetails').mouseout(function(){overMenu = false;setTimeout(menuSlideUp, 300);});
	
	//$('img').attr("alt","");
	
	
	
});/* TOTALEND */



