/***** FUNCTIONS *****/

/***** DOCUMENT READY *****/
$(function(){
	// info layer
	$(".infolayer-close").click(function(){
		$(this).parents(".infolayer").hide();
		return false;
	});
	$("#allianz-websites a#allianz-websites-opener").click(function(){
		$(this).next(".infolayer").show();
		return false;
	});
	
	if ($("#nav ul li ul").length == 0) {
		$("body").attr("id", "only-firstnav");
	} 
	
	// info icon - layer
	// replace old html with new
	var boxHtml = "\
		<span class='info-icon'></span>\
		<span class='infolayer'>\
			<span class='infolayer-inner'>\
				<strong class='infolayer-title'>Info</strong>\
				<span class='infolayer-content'>\
					Dummy text\
				</span>\
			</span>\
		</span>\
	";
	$(".info").each(function(){
		var infoText = $(this).children("span").text();
		infoText = infoText.replace("[","");
		infoText = infoText.replace("]","");
		$(this).children("span").html("");
		$(this).append(boxHtml);
		$(".infolayer-content", this).text(infoText);
		$(".info-icon", this).addClass("info-show");
		$(".infolayer-inner", this).addClass("info-show");
		$(".infolayer-content", this).addClass("info-show");
	});
	
	$(".info").hover(function(){
		$(this).addClass("info-hover");
		$(".infolayer", this).show();
	},function(){
		$(this).removeClass("info-hover");
		$(".infolayer", this).hide();
	});
	
	$(".info2").hover(function(){
		$(this).addClass("info-hover");
		$(".infolayer", this).show();
	},function(){
		$(this).removeClass("info-hover");
		$(".infolayer", this).hide();
	});
	
	
	// equalise heights in mod-container
	var h, highest;
	$(".mod-container").each(function(){
		highest = 0;
		$(".mod", this).each(function(){
			h = $(this).height();
			if ($(".bottom", this).length > 0) {
				h = h + $(".bottom", this).height(); // extend height if there is absolutely positioned bottom content
			}			
			if (h > highest) {
				highest = h;
			}
		});		
		$(".mod", this).height(highest);
	});
	
	// content tools => todo: is this really useful? persistence logic needed! but functionality is already provided by the browser!
	var current_font_size;
	$(".content-tool-font-increase").click(function(){
		current_font_size = parseFloat($("body").css("font-size"), 10);
		$("body").css("font-size", current_font_size + 2);		
		return false;
	});
	$(".content-tool-font-decrease").click(function(){
		current_font_size = parseFloat($("body").css("font-size"), 10);
		$("body").css("font-size", current_font_size - 2);
		return false;
	});
	$(".content-tool-print").click(function(){
		window.print();
	});
	
	// search
	$("#search .text").focus(function(){
		$(this).select();
	});
	
	// location search
	$("#search-location .text").focus(function(){
		$(this).select();
	});
	
	/** FLAPS **/
	$(".flap").each(function() {
		$(this).not(".flap-open").find(".flap-body", this).hide();
		$(".flap-header", this).css("cursor", "pointer").click(function() {
			var flap$ = $(this).closest(".flap");
			if ($(".flap-body", flap$).is(":visible")) {
				flap$.removeClass("flap-open").find(".flap-body").slideUp();
			} else {
				flap$.addClass("flap-open").find(".flap-body").slideDown();
			}
			return false;
		});
	});
	

	/*** old functions ***/
	
	/* zoom images */
	$(".zoomImage").each( function(){
		var imageUrl = $(this).attr("href");
		$(this).attr("href","javascript:;");
		$(this).bind("click", {url:imageUrl}, zoomImage);
	});	

	/* popup */
	$(".popup").each( function(){
		var imageUrl = $(this).attr("href");
		$(this).attr("href","javascript:;");
		$(this).bind("click", {url:imageUrl}, zoomImage);
	});	

	/* check all anchor-tags for keywords "mailto" and "popup" to open popup-contact-form */
	$("a").each( function(){
		var Wert = $(this).attr("href");
  	var Ausdruck = /(\w.+)\:(.+)/;
  	Ausdruck.exec(Wert);
		if (RegExp.$1 == "mailto")
		{
			// Split into Mail + Param
			Ausdruck = /(.+)\?(\w.+)/;
			Ausdruck.exec(trim(RegExp.$2));
		  var site = trim(RegExp.$2);
		  var mail = trim(RegExp.$1);
		  
		  // Always remove Ext-Link-Icon 	
			$(this).children(":first").hide();	 
			if (site == "alternativ") {
			  $(this).attr("href","javascript:;");
		  	$(this).attr("target","_self");
		  	$(this).bind("click", {url:'/_admincontent/dialogcenter.jsp?page=allgemein&emailtoperson='+mail}, zoomImage);
		  }
			if (site == "default") {
			  $(this).attr("href","javascript:;");
		  	$(this).attr("target","_self");
		  	$(this).bind("click", {url:'/_admincontent/dialogcenter.jsp?page=allgemein'}, zoomImage);
		  }
		}
  	Ausdruck = /(.+)\?(.+)/;
  	Ausdruck.exec(Wert);
  	var param = trim(RegExp.$2);
		if (param == "schaden")
		{
		  	$(this).attr("href","javascript:;");
		  	$(this).attr("target","_self");
	  		$(this).bind("click", {url:'/_admincontent/dialogcenter.jsp?page=schaden'}, zoomImage);
		  	$(this).children(":first").hide();
		}
		if (param == "feedback")
		{
		  	$(this).attr("href","javascript:;");
		  	$(this).attr("target","_self");
	  		$(this).bind("click", {url:'/_admincontent/dialogcenter.jsp?page=feedback'}, zoomImage);
		  	$(this).children(":first").hide();
		}
		if (param == "popup")
		{
		  	var site = trim(RegExp.$1);
		  	$(this).attr("href","javascript:;");
		  	$(this).attr("target","_self");
	  		$(this).bind("click", {url:'/system/modules/at.dmc.allianz.at.templates/pages/image.jsp?imageUrl='+site}, zoomImage);
		  	$(this).children(":first").hide();
		}
		
		// Reset RegExp.
	 	Ausdruck.exec("reset reset");
	});
});


/*** Old Functions ***/

/* popup */
function popup(url,title) {
	window.open(url,title,"width=680,height=610,resizable=yes,scrollbars=yes"); 
}

function popupCenter(url,w,h) {
  x = screen.availWidth/2-w/2;
  y = screen.availHeight/2-h/2;
  var popupWindow = window.open(url,'','width='+w+',height='+h+',left='+x+',top='+y+',screenX='+x+',screenY='+y);
  if (popupWindow) return false;
	else return true;
}

function zoomImage(event) {
	var url = event.data.url;
	window.open(url,"image","width=680,height=650,resizable=yes,scrollbars=yes"); 
}

function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

