/*
 * Smoothbox by Boris Popoff (http://gueschla.com)
 *
 * Based on Cody Lindley's Thickbox, MIT License
 *
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 */
window.addEvent('domready', TB_init);

TB_WIDTH = 0;
TB_HEIGHT = 0;
TB_OPACITY = 0.6;

// add smoothbox to href elements that have a class of .smoothbox
function TB_init(){
	$$("a.smoothbox").each(function(el){el.onclick=TB_bind});
}

function TB_bind(event) {
	var event = new Event(event);
	// stop default behaviour
	event.preventDefault();
	// remove click border
	this.blur();
	var caption = this.title || this.name || "";
	var group = this.rel || false;
	TB_show(caption, this.href, group);
	this.onclick=TB_bind;
	return false;
}

function TB_show(caption, url, rel) {
	if ( !$("TB_overlay") )
	{
		new Element('iframe').setProperty('id', 'TB_HideSelect').injectInside(document.body);
		new Element('div').setProperty('id', 'TB_overlay').injectInside(document.body);
		TB_overlaySize();
	}
	
	if ( !$("TB_window") )
	{
		new Element('div').setProperty('id', 'TB_window').injectInside(document.body);
	}
	
	$("TB_overlay").onclick=TB_remove;
	window.onscroll=TB_positionEffect;

	// check if a query string is involved
	var baseURL = url.match(/(.+)?/)[1] || url;

	var queryString = url.match(/\?(.+)/)[1];
	var params = TB_parseQuery( queryString );
	
	TB_WIDTH = (params['width']*1);
	TB_HEIGHT = (params['height']*1);
	
	// set the TB_window to have the same height as input params
	$("TB_window").setStyle('height',TB_HEIGHT + "px");

	urlNoQuery = url.split('TB_');		
	$("TB_window").innerHTML += "<iframe scrolling='no' frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='TB_iframeContent' name='TB_iframeContent' style='width:"+TB_WIDTH+"px;height:"+TB_HEIGHT +"px;' onload='TB_showWindow()'> </iframe>";

	TB_position();
	$('TB_overlay').setStyle('opacity',TB_OPACITY);	

	if(frames['TB_iframeContent'] == undefined){//be nice to safari
		$(document).keyup( function(e){ var key = e.keyCode; if(key == 27){TB_remove()} });
		TB_showWindow();
	}
	
	window.onresize=function(){ TB_position();  TB_overlaySize();}  

	document.onkeyup = function(event){ 	
		var event = new Event(event);
		if(event.code == 27){ // close
			TB_remove();
		}	
	}
}

//helper functions below

function TB_showWindow(){
	var myFX = new Fx.Style('TB_window', 'opacity',{duration: 10, transition: Fx.Transitions.sineInOut, onComplete:function(){if ($('TB_load')) { $('TB_load').remove();}} }).start(0,0.99);
}

function TB_remove() {
 	$("TB_overlay").onclick=null;
	document.onkeyup=null;
	document.onkeydown=null;
	if ($('TB_imageOff')) $("TB_imageOff").onclick=null;
	$('TB_window').remove();
	$('TB_overlay').remove();
	window.onscroll=null;
	window.onresize=null;	
	$('TB_HideSelect').remove();
	TB_init();
	return false;
}

function TB_position() {
	$("TB_window").setStyles({width: TB_WIDTH+'px', 
				 left: (window.getScrollLeft() + (window.getWidth() - TB_WIDTH)/2)+'px',
				 top: (window.getScrollTop() + (window.getHeight() - TB_HEIGHT)/2)+'px'});
}

function TB_positionEffect() {
	new Fx.Styles('TB_window', {duration: 10, transition: Fx.Transitions.sineInOut}).start({
		'left':(window.getScrollLeft() + (window.getWidth() - TB_WIDTH)/2)+'px',
		'top':(window.getScrollTop() + (window.getHeight() - TB_HEIGHT)/2)+'px'});
}

function TB_overlaySize(){
	// we have to set this to 0px before so we can reduce the size / width of the overflow onresize 
	$("TB_overlay").setStyles({"height": '0px', "width": '0px'});
	$("TB_HideSelect").setStyles({"height": '0px', "width": '0px'});
	$("TB_overlay").setStyles({"height": window.getScrollHeight()+'px', "width": window.getScrollWidth()+'px'});
	$("TB_HideSelect").setStyles({"height": window.getScrollHeight()+'px',"width": window.getScrollWidth()+'px'});
}

function TB_load_position() {
	if ($("TB_load")) { $("TB_load").setStyles({left: (window.getScrollLeft() + (window.getWidth() - 56)/2)+'px', top: (window.getScrollTop() + ((window.getHeight()-20)/2))+'px',display:"block"}); }
}

function TB_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;
}
