/*
 * nfl.ui.modal
 * functions for creating modal windows
 * content can be a url, or a html fragment.
 * @requires prototype, nfl
 * @author Arianna Winters
 */
nfl.ui.modal			= {
	mask: 		false,
	container:	false,
	frame: 		false,
	active:		false,
	returnValue:null,
	defaultUrl: '/external/loading.html',
	__tabable:	['A','BUTTON','TEXTAREA','INPUT','IFRAME'],
	tabIndexes:	{disable:function(){},enable:function(){},indexed:[]},
	masks:		{setSize:function(){}},
	selectboxes:{hide:function(){},show:function(){}},
	defaultSize:{width:4,height:4},
	show:		function(){/*stub function*/},
	hide:		function(){/*stub function*/},
	originalGeometry:	{}
}
if(document.all){
	/* rewrite dormant functions for ie */
	nfl.ui.modal.tabIndexes.disable	= function(){
		var i = 0;
		for (var j = 0; j < nfl.ui.modal.__tabable.length; j++) {
			var tagElements = document.getElementsByTagName(nfl.ui.modal.__tabable[j]);
			for (var k = 0 ; k < tagElements.length; k++) {
				nfl.ui.modal.tabIndexes.indexed[i] = tagElements[k].tabIndex;
				tagElements[k].tabIndex="-1";
				i++;
			}
		}
	};
	nfl.ui.modal.tabIndexes.enable	= function(){
		var i = 0;
		for (var j = 0; j < nfl.ui.modal.__tabable.length; j++) {
			var tagElements = document.getElementsByTagName(nfl.ui.modal.__tabable[j]);
			for (var k = 0 ; k < tagElements.length; k++) {
				tagElements[k].tabIndex = nfl.ui.modal.tabIndexes.indexed[i];
				tagElements[k].tabEnabled = true;
				i++;
			}
		}
	};
	nfl.ui.modal.selectboxes.hide	= function() {
		var x = document.getElementsByTagName("SELECT");
		for (i=0;x && i < x.length; i++) {
			x[i].style.visibility = "hidden";
		}
	};
	nfl.ui.modal.selectboxes.show	= function(){
		var x = document.getElementsByTagName("SELECT");
		for (i=0;x && i < x.length; i++){
			x[i].style.visibility = "visible";
		}
	};
}else{
	document.onkeypress = function(e){
	    if(nfl.ui.modal.active && e.keyCode == 9) return false;
	};
}
nfl.ui.modal.create			= function(htmlFragment,options){
	var options			= (typeof options !== 'undefined')?options:{};
	options.scrolling	= (typeof options.scrolling !== 'undefined')?options.scrolling:'auto';
	
	if(!nfl.ui.modal.mask || !nfl.ui.modal.container){
		body 	= document.getElementsByTagName('BODY')[0];
		if(!nfl.ui.modal.mask){
			mask	= document.createElement('div');
			mask.id = 'modal-mask';
			body.appendChild(mask);
			nfl.ui.modal.mask	= $("modal-mask");
			if(options.maskclickable){
				// attach click on mask event to close window
				nfl.ui.modal.mask.observe('click',nfl.ui.modal.hide);
			}
		}
		if(!nfl.ui.modal.container){
			cont 	= document.createElement('div');
			cont.id = 'modal-container';
			body.appendChild(cont);
			nfl.ui.modal.container	= $('modal-container');
		}
	}
	var containerContent	 = '<div id="modal-inner">';
	containerContent		+= '\t<div id="modal-title-bar">';
	containerContent		+= '\t\t<div id="modal-controls" onclick="nfl.ui.modal.hide(false);">';
	containerContent		+= '\t\t\t<img src="'+nfl.global.imagepath +'/img/common/modal/close.gif" id="modal-controls-close" />';
	containerContent		+= '\t\t</div>';
	containerContent		+= '\t\t<div id="modal-title" onclick="document.fire(\'nfl:modal:click\',1);"></div>';
	containerContent		+= '\t</div>';
	
	if(htmlFragment){
		containerContent	+= '\t<div id="modal-inner-content">'+htmlFragment+'</div>';
	}else{
		containerContent	+= '\t<div id="modal-frame-container"><iframe src="'+ nfl.ui.modal.defaultUrl +'" style="width:100%;height:100%;background-color:transparent;" scrolling="'+ options.scrolling +'" frameborder="0" allowtransparency="true" id="modal-frame" name="modal-frame" width="100%" height="100%"></iframe></div>';
	}
	containerContent		+= '\t\t<div id="modal-footer-area" onclick="document.fire(\'nfl:modal:click\',2);"></div>';
	containerContent		+= '</div>';
	nfl.ui.modal.container.update(containerContent);
	nfl.ui.modal.frame 				= $("modal-frame");
}
nfl.ui.modal.show	= function(content,options){
	options				= (typeof options !== 'undefined')?options:{};
	//nfl.ui.modal.mask		= $('modal-mask')
	//nfl.ui.modal.container	= $('modal-container');
	console.log('mask.show called. set objects. nfl.ui.modal.mask['+nfl.ui.modal.mask+'],nfl.ui.modal.container['+nfl.ui.modal.container+']');
	nfl.ui.modal.closeable	= (typeof options.closeable == 'boolean')?options.closeable:true;
	console.log('mask.show called. set objects. nfl.ui.modal.mask['+nfl.ui.modal.mask+'],nfl.ui.modal.container['+nfl.ui.modal.container+']');
	if(nfl.ui.modal.closeable && $('modal-controls')){$('modal-controls').show()}else{$('modal-controls').hide()} 
	if(nfl.ui.modal.mask){
		nfl.ui.modal.tabIndexes.disable();
		nfl.ui.modal.mask.setStyle({'display':'block'});
		console.log('mask.show called. nfl.ui.modal.mask.id = '+nfl.ui.modal.mask.id);
		//var titleBarHeight = parseInt($("modal-title-bar").offsetHeight, 10);
		var titleBarHeight = false;
		if(typeof options.titlebar !== 'undefined'){
			if(typeof options.titlebar.height !== 'undefined'){ titleBarHeight	= options.titlebar.height; console.log('setting titlebar height from options');}
		}
		if(titleBarHeight === false && typeof nfl.ui.modal.originalGeometry['modal-title-bar'] !== 'undefined'){
			if(nfl.ui.modal.originalGeometry['modal-title-bar'].height){ titleBarHeight = nfl.ui.modal.originalGeometry['modal-title-bar'].height;};
		}
		if(titleBarHeight === false){titleBarHeight = parseInt($('modal-title-bar').getStyle('height')); console.log('set titlebarheight via dom');}
		console.log('titleBarHeight = '+titleBarHeight);
		nfl.ui.modal.masks.setSize();
	}
	if(nfl.ui.modal.container && nfl.ui.modal.container.morph){
		var viewPortDims = document.viewport.getDimensions();
		nfl.ui.modal.clear();
		if(!nfl.ui.modal.active){
			//move it to center
			nfl.ui.modal.container.setStyle({'display':'block','width':nfl.ui.modal.defaultSize.width+'px','height':nfl.ui.modal.defaultSize.height+'px','left':((viewPortDims.width/2) - (nfl.ui.modal.defaultSize.width/2))+'px','top':((viewPortDims.height/2) - (nfl.ui.modal.defaultSize.height/2))+'px'});
		}
		console.log('current height: '+ nfl.ui.modal.container.getStyle('height')+', new height:'+(options.height+titleBarHeight)+'px');
		nfl.ui.modal.container.morph('height: '+(options.height+titleBarHeight)+'px; top: '+((viewPortDims.height/2) - (options.height / 2)) +'px;',{
			afterFinish:function(){
				nfl.ui.modal.container.morph('width: '+options.width+'px; left: '+((viewPortDims.width/2) - (options.width / 2))+'px',{afterFinish:function(){
					$('modal-title-bar').morph('height:'+nfl.ui.modal.originalGeometry['modal-title-bar'].height+'px;',{afterFinish:function(){
						nfl.ui.modal.load(content,options);
					}.bind(this)});
				}.bind(this)});
			}.bind(this)
		});
		//top: '+((viewPortDims.height/2) - (options.height / 2)) +'px; left: '+((viewPortDims.width/2) - (options.width / 2))+'px');
	}else{
		nfl.ui.modal.container.update();
		nfl.ui.modal.container.setStyle({'display':'block'});
		nfl.ui.modal.center(options.width, options.height);
		nfl.ui.modal.container.setStyle({'width':(options.width+"px"),'height':((options.height+titleBarHeight)+"px")});
		nfl.ui.modal.load(content,options)
	}
	nfl.ui.modal.active	= true;
	// need to set the width of the iframe to the title bar width because of the dropshadow
	// some oddness was occuring and causing the frame to poke outside the border in IE6
	
}
nfl.ui.modal.load	= function(content,options){
	if(content.startsWith('http')){
		//this is a url
		if(!nfl.ui.modal.frame){ nfl.ui.modal.create(false, options);}
		options.player	= (typeof options.player == 'undefined')?{}:options.player;
		if(typeof options.player.width == 'undefined'){ options.player.width = options.width;}
		if(typeof options.player.height == 'undefined'){ options.player.height = options.height;}
		
		if(typeof options.player == 'undefined'){}
		nfl.ui.modal.container.removeClassName('creating');
		nfl.ui.modal.container.addClassName('loading');
		nfl.ui.modal.frame.setStyle({'width':((parseInt(options.player.width) - 4)+"px"),'height':(options.player.height+"px")});
		nfl.ui.modal.frame.src = content;
		if(typeof options.zones !== 'undefined'){
			if(typeof options.zones['1'] !== 'undefined' && $('modal-title')){
				$('modal-title').update('<a href="'+options.zones['1'].href+'" target="_blank" style="display:block"></a>');
			}
			if(typeof options.zones['2'] !== 'undefined' && $('modal-footer-area')){
				$('modal-footer-area').update('<a href="'+options.zones['2'].href+'" target="_blank" style="display:block"></a>');
			}
		}
	}else{
		nfl.ui.modal.create(content,options);
	}
	nfl.ui.modal.selectboxes.hide();
	window.setTimeout("nfl.ui.modal.setTitle('"+options.title+"');", 600);
}
nfl.ui.modal.hide	= function(){
	nfl.ui.modal.active = false;
	var body	= $(document.getElementsByTagName("BODY")[0]);
	body.setStyle({overflow:''});
	nfl.ui.modal.tabIndexes.enable();
	if(!nfl.ui.modal.mask){ console.log('no mask element found!!');}
	nfl.ui.modal.mask.setStyle({'display':'none'});
	nfl.ui.modal.container.removeClassName('loading');
	if(nfl.ui.modal.container && nfl.ui.modal.container.morph){
		var viewPortDims = document.viewport.getDimensions();
		nfl.ui.modal.clear();
		nfl.ui.modal.container.morph('width:'+nfl.ui.modal.defaultSize.width+'px;height:'+nfl.ui.modal.defaultSize.height+'px;left:'+((viewPortDims.width/2) - (nfl.ui.modal.defaultSize.width/2))+'px;top:'+((viewPortDims.height/2) - (nfl.ui.modal.defaultSize.height/2))+'px',{afterFinish:function(){
			nfl.ui.modal.container.setStyle({'display':'none'});
			document.fire('modal:closed');
		}});
	}else{
		nfl.ui.modal.container.setStyle({'display':'none'}); document.fire('modal:closed');
	}
	if(nfl.ui.modal.frame){nfl.ui.modal.frame.src = nfl.ui.modal.defaultUrl;}
	nfl.ui.modal.selectboxes.show();
}
nfl.ui.modal.clear	= function(){
	if($('modal-title-bar')){
		if(!nfl.ui.modal.originalGeometry['modal-title-bar']){
			nfl.ui.modal.originalGeometry['modal-title-bar']	= {width:parseInt($('modal-title-bar').getStyle('width')),height:parseInt($('modal-title-bar').getStyle('height'))};
			console.log('storing modal-title. width = '+nfl.ui.modal.originalGeometry['modal-title-bar'].width+', height = '+nfl.ui.modal.originalGeometry['modal-title-bar'].height);
		}
		$('modal-title-bar').setStyle({height:'0px'});
	}
	if($('modal-title')){$('modal-title').update();}
	if($('modal-controls')){ $('modal-controls').hide();}
	if($('modal-inner-content')){ $('modal-inner-content').update();}
	if(nfl.ui.modal.container){ nfl.ui.modal.container.removeClassName('loading'); }
	if(nfl.ui.modal.frame){try{nfl.ui.modal.frame.remove();nfl.ui.modal.frame = false;}catch(e){}}
}
nfl.ui.modal.center	= function(width,height){
	if(nfl.ui.modal.active){
		var width	= (width == null || isNaN(width))? nfl.ui.modal.container.offsetWidth:width;
		var height	= (height == null || isNaN(height))? nfl.ui.modal.container.offsetHeight:height;

		var offsets	= document.viewport.getScrollOffsets();
		nfl.ui.modal.setMaskSize();
		
		var titleBarHeight = $("modal-title-bar").getHeight();
		var viewPortDims = document.viewport.getDimensions();
		nfl.ui.modal.container.style.top	= ((viewPortDims.height/2) - (height / 2)) + "px";
		nfl.ui.modal.container.style.left	= ((viewPortDims.width/2) - (width / 2)) + "px";
		//alert(fullWidth + " " + width + " " + gPopupContainer.style.left);
	}
}
nfl.ui.modal.setMaskSize	= function(){
	var body = document.getElementsByTagName("BODY")[0];
	var viewPortDims = document.viewport.getDimensions();
	var fullHeight = viewPortDims.height;
	var fullWidth = viewPortDims.width;
	modalHeight		= (viewPortDims.height > body.scrollHeight)?viewPortDims.height:body.scrollHeight;
	modalWidth		= (viewPortDims.width > body.scrollWidth)?viewPortDims.width:body.scrollWidth;			
	nfl.ui.modal.mask.setStyle({width:modalWidth,height:modalHeight});
}
nfl.ui.modal.setTitle		= function(title){
	var title	= (typeof title !== 'undefined')?title:false;
	if(!title && nfl.ui.modal.frame){
		if(window.frames["modal-frame"].document.title == null) {
			window.setTimeout("nfl.ui.modal.setTitle();", 10);
		} else {
			title = window.frames["modal-frame"].document.title;
		}
	}
	$("modal-title").update(title);
}
document.observe('dom:loaded',nfl.ui.modal.create);
Event.observe(window, "resize",  nfl.ui.modal.center);
//Event.observe(window, "scroll",  nfl.ui.modal.center);
//Event.observe(window, "onscroll",nfl.ui.modal.center);