function ___getPageSize(){var xScroll,yScroll;if(window.innerHeight&&window.scrollMaxY){xScroll=window.innerWidth+window.scrollMaxX;yScroll=window.innerHeight+window.scrollMaxY;}else if(document.body.scrollHeight>document.body.offsetHeight){xScroll=document.body.scrollWidth;yScroll=document.body.scrollHeight;}else{xScroll=document.body.offsetWidth;yScroll=document.body.offsetHeight;}
var windowWidth,windowHeight;if(self.innerHeight){if(document.documentElement.clientWidth){windowWidth=document.documentElement.clientWidth;}else{windowWidth=self.innerWidth;}
windowHeight=self.innerHeight;}else if(document.documentElement&&document.documentElement.clientHeight){windowWidth=document.documentElement.clientWidth;windowHeight=document.documentElement.clientHeight;}else if(document.body){windowWidth=document.body.clientWidth;windowHeight=document.body.clientHeight;}
if(yScroll<windowHeight){pageHeight=windowHeight;}else{pageHeight=yScroll;}
if(xScroll<windowWidth){pageWidth=xScroll;}else{pageWidth=windowWidth;}
arrayPageSize=new Array(pageWidth,pageHeight,windowWidth,windowHeight);return arrayPageSize;};function ___getPageScroll(){var xScroll,yScroll;if(self.pageYOffset){yScroll=self.pageYOffset;xScroll=self.pageXOffset;}else if(document.documentElement&&document.documentElement.scrollTop){yScroll=document.documentElement.scrollTop;xScroll=document.documentElement.scrollLeft;}else if(document.body){yScroll=document.body.scrollTop;xScroll=document.body.scrollLeft;}
arrayPageScroll=new Array(xScroll,yScroll);return arrayPageScroll;};



jQuery(function($){

	var pathToSwfs = "swf/";
	var pathToImages = "img/";


	// zusammen mit CSS den gepunkteten Rahmen loswerden
	$("a").click(function() {
		this.blur();
	});


	// füge Drucker-Icon hinzu
	//$('<p id="print-icon">Drucken</p>').click(function() {
	//	window.print();
	//}).appendTo("#container");
	$('<p id="print-icon" onClick="printPage();">Drucken</p>').appendTo("#container");



	// setup Mainmenü: subnavi anfügen, menü-hover initialisieren
	$("#nav>li").each(function(i) {
		if (!submenus[i]) return;

		var thisLi = $(this).append($(submenus[i]).hide().css("opacity", .9));

		var menuOpenerTimeout = null,
			thisLink = $(">a", this),
			thisSubmenu = $(">ul", this);


		thisLi.hover(function() {
			thisLink.addClass("hasfocus");

			menuOpenerTimeout = window.setTimeout(function() {
				menuOpenerTimeout = null;
				thisSubmenu.slideDown(function() {
					// falls man zu schnell rein-raus-rein fährt, kann hasfocus verloren gehen
					// deswegen hier noch mal hinzufügen
					thisLink.addClass("hasfocus");
				});
			}, 250);


		}, function() {
			if (menuOpenerTimeout) {
				window.clearTimeout(menuOpenerTimeout);
				thisLink.removeClass("hasfocus");
				return;
			}
			thisSubmenu.slideUp(function() {
				thisLink.removeClass("hasfocus");
			});
		});
	});





	// dropdown in metanav initialisieren
/*	$("#metanavs li.dropdown").each(function() {
		var dropwDownTimeout = null;
		$(this).hover(function() {
			var ul = $(">ul", this);
			dropwDownTimeout = window.setTimeout(function() {
				ul.slideDown();
				dropwDownTimeout = null;
			}, 100);

		}, function() {
			if (dropwDownTimeout) {
				window.clearTimeout(dropwDownTimeout);
			}
			else {
				$(">ul", this).slideUp();
			}

		});
	});
*/

	$("#metanavs li.dropdown").each(function() {
        var slideUpTimeout;
        var el = this;
		$(el).hover(function() {
            window.clearTimeout(slideUpTimeout);
			var ul = $(">ul", el).slideDown();
		}, function() {
            slideUpTimeout = window.setTimeout(function() {
				$(">ul", el).slideUp();
            }, 500);
		});
	});


	// label "SEARCH" in input, diesen bei focus ggf. löschen
	$("#search label").each(function() {
		var thisLabel = $(this).hide(),
			labelFor = thisLabel.attr("for"),
			input = $("#"+labelFor),
			newValue = $(this).hide().text().toUpperCase();
		//alert(this["for"]);
		if (input[0] && !input[0].value) input[0].value = newValue;

		input.bind("focus", function() {
			if (this.value == newValue) this.value = "";
		}).bind("blur", function() {
			if (!this.value) this.value = newValue;
		});

	});



	// flvplayer an die entsprechenden Links binden
	$("a.flvplayer, a.swfplayer").bind("click", function() {
		if (!swfobject) {
			alert("swfobject is not embedded, therefore cannot embed player");
			return;
		}

		var sizes = this.hash.match(/(\d+)x(\d+)/);
		var url = this.href.replace("#"+sizes[0], "#");
		if (url.substring(url.length-1) == "#") url = url.substring(0, url.length-1);
		//alert("Öffne Video "+url+" in "+sizes[1]+"x"+sizes[2]);
		var jWin = $(window);
		var pageSize = ___getPageSize();

		var isSWF = $(this).hasClass("swfplayer");


		var screenSize = [$(window).width()-20, $(window).height()-20];
		// falls zu groß für Screen -> resize
		if (sizes[1] > screenSize[0] || sizes[2] > screenSize[1]) {
			var scale = Math.min((screenSize[0])/sizes[1], (screenSize[1])/sizes[2]);
			sizes[1] = sizes[1]*scale;
			sizes[2] = sizes[2]*scale;
		}



		//
		var div = $("<div id=\"fullscreen-flvplayer-container\"><div id=\"fullscreen-close\"></div><div id=\"fullscreen-flvplayer\"></div></div>").css({"margin-left": -Math.floor(sizes[1]/2)+jWin.scrollLeft(), "margin-top": -Math.floor(sizes[2]/2)+jWin.scrollTop()});

		$('body').append($('<div id="jquery-overlay">').css({background:"#000",opacity:.92,width:pageSize[0],height:pageSize[1]}).fadeIn(function(){

			$("body").append(div);

			// embed normal swf
			if (isSWF) {
				swfobject.embedSWF(url, "fullscreen-flvplayer", sizes[1], sizes[2], "9.0.0", pathToSwfs+"expressInstall.swf", {}, {}, {});
			}

			else {
				var flashvars = {
					"file": url//,
					//"image": "video/Dynafit_Intro.jpg"
				};
				var params = {};
				var attributes = {};

				swfobject.embedSWF(pathToSwfs+"player.swf", "fullscreen-flvplayer", sizes[1], sizes[2], "9.0.0", pathToSwfs+"expressInstall.swf", flashvars, params, attributes);
			}



			var bg = $(this);
			bg.add("#fullscreen-close").click(function() {
				div.remove();
				bg.fadeOut(function() {
					$(this).remove();
				});
			});

		}));










		return false;
	});


	// nur ausführen, wenn lightbox wirklich eingebunden
	if ($.fn.lightBox) {
		//absolutePath = window.location.protocol == "file:" ? "" : "";
		// lightbox für gallery binden
		$(".imagegallery a").lightBox({
			overlayOpacity:0.92,
			imageLoading:pathToImages+'lightbox/ico-loading.gif',
			imageBtnPrev:pathToImages+'lightbox/btn-prev.gif',
			imageBtnNext:pathToImages+'lightbox/btn-next.gif',
			imageBtnClose:pathToImages+'lightbox/btn-close.gif',
			imageBlank:pathToImages+'lightbox/blank.gif',
			containerBorderSize:0
		});
	}









	// links mit highlight binden
	var selectors = ["bg-light", "bg-dark", "bg-emphasis"];
	for (var i = 0, selector; selector = selectors[i]; i++) {
		$("."+selector).not(".no-hover").each(function() {
			var thisContainer = $(this),
				theClass = selector;
			$("a", this).hover(function() {
				thisContainer.addClass(theClass+"-hover").removeClass(theClass);
			}, function() {
				thisContainer.addClass(theClass).removeClass(theClass+"-hover");
			});
		});
	}

	$("a img").not(".no-hover").hover(function() {
			if ($(this).attr('class').indexOf('reverse') != -1) {
					$(this).css("opacity", 1);
			} else {
					$(this).css("opacity", 0.86);
			}
	}, function() {
			if ($(this).attr('class').indexOf('reverse') != -1) {
					$(this).css("opacity", 0.86);
			} else {
					$(this).css("opacity", 1);
			}
	});

	$("a img").each(function (i){
			if ($(this).attr('class').indexOf('reverse') != -1) {
					$(this).css("opacity", 0.86);
			}
	});
/*
	$("a img").not(".no-hover").hover(function() {
		$(this).css("opacity", 0.8);
	}, function() {
		$(this).css("opacity", 1);
	});
*/



	// füge zentrierung für centerimage hinzu
	$("img.centerimage").each(function() {
		var thisImage = $(this);

		replaceWith = $('<div></div>').css({
			"max-width": thisImage.width(),
			//"overflow": "hidden",
			//"position": "relative"
			"background-position": "center center",
			"background-repeat": "no-repeat",
			"background-image": "url("+this.src+")"
			}).height(thisImage.height());

		if (thisImage.not(".no-hover").closest("a").get(0) != undefined) {
			replaceWith.hover(function() {
				$(this).css("opacity", 0.8);
			}, function() {
				$(this).css("opacity", 1);
			});
		}

		thisImage.replaceWith(replaceWith);
	});








	// die Inhaltsboxen an das Grid anpassen
	var gridBoxes = $("div.gridheight-1, div.gridheight-2, div.gridheight-3"),
		gridHeight = window.gridHeight || 143,
		//gridHeight = 72,
		gridPadding = 2;

	function onResizeGrid() {
		gridBoxes.each(function() {
			thisDiv = $(this);

			this.originalMinHeight = this.originalMinHeight || thisDiv.css("min-height");

			thisDiv.css("min-height", this.originalMinHeight);
			//thisDiv.height("auto");
			thisHeight = thisDiv.height();
			nrOfCells = Math.ceil((thisHeight-gridHeight)/(gridHeight+gridPadding)) + 1;

			//console.log(thisHeight + " => " + nrOfCells);
			//thisDiv.height(nrOfCells*gridHeight + (nrOfCells-1)*gridPadding);
			thisDiv.css("min-height", nrOfCells*gridHeight + (nrOfCells-1)*gridPadding);
		});
	}
	onResizeGrid();
	$(window).bind("resize", onResizeGrid);




	var fillers = $("div.fillheight");
	function onResizeFiller() {
		fillers.each(function() {
			var h = Math.max(0, 715-this.offsetTop);
			if (!h) $(this).hide();
			else $(this).height(h).show();
			// position on page
			//console.log(this.offsetTop)
			//console.log(this.offsetParent.offsetTop)
		});
	}
	onResizeFiller();
	$(window).bind("resize", onResizeFiller);

});

function swapImages (id) {

	var arrOfId = id.split('_');
	$('.' + arrOfId[0]).hide();
	$('#' + id).show();

}

function printPage () {

    var langId = $("#langid").val();
    if (langId == "") {
        langId = 0;
    }

	window.open("/index.php?id=" +$("#pageid").val() +"&L=" +parseInt(langId) +"&type=98");

}
