/*

 * Smoothbox v20080623 by Boris Popoff (http://gueschla.com)

 * To be used with mootools 1.2

 *

 * Based on Cody Lindley's Thickbox, MIT License

 *

 * Licensed under the MIT License:

 *   http://www.opensource.org/licenses/mit-license.php

 */



// on page load call TBEX_init

window.addEvent('domready', TBEX_init);



// prevent javascript error before the content has loaded

TBEX_WIDTH = 0;

TBEX_HEIGHT = 0;

var TBEX_doneOnce = 0;

var TBEX_closeCallback = null;



// add smoothbox to href elements that have a class of .smoothbox

function TBEX_init(){

	$$("a.smoothboxex").each(function(el){el.onclick=TBEX_bind});

}



function TBEX_bind(event){

	var event = new Event(event);

	// stop default behaviour

	event.preventDefault();

	// remove click border

	this.blur();

	// get caption: either title or name attribute

	var caption = this.title || this.name || "";

	// get rel attribute for image groups

	var group = this.rel || false;

	// display the box for the elements href

	TBEX_show(caption, this.href, group);

    this.onclick = TBEX_bind;

    return false;

}



// called when the user clicks on a smoothbox link

function TBEX_show(caption, url, rel, loading, disable_close, close_callback) {



  // set default closing mechanism

  if(!disable_close) { disable_close = 0; }



  // set close callback

  if(close_callback) {

	TBEX_closeCallback = close_callback;

  } else {

	TBEX_closeCallback = null;

  }

  

  // create iframe, overlay and box if non-existent

  if (!$("TBEX_overlay")) {

    new Element('div').setProperty('id', 'TBEX_overlay').inject(document.body);

    $('TBEX_overlay').setOpacity(0.6);

  }



  if (!$("TBEX_window")) {

    new Element('div').setProperty('id', 'TBEX_window').inject(document.body);

    $('TBEX_window').setOpacity(0);

  }

  else 

  {

    $('TBEX_window').dispose();

	// SEMODS also kill TB

	if($('TB_window')) {

		$('TB_window').dispose();

	}

    new Element('div').setProperty('id', 'TBEX_window').inject(document.body);

    $('TBEX_window').setOpacity(0);

  }



	if (!$("TBEX_load")) {

	  new Element('div').setProperty('id', 'TBEX_load').inject(document.body);

	  $('TBEX_load').innerHTML = "<img src='./images/tbex_loadingAnimation.gif' alt='Loading' />";

	}

	//$('TBEX_load').show();

	$('TBEX_load').setStyle('display','block');

	//document.getElementByID('TBEX_load').style.display = "";



  if(disable_close == 0) { $("TBEX_overlay").onclick=TBEX_remove; } else { $("TBEX_overlay").onclick = ''; }

  //window.onscroll = TBEX_position;

    

  // check if a query string is involved

  var baseURL = url.match(/(.+)?/)[1] || url;

    

  // CODE TO SHOW IFRAME

  var queryString = url.match(/\?(.+)/)[1];

  var params = TBEX_parseQuery(queryString);

    

	// autosize - fullscreen   

  if(!params['width'] || !params['height']) {

//      TBEX_HEIGHT = (document.all?document.body.parentNode.clientHeight:window.innerHeight) - 20;

      TBEX_HEIGHT = (document.all?document.documentElement.clientHeight:window.innerHeight) - 40;

      TBEX_WIDTH  = (document.all?document.body.clientWidth:window.innerWidth) - 40;

  } else {

	TBEX_WIDTH = (params['width'] * 1) + 30;

	TBEX_HEIGHT = (params['height'] * 1) + 40;

  }

        

  var ajaxContentW = TBEX_WIDTH - 30, ajaxContentH = TBEX_HEIGHT - 45;



  if (url.indexOf('TBEX_iframe') != -1) {

    urlNoQuery = url.split('TBEX_');

    $("TBEX_window").innerHTML += "<div id='TBEX_title'><div id='TBEX_ajaxWindowTitle'>" + caption + "</div><div id='TBEX_closeAjaxWindow'><a href='#' id='TBEX_closeWindowButton' title='Close'>X</a></div></div><iframe frameborder='0' hspace='0' src='" + urlNoQuery[0] + "&in_smoothbox=true' id='TBEX_iframeContent' name='TBEX_iframeContent' scrolling='auto' style='width:" + (ajaxContentW + 29) + "px;height:" + (ajaxContentH + 17) + "px;' onload='TBEX_showWindow()'> </iframe>";

  }

  else {

    $("TBEX_window").innerHTML += "<div id='TBEX_title'><div id='TBEX_ajaxWindowTitle'>" + caption + "</div><div id='TBEX_closeAjaxWindow'><a href='#' id='TBEX_closeWindowButton'>X</a></div></div><div id='TBEX_ajaxContent' style='width:" + ajaxContentW + "px;height:" + ajaxContentH + "px;'></div>";

  }



  $("TBEX_closeWindowButton").onclick = TBEX_remove;



  if (url.indexOf('TBEX_inline') != -1) {

    $("TBEX_ajaxContent").innerHTML = ($(params['inlineId']).innerHTML);

    TBEX_position();

	$('TBEX_load').dispose();

    TBEX_showWindow();

  } else if (url.indexOf('TBEX_iframe') != -1) {

    TBEX_position();

    if (frames['TBEX_iframeContent'] == undefined) {//be nice to safari

      $(document).keyup(function(e){

                    var key = e.keyCode;

                    if (key == 27) {

                        TBEX_remove()

                    }

      });

      TBEX_showWindow();

    }

  } else {

	



    var handlerFunc = function(){

          TBEX_position();

		  $("TBEX_load").dispose();

          TBEX_showWindow();

    };

    new Request.HTML({

                    method: 'get',

                    update: $("TBEX_ajaxContent"),

                    onComplete: handlerFunc

    }).get(url);

  }



  document.onkeyup = function(event){

      var event = new Event(event);

      if (event.code == 27) { // close

          TBEX_remove();

      }

  }





}



//helper functions below



function TBEX_showWindow(){



    if($('TBEX_load')) {

    	$('TBEX_load').dispose();

    }



    if (TBEX_doneOnce == 0) {

        TBEX_doneOnce = 1;

        

        $('TBEX_window').set('tween', {

            duration: 250

        });

        $('TBEX_window').tween('opacity', 0, 1);

        

    }

    else {

        $('TBEX_window').setStyle('opacity', 1);

    }



//  $('TBEX_window').setStyle('opacity', 1);

}



function TBEX_remove(){

    $("TBEX_overlay").onclick = null;

    document.onkeyup = null;

    document.onkeydown = null;

    

    if ($('TBEX_closeWindowButton')) 

        $("TBEX_closeWindowButton").onclick = null;

    

    $('TBEX_window').set('tween', {

        duration: 250,

        onComplete: function(){

            $('TBEX_window').dispose();

			// SEMODS also kill TB

			if($('TB_window')) {

				$('TB_window').dispose();

			}

			if(TBEX_closeCallback) {

				TBEX_closeCallback();

			}

        }

    });

    $('TBEX_window').tween('opacity', 1, 0);

    

    

    

    $('TBEX_overlay').set('tween', {

        duration: 400,

        onComplete: function(){

            $('TBEX_overlay').dispose();

			// SEMODS also kill TB

			if($('TB_overlay')) {

				$('TB_overlay').dispose();

			}

        }

    });

    $('TBEX_overlay').tween('opacity', 0.6, 0);

    

    window.onscroll = null;

    window.onresize = null;

    

    TBEX_init();

    TBEX_doneOnce = 0;

    return false;

}



function TBEX_position() {

//    $('TB_window').set('morph', {

//        duration: 75

//    });

//    $('TB_window').morph({

//		width: TB_WIDTH + 'px',

//		left: (window.getScrollLeft() + (window.getWidth() - TB_WIDTH) / 2) + 'px',

//		top: (window.getScrollTop() + (window.getHeight() - TB_HEIGHT) / 2) + 'px'

//	});	



	$('TBEX_window').setStyle('marginLeft', '-' + parseInt(TBEX_WIDTH / 2) + 'px');

	$('TBEX_window').setStyle('width', TBEX_WIDTH + 'px');



     //$("#TBEX_window").css({marginLeft: '-' + parseInt(TBEX_WIDTH / 2) + 'px', width: TBEX_WIDTH + 'px'});

     //if ( !(jQuery.browser.msie && typeof XMLHttpRequest == 'function') ) { // take away IE6

     //    $("#TBEX_window").css({marginTop: '-' + parseInt(TBEX_HEIGHT / 2) + 'px'});

     //}

     if ( !(SEMods.B.isIE && typeof XMLHttpRequest == 'function') ) { // take away IE6

		$('TBEX_window').setStyle('marginTop', '-' + parseInt(TBEX_HEIGHT / 2) + 'px');

         //$("#TBEX_window").css({marginTop: '-' + parseInt(TBEX_HEIGHT / 2) + 'px'});

     }



}





function TBEX_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;

}

