/*
HOW TO USE:

in your web page, on event, run the popInfo(params) function

params can be formated like so : {title:'text',status:'text',maskColor:'#000000',maskOpacity:0.5,className:'text',content:'../url.php',width:'XX',height:'YY'}	
the minimum you must fill is the content : popInfo({content:'./theUrlOfYourInfo'});

include this js file in your page as well as the splash.css
*/


//Messy version...  a cleaning will be done in the future
var divToHide = null;
var theParams = null;

function popInfo(params){


//params =   {title:'text',status:'text',maskColor:'#000000',maskOpacity:0.5,class:'text',content:'../url.php',width:'XX',height:'YY',hide:['eltIdToHide','eltIdToHide']}	
	theParams = params;
	
	if(params.hide != undefined)
		divToHide = params.hide;
	else
		divToHide = [];	
		
	createMask(params.maskColor,params.maskOpacity);
	var infoParams = {content:theParams.content,title:theParams.title,status:theParams.status,width:theParams.width,height:theParams.height}
	createInfoDiv(infoParams);	
	
}

function createMask(theColor,theOpacity){

	divToHide.each(function(th){
				$(th).setStyle({visibility:'hidden'});
		});
	//checking color value
	if(theColor == undefined || !isColor(theColor)){
		theColor = '#000000';
	}
	
	if(theOpacity == undefined){
		theOpacity = 0.5;
	}
	else{
		if(theOpacity < 0){
			theOpacity = 0;
		}
		
		if(theOpacity > 1){
			theOpacity =1;
		}
	}

	if($('splashMask') == null){
		var theB = $$('body')[0]; //get the body Elt
	var maskWidth = $('general').getWidth();
	var maskHeight = $('general').getHeight();
	
/*	if(!Prototype.Browser.IE){
		maskHeight = '100%';
	}
	else{
		maskHeight += 'px';
	}*/

	maskHeight = $('general').getHeight()+'px';
				
		//creates the mask layer
		theB.insert({Bottom:'<div id="splashMask" class="splashMask"></div>'});
		
		//Set parameters
		$('splashMask').setStyle({backgroundColor:theColor,opacity:theOpacity,height:maskHeight});
	}
	else{
		$('splashMask').show();


		var maskWidth = $('general').getWidth();
		var maskHeight = $('general').getHeight();
		
		if(!Prototype.Browser.IE){
			maskHeight = '100%';
		}
		else{
			maskHeight += 'px';
		}
	
		//Set parameters
		$('splashMask').setStyle({backgroundColor:theColor,opacity:theOpacity,height:maskHeight});
		
	}	
}

function createInfoDiv(params){

		if(params == undefined)
			params = '{"arg0":"null"}'.evalJSON();

		//params; // params of the maximal form: {title:'text',status:'text',className:'text',content:'../url.php',width:'XX',height:'YY'}  //can be improved later
		var theB = $$('body')[0]; //get the body Elt
	
		var maskWidth = $('general').getWidth();
		var maskHeight =  $('general').getHeight();

		//== creates the mask layer ==//
		var theClass = 'InfoDiv';
		if(params.className != undefined){
			theClass = params.className;
		}
		
		var theWidth = 640;
		var theHeight= 300;
		var defaultSize = false;
		
		if(params.width != undefined){
			theWidth = params.width;
			defaultSize = true;
		}
		
		if(params.height != undefined){
			theHeight = params.height;
			defaultSize = true;
		}
			
		//add size or Top and Bottom bars
		FulltheHeight = theHeight + 60;
			
		var theLeft = Math.round((maskWidth-theWidth)/2);
		var theTop = Math.round((maskHeight-theHeight)/2) - 60;
		
		//make parameters
		theTitle = '...';
		if(params.title != undefined)
			theTitle = params.title;
			
		theStatus = '...';
		if(params.status != undefined)
			theStatus = params.status;
	
		if($('infoDiv') == null){	
			theB.insert({Bottom:'<div id="infoDiv" align="center" class="'+theClass+'"></div>'});
		}
		else{
			$('infoDiv').show();
		}

		//Center the empty Div
		$('infoDiv').setStyle({top:theTop+'px',left:theLeft+'px'});//,width:theWidth+'px',height:FulltheHeight+'px'});
		
		
		new Ajax.Updater('infoDiv', './skull.html', {
			parameters: {action: 'theAction'},
			asynchronous:false,
			evalScripts: true,
			onComplete:function(r){}
			});			
		
		nameInfoDiv({title:theTitle, status:theStatus});
		if(defaultSize)
			centerInfoDiv();
		
		//fill the content
		new Ajax.Updater('infoContent', params.content, {
 		parameters: {action: 'theAction'},
		asynchronous:false,
		evalScripts: true,
		onComplete:function(r){}
		});
		if(defaultSize)
			centerInfoDiv(true);
		else
			centerInfoDiv();
}

function nameInfoDiv(params){
	$('infoTitle').update(params.title);
	$('infoStatus').update(params.status);	
}

function centerInfoDiv(fromVals){

	var maskWidth = $('general').getWidth();
	var maskHeight = $('general').getHeight();
	
	if(fromVals != undefined && fromVals == true){
		var theW=theParams.width;				
		var theH=theParams.height;			
	}else
	{
		var theW=$('infoContent').getWidth();				
		var theH=$('infoContent').getHeight();	
	}
	
	
	var ll = (Math.round((maskWidth-theW)/2));
	var tt = (Math.round((maskHeight-theH)/2) - 60);	
	//$('topBar').setStyle({width:theW+'px'});$('bottomBar').setStyle({width:theW+'px'});
	$('infoDiv').setStyle({left:ll+'px',top:tt+'px'});
}

function closeMask(){
	$('splashMask').hide();
	$('infoDiv').hide();	
	divToHide.each(function(th){
				$(th).setStyle({visibility:'visible'});
	});
}

function isColor(theStr){ //is a color ? of the form   #HHHHHH  (h being and hexa digit)
	return (theStr.match('^#[0-9a-f]{6}$'));
}

function setClass(element,newClassName)
{
	$(element).removeClassName($(element).readAttribute('class'));
	$(element).addClassName(newClassName);
}

