// Aquest arxiu conté les funcions per manipular les finestres.
//------------------------------------------------------------------------------------------------------//
// Funció per obrir sempre en la mateixa finestra amb handler delegat.
function openUniqueWindow(url,features,replace){return openWindow(url,"HandlerUniqueWindows",features,replace);}
//------------------------------------------------------------------------------------------------------//
// Millor de la funció window.open. Permet centrar automaticament la finestra, apart de fer
// un parseig dels parametres per millorar-ne la compatibilitat amb els navegadors.
function windowOpen(url,name,features,replace)
{
	//preparem els parametres en minuscules i amb els separadors adecuats.
	features=features.toLowerCase();
	features=features.replace(/;/g,',');
	features=features.replace(/:/g,'=');
	features=features.replace(/=yes/g,'=1'); //millora compatibilitat.
	features=features.replace(/=no/g,'=0');  //millora compatibilitat.
	
	//Variables d'us local.
	var align=false,width,height=null
	
	//Extraiem els parametres que ens interessen i els copiem a variables locals.
	var f = features.split(',')
	for (var b=0;b<f.length;b++)
	{
		if  (f[b].indexOf('align')!=-1)  {align=(f[b].split('=')[1]==1);f[b]='';}
		if  (f[b].indexOf('width')!=-1)  {width=parseInt(f[b].split('=')[1]);f[b]='';}
		if (f[b].indexOf('height')!=-1) {height=parseInt(f[b].split('=')[1]);f[b]='';}
	}
	
	//Llencem la finestra en funció de l'alineació.
	if (!align)
	{
		 //Si no cal centrat, llencem finestra normal.
		return window.open(url,name,features,replace).focus;
	}
	else
	{
		//S i el width o el height son nuls o no son valors numerics, llencem excepció.
		if (width==null||isNaN(width))  {throw new Error('If you use autoalign width parameter must be supplied.');return null;}
		if (height==null||isNaN(height)){throw new Error('If you use autoalign height parameter must be supplied.');return null;}
	
		// Si fem servir autoalienació no podem especificar ni top ni left. I llencem excepció.
		if ((0+(features.indexOf('top')!=-1)+(features.indexOf('left')!=-1))){throw new Error('If you use autoalign, you can\'t specify top or left in features parameter.');return null;}
	
		// Calculem la posicio al mig de la pantalla.
		var top =((screen.availHeight - height)/2);
		var left=((screen.availWidth - width)/2);
		var position='top='+top+',left='+left+',width='+width+',height='+height
		
		//Obrim la finestra amb els parametres de posició i els demanats en els parametres i la retornem.
		var w=window.open(url,name,position+','+f.join(','),replace)
		w.focus();
		return w;
	}
	return null;
}
//------------------------------------------------------------------------------------------------------//