/**
* Submodal Dialog
* Original By Seth Banks (webmaster at subimage dot com)  http://www.subimage.com/
* Contributions by Eric Angel (tab index code), Scott (hiding/showing selects for IE users), Todd Huss (submodal class on hrefs, moving div containers into javascript, phark method for putting close.gif into CSS), Thomas Risberg (safari fixes for scroll amount), Dave Campbell (improved parsing of submodal-width-height class)
* Rewritten by Horia Traian on 03/02/2007
*/

var pop_callback_func;
var pop_has_close_btn = true;
var pop_attach_events = pop_attach_events != null ? pop_attach_events : false;
var pop_events_class  = 'popup';
var pop_loading_txt   = 'Please Wait...';
var pop_loading_url   = '';
var pop_url 		  = '';

var pop_iframe_id 	  = 'pop_iframe';
var pop_mask_id 	  = 'pop_mask';
var pop_mask_class 	  = 'pop_mask_back';
var pop_container_id  = 'pop_container';
var pop_inner_id 	  = 'pop_inner';
var pop_close_btn_id  = 'pop_controls';
var pop_title_bar_id  = 'pop_title_bar';
var pop_contents_id   = 'pop_contents';
var pop_title_id	  = 'pop_title';
var pop_loading_class = 'pop_loading';
var pop_confirm_class = 'info';
var pop_current_class = '';

var pop_type = null;
var pop_dialog_id = null;
var pop_mask = null;
var pop_container = null;
var pop_iframe = null;
var pop_close_btn = null;
var pop_contents = null;
var pop_title = null;
var pop_curr_width = null;
var pop_curr_height = null;
var pop_overflow_x = '';
var pop_overflow_y = '';


var pop_hide_selects = window.ie6 ? true : false;
var pop_is_shown = false;
var pop_tab_indexes = new Array();
var pop_timer = null;
var pop_repeat_call = false;
var pop_repeat_time = 3000; // 3 seconds

// Pre-defined list of tags we want to disable/enable tabbing into
var pop_tabbable_tags = new Array("A","BUTTON","TEXTAREA","INPUT","IFRAME");

addEvent(window, "load", popup_init);
addEvent(window, "resize", popup_center);
//addEvent(window, "scroll", popup_center);
window.onscroll = popup_center;

// If using Mozilla or Firefox, use Tab-key trap.
if (!document.all) {
	document.onkeypress = keyDownHandler;
}

function popup_init() {
	// Add the HTML to the body
	var body = document.getElementsByTagName('body')[0];

	pop_mask = document.createElement('div');
	pop_mask.id = pop_mask_id;
	pop_mask.className = pop_mask_class;

	pop_container = document.createElement('div');
	pop_container.id = pop_container_id;
	pop_container.innerHTML = '' +
	'<div id="'+pop_inner_id+'">' +
	'<div id="'+pop_title_bar_id+'">' +
	'<div id="'+pop_title_id+'"></div>' +
	'<div id="'+pop_close_btn_id+'">' +
	'<a onclick="popup_hide(false);"><span>Close</span></a>' +
	'</div>'+
	'</div>'+
	'<div id="'+pop_contents_id+'" class="'+pop_loading_class+'">'+pop_loading_txt+'</div>' +
	'<iframe src="'+pop_loading_url+'" id="'+pop_iframe_id+'" name="'+pop_iframe_id+'" style="width:100%;height:100%;background-color:transparent;" scrolling="auto" frameborder="0" allowtransparency="true" width="100%" height="100%"></iframe>'+
	'</div>';

	body.appendChild(pop_mask);
	body.appendChild(pop_container);

	pop_title = document.getElementById(pop_title_id);
	pop_close_btn = document.getElementById(pop_close_btn_id);
	pop_contents = document.getElementById(pop_contents_id);
	pop_contents.className = pop_loading_class;
	pop_iframe = document.getElementById(pop_iframe_id);
	pop_iframe.style.display = 'none';

	// Add onclick handlers to 'a' elements of class submodal or submodal-width-height
	if(pop_attach_events) {
		var elms = document.getElementsByTagName('a');
		for (i = 0; i < elms.length; i++) {
			if (elms[i].className.indexOf(pop_events_class) >= 0) {
				elms[i].onclick = function(){
					// default width and height
					var width = 400;
					var height = 200;
					// Parse out optional width and height from className
					var startIndex = this.className.indexOf(pop_events_class);
					var endIndex = this.className.indexOf(" ", startIndex);
					if (endIndex < 0) {
						endIndex = this.className.length;
					}
					var clazz = this.className.substring(startIndex, endIndex);
					params = clazz.split('-');
					if (params.length == 3) {
						width = parseInt(params[1]);
						height = parseInt(params[2]);
					}
					popup_url(this.title,this.href,width,height,null); return false;
				}
			}
		}
	}
}

function popup_url(title, url, width, height, has_close_btn, callback, repeat_call, repeat_time)
{
	return popup_show('url', title, url, width, height, has_close_btn, callback, null, null, repeat_call, repeat_time);
}

function popup_dlg(title, dialog_id, width, height, has_close_btn, callback)
{
	return popup_show('id', title, dialog_id, width, height, has_close_btn, callback);
}

function popup_wait(title, contents, width, height)
{
	return popup_show('', title, contents, width, height, false, null, pop_loading_class);
}

function popup_status(title, url, width, height)
{
	return popup_show('url', title, url, width, height, false, null, pop_loading_class, null, true);
}

function popup_confirm(title, contents, width, height, callback)
{
	return popup_show('id', title, contents, width, height, true, callback, pop_confirm_class, '');
}

function popup_show(type, title, data, width, height, has_close_btn, callback, contents_class, mask_class, repeat_call, repeat_time)
{
	disableTabIndexes();
	// for IE 5 & 6
	if (pop_hide_selects) {
		hideSelectBoxes();
	}

	pop_is_shown = true;
	pop_type = type;
	pop_callback_func = callback;	

	if(has_close_btn == null) {
		has_close_btn = pop_has_close_btn;
	}

	if(contents_class == null) {
		contents_class = '';
	}

	if(mask_class == null) {
		mask_class = pop_mask_class;
	}

	pop_current_class = contents_class;

	if(repeat_call != null) {
		pop_repeat_call = repeat_call;
	}

	if(repeat_time != null) {
		pop_repeat_time = repeat_time;
	}

	pop_mask.className = mask_class;
	pop_title.innerHTML = (title != null) ? title : '';
	pop_close_btn.style.display = has_close_btn ? 'block' : 'none';
	pop_contents.className = pop_current_class;

	pop_mask.style.display = "block";
	pop_container.style.display = "block";
	pop_contents.style.display = 'block';

	popup_center(width, height);
	popup_resize(width, height);
	
	// Important : hidding the scrollbars needs to be done after the centering and resizing of the popup
	// due a bug in Firefox
	pop_overflow_x = (document.body.style.overflowX != '') ? document.body.style.overflowX : 'auto';
	pop_overflow_y = (document.body.style.overflowY != '') ? document.body.style.overflowY : 'auto';
	document.body.style.overflowX = 'hidden';
	document.body.style.overflowY = 'hidden';

	switch(pop_type) {

		case 'url':
		pop_url = data;
		pop_iframe.src = pop_url;
		pop_contents.style.display = 'none';
		pop_contents.className = '';
		pop_iframe.style.display = 'block'

		if(pop_repeat_call) {
			popup_reload_iframe();
		}
		break;

		case 'id':
		pop_dialog_id = data;
		var obj = document.getElementById(pop_dialog_id);
		if(obj != null) {
			pop_contents.innerHTML = obj.innerHTML;
			obj.innerHTML = '';
		}
		break;

		default:
		pop_contents.innerHTML = data;
		break;
	}
	
	return true;
}


function popup_show_iframe(title, width, height)
{
	disableTabIndexes();
	// for IE 5 & 6
	if (pop_hide_selects) {
		hideSelectBoxes();
	}

	pop_is_shown = true;
	pop_title.innerHTML = (title != null) ? title : '';
	pop_contents.style.display = 'none';
	pop_iframe.style.display = 'block';
	pop_close_btn.style.display = 'block';
	pop_mask.style.display = "block";
	pop_container.style.display = "block";

	popup_center(width, height);
	popup_resize(width, height);
}

function popup_reload_iframe()
{
	var doc = window.frames[pop_iframe_id].document;
	pop_contents.style.display = 'none';
	pop_iframe.style.display = 'block';

	if(doc != null && doc.body.innerHTML.length > 0) {
		pop_contents.innerHTML = window.frames[pop_iframe_id].document.body.innerHTML;
	}
	pop_iframe.src = pop_url + '?rid=' + Math.random();
	window.setTimeout("popup_reload_iframe();", pop_repeat_time);
}

function popup_resize(width, height)
{
	var tbar_height = parseInt(document.getElementById(pop_title_bar_id).offsetHeight, 10);
	pop_container.style.width = width + "px";
	pop_container.style.height = (height + tbar_height) + "px";
	var tbar_width = parseInt(document.getElementById(pop_title_bar_id).offsetWidth, 10);
	pop_iframe.style.width = tbar_width + "px";
	pop_iframe.style.height = height + "px";
	pop_contents.style.width = tbar_width + "px";
	pop_contents.style.height = height + "px";
}

var gi = 0;
function popup_center(width, height) {
	if (pop_is_shown == true) {
		if (width == null || isNaN(width)) {
			width = pop_container.offsetWidth;
		}
		if (height == null) {
			height = pop_container.offsetHeight;
		}
		var fullHeight = getViewportHeight();
		var fullWidth = getViewportWidth();
		// scLeft and scTop changes by Thomas Risberg
		var scLeft,scTop;
		if (self.pageYOffset) {
			scLeft = self.pageXOffset;
			scTop = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) {
			scLeft = document.documentElement.scrollLeft;
			scTop = document.documentElement.scrollTop;
		} else if (document.body) {
			scLeft = document.body.scrollLeft;
			scTop = document.body.scrollTop;
		}
		pop_mask.style.height = fullHeight + "px";
		pop_mask.style.width = fullWidth + "px";
		pop_mask.style.top = scTop + "px";
		pop_mask.style.left = scLeft + "px";
		window.status = pop_mask.style.top + " " + pop_mask.style.left + " " + gi++;
		var titleBarHeight = parseInt(document.getElementById(pop_title_bar_id).offsetHeight, 10);
		var topMargin = scTop + ((fullHeight - (height+titleBarHeight)) / 2);
		if (topMargin < 0) { topMargin = 0; }
		pop_container.style.top = topMargin + "px";
		pop_container.style.left =  (scLeft + ((fullWidth - width) / 2)) + "px";
	}
}

/**
* @argument callback_func - bool - determines if we call the return function specified
* @argument returnVal - anything - return value
*/
function popup_hide(callback_func) {

	if (pop_mask == null) {
		return;
	}

	pop_is_shown = false;

	document.body.style.overflowX = pop_overflow_x;
	document.body.style.overflowY = pop_overflow_y;

	pop_mask.style.display = "none";
	pop_container.style.display = "none";
	pop_iframe.style.display = 'none';

	restoreTabIndexes();

	// display all select boxes
	if (pop_hide_selects == true) {
		displaySelectBoxes();
	}

	if (callback_func == true && pop_callback_func != null) {
		pop_callback_func(window.frames[pop_iframe_id].returnVal);
	}

	switch(pop_type) {
		case 'id':
		var obj = document.getElementById(pop_dialog_id);
		obj.innerHTML = pop_contents.innerHTML;
		break;
	}

	pop_iframe.src = pop_loading_url;
	pop_contents.className = pop_loading_class;
	pop_contents.innerHTML = pop_loading_txt;
}

// Tab key trap. iff popup is shown and key was [TAB], suppress it.
// @argument e - event - keyboard event that caused this function to be called.
function keyDownHandler(e) {
	if (pop_is_shown && e.keyCode == 9)  return false;
}

// For IE.  Go through predefined tags and disable tabbing into them.
function disableTabIndexes() {
	if (document.all) {
		var i = 0;
		for (var j = 0; j < pop_tabbable_tags.length; j++) {
			var tagElements = document.getElementsByTagName(pop_tabbable_tags[j]);
			for (var k = 0 ; k < tagElements.length; k++) {
				pop_tab_indexes[i] = tagElements[k].tabIndex;
				tagElements[k].tabIndex="-1";
				i++;
			}
		}
	}
}

// For IE. Restore tab-indexes.
function restoreTabIndexes() {
	if (document.all) {
		var i = 0;
		for (var j = 0; j < pop_tabbable_tags.length; j++) {
			var tagElements = document.getElementsByTagName(pop_tabbable_tags[j]);
			for (var k = 0 ; k < tagElements.length; k++) {
				tagElements[k].tabIndex = pop_tab_indexes[i];
				tagElements[k].tabEnabled = true;
				i++;
			}
		}
	}
}

/**
* Hides all drop down form select boxes on the screen so they do not appear above the mask layer.
* IE has a problem with wanted select form tags to always be the topmost z-index or layer
* Thanks for the code Scott!
*/
function hideSelectBoxes() {
	for(var i = 0; i < document.forms.length; i++) {
		for(var e = 0; e < document.forms[i].length; e++){
			if(document.forms[i].elements[e].tagName == "SELECT") {
				document.forms[i].elements[e].style.visibility="hidden";
			}
		}
	}
}

/**
* Makes all drop down form select boxes on the screen visible so they do not reappear after the dialog is closed.
* IE has a problem with wanted select form tags to always be the topmost z-index or layer
*/
function displaySelectBoxes() {
	for(var i = 0; i < document.forms.length; i++) {
		for(var e = 0; e < document.forms[i].length; e++){
			if(document.forms[i].elements[e].tagName == "SELECT") {
				document.forms[i].elements[e].style.visibility="visible";
			}
		}
	}
}

/**
* X-browser event handler attachment and detachment
* @argument obj - the object to attach event to
* @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
* @argument fn - function to call
*/
function addEvent(obj, evType, fn){
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, false);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else {
		return false;
	}
}

/**
* Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/ *
* Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
* Gets the full width/height because it's different for most browsers.
*/
function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight;
	return window.undefined;
}

function getViewportWidth() {
	if (window.innerWidth!=window.undefined) return window.innerWidth;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth;
	if (document.body) return document.body.clientWidth;
	return window.undefined;
}
