$().ready(function(){
	_initLayer();
});

function _initLayer(){
	/*
	 * Opens Layer on click
	 */
	$('div#headervisual-zone img').click(function ()		{
			_openLayer();
		}
	).css('cursor','pointer');


	/*
	 * Assigning click events in elements to close overlay
	 */
	$('#close-layer').click(function(){
		_closeLayer();									
	});

	// If window was resized, calculate the new overlay dimensions
	$(window).resize(function() {
		// Get page sizes
		var arrPageSizes = ___getPageSize();
		// Style overlay and show it
		$('#page-overlay').css({
			width:		arrPageSizes[0],
			height:		arrPageSizes[1]
		});
	});
}



function _openLayer()
{
	var arrPageSizes = ___getPageSize();
	/*
	 * Applies the Overlay HTML markup into body tag and Styles it,
	 * than prepare the close functionality
	 */
	$('div#headervisual-zone').append('<div id="page-overlay"></div>');	
	$('#page-overlay').css({
		opacity:			0.75,
		width:				arrPageSizes[0],
		height:				arrPageSizes[1]
	}).fadeIn();

	/*
	 * Hide some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
	 */
	$('embed, object, select').css({ 'visibility' : 'hidden' });

	// Scroll to top
	window.scrollTo(0, 0);

	//Shows the Testimonial and renders the Headline
	$("#testimonial-wrapper").show();
	_buildTestimonialNavigation();
}



/*
 * Hides the Layer and removes the page overlay
 */
function _closeLayer(){
	$("div#testimonial-wrapper").hide();
	$("div#page-overlay").fadeOut().remove();
	$("div#page-overlay").detach();
	// Show some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
	$('embed, object, select').css({ 'visibility' : 'visible' });
}



/*
 * Renders the Headlines inside the Layer
 */
function _renderHeadlines(currentTestimonial)
{
	if(typeof sIFR == "function") {
		sIFR.replaceElement(named({sSelector: "div" + currentTestimonial + " div.title-section div.sifr-layer h1", sFlashSrc: SIFR_SWF_PATH , sColor:"#e60a3c", sFlashVars:"offsetTop=-1" , sWmode:"transparent"}));
		return false;
	};
}



/*
 * 
 */
function _buildTestimonialNavigation()
{
	$(".testimonial-features ul").idTabs(function(id,list,set)
	{
		$("a",set).parent("li.active").removeClass("active");
		$("a",set).filter("[href='" + id + "']",set).parent().addClass("active");

		for(i in list)
		{
			$(list[i]).hide();
		};

		$(id).show();

		// Builds the Slide Navigation inside the Testimonial
		_buildSlideNavigation(id);

		// Headline rendering
		_renderHeadlines(id);
		
		return false;
	});
}



function _buildSlideNavigation(currentTestimonial)
{
	/*
	 * Die untere Zeile wird verwendet wenn die Navigation eigenständig
	 * und nicht für das entsprechende Testimonial erstellt werden soll.
	 * So erfolgt der call aus der Funktion _buildTestimonialNavigation()
	 * 
	 * $(".slide-navigation").idTabs(settings,function(id,list,set) 
	 */

	$(currentTestimonial + " .slide-navigation").idTabs(function(id,list,set)
	{
		$("a",set).removeClass("active").filter("[href='" + id + "']",set).addClass("active");

		for(i in list)
		{
			$(list[i]).hide();
		};

		$(id).show();

		return false;
	});
}



/*
* getPageSize() by quirksmode.com
*
* @return Array Return an array with page width, height and window width, height
*/
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){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};
