/*	$Id: zeugs.css,v 0.9 28.10.2009 22:34 kniebes Exp $ 
*/



var zeugs = {
	currentMethod: '',

	getData: function(method) {
		if ($("#panel")) $("#panel").remove();
		if ($("#closepanel")) $("#closepanel").remove();
		$("#navigation a").removeClass("current");

		if (zeugs.currentMethod != method) {
			var selector = "#navigation a[rel='" + method + "']";
			$(selector).addClass("activityindicator");
			$(selector).addClass("current");
			$.getJSON("/API/" + method,
				function(data) {
					if (data.success == true) {
						$("#navigation").after('<div id="panel" style="display: none;" class="slot"><div class="slot-narrow">' + data.data + '</div></div>');
						$("#panel").slideDown();
						zeugs.addCloseButton();
					} else alert(data.message);
					$(selector).removeClass("activityindicator");
				}
			);
			zeugs.currentMethod = method;
		} else {
			zeugs.currentMethod = "";
		}
	},

	showData: function(method) {
		if ($("#panel")) $("#panel").remove();
		if ($("#closepanel")) $("#closepanel").remove();
		$("#navigation a").removeClass("current");

		if (zeugs.currentMethod != method) {
			var selector = "#navigation a[rel='" + method + "']";
			$(selector).addClass("activityindicator");
			$(selector).addClass("current");
			if ($('#'+method)) {
				var content = $('#'+method).html();
				$("#navigation").after('<div id="panel" style="display: none;" class="slot"><div class="slot-narrow">' + content + '</div></div>');
				$("#panel").slideDown();
				zeugs.addCloseButton();
				$(selector).removeClass("activityindicator");
			}
			zeugs.currentMethod = method;
		} else {
			zeugs.currentMethod = "";
		}
	},

	liveSearch: function(query) {
		if ($('#panel')) $('#panel').remove();
		if ($("#closepanel")) $("#closepanel").remove();
		$("#navigation a").removeClass("current");
		$("#extern").hide();
		$("body").scrollTop(0);
		
		if (query.length > 2) {
			$("#lq").addClass("activityindicator").attr("disabled", true);
			$.getJSON("/API/livesearch?q=" + escape(query),
				function(data) {
					if (data.success == true) {
						$("#navigation").after('<div id="panel" style="display: none;" class="slot"><div class="slot-narrow searchresults">' + data.data + '</div></div>');
						$("#panel").slideDown();
						zeugs.addCloseButton();
					};
					$("#lq").removeClass("activityindicator").removeAttr("disabled");
				}
			);
		}
	}, 

	addCloseButton: function() {		
		$("#panel").after('<div id="closepanel"><a href="#" title="Panel schliessen">&uarr;&nbsp;&uarr;&nbsp;&uarr;</a></div>'); 
		$("#closepanel a").click(function() {
			$("#navigation a.getdata").removeClass("current");
			$("#closepanel").remove();
			$("#panel").slideUp();
			return false;
		}) 
	},
	
	notify: function(text) {
		$("#notification").html(text);
		$('#notification').fadeIn()
		setTimeout("$('#notification').fadeOut()", 9000);
	},
	
	commentActivity: function(active) {
		if (active == true) {
			$('#cform input').attr('disabled', 'true');
			$('#cform textarea').attr('disabled', 'true');
			$('#cform input.submit').addClass('sending').val('Übertrage Daten...');
			
		} else {
			$('#cform input').attr('disabled', '');
			$('#cform textarea').attr('disabled', '');
			$('#cform input.submit').attr('disabled', '').removeClass('sending').val('Speichern');
		}
	}
}



this.tooltip = function() {	
	xOffset = 8;
	yOffset = 20;		
	$("a.tooltip").hover(
		function(e) {											  
			this.t = this.title;
			this.title = "";									  
			$("body").append("<p id='tooltip'>"+ this.t +"</p>");
			$("#tooltip")
				.css("top",(e.pageY - xOffset) + "px")
				.css("left",(e.pageX + yOffset) + "px")
				.fadeIn("fast");		
    	},
		function() {
			this.title = this.t;		
			$("#tooltip").remove();
	    }
	);	
	$("a.tooltip").mousemove(function(e) {
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});		
	
	// ALT Tag as tooltip on photos p(img). <a><img alt="foo" /></a>
	$(".img img").hover(
		function(e) {											  
			this.t = this.alt;
			this.alt = "";									  
			$("body").append("<p id='tooltip'>"+ this.t +"</p>");
			$("#tooltip")
				.css("top",(e.pageY - xOffset) + "px")
				.css("left",(e.pageX + yOffset) + "px")
				.fadeIn("fast");		
    	},
		function() {
			this.alt = this.t;		
			$("#tooltip").remove();
	    }
	);	
	$(".img img").mousemove(function(e) {
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};



$(document).ready(function() {
	tooltip();

	$("a[rel^='prettyPhoto']").prettyPhoto({theme: 'dark_square', opacity: 1});
	
	
	$("a.getdata").click(function() {
		$("#extern").hide();
		zeugs.getData($(this).attr("rel"));
		return false;
	})

	$("a.showdata").click(function() {
		$("#extern").hide();
		zeugs.showData($(this).attr("rel"));
		return false;
	})

	$("form#searchform").submit(function() {
		zeugs.liveSearch($("#lq").val());
		return false;
	})

	$("#lq").attr('autocomplete', 'off')
		.val("Suchen...")
		.css("color", "#666")
		.focus(function() {
			if ($("#lq").val() == "Suchen...") {
				$("#lq").val("").css("color", "#ddd");
			}
		})
		.blur(function() {
			if ($("#lq").val() == "") {
				$("#lq").val("Suchen...").css("color", "#666");
			}
		});

	$("#extern").hide();
	$("#toggle_extern").click(function() {
		$(".extra").hide();
		if ($("#extern").css('display') == 'block') {
			$("#navigation a").removeClass("current");
			zeugs.currentMethod = '';
		} else {
			$("#extern").show();
			if ($("#panel")) $("#panel").remove();
			if ($("#closepanel")) $("#closepanel").remove();
			$("#navigation a").removeClass("current");
			var selector = "#navigation a[rel='more']";
			$(selector).addClass("current");
			zeugs.currentMethod = 'more';
		}
		return false;
	})
	
	$("#about").hide();
	$("#toggle_about").click(function() {
		$(".extra").hide();
		if ($("#about").css('display') == 'block') {
			$("#navigation a").removeClass("current");
			zeugs.currentMethod = '';
		} else {
			$("#about").show();
			if ($("#panel")) $("#panel").remove();
			if ($("#closepanel")) $("#closepanel").remove();
			$("#navigation a").removeClass("current");
			var selector = "#navigation a[rel='about']";
			$(selector).addClass("current");
			zeugs.currentMethod = 'about';
		}
		return false;
	})
	
	$("#cform").submit(function() {
		var data = $("#cform").serialize();
		zeugs.commentActivity(true);
		$.ajax({
			type: "POST",
			url: "/API/comments/put",
			data: data,
			dataType: "json",
			success: function(data) {
				zeugs.commentActivity(false);
				zeugs.notify(data.message);
				setTimeout("$('#commentinfo').remove()", 5000);
				if (data.success == true) {
					//$("#Body").val('');
					$("#cform")[0].reset();
					if (data.data != '') {
						if ($('.commentlist').length == 0) {
							$("#kommentarformular").before('<div class="item"><ul class="commentlist"></ul></div>'); 
						}
						$('.commentlist').append(data.data)
					}
				} 
			}
		});
		return false;
	})
	
	$(".commentform label").hide();
	
	var inputs = jQuery(".textelement");
	inputs.each(function(){
		if(jQuery(this).attr("title") != undefined) {
			if (this.value == '') this.value = jQuery(this).attr("title");
			this.onfocus = function() {
				if (this.value == this.getAttribute("title")) {
					this.value = "";
				}
				return false;
			}
			this.onblur = function() {
				if (this.value.length < 1) {
					this.value = this.getAttribute("title");
				}
			}
		}
	});
});

