/*
 * Thickbox 3 - One Box To Rule Them All.
 * By Cody Lindley (http://www.codylindley.com)
 * Copyright (c) 2007 cody lindley
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
 *
 * Modifications © 2007 Robert Heinig:
 *		german labels, easily translatable
 *		wrap-around navigation
 *		navigation by clicking on left and right image thirds with hover image
 *		internationally valid navigation keys
 *		bunch of minor cleanups
 *		picks up title attribute of a thumbnail img if the parent anchor lacks one
 *		picks up the thumbnail alt attribute and an optional meta attribute and displays
 *			these to the left & right of the caption (useful für e.g.photographic parameters)
 *			but only if the caption leaves enough room
 *		fixes for title and/or alt attributes containing '"&<
 *		fixed centering for caption and counter
 *		Ctrl-"F" key accesses image with same name in a "Full" subfolder if it exists
 *		multiline caption will properly wrap and increase the thickbox height,
 *			even the centering takes the proper footer height into account
 *		reloads on window resize
 *		direct support for video clips
*/

var TB_PathToImage = "/thickbox/loadingAnimation.gif";
var TB_Lang = {
	pic:	"Bild ",
	from:	" von ",
	prevA:	"&lt; Voriges",
	prevT:	"Voriges (Pfeil links)",
	nextA:	"N&auml;chstes &gt;",
	nextT:	"N&auml;chstes (Pfeil rechts)",
	firstA:	"&laquo; Erstes",
	firstT:	"Erstes (Pos1)",
	lastA:	"Letztes &raquo;",
	lastT:	"Letztes (Ende)",
	close:	"Schlie&szlig;en",
	ESC:	"&nbsp;(ESC)",
	autoA:	"Auto &rArr;",
	autoT:	"Diashow starten (Leertaste)",
	stopA:	"Stop &times;",
	stopT:	"Diashow beenden (Leertaste)",
	noFull:	"Tut mir leid, die Originalauflösung für dieses Bild ist nicht online!",
	objWait:	"Bitte warten Sie, w&auml;hrend das Objekt geladen wird...",
	noPlayer:	"Es konnte kein Abspielplugin gefunden werden"
};
var TB_Const = {
	autoDly:	5000,	// ms wait in auto slideshow
	ajaxDftW:	600,	// default ajax content width
	ajaxDftH:	380,	// default ajax content height
	vidMinW:	280,	// with less M$ WMP plugin cuts off volume slider
	vidAddH:	47,		// what M$ WMP plugin needs for nav controls
	ajaxPadW:	30,		// 2*15
	ajaxPadH:	43,		// Header rendered at 27px + padding: 4+12
	iframePadW:	29,
	iframePadH:	17,
	Full:		"Full",	// To insert into URL upon Ctrl-F
	keyESC:		27,
	keyAuto:	32,		// key to start/stop auto
	keyPrev1:	33,		// Page Up
	keyPrev2:	37,		// Arrow left
	keyPrev3:	38,		// Arrow up
	keyNext1:	34,		// Page Down
	keyNext2:	39,		// Arrow right
	keyNext3:	40,		// Arrow down
	keyFirst:	36,		// Home
	keyLast:	35,		// End
	keyFull:	70,		// "F"
	captionPad:	40,		// TB_ImgFooter horizontal padding
	captionH:	16,		// Height of a caption line
	metaPad:	52,		// captionPad plus 2*min_distance caption/metadata
	minXPad:	64,		// minimum diff pagewidth - image width (room outside TB, TB border, image margin, image border)
	minYPad:	76,		// minimum diff pageheight - (image+caption) height (room outside TB, TB border, image margin, image border, footer line2)
	imgXPad:	18,		// (image margin+image border)*2
	imgYPad:	49,		// (image border+image margin)*2+footer line2 height+total footer V. padding
	metaFont:	{"font-size":"10px", "font-weight":"bold"},
	countFont:	{"font-size":"10px", "font-weight":"normal"}
};

/* !!!!!!!!!!!!!!!!! edit below this line at your own risk !!!!!!!!!!!!!!!!!!!!!!! */

var TB_auto, TB_autofn, TB_sizetmr, TB_sizefn;

var TB_Players = {
	QT:		{
		ext:/\.(mov|sdv|flc,au,qt,mqv)$/i,
		type:"video/quicktime",
		clsid:"02BF25D5-8C17-4B23-BC80-D3488ABDDC6B",
		codebase:"http://www.apple.com/qtactivex/qtplugin.cab",
		pluginspage:"http://www.apple.com/quicktime/download"
	},
	WM:		{
		ext:/\.(avi|wmv|wmx|asf|asx|wma|wax)$/i,
		type:"application/x.mplayer2",
		clsid:"22D6F312-B0F6-11D0-94AB-0080C74C7E95",
		codebase:"http://www.microsoft.com/ntserver/netshow/download/en/nsmp2inf.cab",
		pluginspage:"",
		modparams: function(p) {p.autostart=p.autoplay;p.autoplay=undefined;p.autosize=(p.scale!=="1");p.scale=undefined;}
	},
	Flash:		{
		ext:/\.(swf|spl)$/i,
		type:"application/x-shockwave-flash",
		clsid:"D27CDB6E-AE6D-11CF-96B8-444553540000",
		codebase:"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab",
		pluginspage:"http://www.macromedia.com/go/getflashplayer"
	},
	Real:		{
		ext:/\.(rm)$/i,
		type:"",
		clsid:"CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA",
		codebase:"",
		pluginspage:"http://www.real.com/player/",
		modparams: function(p) {p.autostart=p.autoplay;p.autoplay=undefined;p.controls=(p.controller?"all":"imagewindow");if(p.scale==="1"){p.center=true;}else{p.maintainaspect=true;};p.scale=undefined;}
	},
	PDF:		{
		ext:/\.(pdf)$/i,
		type:"",
		clsid:"",
		codebase:"",
		pluginspage:"",
		modparams: function(p) {p.movie=p.src;p.src=undefined;}
	},
	DivX:		{
		ext:/\.(divx)$/i,
		type:"video/divx",
		clsid:"67DABFBF-D0AB-41FA-9C46-CC0F21721616",
		codebase:"http://go.divx.com/plugin/DivXBrowserPlugin.cab",
		pluginspage:""
	}
};

//on page load call tb_init
$(function(){   
	tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
	var imgLoader = new Image();// preload image
	imgLoader.src = TB_PathToImage;
});

//add thickbox to href & area elements that have a class of .thickbox
function tb_init(domChunk){
	$(domChunk).click(function(){
		if($.fn.TooltipHide) {$.fn.TooltipHide();}
		var q = $(this).children();
		var t = this.title || this.name || q.attr("title") || null;
		var a = this.href || this.alt;
		var g = this.rel || false;
		tb_show(t,a,g,[q.attr("meta"),q.attr("alt")].join("|"));
		this.blur();
		return false;
	});
}

function tb_show(caption, url, imageGroup, meta) {
	//function called when the user clicks on a thickbox link
	var baseURL, queryString, params, imgPreloader, sAdd;
	var TB_Prev, TB_Next, TB_imageCount, TB_TempArray, TB_Counter, TB_FoundIndex, TB_Width, TB_Height;
	var ajaxContentW, ajaxContentH, bVideo;

	function tb_encode(s) {
		if (typeof s=="string") {
			return s.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/</g,"&lt;").replace(/\[/g,"<").replace(/\]/g,">");
		} else {
			return "";
		}
	}
	function tb_attr(s) {
		return "'" + tb_encode(s).replace(/'/g,"&apos;") + "'";
	}
	function tb_switch(c,u,g,t,a) {
		if (a && TB_auto) {
			clearInterval(TB_auto);
			TB_auto = null;
		}
		$(document).unbind("keydown");
		$(window).unbind("resize");
		if($.fn.TooltipHide) {$.fn.TooltipHide();}
		$("#TB_window").remove();
		$("body").append("<div id='TB_window'></div>");
		tb_show(c,u,g,t);
		return false;
	}
	function goPrev() {
		return TB_Prev ? tb_switch(TB_Prev.caption, TB_Prev.url, imageGroup, TB_Prev.meta, true) : true;
	}
	function goNext() {
		return TB_Next ? tb_switch(TB_Next.caption, TB_Next.url, imageGroup, TB_Next.meta, true) : true;
	}
	function goNextA() {
		return TB_Next ? tb_switch(TB_Next.caption, TB_Next.url, imageGroup, TB_Next.meta, false) : true;
	}
	function tb_autohtml() {
		return "<a href='#' title='" +
			(TB_auto ? TB_Lang.stopT : TB_Lang.autoT) +
			"'>" +
			(TB_auto ? TB_Lang.stopA : TB_Lang.autoA) +
			"</a>"
	}
	function tb_toggleauto() {
		var jA;
		if (TB_auto) {
			clearInterval(TB_auto);
			TB_auto = null;
		} else {
			TB_autofn = goNextA;
			TB_auto = setInterval("TB_autofn()",TB_Const.autoDly);
		}
		jA = $("#TB_auto");
		if (jA.length) {jA.html(tb_autohtml());}
	}
	function makelink(index,id,itext) {
		var elem = TB_TempArray[index], q = $(elem).children();
		return {
			caption: elem.title || elem.name || q.attr("title") || null,
			url: elem.href || elem.alt,
			html: "<span id='TB_"+id+"'><a href='#' title='"+TB_Lang[itext+"T"]+"'>"+TB_Lang[itext+"A"]+"</a></span>",
			meta: [q.attr("meta"),q.attr("alt")].join("|")
		};
	}
	function showimage(imgW0, imgH0) {
		var sAlt="", aMeta, jD, sMetaL="", sMetaR="", wMetaL=0, wMetaR=0, sName, sParm;
		var pagesize, x, dy=TB_Const.captionH, dy0, i, y, imgW, imgH, sType, oPlayer;
		var imgCountW=0, captionW=0;

		aMeta = meta.replace(/,\s*/g,"|").split("|");
		meta = meta.split("|").reverse().join("|").replace(/,\s*/g,"|");
		if (aMeta.length) { sAlt=aMeta[0]; }

		// Resizing large images - orginal by Christian Montoya edited by cody & RH.
		pagesize = tb_getPageSize();
		x = pagesize[0] - TB_Const.minXPad;
		if (caption || aMeta.length || TB_imageCount) {
			// create invisible dummy to measure items
			$("body").append("<span id='TB_dummy'></span>");
			jD = $("#TB_dummy").css('display','block');
		}
		for (i=0;i<4;i++) {
			dy0 = dy;
			y = pagesize[1] - dy - TB_Const.minYPad;
			imgW = imgW0;
			imgH = imgH0;
/* RH - until I find out how to get the blasted M$ plugin to zoom a movie this is out
			if (bVideo && !params.scale) {
				// Double very small videos
				if (2*imgW<=x && 2*imgH<=y) {
					imgW *= 2; imgH *= 2;
				}
			}
*/
			// shrink large images
			if (imgW > x) {
				imgH = Math.floor(imgH * (x / imgW));
				imgW = x;
			}
			if (imgH > y) {
				imgW = Math.floor(imgW * (y / imgH));
				imgH = y;
			}
			TB_Width = imgW + TB_Const.imgXPad;
			TB_Height = imgH + dy + TB_Const.imgYPad;
			// if there's a caption, check if it will word-wrap
			if (!caption) break;
			dy = jD.text("").width(TB_Width-TB_Const.captionPad).text(caption).height();
			captionW = jD.width();
			if (dy==dy0) break;
			// dy is re-used below
		}

		// Measure footer item sizes
		if (jD) {
			jD.removeAttr('style');
			jD.text("").width(pagesize[0]);
			if (aMeta.length) {
				// distribute as many meta elements left&right of caption as will fit
				x = ( TB_Width - TB_Const.metaPad - captionW ) / 2;
				jD.css(TB_Const.metaFont);
				do {
					if (wMetaL<=wMetaR) {
						sAdd = sMetaL;
						sMetaL = (sMetaL ? sMetaL + ", " : "") + aMeta.shift();
						wMetaL = jD.text(sMetaL).width();
						if (wMetaL>x) {sMetaL=sAdd;}
					} else {
						sAdd = sMetaR;
						sMetaR = aMeta.pop() + (sMetaR ? ", " + sMetaR : "");
						wMetaR = jD.text(sMetaR).width();
						if (wMetaR>x) {sMetaR=sAdd;}
					}
				} while(aMeta.length && (wMetaL<x || wMetaR<x) );
			}
			if (TB_imageCount) {
				imgCountW = jD.text("").css(TB_Const.countFont).text(TB_imageCount).width();
			}
			jD.remove();
		}

		// build html for window
		sAdd = "<div id='TB_ImgContainer'>";
		if (bVideo) {
			// decide upon a player config
			if (!params.player && params.type) {
				for (sName in TB_Players) {
					if (params.type==TB_Players[sName].type) {
						params.player = sName;
					}
				}
			}
			if (!params.player) {
				for (sName in TB_Players) {
					if (baseURL.match(TB_Players[sName].ext)) {
						params.player = sName;
					}
				}
			}
			if (!params.player) { params.player="QT"; }
			oPlayer = TB_Players[params.player];
			// adapt parameters to player and set defaults
			params.src = baseURL;
			if (params.autoplay===undefined) params.autoplay=(params.auto===undefined?true:Boolean(params.auto));
			if (params.controller===undefined) params.controller=true;
			if (params.loop===undefined) params.loop=false;
			if (params.scale===undefined) params.scale=(imgW==imgW0?"1":"aspect");
			if (params.type===undefined) params.type=oPlayer.type;
			if (oPlayer.modparams) {oPlayer.modparams(params);}
			sType = params.type ? " type='" + params.type + "'" : "";
			params.player = params.width = params.height = params.auto = undefined;
			// embed the clip
/*
			sAdd += "<embed src='" + baseURL + "' width='" + imgW + "' height='" + imgH +
				(params.auto ? "' autostart='true" : "") +
				(params.loop ? "' loop='true" : "") +
				(params.type ? "' type='" + params.type : "") +
				(imgW==2*imgW0 ? "' scale='2" : "") +
				"'/>";
*/
			sParm = "";
			for (sName in params) {
				if (sName && params[sName]) {
					sParm += "<param name='" + sName + "' value='" + params[sName] + "'/>";
				}
			}
			sAdd += "<object id='TB_object' classid='clsid:" + oPlayer.clsid +
				"' width='" + imgW + "' height='" + imgH +
				"' codebase='" + oPlayer.codebase +
				"' standby='" + TB_Lang.objWait + "'>" + sParm +
				"<object data='" + baseURL + "' width='" + imgW + "' height='" + imgH +
				"'" + sType + " standby='" + TB_Lang.objWait + "'>" +
				sParm + TB_Lang.noPlayer + "</object></object>";
			// No hover nav: it's killed by media player plugin anyway
		} else {
			// now the show star
			sAdd += "<img src='" + url + "' width='" + imgW + "' height='" + imgH + "' alt=" + tb_attr(sAlt) + "/>"
			// Add hover-nav
			sAdd += "<div id='TB_hoverNav' style='height:" + imgH + "px'>";
			if (TB_imageCount) {
				sAdd += "<a id='TB_hoverPrev' href='#' title='"+(TB_FoundIndex?TB_Lang.prevT:TB_Lang.lastT)+"'></a>";
			}
			sAdd += "<a href='#' id='TB_hoverOff' title=" +
				tb_attr(meta||TB_Lang.close) + "></a>";
			if (TB_imageCount) {
				sAdd += "<a id='TB_hoverNext' href='#' title='" +
					((TB_FoundIndex==TB_TempArray.length-1)?TB_Lang.firstT:TB_Lang.nextT) + "'></a>";
			}
			sAdd += "</div>";
		}
		// now for the footer
		sAdd += "</div><div id='TB_ImgFooter'>";
		if (meta||caption) {
			// Since for videos we can't get our metadata tooltip over the player, we put it over the footer line 1
			sAdd += "<div id='TB_footLine1'" + (bVideo&&meta?(" title="+tb_attr(meta)):"") + " style='height:" + dy + "px;'>";
			if (sMetaL) {sAdd += "<div id='TB_LeftTip'>" + tb_encode(sMetaL) + "</div>";}
			if (sMetaR) {sAdd += "<div id='TB_RightTip'>" + tb_encode(sMetaR) + "</div>";}
			// properly center caption (FF neglects to subtract parent's padding)
			if (caption) {sAdd += "<div id='TB_caption' style='width:"+captionW+"px; margin-left:-"+(Math.floor(captionW/2))+"px;'>"+tb_encode(caption)+"</div>";}
			sAdd += "</div>";
		}
		sAdd += "<div id='TB_footLine2'>";
		if (TB_imageCount) {sAdd += "<div id='TB_footNav'>"+TB_Prev.html+"&nbsp;&nbsp;"+TB_Next.html+"</div>";}
		sAdd += "<div id='TB_footClose'>";
		if (TB_imageCount) {sAdd += "<span id='TB_auto'>" + tb_autohtml() + "</span>&nbsp;&nbsp;";}
		sAdd += "<a href='#' id='TB_closeWindowButton' title='"+TB_Lang.close+TB_Lang.ESC+"'>"+TB_Lang.close+"</a></div>";
		if (TB_imageCount) {sAdd += "<div id='TB_footCount' style='width:"+imgCountW+"px; margin-left:-"+(Math.floor(imgCountW/2))+"px;'>"+TB_imageCount+"</div>";}
		sAdd += "</div></div>";
		// add window to DOM
		$("#TB_window").append(sAdd);
		// activate fancy tooltips if plugin present
		if($.fn.Tooltip) {$("#TB_hoverNav a, #TB_footLine1").Tooltip({track:true,showBody:"|",showURL:false});}

		// event handlers and preloading
		if (TB_Prev) {
			$("#TB_prev, #TB_hoverPrev").click(goPrev);
			TB_Prev.imgPre = new Image();
			TB_Prev.imgPre.src = url;
		}
		if (TB_Next) {
			$("#TB_next, #TB_hoverNext").click(goNext);
			TB_Next.imgPre = new Image();
			TB_Next.imgPre.src = url;
		}
		$("#TB_closeWindowButton, #TB_hoverOff").click(tb_remove);
		if (TB_imageCount) {$("#TB_auto").click(tb_toggleauto);}

		// key press handling
		$(document).keydown( function(e){
			var aUrl, sUrl, xrq, tmp;
			switch ( typeof e.which == "undefined" ? e.keycode : e.which ) {
			case TB_Const.keyESC:
				tb_remove();
				break;
			case TB_Const.keyNext1:
			case TB_Const.keyNext2:
			case TB_Const.keyNext3:
				goNext();
				break;
			case TB_Const.keyPrev1:
			case TB_Const.keyPrev2:
			case TB_Const.keyPrev3:
				goPrev();
				break;
			case TB_Const.keyFirst:
				tmp = makelink(0,"next","first");
				tb_switch(tmp.caption, tmp.url, imageGroup, tmp.meta, true);
				break;
			case TB_Const.keyLast:
				tmp = makelink(TB_TempArray.length-1,"prev","last");
				tb_switch(tmp.caption, tmp.url, imageGroup, tmp.meta, true);
				break;
			case TB_Const.keyAuto:
				tb_toggleauto();
				break;
			case TB_Const.keyFull:
				if (e.ctrlKey) {
					// Ctrl-F: show a "full resolution" image
					// insert "Full" between path and file name components of URL
					aUrl=url.split("/");
					if (aUrl.length<=1) aUrl=url.split("\\");
					aUrl.splice(aUrl.length-1,0,TB_Const.Full);
					sUrl=aUrl.join("/");
					// Local file: Just redirect
					if (aUrl[0]=="file:" ) {
						self.location=sUrl;
					} else {
						// Check availability first (sorry, jQuery's ajax won't work with HEAD)
						function ajaxrs() {
							if (xrq.readyState==4) {
								switch (xrq.status) {
								case 200: self.location=sUrl; break;
								case 404: alert(TB_Lang.noFull); break;
								}
							}
						}
						try {
							xrq = new XMLHttpRequest();
						} catch (e) {
							try {
								xrq = new ActiveXObject("Msxml2.XMLHTTP");
							} catch (e) {
								try {
									xrq = new ActiveXObject("Microsoft.XMLHTTP");
								} catch (e) {
									xrq = false;
								}
							}
						}
						if (xrq) {
							xrq.open("HEAD",sUrl,true);
							xrq.onreadystatechange=ajaxrs;
							xrq.send(null);
						}
					}
				}
				break;
			}
			return false;
		});

		// center and show window
		tb_position(TB_Width,TB_Height);
		tb_showIframe();

		// react to window resize (firefox doesn't need the delay, it already does this
		// internally, but it doesn't hurt either. IE7 will crash without).
		TB_sizefn = function (){
			return tb_switch(caption, url, imageGroup, meta, false);
		};
		$(window).resize( function (){
			if (TB_sizetmr) clearInterval(TB_sizetmr);
			TB_sizetmr = window.setTimeout("TB_sizefn()",$.browser.msie?100:10);
		});

	}

	try {
		if (typeof document.body.style.maxHeight==="undefined") {//if IE 6
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			sAdd="<iframe id='TB_HideSelect'></iframe>";
		}else{//all others
			sAdd="";
		}
		if ($("#TB_overlay").length===0){
			$("body").append(sAdd + "<div id='TB_overlay'></div><div id='TB_window'></div>");
			$("#TB_overlay").click(tb_remove);
		}

		if (caption===null) {caption="";}
		$("body").append("<div id='TB_load'><img src='"+TB_PathToImage+"' /></div>");//add loader to the page
		$('#TB_load').show();//show loader

		baseURL = url.split("?")[0];	// strip parameters if any
		queryString = url.replace(/^[^\?]+\??/,'');
		params = tb_parseQuery( queryString );
		bVideo = baseURL.match(/\.(mpe?g|mov|avi|wmv)/gi);
		TB_Prev = TB_Next = null;
		TB_imageCount="";
		if ( bVideo || baseURL.match(/\.(jpe?g|png|gif|bmp)/gi) ) {
			// show an image or movie
			TB_FoundIndex=-1;
			if(imageGroup){
				TB_TempArray = $("a[@rel="+imageGroup+"]").get();
				for (TB_Counter=0; TB_Counter < TB_TempArray.length; TB_Counter++) {
					if (!(TB_TempArray[TB_Counter].href==url)) {
						if (TB_FoundIndex>=0) {
							TB_Next = makelink(TB_Counter,"next","next");
							break;
						} else {
							TB_Prev = makelink(TB_Counter,"prev","prev");
						}
					} else {
						TB_FoundIndex = TB_Counter;
						TB_imageCount = TB_Lang.pic + (TB_Counter+1) + TB_Lang.from + (TB_TempArray.length);
					}
				}
				if ( !TB_Next ) {
					TB_Next = makelink(0,"next","first");
				}
				if ( !TB_Prev ) {
					TB_Prev = makelink(TB_TempArray.length-1,"prev","last");
				}
			}

			if (bVideo) {
				ajaxContentW = (params.width*1) || TB_Const.ajaxDftW;
				if (ajaxContentW<TB_Const.vidMinW) ajaxContentW=TB_Const.vidMinW;
				ajaxContentH  = ((params.height*1) || TB_Const.ajaxDftH) + TB_Const.vidAddH;
				showimage(ajaxContentW, ajaxContentH);
			} else {
				imgPreloader = new Image();
				imgPreloader.onload = function(){
					imgPreloader.onload = null;
					showimage(imgPreloader.width, imgPreloader.height);
				};
				imgPreloader.src = url;
			}
			TB_autofn = goNextA;

		}else{
			//code to show html pages or movie clips
			ajaxContentW = (params.width*1) || TB_Const.ajaxDftW;
			ajaxContentH  = (params.height*1) || TB_Const.ajaxDftH;
			TB_Width = ajaxContentW + TB_Const.ajaxPadW;
			TB_Height = ajaxContentH + TB_Const.ajaxPadH;
			// Start building DOM structure for content
			//TO-DO: Prev/Next here, too!
			sAdd="<div id='TB_title'><div id='TB_ajaxWindowTitle'>" + tb_encode(caption) +
				"</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton' title='" + 
				TB_Lang.close + "'>" + TB_Lang.close + "</a>" + TB_Lang.ESC + 
				"</div></div>";
			if (params.TB_iframe){
				sAdd +=
					"<iframe frameborder='0' hspace='0' src='" + url.split('TB_')[0] + 
					"' id='TB_iframeContent' name='TB_iframeContent' style='width:" + (ajaxContentW+TB_Const.iframePadW) + 
					"px;height:" + (ajaxContentH+TB_Const.iframePadH) + "px;' onload='tb_showIframe()'> </iframe>";
			}else{
				if($("#TB_window").css("display") != "block"){
					if(!params.modal){
						sAdd += "<div id='TB_ajaxContent' style='width:" +
							ajaxContentW + "px;height:" + ajaxContentH + "px;'></div>";
					}else{
						$("#TB_overlay").unbind();
						sAdd = "<div id='TB_ajaxContent' class='TB_modal' style='width:" +
							ajaxContentW + "px;height:" + ajaxContentH + "px;'></div>";
					}
				}else{
					$("#TB_ajaxContent")[0].style.width = ajaxContentW +"px";
					if (!bVideo) {$("#TB_ajaxContent")[0].style.height = ajaxContentH +"px";}
					$("#TB_ajaxContent")[0].scrollTop = 0;
					$("#TB_ajaxWindowTitle").html(caption);
					sAdd="";
				}
			}
			// expand DOM
			if (sAdd) {$("#TB_window").append(sAdd);}
			$("#TB_closeWindowButton").click(tb_remove);

			// Fill in content
			if (params.TB_inline){
				$("#TB_ajaxContent").html($('#' + params.inlineId).html());
				tb_position(TB_Width,TB_Height);
				tb_showIframe();
			}else if (params.TB_iframe){
				tb_position(TB_Width,TB_Height);
				if(frames.TB_iframeContent===undefined){//be nice to safari
					tb_showIframe();
					$(document).keyup( function(e){ var key = e.keyCode; if(key == TB_Const.keyESC){tb_remove();}});
				}
			}else{
				$("#TB_ajaxContent").load(url + "&random=" + (new Date().getTime()),function(){//to do a post change this load method
					tb_position(TB_Width,TB_Height);
					$("#TB_load").remove();
					tb_init("#TB_ajaxContent a.thickbox");
					$("#TB_window").css({display:"block"});
				});
			}
			if(!params.modal){
				document.onkeyup = function(e){
					if (e===null) {e=event;} // ie
					if ( (e.which||e.keycode)==TB_Const.keyESC ) {tb_remove();}
				};
			}
		}

	} catch(e) {
//		alert ("Exception: " + e);
	}
}

//helper functions below
function tb_showIframe(){
	$("#TB_load").remove();
	$("#TB_window").css({display:"block"});
}

function tb_remove() {
	if (TB_sizetmr) {
		clearInterval(TB_sizetmr);
		TB_sizetmr = null;
	}
	if (TB_auto) {
		clearInterval(TB_auto);
		TB_auto = null;
	}
	if($.fn.TooltipHide) {$.fn.TooltipHide();}
	$("#TB_hoverOff,#TB_overlay,#TB_closeWindowButton,#TB_prev,#TB_hoverPrev,#TB_next,#TB_hoverNext").unbind("click");
	$("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').remove();});
	$("#TB_load").remove();
	$(document).unbind("keydown");
	$(window).unbind("resize");
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	return false;
}

function tb_position(W,H) {
	var jW = $("#TB_window"), iH = (jW.height() || H-8) + 8;
	jW.css({marginLeft: '-' + Math.floor(W / 2) + 'px', width: W + 'px', marginTop: '-' + Math.floor(iH / 2) + 'px'});
	// if ( !(jQuery.browser.msie && typeof XMLHttpRequest == 'function')) { // RH: bad for ie7 (intent: take away IE6)
}

function tb_parseQuery(query) {
	var Params = {};
	var KeyVal, key, val;
	if ( ! query ) {return Params;}
	var Pairs = query.split(/[;&]/);
	for ( var i = 0; i < Pairs.length; i++ ) {
		KeyVal = Pairs[i].split('=');
		if ( KeyVal && KeyVal.length==2 ) {
			key = unescape( KeyVal[0] );
			val = unescape( KeyVal[1] ).replace(/\+/g, ' ');
			Params[key] = val;
		}
	}
	return Params;
}

function tb_getPageSize() {
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	return [w,h];
}
