/*
 * ThickBox modified to only open IFRAMES, with header removed:
 *
 * PLEASE NOTE !
 * In order for this to work, jquery lib must be included and
 * jquery must be renamed to $jq (not $)
 *
 *
 * Thickbox 2.1 - jQuery plugin for displaying content in a box above the page
 * 
 * Copyright (c) 2006, 2007 Cody Lindley (http://www.codylindley.com)
 *
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 */

// on page load call IP_init
$jq(document).ready(IP_init);

// add thickbox to href elements that have a class of .thickbox
function IP_init()
{
	$jq("a.thickbox").click(function(event)
	{
		// stop default behaviour
		event.preventDefault();
		// remove click border
		this.blur();
	
		// display the box for the elements href
		IP_show(this.title, this.href);
	});
}

// called when the user clicks on a thickbox link
function IP_show(caption, url,stripKey)
{
	// create iframe, overlay and box if non-existent
	if ( !$jq("#IP_HideSelect").length )
	{
		$jq("body").append("<iframe id='IP_HideSelect' src='/blank.html'></iframe><div id='IP_overlay'></div><div id='IP_window'></div>");
		$jq("#IP_overlay").click(IP_remove);
	}
	// TODO replace or check if event is already assigned
	$jq(window).scroll(IP_position);

	// TODO replace
	IP_overlaySize();

	// TODO create loader only once, hide and show on demand
	$jq("body").append("<div id='IP_load'><img src='/app/projects/common/images/system/boende/maps/loadingAnimation.gif' /></div>");
	IP_load_position();

	// check if a query string is involved
	var baseURL = url.match(/(.+)?/)[1] || url;

	var queryString = url.match(/\?(.+)/)[1];
	var params = IP_parseQuery( queryString );

	IP_WIDTH = (params['width']*1) + 30;
	IP_HEIGHT = (params['height']*1) + 40;

	var ajaxContentW = IP_WIDTH - 30,
		ajaxContentH = IP_HEIGHT - 45;

	// Attach IFrame
	if(stripKey)
	{
	    urlNoQuery = url.split(stripKey);
	    urlToLoad = urlNoQuery[0];
    }
	else
	{
	    urlNoQuery = url.split('IP_');
	    urlToLoad = urlNoQuery[0];
	}

	$jq("#IP_window").append("<iframe frameborder='0' hspace='0' src='"+urlToLoad+"' id='IP_iframeContent' name='IP_iframeContent' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;' onload='IP_showIframe()' allowtransparency='true'> </iframe>");

	IP_position();
	if(frames['IP_iframeContent'] == undefined)
	{//be nice to safari
		$jq("#IP_load").remove();
		$jq("#IP_window").css({display:"block"});
		$jq(document).keyup( function(e){ var key = e.keyCode; if(key == 27){IP_remove()} });
	}
	
	$jq(window).resize(IP_position);
	
	document.onkeyup = function(e)
	{ 	
		if (e == null) { // ie
			keycode = event.keyCode;
		} else { // mozilla
			keycode = e.which;
		}
		if(keycode == 27){ // close
			IP_remove();
		}	
	}
		
}

//helper functions below

function IP_showIframe(){
	$jq("#IP_load").remove();
	$jq("#IP_window").css({display:"block"});
}

function IP_remove() {
 	$jq("#IP_imageOff").unbind("click");
	$jq("#IP_overlay").unbind("click");
	$jq("#IP_closeWindowButton").unbind("click");
	$jq("#IP_window").fadeOut("fast",IP_calledWhenDone);
	$jq("#IP_load").remove();
	return false;
}
function IP_calledWhenDone(x)
{
    $jq('#IP_window').remove();
    $jq('#IP_overlay').remove();
    $jq('#IP_HideSelect').remove();
}
function IP_position() {
	var pagesize = IP_getPageSize();
	var arrayPageScroll = IP_getPageScrollTop();
	var style = {width: IP_WIDTH, left: (arrayPageScroll[0] + (pagesize[0] - IP_WIDTH)/2), top: (arrayPageScroll[1] + (pagesize[1]-IP_HEIGHT)/2)};
	$jq("#IP_window").css(style);
}

function IP_overlaySize(){
	if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX) {	
		yScroll = window.innerHeight + window.scrollMaxY;
		xScroll = window.innerWidth + window.scrollMaxX;
		var deff = document.documentElement;
		var wff = (deff&&deff.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
		var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
		xScroll -= (window.innerWidth - wff);
		yScroll -= (window.innerHeight - hff);
	} else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
		yScroll = document.body.scrollHeight;
		xScroll = document.body.scrollWidth;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		yScroll = document.body.offsetHeight;
		xScroll = document.body.offsetWidth;
  	}
	$jq("#IP_overlay").css({"height": yScroll, "width": xScroll});
	$jq("#IP_HideSelect").css({"height": yScroll,"width": xScroll});
}

function IP_load_position() {
	var pagesize = IP_getPageSize();
	var arrayPageScroll = IP_getPageScrollTop();
	$jq("#IP_load")
		.css({left: (arrayPageScroll[0] + (pagesize[0] - 100)/2), top: (arrayPageScroll[1] + ((pagesize[1]-100)/2)) })
		.css({display:"block"});
}

function IP_parseQuery ( query ) {
	// return empty object
	if( !query )
		return {};
	var params = {};
	
	// parse query
	var pairs = query.split(/[;&]/);
	for ( var i = 0; i < pairs.length; i++ ) {
		var pair = pairs[i].split('=');
		if ( !pair || pair.length != 2 )
			continue;
		// unescape both key and value, replace "+" with spaces in value
		params[unescape(pair[0])] = unescape(pair[1]).replace(/\+/g, ' ');
   }
   return params;
}

function IP_getPageScrollTop(){
	var yScrolltop;
	var xScrollleft;
	if (self.pageYOffset || self.pageXOffset) {
		yScrolltop = self.pageYOffset;
		xScrollleft = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop || document.documentElement.scrollLeft ){	 // Explorer 6 Strict
		yScrolltop = document.documentElement.scrollTop;
		xScrollleft = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScrolltop = document.body.scrollTop;
		xScrollleft = document.body.scrollLeft;
	}
	arrayPageScroll = new Array(xScrollleft,yScrolltop) 
	return arrayPageScroll;
}

function IP_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
	arrayPageSize = new Array(w,h) 
	return arrayPageSize;
}
