// Global variables
var popup_x = 0;						// x position of popup
var popup_y = 0;						// y position of popup
var popup_count = 0;					// used to count number of onMouseOver nestings
var popup_switch_view_elem_id = '';		// tells us which elem_id we want to switch our image viewing
var popup_current_view_elem_id = '';	// tells us that which elem_id is currently viewed

var popup_show_timer = 250;
var popup_hide_timer = 150;
var popup_onunload_timer = 5000;

function getAbsolutePosition(element) {
	var r = { x: element.offsetLeft, y: element.offsetTop };
    if (element.offsetParent) {
		var tmp = getAbsolutePosition(element.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
    }
    return r;
}

function iecompattest(){
	return (document.compatMode && document.compatMode!="BackCompat") ? document.documentElement : document.body
}

function getWindowBoundary() {
	var w = { x : 0, y : 0, width : 0, height : 0 };
	if (window.innerWidth || window.opera) {
		w.x = window.pageXOffset;
     	w.width = window.innerWidth + w.x;
     	w.y = window.pageYOffset;
     	w.height = window.innerHeight + w.y;
    } else if (document.body) {
		ie = iecompattest();
		w.x = ie.scrollLeft;
		w.width = ie.offsetWidth;
     	w.y = ie.scrollTop;
     	w.height = ie.offsetHeight;
    }
	return w;
}

function PopupShow(elem_id, image_url, link_url, width, height, offset_x, offset_y, dir_nesting) {
	if (width > 1000) width = 1000;
	if (height > 1000) height = 1000;
	
	popup_count++;
	popup_switch_view_elem_id = elem_id;
	
	if (popup_current_view_elem_id != elem_id) {
		setTimeout("_do_popup_show('" + elem_id + "','" + image_url + "','" + link_url + "'," + width + "," + height + "," + offset_x + "," + offset_y + ",0,0,'" + dir_nesting + "')", popup_show_timer);
	}
}

function PopupShowForcedDirection(elem_id, image_url, link_url, width, height, offset_x, offset_y, direction_x, direction_y, dir_nesting) {
	if (width > 1000) width = 1000;
	if (height > 1000) height = 1000;
	
	if (direction_x < 0) direction_x = -1;
	else if (direction_x > 0) direction_x = 1;
	if (direction_y < 0) direction_y = -1;
	else if (direction_y > 0) direction_y = 1;
	
	popup_count++;
	popup_switch_view_elem_id = elem_id;
	
	if (popup_current_view_elem_id != elem_id) {
		setTimeout("_do_popup_show('" + elem_id + "','" + image_url + "','" + link_url + "'," + width + "," + height + "," + offset_x + "," + offset_y + "," + direction_x + "," + direction_y + ",'" + dir_nesting + "')", popup_show_timer);
	}
}

function _do_popup_show(elem_id, image_url, link_url, width, height, offset_x, offset_y, direction_x, direction_y, dir_nesting) {
	if (popup_count > 0 && popup_switch_view_elem_id == elem_id) {
		var elem = document.getElementById(elem_id);
		var popup_main = document.getElementById('popup_main');
		var popup_back = document.getElementById('popup_back');
		var popup_tab = document.getElementById('popup_tab');
		var popup_back_table = document.getElementById('popup_back_table');
		if (elem != null && popup_main != null && popup_back != null && popup_tab != null && popup_back_table != null) {
			popup_current_view_elem_id = elem_id;
		
			// calculate positioning of popup
			var r = getAbsolutePosition(elem);
			var w = getWindowBoundary();
			var mid_x = r.x + elem.offsetWidth / 2 + offset_x;
			var mid_y = r.y + elem.offsetHeight / 2 + offset_y;
			var tab_x = 0, tab_y = mid_y, back_x = 0, back_y = 0, back_x = 0, back_y = 0;
			
			var popup_tab_image_url;
			if (mid_x - direction_x * w.width <= w.x + w.width / 2) {  // left side of screen
				// change tab image
				popup_tab_image_url = dir_nesting + 'images/misc/popup_table_a.gif';
				
				tab_x = mid_x;
				back_x = mid_x + 20;
			} else {  // right side of screen
				// change tab image
				popup_tab_image_url = dir_nesting + 'images/misc/popup_table_b.gif';
				
				tab_x = mid_x - 26;
				back_x = mid_x - width - 30;
			}
			if (mid_y - direction_y * w.height <= w.y + w.height / 2) {  // top half of screen
				back_y = mid_y - 30;
			} else {  // bottom half of screen
				back_y = mid_y - height + 30;
			}
			main_x = back_x + 5;
			main_y = back_y + 5;
					
			var popup_main_elem;
			var popup_tab_elem;
			var popup_main_image_elem;
			var spacer_url = dir_nesting + 'images/misc/spacer.gif';
			try {
				popup_main_image_elem = document.createElement('<img src="' + spacer_url + '" border="0">');
				popup_main_image_elem.style.width = width;
				popup_main_image_elem.style.height = height;
				if (link_url != '') {
					popup_main_elem = document.createElement('<a href="' + link_url + '">');
					popup_main_elem.appendChild(popup_main_image_elem);
				} else popup_main_elem = popup_main_image_elem;
			} catch (e) {
				popup_main_image_elem = document.createElement('img');
				popup_main_image_elem.setAttribute('src', spacer_url);
				popup_main_image_elem.setAttribute('width', width);
				popup_main_image_elem.setAttribute('height', height);
				popup_main_image_elem.setAttribute('border', '0');
				if (link_url != '') {
					popup_main_elem = document.createElement('a');	
					popup_main_elem.setAttribute('href', link_url);
					popup_main_elem.appendChild(popup_main_image_elem);
				} else popup_main_elem = popup_main_image_elem;
			}
			popup_main_image_elem.setAttribute('src', image_url);
			if (popup_main.firstChild != null)
				popup_main.removeChild(popup_main.firstChild);
			popup_main.appendChild(popup_main_elem);
			
			try {
				var popup_tab_image_elem = document.createElement('<img src="' + popup_tab_image_url + '" width="26" height="20" border="0">');
				if (link_url != '') {
					popup_tab_elem = document.createElement('<a href="' + link_url + '">');
					popup_tab_elem.appendChild(popup_tab_image_elem);
				} else popup_tab_elem = popup_tab_image_elem;
			} catch (e) {
				var popup_tab_image_elem = document.createElement('img');
				popup_tab_image_elem.setAttribute('src', popup_tab_image_url);
				popup_tab_image_elem.setAttribute('width', '26');
				popup_tab_image_elem.setAttribute('height', '20');
				popup_tab_image_elem.setAttribute('border', '0');
				if (link_url != '') {
					popup_tab_elem = document.createElement('a');	
					popup_tab_elem.setAttribute('href', link_url);
					popup_tab_elem.appendChild(popup_tab_image_elem);
				} else popup_tab_elem = popup_tab_image_elem;
			}
			if (popup_tab.firstChild != null)
				popup_tab.removeChild(popup_tab.firstChild);
			popup_tab.appendChild(popup_tab_elem);
			
			// resize popup main table
			popup_back_table.setAttribute('width', width + 10);
			popup_back_table.setAttribute('height', height + 10);
			
			// move the popup tab
			popup_tab.style.left = tab_x + "px";
			popup_tab.style.top = tab_y + "px"
			
			// move the popup back
			popup_back.style.left = back_x + "px";
			popup_back.style.top = back_y + "px";
			
			// move the popup main
			popup_main.style.left = main_x + "px";
			popup_main.style.top = main_y + "px";
			
			// make layer visible
			popup_tab.style.visibility = '';
			popup_back.style.visibility = '';
			popup_main.style.visibility = '';
		}
	}
}

function PopupHide() {
	popup_count = 0;
	setTimeout("_do_popup_hide()", popup_hide_timer);
}

function _do_popup_hide() {
	if (popup_count <= 0) {
		// hide all duv layers
		var popup_tab = document.getElementById('popup_tab');
		var popup_back = document.getElementById('popup_back');
		var popup_main = document.getElementById('popup_main');
		
		if (popup_tab != null && popup_back != null && popup_main != null) {
			// hide layer
			popup_tab.style.visibility = 'hidden';
			popup_back.style.visibility = 'hidden';
			popup_main.style.visibility = 'hidden';
			
			popup_tab.style.left = "-5000px";
			popup_back.style.left = "-5000px";
			popup_main.style.left = "-5000px";
			
			popup_current_view_elem_id = '';		// no elem_id is currently being viewed, popup is hidden
			
			popup_count = 0;
		}
	}
}

function PopupQuickShow() {
	popup_count++;
}

function PopupInstantHide() {
	popup_count = 0;
	_do_popup_hide();	
}

function PopupOnUnload() {
	setTimeout("PopupInstantHide()", popup_onunload_timer);
}

function _debug_popup_count() {
	alert(popup_count);	
}