$(document).ready(function() {
	initLayer();
});

// preload the background
var backgroundImg = "/_resources/css/import/img/bg_layer.png";

var aLayerContentImages = new Array();
var currentImage = 0;
var useAjax = true;

function initLayer()
{
	
	
	// IE --> AJAX test
	if($.browser.msie)
	{
		var ajaxTest = new ActiveXObject("Msxml2.XMLHTTP") 
		if(typeof ajaxTest != 'object' )
		{
			useAjax = false;
		}
	}
	
	// ajax ok!
	if(useAjax)
	{
		
		// hide layer
		$("div#case-study-layer").hide();

		// hide print-layer
		//$("div#print-layer").fadeTo(0.1, 1000);
		
		// show layer
		$("ul.references a").click(function(){
			showLayer(this.id, this.href);
			return false;
		});
			
		
		// bookmarked ? 
		var uri = window.location + ''; //making a string
		uri = uri.replace("%23","#"); // replacing encoding
		if(uri.indexOf("##") != -1)
		{
			uri = uri.split("#");
			showLayer(uri[2], $("a#"+uri[2])[0].href);

		}

	}

}


function showLayer(id, pUrl)
{
	$.ajax({
		    type: "GET",
			url: pUrl,
			async: false, // blocks showing the layer
			success: function(html)
			{
				
				// "imagetoolbar" craches IE 6
				html = html.replace("imagetoolbar", "");
								
				// nice way, but doesn't work on IE < 7
    			var layerContent = $(html).children("div#case-study-layer").html();
				if(!layerContent)
				{
					layerContent = $(html).find("div").filter("div#case-study-layer").html();
				}

				
				//insert Content
				$("div#case-study-layer").html(layerContent);
				
				// insert print content
				$("div#print-layer").html($(layerContent).filter("div.content").html());
				
				
				// hide first image
				$("img#case-study-main-image").hide();
			
				// order the buttons on the background
				$("div#menu-movingarea").css("z-index", 2);
				$("div#imprint").css("z-index", 2);
				$("div#headline-quadrat-business-area").css("z-index", 2);
					
			
				//  making bookmarkable
				var uri = window.location + '';//making a string
				uri = uri.split("#");
				window.location = uri[0] + "##" + id;
				
				//scroll to top
				window.scrollTo(0,0);
				
				//fadeIn --> flickers in IE ?
				$("div#case-study-layer").fadeIn();
				// just showing
				//$("div#case-study-layer").animate({opacity: 1},3000);
				
				$('#case-study-images').removeClass("no-js");
				
				var imageFlowSocialMedia = new ImageFlow();
				imageFlowSocialMedia.init({
					ImageFlowID: 'case-study-images',
					onClick: 'javascript:void(0)',
					reflections: false,
			        reflectionP: 0.0,
			        slider: Info.browser.isIEpre7 ? false : true,
					buttons: Info.browser.isIEpre7 ? false : true,
				 	opacity: true,
					opacityArray: [10,4,3,2,1],
					imageFocusM: 1.4,
					sliderCursor: 'pointer',
					xStep: 100
				});

				// animate button
				$("a#case-study-business-area-layer-close").click(function(e){
					e.stopPropagation();
					hideLayer();
					return false;		
				});
				
				
  			}
		  });	

}

function hideLayer()
{	
	
	
	// disable bookmark-anchor
	var uri = window.location + '';//making a string
	uri = uri.split("#");
	window.location = uri[0] + '#';

	// hide print content
	$("div#print-layer").html("");
	
	//fadeOut --> flickers in IE ?
	$("div#case-study-layer").fadeOut(400, function()
	{
		// order the buttons to the foreground
		$("div#menu-movingarea").css("z-index", 5);
		$("div#imprint-movingarea").css("z-index", 5);
	});
	// just hiding
	/*$("div#case-study-layer").hide();
	// order the buttons to the foreground
	$("div#menu-movingarea").css("z-index", 5);
	$("div#imprint-movingarea").css("z-index", 5);*/



}
