
var _tx_jo_subMenueOpen = false;
var _tx_jo_mainMenuButtonDelayer = delayTimer(450);

/*
 * main navigation - second level
 */

function tx_jo_init_main_navigation(wizard) {
	var index;
	
	$("#circumstance-wizard").show();
	
	// empty js fallback content
	$("#mainnavi-subbox-wrap").empty();
	
	for (index=1; index<=8; index++) {
		if($("#main-"+index)) {			
			if($("#main-" + index + " div.outer").length > 0) {
				
				$("#mainnavi-subbox-wrap").append("<div id=\"subbox-" + index + "\" class=\"subbox\"><div id=\"main-sub-" + index + "\" class=\"outer\">" + $("#main-" + index + " div.outer").html() + "</div></div>");
			}
		}
	}

	tx_jo_hide_all();

	$("#circumstance-wizard div.cw-left").mouseout(function(e) {
		if(!tx_jo_cw_checkMouseOverLifeWizardMenue_out(e))
		{			
			tx_jo_cw_showKeyvisual(-1);			
		}
	});
	
	$("#mainnavi-subbox-wrap .subbox").mouseover(function(e) { stopTimer();	});
	
	if(true) {
		/*
		 * 1
		 */
		$("#main-1 a").mouseover(function(e) { tx_jo_mainMenueMouseOver(1,e);	});
		$("#main-1 a").mouseout(function(e) { tx_jo_mainMenueMouseOut(1,e); });
		$("div#mainnavi-subbox-wrap div#subbox-1").mouseout(function(e) { tx_jo_mainMenueMouseOut(1,e); });
		
		/*
		 * 2
		 */
		$("#main-2 a").mouseover(function(e) { tx_jo_mainMenueMouseOver(2,e);	});
		$("#main-2 a").mouseout(function(e) { tx_jo_mainMenueMouseOut(2,e); });
		$("div#mainnavi-subbox-wrap div#subbox-2").mouseout(function(e) { tx_jo_mainMenueMouseOut(2,e); });
		
		/*
		 * 3
		 */
		$("#main-3 a").mouseover(function(e) { tx_jo_mainMenueMouseOver(3,e);	});
		$("#main-3 a").mouseout(function(e) { tx_jo_mainMenueMouseOut(3,e); });
		$("div#mainnavi-subbox-wrap div#subbox-3").mouseout(function(e) { tx_jo_mainMenueMouseOut(3,e); });
		
		/*
		 * 4
		 */
		$("#main-4 a").mouseover(function(e) { tx_jo_mainMenueMouseOver(4,e);	});
		$("#main-4 a").mouseout(function(e) { tx_jo_mainMenueMouseOut(4,e); });
		$("div#mainnavi-subbox-wrap div#subbox-4").mouseout(function(e) { tx_jo_mainMenueMouseOut(4,e); });
		
		/*
		 * 5
		 */
		$("#main-5 a").mouseover(function(e) { tx_jo_mainMenueMouseOver(5,e);	});
		$("#main-5 a").mouseout(function(e) { tx_jo_mainMenueMouseOut(5,e); });
		$("div#mainnavi-subbox-wrap div#subbox-5").mouseout(function(e) { tx_jo_mainMenueMouseOut(5,e); });
		
		/*
		 * 6
		 */
		$("#main-6 a").mouseover(function(e) { tx_jo_mainMenueMouseOver(6,e);	});
		$("#main-6 a").mouseout(function(e) { tx_jo_mainMenueMouseOut(6,e); });
		$("div#mainnavi-subbox-wrap div#subbox-6").mouseout(function(e) { tx_jo_mainMenueMouseOut(6,e); });
		
		/*
		 * 7
		 */
		$("#main-7 a").mouseover(function(e) { tx_jo_mainMenueMouseOver(7,e);	});
		$("#main-7 a").mouseout(function(e) { tx_jo_mainMenueMouseOut(7,e); });
		$("div#mainnavi-subbox-wrap div#subbox-7").mouseout(function(e) { tx_jo_mainMenueMouseOut(7,e); });
		
		/*
		 * 8
		 */
		$("#main-8 a").mouseover(function(e) { tx_jo_mainMenueMouseOver(8,e);	});
		$("#main-8 a").mouseout(function(e) { tx_jo_mainMenueMouseOut(8,e); });	
		$("div#mainnavi-subbox-wrap div#subbox-8").mouseout(function(e) { tx_jo_mainMenueMouseOut(8,e); });		
	} 
}

/*
 * 
 */
function tx_jo_hide_all()
{
	$("#mainnavi-subbox-wrap div.subbox").hide();
}

/*
 * 
 */
function tx_jo_checkMouseOverMenue_over(index,e)
{	
	var mouse_x = e.pageX;
	var mouse_y = e.pageY;

	e.stopPropagation();	

	var sub = $("#mainnavi-subbox-wrap div#subbox-"+index);
	var submenu_offset = sub.offset();
	var submenu_x = submenu_offset.left;
	var submenu_y = submenu_offset.top;
	var submenu_height = sub.height();
	var submenu_width = sub.width(); 

	var main = $("#main-"+index+ " a");
	var mainmenu_offset = main.offset();
	var mainmenu_x = mainmenu_offset.left;
	var mainmenu_y = mainmenu_offset.top;
	var mainmenu_height = main.height();
	var mainmenu_width = main.width();

	// alert((mouse_x >= submenu_x && mouse_x <= (submenu_x + submenu_width) && mouse_y >= submenu_y && mouse_y <= (submenu_y + submenu_height)) || (mouse_x >= mainmenu_x && mouse_x <= (mainmenu_x + mainmenu_width + 29) && mouse_y >= mainmenu_y && mouse_y <= (mainmenu_y + mainmenu_height + 8)));
	// checking whether the mouse cursor is within the boundaries of the main menu button AND the submenue block 
	// the paddings of the mainmenu buttons are added statically because the jquery.width() and height() methods are returning the width without padding!!
	if( (mouse_x >= submenu_x && mouse_x <= (submenu_x + submenu_width) && mouse_y >= submenu_y && mouse_y <= (submenu_y + submenu_height)) || (mouse_x >= mainmenu_x && mouse_x <= (mainmenu_x + mainmenu_width + 29) && mouse_y >= mainmenu_y && mouse_y <= (mainmenu_y + mainmenu_height + 8)) ) 
	{		
		// keep showed
		return true;		
	} else {
		// hide
		return false;	
	}	
}

/*
 * 
 */
function tx_jo_checkMouseOverMenue_out(index,e)
{	
	var mouse_x = e.pageX;
	var mouse_y = e.pageY;

	e.stopPropagation();	

	var sub = $("#mainnavi-subbox-wrap div#subbox-"+index);
	var submenu_offset = sub.offset();
	var submenu_x = submenu_offset.left;
	var submenu_y = submenu_offset.top;
	var submenu_height = sub.height();
	var submenu_width = sub.width(); 

	var main = $("#main-"+index+ " a");
	var mainmenu_offset = main.offset();
	var mainmenu_x = mainmenu_offset.left;
	var mainmenu_y = mainmenu_offset.top;
	var mainmenu_height = main.height();
	var mainmenu_width = main.width();
	
	// checking whether the mouse cursor is within the boundaries of the main menu button AND the submenue block 
	// the paddings of the mainmenu buttons are added statically because the jquery.width() and height() methods are returning the width without padding!!
	if( (mouse_x >= submenu_x && mouse_x <= (submenu_x + submenu_width) && mouse_y >= submenu_y && mouse_y <= (submenu_y + submenu_height)) || (mouse_x >= mainmenu_x && mouse_x <= (mainmenu_x + mainmenu_width ) && mouse_y >= mainmenu_y && mouse_y <= (mainmenu_y + mainmenu_height))) 
	{		
		// keep showed
		return true;		
	} else {
		// hide
		return false;
	}	
}

/*
 * 
 */
function tx_jo_show_sub(index)
{	
	$("#main-navigation-section .keyvisual-box .keyvisual-overlay-2").stop(true,true);
	$("div.subbox").stop(true,true);
	$("#navigation-section a").removeClass('active');
	$("li:not(.current)#main-"+index+" a").addClass('active');
	$("div.subbox").hide();

	if(!_tx_jo_subMenueOpen)
	 	$("#mainnavi-subbox-wrap").hide();
					
	if($("div#subbox-"+index).css("display") == "none") {
		if($("#main-navigation-section .keyvisual-box .keyvisual-overlay").css("display") == "block")
		{
			$("#main-navigation-section .keyvisual-box .keyvisual-overlay").hide();
			$("#main-navigation-section .keyvisual-box .keyvisual-overlay-2").show();
		} else {
			$("#main-navigation-section .keyvisual-box .keyvisual-overlay-2").css('filter', 'alpha(opacity=50)');
			$("#main-navigation-section .keyvisual-box .keyvisual-overlay-2").fadeIn();
		}
			
		if(_tx_jo_subMenueOpen)
		{
			$("div#subbox-"+index).fadeIn(1000, function(){
				$("#mainnavi-subbox-wrap div.outer h3").css('opacity','0.9999');
				$("#mainnavi-subbox-wrap div.outer ul li span a").css('opacity','0.9999');
			});			
		} else 
		{			
			$("div#subbox-"+index).show();	
			// $("#mainnavi-subbox-wrap > .inner > ul > li > span > a").css('filter', 'alpha(opacity=10)');
			$("#mainnavi-subbox-wrap").css('filter', 'alpha(opacity=90)');
			$("#mainnavi-subbox-wrap").fadeIn(1000,function(){
				$("#mainnavi-subbox-wrap div.outer h3").css('opacity','0.9999');
				$("#mainnavi-subbox-wrap div.outer ul li span a").css('opacity','0.9999');				
				_tx_jo_subMenueOpen=true; 
				tx_jo_cw_showKeyvisual(-1);
			});	
		}
			
	} else {
		
		$("div#subbox-"+index).show();
	}
	
	tx_jo_cw_closeLifeWizard();
}

/*
 * 
 */
function tx_jo_mainMenueMouseOver(index,e)
{
	stopTimer();

	if(tx_jo_checkMouseOverMenue_over(index,e) && ($("#navigation-section li#main-" + index).hasClass("hassub") || $("#navigation-section li#main-" + index).hasClass("current")))
	{
		_tx_jo_mainMenuButtonDelayer(function(){				
			if($("div#subbox-"+index).css("display") == "none") {
				tx_jo_hide_all();
				tx_jo_show_sub(index);
			}			
		});
	} else if($("#navigation-section li#main-" + index).hasClass("hassub") == false)
	{			
		// actual menuebutton has no subitems .. close the actual sub box if its open
		var subboxes = $("div.subbox");
		// console.log(subboxes);
		subboxes.each(
			function( intIndex ){			
				if($(this).css("display") == "block")
				{
					var parent = $(this);
					_tx_jo_mainMenuButtonDelayer(function(){
						$("#navigation-section a").removeClass('active');
						$("#main-navigation-section .keyvisual-box .keyvisual-overlay-2").fadeOut(1000);
						parent.slideUp("slow", function(){_tx_jo_subMenueOpen=false;});
					});					
				}
			}		
		);
	}	
}

/*
 * 
 */
function tx_jo_mainMenueMouseOut(index,e)
{	
	if(!tx_jo_checkMouseOverMenue_out(index,e))
	{	
		_tx_jo_mainMenuButtonDelayer(function(){			
			$("#main-navigation-section .keyvisual-box .keyvisual-overlay-2").fadeOut(1000);
			var subboxes = $("div.subbox");
			subboxes.each(
					function( intIndex ){			
						if($(this).css("display") == "block")
						{						
							$("#navigation-section a").removeClass('active');
							$("#main-navigation-section .keyvisual-box .keyvisual-overlay-2").fadeOut(1000);
							$(this).slideUp("slow", function(){_tx_jo_subMenueOpen=false;});										
						}
					}		
				);				
		});
	}	
}



/**
 * evaluates a german zip code
 * @param zip
 * @return true or false
 */
function tx_jo_checkGermanZipcode(zip) {
	var regDigits = /[0-9]{5}/;
	var lcValue = zip;		
	if (regDigits.exec(lcValue)) {		
		return true;		
	} else {
		return false;	
	}		
}

function tx_jo_getPageLink(page_uid,title,cssClassParam,url)
{	
	var cssClass = '';
	if(cssClassParam)
		cssClass = ' class="' + cssClassParam + '" ';
	if(!url)
		url = tx_jo_getPageURL(page_uid);	
	var link = '<a href="' + url +'" title="' + title + '"'+ cssClass +'>' + title + '</a>';
	
	return link;
}

function tx_jo_getPageURL(page_uid)
{	
	return window.location.protocol + "//" + window.location.host + "/index.php?id=" + page_uid;
}


/**
 * Redirects to page and request with realurl path.
 * 
 * @param page_uid
 * @return
 */
function tx_jo_redirectToPageUid(page_uid)
{	
	// eID TYPO3 request
	var requestURL = window.location.protocol + "//" + window.location.host + window.location.pathname;
	
	// calls jo/ajax/class.ajax_realurl.php function
	$.ajax(
	{
  		type: "POST", 
  		url: requestURL,
  		data: "eID=pageurl&uid=" + page_uid + "&no_cache=1",
		dataType: "text",
  		success: function(msg){
			// split used to separate url from error messages
			splitUrl = msg.split("######");
			redirectURL = splitUrl[1];
			window.location = redirectURL;
   		},
		error: function(oxhr, status, errorThrown){
//			alert('Fehler beim Laden der Anfrage aufgetreten: ' + status + errorThrown);
		}
	});
}



function tx_jo_showContent(contentID, selfElement){

	var content = $("#"+contentID);
	content.parent().find(".content-preview").hide();
	content.parent().find("a").removeClass("current");
	$(selfElement).addClass("current");
		content.show();
}

function tx_jo_urldecode (str) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer
    // +      input by: Ratheous
    // +   improved by: Orlando
    // %        note 1: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urldecode('Kevin+van+Zonneveld%21');
    // *     returns 1: 'Kevin van Zonneveld!'
    // *     example 2: urldecode('http%3A%2F%2Fkevin.vanzonneveld.net%2F');
    // *     returns 2: 'http://kevin.vanzonneveld.net/'
    // *     example 3: urldecode('http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a');
    // *     returns 3: 'http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a'
    
    var hash_map = {}, ret = str.toString(), unicodeStr='', hexEscStr='';
    
    var replacer = function (search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The hash_map is identical to the one in urlencode.
    hash_map["'"]   = '%27';
    hash_map['(']   = '%28';
    hash_map[')']   = '%29';
    hash_map['*']   = '%2A';
    hash_map['~']   = '%7E';
    hash_map['!']   = '%21';
    hash_map['%20'] = '+';
    hash_map['\u00DC'] = '%DC';
    hash_map['\u00FC'] = '%FC';
    hash_map['\u00C4'] = '%D4';
    hash_map['\u00E4'] = '%E4';
    hash_map['\u00D6'] = '%D6';
    hash_map['\u00F6'] = '%F6';
    
    hash_map['\u00DF'] = '%DF';
    hash_map['\u20AC'] = '%80';
    hash_map['\u0081'] = '%81';
    hash_map['\u201A'] = '%82';
    hash_map['\u0192'] = '%83';
    hash_map['\u201E'] = '%84';
    hash_map['\u2026'] = '%85';
    hash_map['\u2020'] = '%86';
    hash_map['\u2021'] = '%87';
    hash_map['\u02C6'] = '%88';
    hash_map['\u2030'] = '%89';
    hash_map['\u0160'] = '%8A';
    hash_map['\u2039'] = '%8B';
    hash_map['\u0152'] = '%8C';
    hash_map['\u008D'] = '%8D';
    hash_map['\u017D'] = '%8E';
    hash_map['\u008F'] = '%8F';
    hash_map['\u0090'] = '%90';
    hash_map['\u2018'] = '%91';
    hash_map['\u2019'] = '%92';
    hash_map['\u201C'] = '%93';
    hash_map['\u201D'] = '%94';
    hash_map['\u2022'] = '%95';
    hash_map['\u2013'] = '%96';
    hash_map['\u2014'] = '%97';
    hash_map['\u02DC'] = '%98';
    hash_map['\u2122'] = '%99';
    hash_map['\u0161'] = '%9A';
    hash_map['\u203A'] = '%9B';
    hash_map['\u0153'] = '%9C';
    hash_map['\u009D'] = '%9D';
    hash_map['\u017E'] = '%9E';
    //hash_map['\u0178'] = '%9F'; produces errors for ß char
    hash_map['\u00C6'] = '%C3%86';
    hash_map['\u00D8'] = '%C3%98';
    hash_map['\u00C5'] = '%C3%85';
 
    for (unicodeStr in hash_map) {
        hexEscStr = hash_map[unicodeStr]; // Switch order when decoding
        ret = replacer(hexEscStr, unicodeStr, ret); // Custom replace. No regexing
    }
    
    // End with decodeURIComponent, which most resembles PHP's encoding functions
    ret = decodeURIComponent(ret);
 
    return ret;
}
/* 
##################################################
Image Map
################################################## 
*/

function highlightImagemapAndList(name) {
		changeImagemapPicture(name);
		
		var id = "#imagemap_" + name;
		if($(id).length > 0) {
			var isLiActive = $(id).hasClass("active-without-subitems");
	
			if (!isLiActive) {
				$(id).addClass("active-without-subitems");
			}
		}
	}

function changeImagemapPicture(name) {
		function tx_jo_checkimage(url) { 
			var img = new Image;
			img.onload = function() { $('#imagemapPicture').attr('src', url); };
			img.onerror = function() { } ; 
			img.src = url; 
		} 
		tx_jo_checkimage('fileadmin/user_upload/dev/imagemap/imagemap_'+name+'.jpg');
}

function resetImagemapAndList(name) {
		changeImagemapPicture(name);
		$('#style_map li').removeClass("active-without-subitems");
}

// handler for click on image map - redirects to link target
function gotoImageMapTarget(name) {
	var link = $("#imagemap_" + name + " > a");
	if(link.length > 0) {
		window.location.href = window.location.protocol + "//" + window.location.host + '/' + link.attr('href');
	}
}

/* 
##################################################
 JS initializations for no JS support
 - dynamically hides elements, so that content is accessible, if no JS is enabled
################################################## 
*/

jQuery(document).ready(function(){
	// footers tip-a-friend links
	var tipafriendHeight = jQuery('#tipafriend-box').height();
	
	jQuery('#metalinks-section').css('height', '89px');
	jQuery('#tipafriend-box').hide();
	jQuery('div.socialbookmarks a').hide();
//	jQuery('#metalinks-section .metalinks-box #tipafriend-box div.socialbookmarks a').css('visibility', 'hidden');
	jQuery('div.socialbookmarks a').css('visibility', 'hidden');
	
	// sitemaps third level
	if(jQuery('.sitemap .level-3').length > 0) {
		jQuery('.sitemap .level-3').each(
				function() {
					jQuery(this).hide();
					jQuery(this).parent().removeClass('current');
					jQuery(this).parent().find('.arrow').show();
				}
		);
	}
	
	// contact teasers
	jQuery('.foldable .content').hide();
	
	jQuery('#metalinks-section li.tipafriend').click(function(e){
		tx_jo_toggle_tipafriend(tipafriendHeight);
		return false;
	});
	
	
	// add cloned keyvisual, if startpage
/*	if(jQuery('#circumstance-wizard').length > 0){
		var keyVis = jQuery('#keyvisual-bg');
		var keyVis2 = keyVis.clone();
		keyVis2.attr('id', 'keyvisual-fg');
		keyVis2.attr('style', 'position: absolute; top: 0px; left: 0px; display: none;');
		keyVis.parent().append(keyVis2);
	} */
});


var tx_jo_tipafriendDelayTimer=delayTimer(5000);


function tx_jo_toogleContactTab(element) {

	var tabdiv = jQuery(element).parent();
	
	if(jQuery(tabdiv).hasClass('open')) {
		jQuery(tabdiv).find(".content").slideUp();
		jQuery(tabdiv).removeClass('open');
	} else {
		jQuery(tabdiv).find(".content").slideDown();
		jQuery(tabdiv).addClass('open');
	}
	
}

function tx_jo_toogleFAQTab(element) {

	var tabdiv = jQuery(element).parent();
	
	if(jQuery(tabdiv).hasClass('tab-active')) {
		jQuery(tabdiv).find(".content").slideUp();
		jQuery(tabdiv).removeClass('tab-active');
	} else {
		jQuery(tabdiv).parent().find(".content").parent().removeClass('tab-active');
		jQuery(tabdiv).parent().find(".content").slideUp();
		
		jQuery(tabdiv).find(".content").slideDown();
		jQuery(tabdiv).addClass('tab-active');
	}
}

function tx_jo_toogleLocalizedActions(fieldset) {

	var elem = jQuery(fieldset);
	
	if(jQuery(elem).hasClass('open')) {
		jQuery(elem).parent().find(".actions").slideUp();
		jQuery(elem).removeClass('open');
	} else {
		jQuery(elem).parent().find(".actions").slideDown();
		jQuery(elem).addClass('open');
	}
	
}


/*
 * Gallery Tooltip
 */

jQuery(document).ready(function() {
	var infoBox = "<div id='info-box'></div>";
	$("body").append(infoBox);
	$("#info-box").hide();
});	

function tx_jo_showToolTip(id){
	var element = $("#"+id);
	var offsetX	= parseInt(element.offset().left);
	var offsetY = parseInt(element.offset().top);

	var elementWidth = element.width(); 
	var positionLeft = element.position().left;
	var divpos 		 = element.parent().parent().position().left;
	var parentWidth  = element.parent().parent().parent().width();

	var coord = positionLeft+(elementWidth/2)+divpos;
	var half  = (parentWidth/2);

	var prevButtonWidth = $('.jcarousel-prev').width() + (parseInt( $('.jcarousel-prev').css("margin-right")));
	var marginLeft = 23;
	var marginRight = 28;
	
	var infoBox = $("#info-box");
	infoBox.html(element.find(".info-text").html());
	infoBox.removeAttr('style');
	var posTop = offsetY + element.height();

	if (coord < half){
		infoBox.removeClass("right");
		var posLeft = offsetX + marginLeft;
		infoBox.attr('style', 'left:'+posLeft+'px; top:'+posTop+'px;')
	}else{
		infoBox.addClass("right");
		var posLeft = offsetX - 298 - marginRight + elementWidth;	// 298 = info-box width (+ padding)
		infoBox.attr('style', 'left:'+posLeft+'px; top:'+posTop+'px;')
	}

	infoBox.show();
}


function tx_jo_toggle_tipafriend(height) {
	if($(".metalinks-box li.tipafriend").hasClass("active")) {
		tx_jo_hide_tipafriend();
	}
	else {
		tx_jo_show_tipafriend(height);
	}
}

function tx_jo_show_tipafriend(height) {	
	var box = $("#tipafriend-box").find('.inner');
	box.parent()
	.stop(true)
	.animate({
		syncHeight: height
	},{
		syncElements: box,
		duration: 300, 
		complete: function(){
			box.parent().each(function(idx,el){
			    el.style.overflow ='';
			});
			$("#tipafriend-box .socialbookmarks a").css("visibility", "visible");
			$("#tipafriend-box .socialbookmarks a").show();
		}
	});
	

	
	/*$("div.metalinks-box").mouseout(function(e) {				
		tx_jo_toggleTipafriendBoxIfVisible(e,true);		
	});
	$("div.metalinks-box").mouseover(function(e) {				
		tx_jo_toggleTipafriendBoxIfVisible(e,false);		
	});	*/
	
	$(".metalinks-box li.tipafriend").addClass("active");
	$(".metalinks-box a.down-link").hide();
	$(".metalinks-box a.up-link").show();
}


function tx_jo_hide_tipafriend() {

	$("#tipafriend-box .socialbookmarks a").css("visibility", "hidden");
	$("#tipafriend-box .socialbookmarks a").fadeOut("slow",function() 
		{
			// unbind all event handlers
			$("div.metalinks-box").unbind();
		}
	);
	
	$("#tipafriend-box").slideUp();
	
	$(".metalinks-box a.down-link").show();
	$(".metalinks-box a.up-link").hide();
	$(".metalinks-box li.tipafriend").removeClass("active");
}


function tx_jo_toggleSubMenu (anchor){
	var li = $(anchor).parent();
	li.find('ul.level-3.active').hide();
	li.toggleClass('current');
	li.find('ul.level-3').toggleClass("active");
	li.find('ul.level-3.active').show();
}

/*
 * checks the mouse cursor position and toggles the tipafriendbox after 5 secs if the mousecursor isnt over it
 */
function tx_jo_toggleTipafriendBoxIfVisible(e,mouseOut)
{
	if($(".metalinks-box li.tipafriend").hasClass("active")) {	
		var seam = 1;
		var mouse_x = e.pageX;
		var mouse_y = e.pageY;
		
		// console.log("x: " + mouse_x);
		// console.log("y: " + mouse_y);
		//	console.log("tipafriendBox_x: " + tipafriendBox_x);
		//	console.log("tipafriendBox_y: " + tipafriendBox_y);
		//	console.log("tipafriendBox_height: " + tipafriendBox_height);
		//	console.log("tipafriendBox_width: " + tipafriendBox_width);
		// echo something
		
		// because the bounding box of the boxes are not perfect add a little seam 
		// depending on mouseout or mouseover event 
		if(mouseOut)
		{
			seam = -1;
		}
		
		var tipafriendBox = $(".metalinks-box");
		var tipafriendBox_offset = tipafriendBox.offset();
		var tipafriendBox_x = tipafriendBox_offset.left;
		var tipafriendBox_y = tipafriendBox_offset.top;
		var tipafriendBox_height = tipafriendBox.height();
		var tipafriendBox_width = tipafriendBox.width();	
		
		if((mouse_x+seam) >= tipafriendBox_x && mouse_x <= (tipafriendBox_x + tipafriendBox_width + seam) && (mouse_y+seam) >= tipafriendBox_y && mouse_y <= (tipafriendBox_y + tipafriendBox_height + seam))
		{
			stopTimer();
		} else
		{
			tx_jo_tipafriendDelayTimer(function(){tx_jo_toggle_tipafriend();});		
		}
		
	}
	
}


var _tx_jo_cw_wizardButtonDelayer=delayTimer(2000);
var _tx_jo_cw_defaultImage = -1;
var _tx_jo_cw_wizardLayerisOpen = false;


/*
 * 
 */
function tx_jo_cw_unblur(o){ 
	// nothing, not bitv conform 
	return false;
}

function addFg (){
	var keyVis = jQuery('#keyvisual-bg');
	var keyVis2 = keyVis.clone();
	keyVis2.attr('id', 'keyvisual-fg');
	keyVis2.attr('style', 'position: absolute; top: 0px; left: 0px; display: none;');
	keyVis2.addClass('keyvisual-fg');
	keyVis.parent().append(keyVis2);
} 
function removeFg (){
	$('.keyvisual-fg').each(function(){
		$(this).remove();
	});
}

/*
 * changes the keyvisual to an index if set .. 
 */
function tx_jo_cw_showKeyvisual(index,imagePath,enforceIt)
{	
	// $("div#keyvisual img#keyvisual-fg").stop();
	
	if(!_tx_jo_cw_wizardLayerisOpen || enforceIt)
	{	
		
		var newImageSrc = "";
		if(index > -1)
		{
			addFg();
			newImageSrc = imagePath;
			$("div#keyvisual img#keyvisual-fg").attr("src", newImageSrc);
			
			$("div#keyvisual img#keyvisual-fg").fadeIn(400, function(){
				$("div#keyvisual img#keyvisual-bg").attr("src", newImageSrc);
				$("div#keyvisual img#keyvisual-fg").hide();
				removeFg();
			});
			
		} else {
			addFg();
			if(_tx_jo_cw_defaultImage > -1)
			{
//				newImageSrc = "typo3conf/ext/jo/res/img/keyvisual-wizard-"+defaultImage+".jpg";
				newImageSrc = defaultImageGlobal;
				$("div#keyvisual img#keyvisual-fg").attr("src", newImageSrc);
			} else
			{
//				newImageSrc = "typo3conf/ext/jo/res/img/keyvisual-homepage.jpg";
				newImageSrc = defaultImageGlobal;
				$("div#keyvisual img#keyvisual-fg").attr("src", newImageSrc);
				$("div#keyvisual img#keyvisual-fg").fadeIn(400, function(){
					$("div#keyvisual img#keyvisual-bg").attr("src", newImageSrc);
					$("div#keyvisual img#keyvisual-fg").hide();
					removeFg();
				});			
			}
		}
	}
	
}


/*
 * hide all wizards (from id 0 to 4)
 * if parameter index == -1, then hide all (only used on close button)
 */
function tx_jo_cw_hideWizards(index)
{	
	var isOpen = false;
	var k=0;
	_tx_jo_cw_defaultImage = -1;
		
		// check if one wizard open
	for(k=0; k<5; k++) {
		if($("div#cw-"+k).css("display") == "block") { isOpen = k; }
	}
	
	if ( (isOpen >= 0 && isOpen != index) || index == -1 ) {
			// set opened wizard as index
		index = isOpen;
		var i=0;		
		
		addFg();
//		newImageSrc = "typo3conf/ext/jo/res/img/keyvisual-homepage.jpg";
		newImageSrc = defaultImageGlobal;
		$("div#keyvisual img#keyvisual-fg").attr("src", newImageSrc);
		$("div#keyvisual img#keyvisual-fg").fadeIn(1000, function(){
			$("div#keyvisual img#keyvisual-bg").attr("src", newImageSrc);
			$("div#keyvisual img#keyvisual-fg").hide();
			removeFg();
			
			// hide wizard box if the close button is clicked
			if($("cw-bottom #cw-bottom-"+index).html())
			{				
				$("#main-navigation-section .keyvisual-box .keyvisual-overlay").fadeOut(1000);
				$("div#circumstance-wizard #cw-bottom-"+index).slideDown(1000, function() {
					$("div#circumstance-wizard .cw-right").fadeOut(800, function() {
						$("div#circumstance-wizard .cw-right").removeClass("cw-"+index);
						for(i=0; i<5; i++) { $("li#cw-nav-"+i+" a").removeAttr("style"); }
						$("div#cw-"+index).css("display", "none");						
					})
				});				
				
				tx_jo_cw_hideNowAllWizards(true);	
				
			} else {
				$("#main-navigation-section .keyvisual-box .keyvisual-overlay").fadeOut(1000);
				$("div#circumstance-wizard .cw-right").fadeOut(800, function() {
					$("div#circumstance-wizard .cw-right").removeClass("cw-"+index);
					for(i=0; i<5; i++) { $("li#cw-nav-"+i+" a").removeAttr("style"); }
					$("div#cw-"+index).css("display", "none");				
				});
				tx_jo_cw_hideNowAllWizards(true);
			}		
			
		});				
		
		_tx_jo_cw_wizardLayerisOpen = false;

	} else {
		// nothing to hide		
		tx_jo_cw_hideNowAllWizards();	
	}
	
	
	
}

function tx_jo_cw_hideNowAllWizards(slow)
{		
	$("div#circumstance-wizard .cw-right .inner .cw-bottom").hide();
	_tx_jo_cw_defaultImage = -1;	
		
	if(!slow)
	{
		$("div#circumstance-wizard .cw-right").hide();
		$("div#circumstance-wizard .cw-right .inner").hide();	
	} else
	{
		$("div#circumstance-wizard .cw-right").fadeOut(800);
		$("div#circumstance-wizard .cw-right .inner").fadeOut(800);				
	}
	for(i=0; i<5; i++)
	{
		if($("div#circumstance-wizard div.cw-right").hasClass("cw-"+i))
		{
			$("div#circumstance-wizard div.cw-right").removeClass("cw-"+i);
		}
		
		if(navigator.appVersion.indexOf("MSIE 6")>-1)
		{				
			$("li#cw-nav-"+i+" a").hover(function() {						
				$(this).css('color','#FFFFFF');
	        }, function() {	
	        	$(this).css('color','#333333');
	        });
		}
		
		
	}
	$(".cw-left ul li a").removeAttr("style");
	_tx_jo_cw_wizardLayerisOpen = false;
}


/*
 * opens the circumstance wizard
 */
function tx_jo_cw_openWizard(index,imagePath) {		
	
	window.clearTimeout(timer);
	tx_jo_cw_hideNowAllWizards();
	_tx_jo_cw_defaultImage = index;	
	
	if($("div#circumstance-wizard .cw-right.cw-"+index).css("display") != "block")
	{
			// shade background
		$("#main-navigation-section .keyvisual-box .keyvisual-overlay").css('filter', 'alpha(opacity=50)');
		$("#main-navigation-section .keyvisual-box .keyvisual-overlay").fadeIn(1000);
		
			// change menue background
		var i=0;
		for(i=0; i<5; i++) {
			if(i == index) {
				$("li#cw-nav-"+index+" a").css("background-position", "0px -75px");
				$("li#cw-nav-"+index+" a").css("color", "#FFFFFF");
				
				// ie6 ..				
				if(navigator.appVersion.indexOf("MSIE 6")>-1)
				{				
					$("li#cw-nav-"+i+" a").hover(function() {						
						$(this).css('color','#FFFFFF');
			        }, function() {	
			        	$(this).css('color','#FFFFFF');
			        });
				}				
				
			} else {
				$("li#cw-nav-"+i+" a").css("background-position", "0px -150px");
				
				// ie6 ..				
				if(navigator.appVersion.indexOf("MSIE 6")>-1)
				{				
					$("li#cw-nav-"+i+" a").hover(function() {						
						$(this).css('color','#DE2221');
			        }, function() {
			        	$(this).css('color','#484848');
			        });
				}
			}
		}
		
			// add indexed css classes
		$("div#circumstance-wizard .cw-right").addClass("cw-"+index);
		$("div#cw-"+index).css("display", "block");
			
		if($("div#circumstance-wizard #cw-bottom-"+index).html())
		{
			/* $("div#circumstance-wizard .cw-right").css('filter', 'alpha(opacity=100)'); */
						
			$("div#circumstance-wizard .cw-right").fadeIn(800, function() {
				$("div#circumstance-wizard #cw-bottom-"+index).slideDown();
			});
		} else {
			$("div#circumstance-wizard .cw-right").fadeIn(800);
		}
		_tx_jo_cw_wizardLayerisOpen = true;
		tx_jo_cw_showKeyvisual(index,imagePath,true);
		
	} else {
			// element can not click if it opened
		return false;
	}	
}

	

/*
 * opens the wizard after a certain time.
 */
function tx_jo_cw_openTimedWizard(index,imagePath)
{	 
	tx_jo_cw_openWizard(index,imagePath);
	_tx_jo_cw_wizardLayerisOpen = true;	
}

/*
 * 
 */
function tx_jo_cw_checkMouseOverLifeWizardMenue_out(e) {	
	var mouse_x = e.pageX;
	var mouse_y = e.pageY;

	e.stopPropagation();	

	var lifeWizardMenueDiv = $("#circumstance-wizard div.cw-left");
	var lifeWizardMenueDiv_offset = lifeWizardMenueDiv.offset();
	var lifeWizardMenueDiv_x = lifeWizardMenueDiv_offset.left;
	var lifeWizardMenueDiv_y = lifeWizardMenueDiv_offset.top;
	var lifeWizardMenueDiv_height = lifeWizardMenueDiv.height();
	var lifeWizardMenueDiv_width = lifeWizardMenueDiv.width(); 
	
	if(mouse_x >= lifeWizardMenueDiv_x && mouse_x <= (lifeWizardMenueDiv_x + lifeWizardMenueDiv_width) && mouse_y >= lifeWizardMenueDiv_y && mouse_y <= (lifeWizardMenueDiv_y + lifeWizardMenueDiv_height))
	{		
		// keep showed
		return true;
		
	} else {
		// hide
		return false;
	}	
}

/*
 * 
 */
function tx_jo_cw_closeLifeWizard()
{
	$("#main-navigation-section .keyvisual-box .keyvisual-overlay").fadeOut("fast");
	tx_jo_cw_hideNowAllWizards(true);
}

