/* 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)
 * Rewritten and extended by Anatoly Deryshev
 */

var GB_DONE = false;
var GB_HIDDEN = false;
var GB_DEFAULT_HEIGHT = 'auto';
var GB_DEFAULT_WIDTH = 'auto';
var GB_HEIGHT = null;
var GB_WIDTH = null;
var GB_ANIMATION = false;
var GB_TRIGGER = null;
/**
 * Opens modal window with iframe inside
 * @param	{DOM Object}	trigger		trigger that called modal window creation (link or button)
 * @param	{String}		caption		caption to display in the modal window header 
 * @param	{String}		url			url to open inside the iframe
 * @param	{String}		height		height of the modal window in px, allowed 'auto' for auto sizing or null for default height
 * @param	{String}		width		width of the modal window in px, allowed 'auto' for auto sizing or null for default width
 */
var GB_show = function(trigger, caption, url, height, width) {
	GB_HEIGHT = height? height : GB_DEFAULT_HEIGHT;
	GB_WIDTH = width? width : GB_DEFAULT_WIDTH;
	caption = caption? caption : "";
	if(!GB_DONE) {
		$(document.body).append("<div id='GB_overlay'></div><div id='GB_window'><a id='GB_anchor' title='Modal window anchor' href='javascript: void(0);'>&nbsp;</a><div id='GB_caption'></div>"
			+ "<a class='GB_closeLink' title='Close window' href='javascript: GB_hide();'><img class='GB_closeLinkButton' alt=''/></a></div>");
		$("#GB_overlay").click(GB_hide);
		GB_DONE = true;
	}
	$(window).bind("resize", GB_position);
	$("#GB_frame").remove();
	$("#GB_window").append("<iframe id='GB_frame' src='"+url+"'></iframe>");

	$("#GB_caption").html(caption);
	$("#GB_overlay").show();
	GB_position();
	GB_TRIGGER = trigger;
	$(trigger).bind("blur", GB_triggerBlur);
	
	if(GB_ANIMATION){
		$("#GB_window").slideDown("slow");
	}
	else{
		$("#GB_window").show("fast");
	}
	$("#GB_anchor")[0].focus();
	GB_HIDDEN = false;
	return false;
};
/**
 * Hides modal window
 */
var GB_hide = function() {
	$("#GB_window,#GB_overlay").hide("fast");
	GB_HIDDEN = true;
	$(GB_TRIGGER).unbind("blur", GB_triggerBlur);
	$(window).unbind("resize", GB_position);
	GB_TRIGGER.focus();
	GB_TRIGGER = null;
};
/**
 * Focuses on opened modal window on trigger focus loose
 * @param	{Event}		evt		event object
 */
var GB_triggerBlur = function(evt){
	if (!GB_HIDDEN){
		$("#GB_anchor")[0].focus();
	}
};
/**
 * Handles modal window position and size
 */
var GB_position = function() {
	$("#GB_overlay").hide();
	var docWidth = document.documentElement.clientWidth;
	var docHeight = document.documentElement.clientHeight;
	var width = 'auto' == GB_WIDTH? docWidth - 40 : GB_WIDTH;
	var height = 'auto' == GB_HEIGHT? docHeight - 40 : GB_HEIGHT;
	if (width > docWidth){
		width = docWidth - 40;
	}
	if (height > docHeight){
		height = docHeight - 40;
	}
	var left = (docWidth - width) / 2;
	var top = (docHeight - height) / 2;
	var bgHeight = document.documentElement.scrollHeight || docHeight;
	var bgWidth = document.documentElement.scrollWidth > document.documentElement.offsetWidth? document.documentElement.scrollWidth : docWidth;
	$("#GB_overlay").css({height: bgHeight + "px", width: bgWidth + "px"});
	$("#GB_window").css({width: width + "px", height: height + "px", left: left + "px", top: top + "px"});
	$("#GB_frame").css({height: (height - 22) + "px"});
	$("#GB_overlay").show();
};
