$(document).ready(function(){
	$("body noscript").remove();
	var tool = null;
	//
	imagePreview();
	//
	$('#katcon a').click(function(){
		var m = $(this).attr("id");
		if(m=="up")
		{
			$.post("ajax.php",{katss: "up"});
			$('#kats').slideUp(600,function(){$('#katcon a#up,#katcon a#down').toggle()});
		}
		if(m=="down")
		{
			$.post("ajax.php",{katss: "down"});
			$('#kats').slideDown(600,function(){$('#katcon a#up,#katcon a#down').toggle()});
		}
	});
	//
	$('#kats input').each(function(){
		var id = $(this).val();
		if($(this).is(':checked')){
			$('div#s_'+id+'.sub').show();
			if($('div#s_'+id+'.sub').is(':visible'))
				$('#sub,.subinfo').show();
		}
    });
	$('#kats input').change(function(){
		var id = $(this).val();
		if($(this).is(':checked')){
			$('div#s_'+id+'.sub').show();
			if($('div#s_'+id+'.sub').is(':visible'))
				$('#sub,.subinfo').show();
		}else{
			$('div#s_'+id+'.sub,#sub,.subinfo').hide();
			$('div#s_'+id+'.sub input').attr('checked', false);
		}
    });
	$('a#kats').click(function(){
		var kat_id   = $(this).attr("rel");
		var news	 = $('#news').html();
		var height	 = $('#news').height();
		$('#news').css({
			background: "#fff url(images/load.gif) no-repeat center",
			height	  : height
		}).fadeTo(400,0.5,function(){$(this).html('')});
		$.post("ajax.php", {
				kats: kat_id
		},function(data){
			resulta = data.split("|:");
			if(resulta[0])
			{
				$('a#kats').fadeTo(400,0);
				$('#news').css("background","#fff")
						  .css("height","150px")
						  .fadeTo(200,1,function(){
							  $(this).html("<img id='back' src='images/icons/remove.png' /><div class='kats'>"+resulta[1]+"</div>")
							  		 .find("#back").click(function(){
									$('#news').html(news).css("height",height+"px");
									$('a#kats').fadeTo(200,1);
							  })
						  });
			}
		});
	});
	//
	$('#suche input').keyup(function() {
		var suche 	 = $(this).val();
		var ff		 = $('#suche div');
		if(suche!="")
		{
			$.post("ajax.php", {
				suche: 	suche
			},function(data){
				result = data.split("|:");
				if(result[0])
				{
					if(result[1]=="")
						ff.html("keine Ergebnisse");
					else
						ff.fadeIn(100).html(result[1]);
				}
			});
		}
	}).mouseover(function(){
		var gg = $(this).val();				 
		$('body').click(function(){
			if(gg!='')
				$('#suche div').stop(this).fadeIn(200);
		});
	}).mouseout(function(){
		$('body').click(function(){
			$('#suche div').stop(this).fadeOut(200);
		});
	});
	//
	$(".pic").hover(function(){
			if($(".tool").is(':visible'))
				$(".tool").hide();
			tool = null;
			tool = $(this).find(".tool");
			var p_id = $(this).attr("id");
			if(tool.is(':hidden'))
				tool.show();
			tool.hover(function(){$(this).show()},function(){$(this).hide()});
		
			if(tool.attr("rel")=='false')
			{
					var button = tool.find("a.copy");
					var clip = new ZeroClipboard.Client();
						clip.glue(button[0]);
						clip.addEventListener("mouseover",	function(){							
								tool.show();
								setTimeout(function(){
									if(tool.is(':hidden'))
										tool.show();
								},10);
								button.addClass("hover");
						});
						clip.addEventListener("mouseout",	function(){
								button.removeClass("hover");
						});
						clip.addEventListener("complete",	function(client, text) {
								button.html('<div> &nbsp;Kopiert!</div><img src="images/icons/Check.png">');												
						});
					var inhalt = button.html();
					button.html('<img src="images/code_load.gif" class="load">');
					$.post("ajax.php", {
						gencode: 	"bb",
						p_id: 		p_id
					},function(data){
						result = data.split("|:");
						tool.attr("rel",'true');
						clip.setText(result[1]);
						button.html(inhalt);
					});
			}
	},function(){
	})
	//
	$('.code input, .code textarea').click(function(){
		if($(this).attr("rel")!=1)
			$(this).select().attr("rel","1");
	});
	//
	$("div#seiten a").hover(function(){
		$(this).find("img").toggle();
	},function(){
		$(this).find("img").toggle();
	});
	//
	$("button.load").each(function(){
		$(this).click(function(){
			$(this).replace("<img src='images/load.gif' />");
		})
	})
	//
	$(".choice").each(function(){
		var i_y = $(this).find("input#y");
		var i_n = $(this).find("input#n");
		var a_y = $(this).find('a#y');
		var a_n = $(this).find('a#n');			
		$(this).find('label').remove();
		$(this).find("input").hide();
		a_y.show();	
		a_n.show();
				
		if(i_y.is(':checked'))
			a_y.addClass("checked");
		if(i_n.is(':checked'))
			a_n.addClass("checked");
			
		a_n.click(function(){
			a_n.addClass("checked");
			a_y.removeClass("checked");
			i_n.attr("checked", "checked");
			i_y.removeAttr("checked");
		});
		a_y.click(function(){
			a_y.addClass("checked");
			a_n.removeClass("checked");
			i_y.attr("checked", "checked");
			i_n.removeAttr("checked");
		});
	});
	//
});
function insertAtCaret(areaId,text) { var txtarea = document.getElementById(areaId); var scrollPos = txtarea.scrollTop; var strPos = 0; var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ? "ff" : (document.selection ? "ie" : false ) ); if (br == "ie") { txtarea.focus(); var range = document.selection.createRange(); range.moveStart ('character', -txtarea.value.length); strPos = range.text.length; } else if (br == "ff") strPos = txtarea.selectionStart; var front = (txtarea.value).substring(0,strPos); var back = (txtarea.value).substring(strPos,txtarea.value.length); txtarea.value=front+text+back; strPos = strPos + text.length; if (br == "ie") { txtarea.focus(); var range = document.selection.createRange(); range.moveStart ('character', -txtarea.value.length); range.moveStart ('character', strPos); range.moveEnd ('character', 0); range.select(); } else if (br == "ff") { txtarea.selectionStart = strPos; txtarea.selectionEnd = strPos; txtarea.focus(); } txtarea.scrollTop = scrollPos; }
		
this.imagePreview = function(){
	$("a.thumb").hover(function(e){
		var rel	   = this.rel.split("*");
		$("body").append("<div id='preview'><span><img src='"+ rel[0] +"' /></span><div>"+ rel[1] +"</div></div>");
		genpos(e);			
		$("#preview").show();
    },function(){		
		$("#preview").remove();
    });
	$("a.thumb").mousemove(function(e){
		genpos(e);
	});
}
function genpos(e){		
	var yOffset 						= -50;
	var xOffset							= 40;		
	var bw								= jQuery(window).width();
	var bh								= jQuery(window).height();
	var pw								= $("#preview").width();
	var ph								= $("#preview").height();
	var y 								= e.pageY;
	var x								= e.pageX;
	
//	if(xOffset+x+pw > bw-xOffset)	var left  	= x + (bw - (xOffset+x+pw));
//	else 							var left 	= x+xOffset;
//	if(yOffset+y+ph > bh-yOffset)	var top  	= y + (bh - (y+ph));
//	else 							var top 	= y+yOffset;
	if(xOffset+x+pw > bw)			var left  	= x-pw-xOffset;
	else 							var left 	= x+xOffset;
	if(yOffset+ph+y > bh)			var top  	= y-ph-yOffset;
	else							var top	 	= y+yOffset;	

	$("#preview")
		.css("top",   top +"px")
		.css("left", left +"px")	
}
$.fn.pause = function(milli,type) {
	milli = milli || 1000;
	type = type || "fx";
	return this.queue(type,function(){
	var self = this;
		setTimeout(function(){
			$.dequeue(self);
		},milli);
	});
};
function book() {
	title = $(document).find("title").text();
	if(title=="")
		title = "JJGB - Mach´s einfach einzigartig!"
	url = location.href;
	if (window.sidebar)
		window.sidebar.addPanel(title, url,"");
	else if( window.external )
		window.external.AddFavorite( url, title);
	else if(window.opera && window.print)
	{
		var mbm = document.createElement('a');
		mbm.setAttribute('rel','sidebar');
		mbm.setAttribute('href',url);
		mbm.setAttribute('title',title);
		mbm.click();
	}
	$.post("ajax.php",{bookm: "true"},
	function(data){$("#book,#fly_book").fadeOut(400)});
}
