$(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
		$("a.thumb_link").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();
			
				// init image-buttons
				currentImage = 0;
				preloadingImages(id);
				initButtons(id);
			
				// order the buttons on the background
				$("div#menu-movingarea").css("z-index", 2);
				$("div#imprint-movingarea").css("z-index", 2);
					
			
				//  making bookmarkable
				var uri = window.location + '';//making a string
				uri = uri.split("#");
				window.location = uri[0] + "##" + id;
				
				//fadeIn --> flickers in IE ?
				$("div#case-study-layer").show().fadeIn(300);
				// just showing
				//$("div#case-study-layer").animate({opacity: 1},3000);
				
				// animate button
				$("a#case-study-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);*/



}



function preloadingImages(id)
{ // for faster zooming

	if(typeof aLayerContentImages[id] == "undefined")
	{ // not yet preloaded --> on first load
	
		aLayerContentImages[id] = new Array();
	
		// preload backgroud
		$("<img>").attr("src", backgroundImg);

		
		// suche alle vorhandenen Bilder! 
		var images = $("img.case-study-images").get();
		for(var i = 0; i < images.length; ++i)
		{
			// preloading the images
			// thumbs
			$("<img>").attr("src", images[i].src);
			aLayerContentImages[id][i] = new Array();
			aLayerContentImages[id][i]['src'] = images[i].src;
			aLayerContentImages[id][i]['alt'] = images[i].alt;
			aLayerContentImages[id][i]['count'] = images[i].alt;
			
		}
		
	}

}

function initButtons(id)
{

	if(aLayerContentImages[id].length > 1)
	{ // mehr als nurt ein bild --> activieren
	
		// show buttons
		$("img.case-study-image-next").css("display","block");
		$("img.case-study-image-previous").css("display","block");
		
		// insert the first image
		$("div#case-study-image").html('<img src="'+aLayerContentImages[id][0]['src']+'" alt="'+aLayerContentImages[id][0]['alt']+'" />');
			
		 activateNextButton(id);
		 
		// register events
		$("img.case-study-image-next").click(function(){
			showNextImage(id);			
		});
		// register events
		$("img.case-study-image-previous").click(function(){
			showPreviousImage(id);			
		});
	}
	if(aLayerContentImages[id].length == 1)
	{ // just one
		$("div#case-study-image").html('<img src="'+aLayerContentImages[id][0]['src']+'" alt="'+aLayerContentImages[id][0]['alt']+'" />');
	}
}

function activateNextButton(id)
{
		$("img.case-study-image-next").get(0).src = $("img.case-study-image-next").get(0).src.replace("_d.gif", "_a.gif");
		if($.browser.msie)
		{ // IE needs hand!
			$("img.case-study-image-next").css("cursor","hand");
		}
		else{	
			$("img.case-study-image-next").css("cursor","pointer");
		}	
		

}

function deactivateNextButton(id)
{
		$("img.case-study-image-next").get(0).src = $("img.case-study-image-next").get(0).src.replace("_a.gif", "_d.gif");	
		$("img.case-study-image-next").css("cursor","auto");	
}

function activatePreviousButton(id)
{
		$("img.case-study-image-previous").get(0).src = $("img.case-study-image-previous").get(0).src.replace("_d.gif", "_a.gif");	
		if($.browser.msie)
		{ // IE needs hand!
			$("img.case-study-image-previous").css("cursor","hand");
		}
		else{	
			$("img.case-study-image-previous").css("cursor","pointer");
		}
}
function deactivatePreviousButton(id)
{
		$("img.case-study-image-previous").get(0).src = $("img.case-study-image-previous").get(0).src.replace("_a.gif", "_d.gif");	
		$("img.case-study-image-previous").css("cursor","auto");	
}

function showNextImage(layerId)
{
	
	// next image avaliable?
	if(currentImage < (aLayerContentImages[layerId].length - 1))
	{
		++currentImage;
		$("div#case-study-image").html('<img id="new_pic" style="z-index: 1;" src="'+aLayerContentImages[layerId][currentImage]['src']+'" alt="'+aLayerContentImages[layerId][currentImage]['alt']+'" />'
									  +'<img id="last_pic" style="z-index: 2;" src="'+aLayerContentImages[layerId][currentImage-1]['src']+'" alt="'+aLayerContentImages[layerId][currentImage-1]['alt']+'" />');
		
		// animation
		$("img#last_pic").fadeOut(600, function()
		{
			$("p#case-study-image-text").html(aLayerContentImages[layerId][currentImage]['alt']);
		});
		
		activatePreviousButton(layerId);
		
		if(currentImage == (aLayerContentImages[layerId].length - 1))
		{ // deactivate
			deactivateNextButton(layerId);
		}
		
		
	}

}

function showPreviousImage(layerId)
{
	
	// previous image avaliable?
	if(currentImage > 0)
	{
		--currentImage;
		$("div#case-study-image").html('<img id="new_pic" style="z-index: 1;" src="'+aLayerContentImages[layerId][currentImage]['src']+'" alt="'+aLayerContentImages[layerId][currentImage]['alt']+'" />'
									  +'<img id="last_pic" style="z-index: 2;" src="'+aLayerContentImages[layerId][currentImage+1]['src']+'" alt="'+aLayerContentImages[layerId][currentImage+1]['alt']+'" />');
		
		// animation
		$("img#last_pic").fadeOut(600, function()
		{
			$("p#case-study-image-text").html(aLayerContentImages[layerId][currentImage]['alt']);
		});
		
		activateNextButton(layerId);
		
		if(currentImage < 1)
		{ // deactivate
			deactivatePreviousButton(layerId);
		}
	}

	
}

