/*
 * Modified By Sebastian Spill (sebastian.spill@sunzinet.com)
 * Original By Cody Lindley (http://www.codylindley.com)
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
*/


// On page load call tb_init
$(document).ready(function() {
	// Pass where to apply thickbox
	tb_init('a.thickbox, area.thickbox, input.thickbox');
});

// Add thickbox to href & area elements that have a class of .thickbox
function tb_init(domChunk) {
	$(domChunk).click(function() {
		var a = this.href;
		openLayer(a);
		this.blur();
		return false;
	});
}

// Function called when the user clicks on a thickbox link
function openLayer(inlineId, wWidth, wHeight) {
	try {

		if (wWidth == null){
			wWidth = '500';
		}
		if (wHeight == null){
			wHeight = '580';
		}

		// IE6
		if (typeof document.body.style.maxHeight === "undefined") {
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			// Iframe to hide select elements in IE6
			if (document.getElementById("TB_HideSelect") === null) {
				$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window' style='width:"+wWidth+"px;height:"+wHeight+"px'></div>");
				$("#TB_overlay").click(tb_remove);
			}
		}
		// All others
		else {
			if(document.getElementById("TB_overlay") === null){
				$("body").append("<div id='TB_overlay'></div><div id='TB_window' style='width:"+wWidth+"px;height:"+wHeight+"px'></div>");
				$("#TB_overlay").click(tb_remove);
			}
		}

		if(tb_detectMacXFF()) {
			// Use png overlay so hide flash
			$("#TB_overlay").addClass("TB_overlayMacFFBGHack");
		}
		else {
			//use background and opacity
			$("#TB_overlay").addClass("TB_overlayBG");
		}

		if ($("#TB_window").css("display") != "block") {
			$("#TB_window").append("<div id='TB_borderTop'><div id='TB_TopLeft'></div><div class='TB_colMiddle' style='width:"+(wWidth-50)+"px'><a onclick='printView(\"TB_innerContent\");'><img src='images/print.gif' alt='Drucken' onmouseover='changePic(this, \"images/print_mo.gif\");' onmouseout='changePic(this, \"images/print.gif\");' /></a></div><div id='TB_TopRight'></div><div class='clear'></div></div><div id='TB_Content' style='height:"+(wHeight-50)+"px'><div id='TB_innerContent' style='height:"+(wHeight-50)+"px'></div></div><div id='TB_borderBottom'><div id='TB_BottomLeft'></div><div class='TB_colMiddle' style='width:"+(wWidth-50)+"px'></div><div id='TB_BottomRight'></div><div class='clear'></div></div>");
		}
		// This means the window is already up, we are just loading new content
		else {
			$("#TB_window")[0].style.width = wWidth +"px";
			$("#TB_window")[0].style.height = wHeight +"px";
			$("#TB_Content")[0].scrollTop = 0;
		}

		$("#TB_TopRight").click(tb_remove);
		$("#TB_innerContent").append($('#' + inlineId).children());

		$("#TB_window").unload(function () {
			// Move elements back when you're finished
			$('#' + inlineId).append( $("#TB_innerContent").children() );
		});

		tb_position(wWidth, wHeight);
		$("#TB_window").css({display:"block"});

		$('#TB_innerContent div.scroll-pane').css({height:(wHeight-50) + "px"});
		$('#TB_innerContent div.scroll-pane').jScrollPane({showArrows:true, scrollbarWidth: 5, arrowSize: 14});
	}
	catch(e) {
		// Nothing here
	}
	$.ajax({
		url: '/images/info.php?id=' + myID + '&link=' + inlineId,
		type: 'GET'
	});
}

function tb_remove() {
	$("#TB_TopRight").unbind("click");
	$("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
	// If IE6
	if (typeof document.body.style.maxHeight == "undefined") {
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	return false;
}

function tb_position(wWidth, wHeight) {
	var pageSize = tb_getPageSize();
	$("#TB_window").css({marginLeft: '-' + parseInt(((wWidth / 2)),10) + 'px', marginTop: '-' + parseInt((wHeight/2)+20,10) + 'px'});

	// Take away IE6
	if (typeof document.body.style.maxHeight === "undefined") {
		$("#TB_window").css({marginTop: '-' + parseInt((wHeight / 2),10) + 'px'});
	}
}

function tb_getPageSize() {
	var x,y;
	if (self.innerHeight) {
		// all except Explorer
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) {
		// Explorer 6 Strict Mode
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) {
		// other Explorers
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	arrayPageSize = [x,y];
	return arrayPageSize;
}

function tb_detectMacXFF() {
	var userAgent = navigator.userAgent.toLowerCase();
	if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
		return true;
	}
}
