// Constants
var _div = "popup_";
var _ns = (navigator.appName == "Netscape");

// Class PopupCollection
function PopupCollection() {
	
	// store in the document 
	document.myPopups = this;
	
	// Properties
	this.array = new Array();
	this.active = null;	

	// Load	
	this.Load = function() {
		var html = '';
		for (var i = 0; i < this.array.length; i++) {
			html += this.array[i].Html();
		}
		
		document.body.innerHTML += html;

		for (var i = 0; i < this.array.length; i++) {
			var menu = this.array[i];
			menu.element = document.getElementById(menu.id);
			menu.popup = document.getElementById(_div + menu.id);
			
			// event handlers
			
			menu.element.onkeypress = function(ev) {
			
				if (_ns && ev.keyCode != 13) return;
				if (!_ns && event.keyCode != 13) return;
				
				document.myPopups.Get(this.id).Open();
				
				var menu = document.myPopups.Get(this.id);
				
				if (menu.items.length == 0) {
					window.location = menu.element.href;
				}
				else {
				
					var id = menu.items[0].id;
					var item = document.getElementById(id);
					item.focus();
					
				}
			}

			menu.element.onmouseover = function() {
				document.myPopups.Get(this.id).Open();
			}

			menu.element.onmouseout = function() {
				document.myPopups.Get(this.id).Close();
			}
		}
	}
	
	// Add
	this.Add = function(id, drop) {
		var popup = new Popup(id, drop);
		popup.collection = this;
		this.array[this.array.length] = popup;		
		return popup;
	}
	
	// Get
	this.Get = function(id) {
		for (var i = 0; i < this.array.length; i++) {
			if (this.array[i].id == id) return this.array[i];
		}
		return null;
	}
	
}	

// Class Popup
function Popup(id, drop) {

	// Properties
	this.collection = null;
	this.id = id;
	this.drop = drop;
	this.element = document.getElementById(id);
	this.items = new Array();
	
	// Open
	this.Open = function () {
		
		if (document.myPopups.active) {
			document.myPopups.active.Close();
		}

		document.myPopups.active = this;
		
		if (!this.popup) return;
		
		if (this.drop == "up") {
			this.popup.style.top = offsetTop(this.element) - this.popup.offsetHeight;
		}
		else {
			this.popup.style.top = offsetTop(this.element) + this.element.offsetHeight;
		}
		this.popup.style.left = offsetLeft(this.element);
		this.popup.style.visibility = 'visible';

	}

	// Close
	this.Close = function () {
		document.myPopups.active = null;
		if (this.popup) this.popup.style.visibility = 'hidden';
	}

	// Add
	this.Add = function (id, caption, link, newwindow) {
	
		var item = new PopupItem(this, id, caption, link, newwindow);
		this.items[this.items.length] = item;

	}

	// Html
	this.Html = function () {

		var html = '';
		var id = '';
		
		for (var i = 0; i < this.items.length; i++) {
			
			var item = this.items[i];
			
			if (id != item.menu.id) {
				
				id = item.menu.id;

				if (html != '') html += '</table></div>';
		
				html += '<div id="' + _div + id + '" ';
				html += 'style="visibility:hidden; position:absolute; left:0px; top:0px" ';
				html += 'onmouseover="document.myPopups.Get(\'' + id + '\').Open()" ';
				if (!_ns) html += 'onmouseout="document.myPopups.Get(\'' + id + '\').Close()" ';
				html += 'class="popup">';
				html += '<table cellpadding="0" cellspacing="0">';
			}

			html += '<tr>';
			html += '<td class="popup_item">';
			if (item.link != '') {
				html += '<a id="' + item.id + '" href="' + item.link + '" ' + ((item.newwindow==1)? 'target="blank" ' : '') + 'class="popup_item_text" style="width:100%">'
				html += item.caption;
				html += '</a>';
			}
			else {
				html += '<span id="' + item.id + '" class="popup_item_text" style="width:100%">';
				html += item.caption;
				html += '</span>';
			}

			html += '</td>';
			html += '</tr>';

		}

		if (html != '') {
			html += '</table></div>';
		}

		return html;

	}

}

// Class PopupItem
function PopupItem(menu, id, caption, link, newwindow) {
	
	// Properties
	this.menu = menu;
	this.id = id;
	this.caption = caption;
	this.link = link;
	this.newwindow = newwindow;
	
}

// Generic DHTML
function offsetLeft(element) {

	var left = 0;
	if (element.offsetParent.tagName != "BODY") {
		left += offsetLeft(element.offsetParent);
	}
	return element.offsetLeft + left;

}

function offsetTop(element) {

	var top = 0;
	if (element.offsetParent.tagName != "BODY") {
		top += offsetTop(element.offsetParent);
	}
	return element.offsetTop + top;

}


function GetPrint( id ){

	window.open( 'default.aspx?SqlPage=Print&Action=Print&CpContentId=' + id );

}