/* console catch */

try {
	console.log('console initialised...');
} catch (e) {
	var console = {"log":function() {}};
}

var quicklinksOverlayTimeout;
var serviceNaviOverlayTimeout;

$(document).ready(function(){
	var isHome = false;
	if($('#homeOuter').length > 0) {
		isHome = true;
	}

	// smart font sizing. Read last front size from cookie
	// --ld 2010-11-25
	var currentFontSize = parseInt($('#contentText').css('font-size'));
	if( $.cookie("fontsize") ) {
		setFontSize($.cookie("fontsize"));
	}
	
	// on home page: briefly show service dropdown, then hide it again
	if(isHome) {
		$('#infoSub').show();
		setTimeout( function() {
			$('#infoSub').slideUp(600);
		}, 900);
	}

	// Service + Ratgeber 
	$('#serviceNavi #serviceNaviBtn').mouseenter( function() {
		clearInterval(serviceNaviOverlayTimeout);
		// hide all other quicklink overlays
		$("#serviceNavi ul").fadeIn(100);
	});
	
	$('#serviceNavi #serviceNaviBtn').mouseleave( function() {
		serviceNaviOverlayTimeout = setTimeout("hideServiceNaviOverlay()",700);
	});
	
	$('#serviceNavi ul').mouseenter(function() {
		clearInterval(serviceNaviOverlayTimeout);
	});
	
	$('#serviceNavi ul').mouseleave(function() {
		$(this).fadeOut(100);
	});
	
	// Navigation 
	$('#mainNavi ul li ul li').hover(function() {
		$(this).find("ul").stop(true, true).fadeIn(200);
		$(this).addClass('hover');
	} ,function() {
		$(this).find("ul").stop(true, true).fadeOut(200);
		$(this).removeClass('hover');
	});
	
	// Accordion
	// icon accordions some accordions need to be collapsed on init
	var isCollapsed = false;
	hash = window.location.hash.replace('\!','');
	var acoDefaultActive = 'null';
	if($(hash).parent('div').hasClass('accordion')) {
		acoDefaultActive = $(hash).children('h3');
	}
	
	var bolScrollAfterChange = false;
	
	$(".accordion").accordion({ header: "h3",
								autoHeight: false,
								active: acoDefaultActive,
								collapsible: true,
								icons: {},
								change: function(evt) {
								console.log($(evt.currentTarget.activeElement).parent('div').attr('id'));
									window.location.hash = '!' + $(evt.currentTarget.activeElement).parent('div').attr('id');
									if( bolScrollAfterChange ) {
										$.scrollTo(window.location.hash.replace('\!',''), 300);
										bolScrollAfterChange = false;
									}
							}});
	// has hash on load?
	if(hash) {
		window.location.hash = hash; 
	}
	
	$('.accordion .ui-accordion-content a').click(function(evt) {
		if( this.pathname == window.location.pathname
				&& this.hash != '#outerContainer') {
			$('.accordion').accordion("activate",$(evt.currentTarget.hash + ' h3'));
			window.location.hash = '!' + evt.currentTarget.hash.replace('#','');
			bolScrollAfterChange = true;
			return false;
			
		}
		
	});
	
	$('#loginName').focus( function() {
		if($(this).attr('value') == "Benutzername") {
			$(this).attr('value',''); 
		}
	});
	$('#loginName').blur( function() {
		if($(this).attr('value') == "") {
			$(this).attr('value','Benutzername'); 
		}
	});
	
	$('#loginPw').focus( function() {
		if($(this).attr('value') == "Passwort") {
			$(this).attr('value','');
		}
	});
	$('#loginPw').blur( function() {
		if($(this).attr('value') == "") {
			$(this).attr('value','Passwort'); 
		}
	});

	// Home-Stage
	var allHomeSwitchButtons = new Array();	
	var allHomeImages = $('div.homeScreen .homeContainer');
	allHomeImages.hide();

	var currentImg = Math.floor(Math.random()*allHomeImages.length);
	$(allHomeImages[currentImg]).show();
	var rotationTimeout;
	$('#homeSwitcher ul').html('');
	for(var i=0;i<allHomeImages.length;i++) {
		
		var switchBtn = $('<li>' + (i+1) + '</li>');
		if(i == currentImg) {
			switchBtn.addClass('active');
		}
		switchBtn.click(switchButtonClickHandler);
		
		allHomeSwitchButtons.push(switchBtn);
		$('#homeSwitcher ul').append(switchBtn);
	}
	// init loop
	var nextImage = currentImg + 1;
	if(nextImage >= allHomeImages.length) {
		nextImage = 0;
	}
	clearTimeout(rotationTimeout);
	rotationTimeout = setTimeout(function(){switchHomeBanner(nextImage)},5000);
	
	function switchButtonClickHandler (evt) {
		
		var whichBtn = parseInt($(evt.currentTarget).text()) - 1;
		switchHomeBanner(whichBtn, true);
	}
	
	function switchHomeBanner(target,stopRotate) {
		if(typeof(stopRotate) == 'undefined') {
			var stopRotate = false;
		}
		if(target != currentImg) {
			
			$('#homeSwitcher ul li.active').removeClass('active');
			allHomeSwitchButtons[target].addClass('active');
			
			currentImg = target;
			$(allHomeImages).filter(':visible').fadeOut(1000);
			$(allHomeImages[target]).fadeIn(1000);
			var nextImage = currentImg + 1;
			if(nextImage >= allHomeImages.length) {
				nextImage = 0;
			}
			clearTimeout(rotationTimeout);
			if(!stopRotate) {
				rotationTimeout = setTimeout(function(){switchHomeBanner(nextImage)},5000);
			}
		}
	}
	
	$('#quicklinksButtons a.quickLinkButton').mouseenter( function() {
		clearInterval(quicklinksOverlayTimeout);
		// hide all other quicklink overlays
		$('#quicklinksButtons div.quicklinkOverlay').fadeOut(100);
		
		var quicklinkId = this.id.split("_")[1];
		// 
		$("#quicklinkOverlay_" + quicklinkId).fadeIn(100)
			.offset({left: $(this).offset().left});
	});
	
	
	$('#quicklinksButtons a.quickLinkButton').mouseleave( function() {
		quicklinksOverlayTimeout = setTimeout("hideQuicklinkOverlay()",700);
	});
	
	
	
	$('#quicklinksButtons a.quickLinkButton').click( function(evt) {
		evt.preventDefault();
		var quicklinkId = this.id.split("_")[1];
		
		if($("#quicklinkOverlay_" + quicklinkId).is(':visible')) {
			$('div.quicklinkOverlay').fadeOut(100);
		} else {
			$('div.quicklinkOverlay').fadeOut(100);
			$("#quicklinkOverlay_" + quicklinkId).fadeIn(100);
		}
	});

	$('.quicklinkOverlay').mouseenter(function() {
		clearInterval(quicklinksOverlayTimeout);
	});
	
	$('.quicklinkOverlay').mouseleave(function() {
		$(this).fadeOut(100);
	});
	
	$('#fontSizeMinus').click( function (evt) {
		setFontSize( parseInt($('#contentText').css('font-size')) - 1 );
		evt.preventDefault();
		evt.currentTarget.blur();
	});
	$('#fontSizePlus').click( function (evt) {
		setFontSize( parseInt($('#contentText').css('font-size')) + 1 );
		evt.preventDefault();
		evt.currentTarget.blur();
	});
	
	function setFontSize( intFontSize ) {
		// limit font size to min: 11, max: 16
		if(intFontSize <= 16 && intFontSize >= 11) {
			currentFontSize = intFontSize;
			$('#contentText').css('font-size',intFontSize + 'px');
			$.cookie('fontsize',intFontSize);
		}
	}

	$('#homeFlash').click(function() {
		hideIntroOverlay();
	})

	function hideIntroOverlay() {
			$('#homeFlash').hide();
			$('.homeScreen').show();
			$('#homeSwitcher ul').show();
	}
	
	$('table.tabTable tr td a').parent('td').css('cursor','pointer').click(function(evt) {
		window.location.href = $(this).find('a').attr('href');
	});
	
	$('#mainNavi').children('ul').children('li').children('ul').children('li').children('a').focus(function() {
		$(this).blur();
	});
	
	
	$('div#mainNavi div#serviceNavi a#serviceNaviBtn').click(function() {
		return false;
	}).focus(function() {
		$(this).blur();
	});
	
	/* open all accordions for print-version */
	$('#print').click(function() {
		$('.ui-accordion-content').show();
	});
	// login-part
	$('#login').click( function() {
		$('#loginButton ul').show();
	});
	$('#username, #password').focus( function() {
		$(this).css("background-image", "none");
	});
	
	$("a[rel=imageGallery]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">' + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
	
	
});

function hideQuicklinkOverlay() {
	 $('#quicklinksButtons div.quicklinkOverlay').fadeOut(100);
};

function hideServiceNaviOverlay() {
	 $('#serviceNavi ul').fadeOut(100);
};

function popup(url, width, height, scrollbar, resizable) {
   window.open(url, "popup", "width="+width+",height="+height+",status=yes,scrollbars="+scrollbar+",resizable="+resizable+"");
}
