/* Copyright (c) 2003 Macromedia Inc.

	$Revision: 1.38 $ 

*/

adobe.Loader.requireAsset("/css/iframemanager.css");

PageMaskImage = function () {
	
	if (browser.ua.indexOf('msie 5.0') != -1) { return; };
	
	var imagepath = (browser.appN.indexOf('netscape') != -1) ? '/images/alpha/939598_opacity60_1200x3000.png': '/images/alpha/939598_opacity60.png';
	
	var pagemask = new PNG('pageshield',imagepath,null);
	
	var content = getContentDimensions();
	pagemask.style.width = content.width +'px';
	pagemask.style.height = content.height +'px';
	
	pagemask.style.position = 'absolute';
	pagemask.style.top = 0;
	pagemask.style.left = 0;
	pagemask.style.zIndex = 1000;
	pagemask.style.visibility = 'hidden';
	
	var bodyElement = document.getElementsByTagName('body')[0];
	pagemask = bodyElement.appendChild(pagemask);
	
	return pagemask;
}


/* ////////// EmxPopup //////////////////// */

EmxPopup = function (width,height,contentString,modal,titleString) {
	
	var self = this;
	
	this.isDisplayed = null;
	this.onclose;// onclose event handler
	
	// get dimensions the available window area
	var windowArea = getWindowDimensions();
	
	/* if width or height are larger than available window area resize to the default */
	this.width = (width < windowArea.width) ? width : 500;
	this.height = (height < windowArea.height) ? height : 336;
	
	this.pagemask = new PageMaskImage();
	
	this.popupDIV = document.createElement('div');
	this.popupDIV.setAttribute('id','EmxPopup');
	
	this.popupDIV.style.width = this.width +'px';
	this.popupDIV.style.height = this.height +'px';
	
	this.popupDIV.style.visibility = 'hidden';
	
	var bodyElement = document.getElementsByTagName('body')[0];
	this.popupDIV = bodyElement.appendChild(this.popupDIV);
	
	this.slices = new Array();
	this.slices['topLeft'] = new PNG('topLeft','/images/alpha/popup/corner_tl.png','/images/alpha/popup/corner_tl.gif');
	this.slices['topLeft'] = this.popupDIV.appendChild(this.slices['topLeft']);
	
	this.slices['top'] = new PNG('top','/images/alpha/popup/top.png','/images/alpha/popup/top.gif');
	this.slices['top'] = this.popupDIV.appendChild(this.slices['top']);
	
	this.slices['topRight'] = new PNG('topRight','/images/alpha/popup/corner_tr.png','/images/alpha/popup/corner_tr.gif');
	this.slices['topRight'] = this.popupDIV.appendChild(this.slices['topRight']);
	
	this.slices['right'] = new PNG('right','/images/alpha/popup/right.png','/images/alpha/popup/right.gif');
	this.slices['right'] = this.popupDIV.appendChild(this.slices['right']);
	
	this.slices['bottomLeft'] = new PNG('bottomLeft','/images/alpha/popup/corner_bl.png','/images/alpha/popup/corner_bl.gif');
	this.slices['bottomLeft'] = this.popupDIV.appendChild(this.slices['bottomLeft']);
	
	this.slices['bottom'] = new PNG('bottom','/images/alpha/popup/bottom.png','/images/alpha/popup/bottom.gif');
	this.slices['bottom'] = this.popupDIV.appendChild(this.slices['bottom']);
	
	this.slices['bottomRight'] = new PNG('bottomRight','/images/alpha/popup/corner_br.png','/images/alpha/popup/corner_br.gif');
	this.slices['bottomRight'] = this.popupDIV.appendChild(this.slices['bottomRight']);
	
	this.slices['left'] = new PNG('left','/images/alpha/popup/left.png','/images/alpha/popup/left.gif');
	this.slices['left'] = this.popupDIV.appendChild(this.slices['left']);
	
	
	// calculate the dimensions of the side pieces
	this.slices['top'].style.width = this.slices['bottom'].style.width = parseInt(this.popupDIV.style.width) -14 +'px';
	this.slices['left'].style.height = this.slices['right'].style.height = parseInt(this.popupDIV.style.height) -14 +'px';
	
	
	// create the inner DIV which contains the close button and contentArea
	this.innerDIV = document.createElement('div');
	this.innerDIV.setAttribute('id','innerDIV');
	this.innerDIV = this.popupDIV.appendChild(this.innerDIV);
	this.innerDIV.style.width = parseInt(this.popupDIV.style.width) -16 +'px';
	this.innerDIV.style.height = parseInt(this.popupDIV.style.height) -16 +'px';
	
	
	// create a div to contain the popup title
	this.titleDIV = document.createElement('div');
	this.titleDIV.setAttribute('id','emxpopup-title');
	this.titleDIV.style.width = parseInt(this.innerDIV.style.width) -32 +'px';
	this.titleDIV = this.innerDIV.appendChild(this.titleDIV);
	
	this.titleText = this.titleDIV.appendChild(document.createTextNode(''));
	
	
	// if this is not a modal dialogue show a close icon
	if (!modal) {
		
		// create the popup close button
		this.closeIcon = document.createElement('img');
		this.closeIcon.setAttribute('id','closeIcon');
		this.closeIcon.src = '/images/alpha/popup/close_off.gif';
		this.closeIcon = this.innerDIV.appendChild(this.closeIcon);
		
		// preload the over state image
		this.closeIcon_over = new Image();
		this.closeIcon_over.src = '/images/alpha/popup/close_on.gif';
			
		// define close button behavoirs using old school event handler definition
		this.closeIcon.onmouseout = function () { 
			document.getElementById('closeIcon').src = '/images/alpha/popup/close_off.gif';
		}
		
		this.closeIcon.onmouseover = function () { 
			document.getElementById('closeIcon').src = '/images/alpha/popup/close_on.gif';
		}
		
		/* iframemanager calls emxpopup.close so 
			 only self close if iframemanager is null */
		this.closeIcon.onclick = function () { 
			self.close();
			if (iframeManager != null) iframeManager.close();
		}
	}
	
	
	// create the content area div
	this.contentArea = document.createElement('div');
	this.contentArea.setAttribute('id','emxPopupContentArea');
	this.contentArea.style.width = parseInt(this.innerDIV.style.width) -16 +'px';
	this.contentArea.style.height = parseInt(this.innerDIV.style.height) -24 +'px';
	this.contentArea = this.innerDIV.appendChild(this.contentArea);
	
	if (contentString) this.display(contentString);
	if (titleString) this.setTitle(titleString);
	
	return this;
}


EmxPopup.prototype.resizePageMask = function () {
	
	// hidding element and replacing our reference with a new instance
	// seens to be quicker than removing it from the DOM
	this.pagemask.style.visibility = 'hidden';
	
	this.pagemask = new PageMaskImage();
	
	if (this.isDisplayed != false) {
		this.pagemask.style.visibility = 'visible';
	}
}


/* Alternate function to createPageMask for WinIE 5.0 
	 WinIE 5.0 neither supports PNG images nor image opacity filters
	 instead set the page header and content text to grey */
EmxPopup.setBackgroundText = function (hexStr) {

	var pageHeader = document.getElementById('pageHeader');
	pageHeader.style.color = hexStr;
	
	var content = document.getElementById('layoutLogic');
	content.style.color = hexStr;
}


EmxPopup.calcPosition = function (width,height) {

	// get dimensions the available window area and scroll position
	var windowArea = getWindowDimensions();
	var scrollPosition = getScrollPosition();
	
	/* horizontally center popup over content area,  
		content area defined in the css/html, div#contentBody */
	var mmContentArea = getElementBoxCoordsById('layoutLogic');
	var x1 = (mmContentArea.x1 + 756/2 - width/2);
	
	var y_centered = (windowArea.height/2 - height/2);
	
	if (mmContentArea.y1+32+height > windowArea.height) {
	
		// if bottom edge of popup were to fall below bottom edge of window
		// center popup vertically in window leaving minimum 8 pixels padding
		var y1 = scrollPosition.y + Math.max(y_centered,8);
	
	} else if (mmContentArea.y1+32 < scrollPosition.y) {
	
		// if top edge of popup were to fall above top edge of window
		// adjust calculation by scrollPosition.y
		var y1 = scrollPosition.y+32;
	
	} else {
	
		// default position 32 pixels below top of div#contentBody
		var y1 = mmContentArea.y1+32;
	}
	
	// create an x1,y1,x2,y2 coords object for the popup
	var coords = new BoxCoords(x1,y1,x1+width,y1+height);
	
	return coords;
}


EmxPopup.prototype.setTitle = function (str) {
	
	this.title = (str) ? str : '';
	this.titleDIV.innerHTML = this.title;
	
	if (str != '') {
	
		var titleCoords = getElementBoxCoords(this.titleDIV);
		var titleHeight = titleCoords.y2 - titleCoords.y1;
		this.titleDIV.style.height = titleHeight +'px';
	
	} else {
	
		this.titleDIV.style.height = 12 +'px';
		var titleCoords = getElementBoxCoords(this.titleDIV);
	}
	
	return titleCoords;
}


EmxPopup.prototype.setContent = function (content) {

	if (content != null) {
		this.contentArea.style.overflow = 'auto';
		this.contentArea.innerHTML = content;
	}
}

EmxPopup.prototype.setVisibility = function (state) {
	
	this.popupDIV.style.visibility = state;
}


EmxPopup.prototype.display = function (contentString,title) {

	if (this.isDisplayed != true) {
		
		if (browser.appN.indexOf('microsoft') != -1 && 
				browser.ua.indexOf('mac') == -1) {
			toggleFormElements('hidden');
		}
		
		var boxCoords = EmxPopup.calcPosition(this.width,this.height);
		
		this.popupDIV.style.position = 'absolute';
		this.popupDIV.style.top = boxCoords.y1+'px';
		this.popupDIV.style.left = boxCoords.x1+'px';
		this.popupDIV.style.zIndex = 1001;
		
		if (browser.ua.indexOf('msie 5.0') != -1) {
			// WinIE 5.0 niether supports PNG images nor image opacity filters
			// instead set the page header and content text to grey
			EmxPopup.setBackgroundText('#aaaaaa');
		
		} else {
			
			this.pagemask.style.visibility = 'visible';
		}
		
		this.setVisibility('visible');
		this.isDisplayed = true;
	}
	
	if (title != null) this.setTitle(title);
	if (contentString != null) this.setContent(contentString);
}

/* define function to run on close */
EmxPopup.prototype.defineOnClose = function (func) {
	this.onclose = func;
}

EmxPopup.prototype.close = function () {

	if (browser.ua.indexOf('msie 5.0') != -1) {
			
		// WinIE 5.0 un-grey text 
		EmxPopup.setBackgroundText('#000000');
	} else {
		
		this.pagemask.style.visibility = 'hidden';
	}
	
	this.setVisibility('hidden');
	
	// work around bug where the scroll bar remains 
	// visible after hidding the content div
	this.setContent('');
	
	if (browser.appN.indexOf('microsoft') != -1 && browser.ua.indexOf('mac') == -1) 
	{
		toggleFormElements('visible');
	}
	
	if (this.onclose != null) this.onclose();
	
	this.isDisplayed = false;
}


/* ////////// IframeManager //////////////////// */

IframeManager = function () {
	
	this.iframeElement;			// DOM element
	this.useIframeBorderMask;	// boolean
	this.emxpopup;				// decoration object
	this.isDisplayed;
	this.onclose;				// onclose event handler
	
	return this;
}


IframeManager.createIframe = function (id,url) {
	
	var iframe;
		
	try {
	
		iframe = document.createElement('iframe');

		iframe.id = (id) ? id : 'mmIframe';
		iframe.name = (id) ? id : 'mmIframe';
		iframe.src = (url) ? url : '/blank.html';
		
		iframe.width = 500 +'px';
		iframe.height = 0 +'px';
		
		iframe.setAttribute('frameborder','0');
		
		iframe.style.position = 'absolute';
		iframe.style.top = 0 +'px';
		iframe.style.left = 0 +'px';
		iframe.style.zIndex = 0;
		iframe.style.visibility = 'hidden';
		
		var bodyElement = document.getElementsByTagName('body')[0];
		iframe = bodyElement.appendChild(iframe);
		
		/* Internet Explorer workaround: 
			 In order to use a dynamically created iframe as a form target it needs to be added to the DOM by 
			 setting the outer html property of the newly created and appended iframe element */
		//iframe.outerHTML = '<iframe id="'+id+'" name="'+id+'" src="/blank.html" width="'+500+'" height="0" frameborder="0" scrolling="auto"></iframe>';
		
	} catch (ex) {
	
		// IE5.0 does not allow dynamic iframe creation
		var iframeHTML='<iframe id="'+id+'" name="'+name+'" src="/blank.html" frameborder="0" scrolling="auto"';
		iframeHTML+='style="width:500px; height:0px; position:absolute; top:0; left:0; zIndex:0; visibility:hidden;"';
		iframeHTML+='></iframe>';
		document.body.innerHTML+=iframeHTML;
		iframe = document.getElementById(id);
	}
	
	return iframe;
}


IframeManager.disableTab  = function () {
	
	// loop through all hyperlinks removing them from the tab order
	for (var i=0; i < document.links.length; i++) {
		var index = document.links[i].tabIndex;
		if (index != -1) {
			// save the current tabIndex to mm:tabIndex so that we can restore it latter
			document.links[i].setAttribute('mm:tabIndex',index);
			document.links[i].tabIndex = -1;
		}
	}
	
	// loop through all form elements removing them from the tab order
	for (var i=0; i < document.forms.length; i++) {
		var form = document.forms[i];
		for (var j=0; j < form.elements.length; j++) {
			var index = document.links[i].tabIndex;
			if (index != -1) {
				// save the current tabIndex to mm:tabIndex so that we can restore it latter
				form.elements[j].setAttribute('mm:tabIndex',index);
				form.elements[j].tabIndex = -1;
			}
		}
	}
	
	return;
}


IframeManager.restoreTab  = function () {
	
	for (var i=0; i < document.links.length; i++) {
		// restore any saved tabIndex
		var index = document.links[i].getAttribute('mm:tabIndex');
		if (index != null) document.links[i].tabIndex = index;
	}
	
	for (var i=0; i < document.forms.length; i++) {
		var form = document.forms[i];
		for (var j=0; j < form.elements.length; j++) {
			// restore any saved tabIndex
			var index = form.elements[j].getAttribute('mm:tabIndex');
			if (index != null) form.elements[j].tabIndex = index;
		}
	}
	
	return;
}


IframeManager.prototype.load = function (url) {
	
	try {
		
		// treat the iframe as a standard frame
		frames[this.iframeElement.id].location.href = url;
		
	} catch (ex) {
		
		// treat the iframe as an object
		document.getElementById(this.iframeElement.id).src = url;
	}
}


/* Internet Explorer does not recognize frameborder property for dynamically created iframe elements
	 workaround: create a transparent div with a white border to cover the iframe's frameborder 
	 
	 Note: the bordermask div will prevent elements from receiving mouse events even though it is transparent,
	 this technique can not be used when the iframe contains form elements or users will not be able to focus them
	 with mouse clicks.
*/
IframeManager.prototype.maskIframeBorder = function (iframeArea) {
	
	var bordermask = document.createElement('div');
	bordermask.style.position = 'absolute';
	
	bordermask.style.top = iframeArea.y1 +'px';
	bordermask.style.left = iframeArea.x1 +'px';
	
	bordermask.style.width = parseInt(this.emxpopup.contentArea.style.width)+'px';
	bordermask.style.height = parseInt(this.emxpopup.contentArea.style.height)+'px';
	
	bordermask.style.border = '2px solid #fff';
	bordermask.style.zIndex = 1004;
	
	var bodyElement = document.getElementsByTagName('body')[0];
	this.iframeBorder = bodyElement.appendChild(bordermask);
	
	return this.iframeBorder;
}


IframeManager.prototype.display = function (url,w,h,title) {
	
	if (this.iframeElement == null) { 
		 
		this.iframeElement = IframeManager.createIframe('mmIframe',url);
	
	} else if (url != null) {
		
		// if display was called with a URL try to load it
		this.load(url);
	}
	
	if (this.iframeElement != null && this.isDisplayed != true) {
		
		// if width nor height were passed use defaults
		var width = (w) ? w : 500;
		var height = (h) ? h : 336;
		
		this.emxpopup = (this.emxpopup) ? this.emxpopup : new EmxPopup(width,height);
		this.emxpopup.display(null,title);
		
		var contentArea = getElementBoxCoords(this.emxpopup.contentArea);
		
		this.iframeElement.style.width = contentArea.x2 - contentArea.x1 +'px';
		this.iframeElement.style.height = contentArea.y2 - contentArea.y1 +'px';
		
		this.iframeElement.style.top = contentArea.y1+'px';
		this.iframeElement.style.left = contentArea.x1+'px';
		this.iframeElement.style.zIndex = 1003;
		
		if (browser.appN.indexOf('microsoft') != -1 && browser.ua.indexOf('mac') == -1) {
			if (this.useIframeBorderMask != false) {
				this.iframeBorder = this.maskIframeBorder(contentArea);
				this.iframeBorder.style.visibility = 'visible';
			}
			toggleFormElements('hidden');
		}
		
		this.iframeElement.style.visibility = 'visible';
		
		IframeManager.disableTab();
		this.iframeElement.tabIndex = 1;
		
		this.isDisplayed = true;
	
	} else if (this.iframeElement != null) {
		
		// set "title" next to the close button in EmxPopup
		this.setPopupTitle(title);
	}
}


IframeManager.prototype.setPopupTitle = function (str) {

	var titleBox = this.emxpopup.setTitle(str);
	this.iframeElement.style.top = titleBox.y2 +'px';
}


/* define function to run on close */
IframeManager.prototype.defineOnClose = function (func) {
	this.onclose = func;
}


IframeManager.prototype.close = function () {

	if (this.iframeElement != null && this.isDisplayed != false) {
		
		this.load('/blank.html');
		
		this.iframeElement.style.visibility = 'hidden';
		
		this.emxpopup.close();
		
		if (browser.appN.indexOf('microsoft') != -1 && browser.ua.indexOf('mac') == -1) {
			if (this.iframeBorder != null) this.iframeBorder.style.visibility = 'hidden';
			toggleFormElements('visible');
		}
		
		this.isDisplayed = false;
		
		IframeManager.restoreTab();
		this.iframeElement.tabIndex = -1;
		
		if (this.onclose != null) this.onclose();
	}
}


/* MacIE (perhaps other browsers too...?) get the reference to the iframe
	 confused across multiple pages so we need to remove the iframe on unload
	 in order to have a fresh reference to it the next time we hit a page */
IframeManager.prototype.cleanup = function () {
	try {
		document.removeChild(this.iframeElement);
	} catch (e) {
		// foobar
	}
}


IframeManager.prototype.init = function (id,func) {
	
	// if we forgot to pass an ID for the iframe use a defualt ID
	var iframeID = (id) ? id : 'mmIframe';
	
	if (document.getElementById(iframeID) != null) {
	
		// if an iframe with the defualt ID exist use that
		this.iframeElement = document.getElementById(iframeID);
		this.useIframeBorderMask = false;
	
	 /* Bug 24528: Mozilla can not navigate back|forward in history after an iframe element is 
	 		dynamically added to the DOM. Disabling dynamic iframe creation from init function. */
	}/* else { 
	
		// create a new iframe element
		this.iframeElement = IframeManager.createIframe(iframeID);
		
		// dynamic iframe elements in IE do not reconize the border so create this now
		this.useIframeBorderMask = (browser.appN.indexOf('microsoft') != -1) ? true : false;
	}*/
	
	this.isDisplayed = false;
	this.onclose = func;
}


/* ////////// global scope //////////////////// */

function toggleFormElements (state) {
	
	for (var i=0; i < document.forms.length; i++) {
		var current_form = document.forms[i];
		
		for (var j=0; j < current_form.elements.length; j++) {
			var current_element = current_form.elements[j];
		
			/* select elements burn through the z-index
				hide them and show them later when the iframe is gone */
			if (current_element.type == 'select-one' || 
				current_element.type == 'select-mulitple') {
				current_element.style.visibility = state;
			}
		}
		
	}
	return;
}

/* to prevent pages from hanging if the user clicks something which 
	 needs to call iframeManager.display cue up the call and keep trying
	 until the page has loaded and iframeManager is ready */
function displayIframe () {
	var args = (arguments.length > 0) ? arguments.toString() : null;	
	if (iframeManager != null && iframeManager.display) {
		iframeManager.display(args);
	} else {
		setTimeout('displayIframe('+args+');',200);
	}
}

function setPopupTitle (str) {
	if (str != null && iframeManager != null && iframeManager.setPopupTitle) {
		iframeManager.setPopupTitle(str);
	} else {
		setTimeout('setPopupTitle("'+str+'");',200);
	}
}


onunload = function () {
	if (iframeManager.isDisplayed != false) iframeManager.close();
	iframeManager.cleanup('mmIframe');
}

onresize = function () {
	if (iframeManager.emxpopup != null && iframeManager.isDisplayed != false) {
		iframeManager.emxpopup.resizePageMask();
	}
}

var browser = new BrowserDescription();
var iframeManager = new IframeManager();

function initIframeManager () {
	iframeManager.init('mmIframe');
}

registerOnLoadFunc(initIframeManager);
