/*
 * jquery image detail
 * By Anian Thrainer
 * Copyright (c) 2011 Anian Thrainer
*/

function at_detectMacXFF() { //determine if it's MacXFF
	var userAgent = navigator.userAgent.toLowerCase();
	if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
		return true;
	}
}

function detailModalClose() { //removes modal window and overlay
	$("#AT_mask").remove();
	$("#AT_detail").remove();
}

function at_detail(ref) {//create modal window  
	try { //create mask depending on browser
		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			if (document.getElementById("AT_HideSelect") === null) {//iframe to hide select elements in ie6
				$("body").append("<iframe id='AT_HideSelect'></iframe><div id='mask'></div>");
			}
		}else{//all others
			if(document.getElementById("AT_mask") === null){
				$("body").append("<div id='AT_mask'></div>");
			}
		}
		
		if(at_detectMacXFF()){ //is it a MacXFF
			$("#AT_mask").addClass("AT_maskMacFFBGHack");//use png overlay so hide flash
		}else{
			$("#AT_mask").addClass("AT_maskBG");//use background and opacity
		}
	} catch(e) {
		//nothing here
	}
	
	$("#AT_mask").click(function() {
		detailModalClose();
	});
	var modalWindow = $("<div id='AT_detail' />");
	var modalContent = $("<div id='AT_detailHd'><a class='detailClose' href='#'></a></div><div id='AT_imageframe'><img src='" + ref + "' class='detailIMG' alt='detail' /><img src='images/loading.gif' class='AT_load' alt='loading...' /></div> ");
	
	modalWindow.append(modalContent);
	
	$("body").append(modalWindow); //show modal
	
	$(".detailIMG").hide();
	
	$(".detailClose").click(function(e) {
		e.preventDefault();
		detailModalClose();
	});

	$(".detailIMG").load(function() {
		$(".AT_load").remove();
		
		var newWidth = $(".detailIMG").width();
		var newHeight = $(".detailIMG").height();
		var marginLeft = newWidth / 2; //adding margins to center modal window
		var newLeft = newWidth - 16;
		
		$("#AT_imageframe").css({
			"width": newWidth + "px",
			"height": newHeight + "px"
		});
		
		$("#AT_detailHd").css({
			"width": newWidth + "px"
		});
		
		$(".detailClose").css({
			"right": "auto",
			"left": newLeft + "px"
		});
		
		newHeight += $("#AT_detailHd").height(); //höhe überschrift dazuzählen
		$("#AT_detail").css({
			"width": newWidth + "px",
			"height": newHeight + "px",
			"top": "10px",
			"left": "50%",
			"margin": "0px 0px 0px -" + marginLeft + "px"
			
		});
		
		$(".detailIMG").fadeIn('slow');
	});
		
}
