var animationSpeed = 200;

// JQUERY
$(function(){			
	// Contentbox automatisch schlieszen und schön auffahren
//	$('#cnt-bg-middle').hide();
	var showContentInterval = window.setInterval(showContent, 700);
	
	function showContent() {
		$('#cnt-bg-middle').animate({
			height: 'show'
		}, 1200);		    
		window.clearInterval(showContentInterval);
	}
	/** Content Toggle Funktion **/
	$("a#cnt-toggle").click().toggle(function() {		
		$('#cnt-bg-middle').animate({
			height: 'hide'
		}, 'slow', function () {			
			$("a#cnt-toggle").addClass("cnt-closed");
			$('#cnt-bg-bottom').css('margin-top', '-2px');
		});
	}, function() {		
		$('#cnt-bg-middle').animate({
			height: 'show'
		}, 'slow', function () {			
			$("a#cnt-toggle").removeClass("cnt-closed");
			$('#cnt-bg-bottom').css('margin-top', '0px');
		});
	});
	
	/** Navigation **/
	// hide all sub trees except the current one
	$('#nav ul ul').each(function () {		
		if (!$(this).parent("li").hasClass('current_page_parent')) {
			$(this).hide();						
		}
	});
	// no fading in IE because it looks ugly
	if (typeof $.browser.msie == "undefined") {	
		var argsHide = { height: 'hide', opacity: 'hide' };
		var argsShow = { height: 'show', opacity: 'show' };				
	} else {
		var argsHide = { height: 'hide' };
		var argsShow = { height: 'show' };
	}
	$('#nav > ul > li > a').each(function () {
		// Home-btn stay normal
		if (!$(this).parent().hasClass("page_item_home") ) {
			$(this).removeAttr('href');
			$(this).click(function () {
				if ($(this).next("ul").is(":visible") == false) {
					// close others				
					$('#nav ul li ul').animate(argsHide);				
					$('#nav ul li').removeClass('current_page_parent');
					// open itself
					$(this).next("ul").animate(argsShow);
					$(this).parent("li").addClass('current_page_parent');
				} else {
					// close itself
					$(this).next("ul").animate(argsHide);
					$(this).parent("li").removeClass("current_page_parent");
				};			
			});
		}
	});
	
	/*** Search - onClick delete label ***/
	$("#search-text").click(function(){
		$("#search-text").val("");
	});
	
	createBgAjaxLinks();
	adaptTables();
	
	/** Tabs Content - AJAX Loader **/
	$('#cnt-tabs a').each(function() {
		$(this).attr('click', $(this).attr('href'));
		$(this).removeAttr('href');
		$(this).css('cursor','pointer');
		$(this).unbind('click');
		$(this).click(function() {
			$('#cnt-tabs a').each(function() {
				$(this).removeClass("active-tab");
			});
			$('#content').html($("#contentLoader").html());
			// Google Anal Tracking
			try {
			var pageTracker = _gat._getTracker("UA-10262650-1");
			pageTracker._trackPageview($(this).attr('click') + "?btn=content");
			} catch(err) {}
				
			$('#contentBG').load($(this).attr('click') + "?btn=content", function() {
				$('#content').hide();
				$('#content').html($("#contentBG").html());
				if (typeof $.browser.msie == "undefined") {
					$('#content').animate({
						height: 'show',
						opacity: 'show'
					}, 400);
				} else {
					$('#content').show();
				}
				adaptTables();
			});
			$(this).addClass("active-tab");
		});
	});
	
	/** Home Teaser **/	
	// add some html
	$("#home-teaser > ul > li").each(function(){
		if(this.childNodes[0] && this.childNodes[0].nodeType == 3) {
			var data = $.trim(this.childNodes[0].data);
			if(data) {
				this.childNodes[0].data = data;
				$(this.childNodes[0]).wrap('<h2></h2>');
			}
		}
		$(this).append('<div class="hr"></div>');		
		$("ul", this).wrap('<div class="box"></div>');
		$(".box", this).prepend('<div class="hr"></div>');	
	});
	$("#home-teaser > ul > li:last-child > .hr").remove();
	// hide sub content
	$("#home-teaser .box").hide();
	// toggle function
	$("#home-teaser h2").toggle(function(){
		$(this).addClass("open");
		$(this).next(".box").animate({ height: 'show', opacity: 'show' }, 500);
	},function(){
		$(this).removeClass("open");
		$(this).next(".box").animate({ height: 'hide', opacity: 'hide' }, 500);
	});
	
	/** Fancybox **/
	// open booking form in fancybox (but not in IE because it doesn't allow third party cookies - http://adamyoung.net/IE-Blocking-iFrame-Cookies)
	//if (typeof $.browser.msie == "undefined") { // it works now, don't know why - 15.07.2010
	$("#nav #nav-onl-buchen, #cnt-footer #onl-buchen, #cnt-head #booking-button, #content a[href^=http://80.123.202.242/WBE3]").fancybox({
		'type': 'iframe',
		'width': 970,
		'height': 600,
		'hideOnOverlayClick': false,
		'overlayColor': '#000',
		'overlayOpacity': 0.7,
		'titleShow': false
	});		
	//}
	
	/** Newsletter form **/		
	$("#newsletter-form-submit").click(function(){
		$("#newsletter-form .wpcf7-not-valid-tip-no-ajax").remove();
		var errors = false;
		var last_name = $("#newsletter-form-name");
		var email = $("#newsletter-form-email");		
		if (last_name.val() == "") {
			errors = true;
			last_name.after('<div class="wpcf7-not-valid-tip-no-ajax">' + lang_labels.form_empty_field + '</div>');			
		}
		if (email.val() == "") {
			errors = true;
			email.after('<div class="wpcf7-not-valid-tip-no-ajax">' + lang_labels.form_empty_field + '</div>');			
		}		
		var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;		
		if (email.val() != "" && !filter.test(email.val())) {
			errors = true;
			email.after('<div class="wpcf7-not-valid-tip-no-ajax">' + lang_labels.form_invalid_email + '</div>');			
		}		
		if (errors) {
			return false;
		}
	});
	
	// Set wmode for embedded youtube videos
	$("embed").attr("wmode", "transparent");
	
	/** Newsletter HoverAd **/
	$("#gewinnspiel").animate( { "marginTop": 0 }, 1300);
	$("#gewinnspiel-close").click(function() {
		$("#gewinnspiel").animate( { "marginTop": -1000 }, 1300, function() {
			$("#gewinnspiel").remove();
		});
	});
	
	
});

function createBgAjaxLinks() {	
	/* Ajax BG Image Reload */
	$('#bgimage-btn').each(function() {
		$(this).attr('click', $(this).attr('href'));
		$(this).removeAttr('href');
		$(this).css('cursor','pointer');
		$(this).unbind('click');
		$(this).click(function() {			
			// Hintergrund und cnt-head-nav werden ausgeblendet, ausgetauscht und wieder angezeigt
			$('#hcenterBG').load($('#bgimage-btn').attr('click') + "&btn=bgimage", function() {
				$('#hcenter').animate({
					opacity: 'hide'
				}, animationSpeed, function() {
					$('#hcenter').html($('#hcenterBG').html());
					$('#hcenter').animate({
						opacity: 'show'
					}, animationSpeed);
				});				
			});

			$('#img-navBG').load($('#bgimage-btn').attr('click') + "&btn=preview", function() {				
				if (typeof $.browser.msie == "undefined") {				
					$('#img-nav').animate({
						opacity: 'hide'
					}, animationSpeed, function() {						
						// Contentbox automatisch schlieszen
						if (!$("a#cnt-toggle").hasClass("cnt-closed")) {
							$('a#cnt-toggle').trigger('click');
						}
						$('#img-nav').html($('#img-navBG').html());
						$('#img-navBG').html('');
						createBgAjaxLinks();
						$('#img-nav').animate({
							opacity: 'show'
						}, animationSpeed, function() {
						});
					});				
				} else {
					$('#img-nav').html($('#img-navBG').html());
					$('#img-navBG').html('');
					createBgAjaxLinks();
				}
			});
			
		});
	});
}

function adaptTables() {
	if ($("#content .dmc-table-headline").length > 1) {		
				
		/** Table - reset style height, add from wordpress **/
		$("#content .dmc-table").each(function() {
			$(this).height(10).addClass("dmc-table-adapted");			
		});
		/** Table - Close add begin **/
		$("#content .dmc-table-headline").nextUntil(".dmc-table-headline").hide();
		
		$("#content .dmc-table-headline").each(function() {
			$(this).addClass("table-open");
			$("td", this).prepend('<span class="label-open">' + lang_labels.open + '</span><span class="label-close">' + lang_labels.close + '</span>');
		});
	
		/** Table Headline - Close **/
		$("#content .dmc-table-headline").toggle(function () {
			$(this).removeClass("table-open").addClass("table-close");
		// Deaktiviert wegen IE
		//	$(this).nextUntil(".dmc-table-headline").animate({
		//		opacity: 'show'
		//  });
			$(this).nextUntil(".dmc-table-headline").show();
		//	alert("toggle me");
		}, function () {
			$(this).addClass("table-open").removeClass("table-close");
		// Deaktiviert wegen IE
		//	$(this).nextUntil(".dmc-table-headline").animate({
		//  	opacity: 'hide'
		//  });
			$(this).nextUntil(".dmc-table-headline").hide();
		});
		
		/** Open Table Section if get parameter "opentable" is given or JS Variable setOpentable=1 is set **/
		var paramOpentable = -1;
		if (getUrlVars()["opentable"] > 0) {
			paramOpentable = getUrlVars()["opentable"];
		}
		
		isDefined = function(variable){ 
			return this[variable] === undefined ? false : true;
		};
		if (isDefined("getOpentable")) {
			paramOpentable = getOpentable;
		}
		var countTablesections = 0;
		$("#content .dmc-table-headline").each(function() {
			countTablesections++;
			if (paramOpentable == countTablesections) {
				/* of current section is to open, trigger event click to active the toggle */
				$(this).trigger('click');	
			}
		});
		
	} // if counterHeadlines
	
	/** Table - alternating td bg color **/
	var dmcTdCount = 0;
	$("#content .dmc-table tr").each(function () {
		var thisClass = $(this).attr("class");
		var isTable = thisClass.match(/dmc-table-headline/g);
		//alert(isTable);
		if (dmcTdCount % 2 == 0 && isTable != "dmc-table-headline" && $(this).children("td").attr("class") != "dmc-table-subheadline") {
			$(this).addClass("dmc-table-color2");	
		} else if (isTable == "dmc-table-headline" || $(this).children("td").attr("class") == "dmc-table-subheadline") {
			dmcTdCount = 0;
		}
		dmcTdCount++;
	});
}

function getUrlVars()
{
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	for(var i = 0; i < hashes.length; i++)
	{
    hash = hashes[i].split('=');
    vars.push(hash[0]);
    vars[hash[0]] = hash[1];
	}
	return vars;
}
