/* **********************************
 * Functions
 * **********************************/
function addEventHandler(p_element, p_event, p_func) {
	if (p_element.attachEvent)	// *** IE Event Model ***
		p_element.attachEvent('on' + p_event, p_func);
	else if(p_func != undefined) 	// *** DOM Level 2 Event Model: !!NOTE!! NOT DEFENSIVE  ***
		p_element.addEventListener(p_event, p_func, false);
}	// *** End Function: addEventHandler(HTMLElement, String, Function) ***

/* **********************************
 * Generic Objects
 * **********************************/
function FlashObject() {
	var self=this;

	this.init=function() {
	}

	this.init();
}

function ModalBox() {
	var m_modal_bg;
	var m_box;
	var m_dragger;
	var m_parent=document.body;
	var m_content_parent={};
	var m_use_close_div=false;
	var m_close = null;
	var m_closeBefore=function() {};
	var m_closeAfter=function() {};
	var self=this;

	this.init=function() {
		m_modal_bg = document.createElement('div');
		m_box = document.createElement('div');
		
		self.initStyles();

	}

	this.initStyles=function() {
		// Opaque Background
		m_modal_bg.id = 'modal_bg';
		m_modal_bg.style.position='absolute';
		m_modal_bg.style.top='0px';
		m_modal_bg.style.left='0px';
		m_modal_bg.style.width='100%';
		m_modal_bg.style.height='100%';
		m_modal_bg.style.opacity='.5';
		m_modal_bg.style.filter='alpha(opacity=50)';
		m_modal_bg.style.backgroundColor='#000000';
		m_modal_bg.style.zIndex = '55';

		// Box Object
		m_box.id='popup_box';
		m_box.className='popup_box';
		m_box.style.top='50%';
		m_box.style.left='50%';
		m_box.style.zIndex='56';
	}

	this.addToBox=function(p_content,p_parent) {  // set an object to which the content should return on clear
		var p_element;
		if (typeof p_content == "string") {
			p_element=document.createElement('span');
			p_element.innerHTML = p_content;
		} else {
			p_element=p_content;
		}
		if (p_element.id != null && p_parent != null) {
			self.setContentParent(p_element.id,p_parent);
		}
		m_box.appendChild(p_element);
	}

	this.useCloseLink=function() {
		m_use_close_div = true;
		m_close = self.getCloseLink();
	}
	
	this.setBoxContent=function(p_content,p_parent) {
		self.clearElement(m_box);
		self.addToBox(p_content,p_parent);
	}

	this.setStyle=function(p_style_name,p_style_setting) {
		m_box.style[p_style_name] = p_style_setting;
	}

	this.setParent=function(p_element) {
		m_parent=(typeof p_element == "string" ? document.getElementById(p_element) : p_element);
	}

	this.setContentParent=function(p_id, p_element) {
		var p_parent_element=(typeof p_element == "string" ? document.getElementById(p_element) : p_element);
		m_content_parent[p_id] = p_parent_element;
	}

	this.setSpecialClickClose=function() {
		addEventHandler(m_modal_bg,'click',this.close);
	}

	this.setAfterCloseStep=function(p_fn) {
		m_closeAfter = p_fn;
	}
	
	this.clearSpecialClickClose=function() {
		addEventHandler(m_modal_bg,'click',null);
	}

	this.getCloseLink=function() {
		var cl = document.createElement('div');
		cl.style.textAlign='right';
		cl.style.fontSize='10px';
		cl.style.textDecoration='underline';
		cl.style.fontFamily='helvetica';
		cl.innerHTML = 'Close';
		addEventHandler(cl,'click',this.close);
		return cl;
	}

	this.insertCloseLink = function() {
		if (m_close != null) {
			if (m_box.hasChildNodes()) {
				var c1 = m_box.childNodes[0];
				m_box.insertBefore(m_close,c1);
			} else {
				m_box.appendChild(m_close);
			}
				
		}
	}

	this.extractCloseLink = function() {
		if (m_close != null) {
			if (m_close.parentNode == m_box) {
				m_box.removeChild(m_close);
			}
		}
	}
	
	this.clearElement=function(p_element) {
		if (p_element.id != null && m_content_parent[p_element.id] != null) {
			m_content_parent[p_element.id].appendChild(p_element);
			delete m_content_parent[p_element.id];
		} else {
			while (p_element.hasChildNodes()) {
				if (p_element.childNodes[0].hasChildNodes()) {
					self.clearElement(p_element.childNodes[0]);
				} else {
					p_element.removeChild(p_element.childNodes[0]);
					delete p_element;
				}
			}
		}
	}

	this.centerElement=function(p_element,p_parent) {
		var h_e = p_element.offsetHeight;
		var w_e = p_element.offsetWidth;
		var h_p = p_parent.offsetHeight;
		var w_p = p_parent.offsetWidth;
		if (h_p > 0) {
			var p_top = ((.5 - ((h_e / h_p) / 2)) * 100);
			p_element.style.top = (p_top < 0 ? "0%" :  p_top + "%");
		}
		if (w_p > 0) {
			var p_left = ((.5 - ((w_e / w_p) / 2)) * 100);
			p_element.style.left = (p_left < 0 ? "0%" : p_left + "%");
		}
	}

	this.open=function() {
		self.insertCloseLink();
		m_parent.appendChild(m_modal_bg);
		//$(m_modal_bg.id).fade({ duration: .5, from: 0, to: .5 });

		m_parent.appendChild(m_box);
		//m_dragger = new Draggable(m_box.id, { handle: 'login_banner' });
		self.centerElement(m_box,m_modal_bg);

		//$(m_box.id).fade({ duration: .5, from: 0, to: 1 });
	}

	this.close=function() {
		self.extractCloseLink();
		m_parent.removeChild(m_box);
		m_parent.removeChild(m_modal_bg);
		self.clearSpecialClickClose();
		self.clearElement(m_box);
		m_closeAfter();
	}

	this.getBox=function() {
		return m_box;
	}

	this.getBG=function() {
		return m_modal_bg;
	}

	this.init();
}

