/* Greybox Redux
 * Required: http://jquery.com/
 * Written by: John Resig
 * Based on code by: 4mir Salihefendic (http://amix.dk)
 * License: LGPL (read more in LGPL.txt)
 */

var GB_DONE = false;
var GB_HEIGHT = 400;
var GB_WIDTH = 400;

function GB_show(caption, url, height, width) {
  GB_HEIGHT = height || 400;
  GB_WIDTH = width || 400;
  if(!GB_DONE) {
    $(document.body)
      .append("<div id='GB_overlay'></div><div id='GB_window'>"
        + "<img src='includes/greybox/close.gif' alt='Close window' id='gClose'/></div>");
    $("#gClose").click(GB_hide);
    $("#GB_overlay").click(GB_hide);
    $(window).resize(GB_position);
    GB_DONE = true;
  }

  $("#GB_frame").remove();
  $("#GD_shadow").remove();
	ext = url.substr((url.length-3),3);
	if (ext == "jpg" || ext == "gif" || ext == "png"){
		$("#GB_window").append(GB_shadow("<img src='"+url+"' alt='Close window' id='gContent'/>"));
	} else {
		$("#GB_window").append(GB_shadow("<iframe id='GB_frame' scrolling='0' noresize='noresize' frameborder='0' src='"+url+"'></iframe>"));
	}

  //$("#GB_caption").html(caption);
	var site_height = $('.LayerContent').innerHeight();
	if ($('body').innerHeight() > site_height){
		site_height = $('body').innerHeight();
	}
	$("#GB_overlay").css('height', site_height);
  $("#GB_overlay").show();
  GB_position();

  if(GB_ANIMATION)
    $("#GB_window").slideDown("slow");
  else
    $("#GB_window").show();

}

function GB_shadow(content)
{
	if (content != false){
	return '<table width="100%" height="100%" cellspacing="0" cellpadding="0" class="shadow" id="GD_shadow"><tr><td class="tl"><img src="includes/greybox/tl.png" /></td> <td class="tm"><img src="includes/greybox/tm.png" /></td><td class="tr"><img src="includes/greybox/tr.png" /></td></tr><tr><td class="ml"><img src="includes/greybox/ml.png" /></td><td style="background-color:#000000;">'+content+'</td><td class="mr"><img src="includes/greybox/mr.png" /></td></tr><tr><td class="bl"><img src="includes/greybox/bl.png" /></td><td class="bm"><img src="includes/greybox/bm.png" /></td><td class="br"><img src="includes/greybox/br.png" /></td></tr></table>';
	}
}

function GB_hide() {
  $("#GB_window,#GB_overlay").hide();
}

function GB_position() {
	var de = document.documentElement;
	var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var ml = (GB_WIDTH/2)*-1;
	var mt = (GB_HEIGHT/2)*-1;
	$("#GB_window").css({width:GB_WIDTH+20+"px",height:GB_HEIGHT+20+"px"});
	$("#GB_window").css('margin-left', ml-10+'px');
	$("#GB_window").css('margin-top', mt+'px');
	$("#GB_frame").css("height",GB_HEIGHT+"px");
}
