/*
Funcions necessàries per insertar les notícies de la pàgina principal.
Requisits:
  .Prèviament s'ha de carregar l'arxiu dels estils de le noticies:
           <link href="noticies/noticies.css" rel="stylesheet" type="text/css">
  .Prèviament s'ha de carregar l'arxiu del contingut de les notícies:
  	  <script language="JavaScript"
	     src="http://www2.ub.edu/comunicacions/franja_contingut.js"
		 type="text/javascript">
	  </script>

Contingut de les notícies
  .Cada notícia esta formada por el següent 'array':
		.contenido_noticias[0]: títol de la noticia
		.contenido_noticias[1]: resum de la noticia. Pot no tenir
		.contenido_noticias[2]: url de la notícia complerta
		.contenido_noticias[3]: url de la imatge de la notícia. Alguns venen sense imatge
		.contenido_noticias[4]: ample de la foto (width)
		.contenido_noticias[5]: alt de la foto (height)
  .Totes les noticies estan contingudes en l''array' 'contenido_noticies_total'. 
  .Té un total de 15 noticies i conté elements 'contenido_noticies'.
  
  . Per obtenir el resum de la primera notícia hem de fer contenido_noticies_total[0][1]

Funcions pensades per a la versió text:
	.fila_noti_txt
	.totes_noti_txt
	
Funcions pensades per a la versió amb imatges:
	.noti_amb_img(pos_noticia)
	.noti_sense_img(pos_noticia) 
	.totes_noti_img(quantes_noticies,quantes_img)

Funcions generals:
	.avuipetit()
	.normalitza_tamany(mida_orig, mida_maxima)
	.tamany_maxim_foto(dim)

Modificat: 28-12-2005 (Comunicació, Jordi Guilleumes) Afegit text alternatiu a les imatges.
*/

var paramsWindowOpen = "','','location=yes, menubar=yes, status=yes, toolbar=yes, scrollbars=yes, resizable=yes, width=800, height=600')\">";


/*
versio txt
retorna les files necessaries per a la noticia pos_noticia
*/
function fila_noti_txt(pos_noticia) {
	document.write("<tr><td class='noti_titol_txt'>" + contenido_noticias_total[pos_noticia][0] + "</td></tr>");
	document.write("<tr><td class='noti_resum_txt'>" + contenido_noticias_total[pos_noticia][1] + "</td></tr>");
	document.write("<tr><td align='right'><a href='" + contenido_noticias_total[pos_noticia][2] + "' target='_blank' class='noti_link_txt'>Llegiu-ne més</a></td></tr>");
	document.write("<tr><td>&nbsp;</td></tr>");
}

/*
Versió solament text
Retorna una taula amb totes les notícies. El nombre de noticies que retorna és 'quantes_notices'.
*/
function totes_noti_txt(quantes_noticies){
	var MAXIM_NOTICIES = 15;
	var noticies_mostra = quantes_noticies;
	if (noticies_mostra > MAXIM_NOTICIES) {
		noticies_mostra = MAXIM_NOTICIES;
	}
	document.write("<table width='100%' border='0'>");
	for (var i=0;i<noticies_mostra;i++){
		fila_noti_txt(i);
	}
	document.write("</table>");
}


/*
versió amb imatges
Retorna les files necessàries per a les notícies amb imatge per a la noticia 'pos_noticia'.
*/
function noti_amb_img(pos_noticia) {
	var ample_noti = contenido_noticias_total[pos_noticia][4];
	var alt_noti = contenido_noticias_total[pos_noticia][5];
	var hi_ha_imatge = true;
	if (contenido_noticias_total[pos_noticia][4]== 0 ||
	  contenido_noticias_total[pos_noticia][5] == 0 ||
	  contenido_noticias_total[pos_noticia][3] == "") {
		hi_ha_imatge = false;
	}
	if (hi_ha_imatge) {
		ratio_1 = normalitza_tamany(ample_noti, tamany_maxim_foto("w"));
		ample_noti = ample_noti * ratio_1;
		alt_noti = alt_noti * ratio_1;
		
		ratio_2 = normalitza_tamany(alt_noti, tamany_maxim_foto("h"));
		ample_noti = ample_noti * ratio_2;
		alt_noti = alt_noti * ratio_2;
	} else {
		//no hi ha foto
		ample_noti = tamany_maxim_foto("w");
		alt_noti = tamany_maxim_foto("h");
	}
	document.write("<tr><td>\n");
	document.write("<table width='100%' border='0' summary='Notícia amb imatge'>\n");
	document.write("<tr>");
	document.write("<td align='right' width='"+tamany_maxim_foto("w")+"'><img src='"+contenido_noticias_total[pos_noticia][3]+"' width='"+ample_noti+"' height='"+alt_noti+"' alt='Imatge de la notícia' title='Imatge de la notícia' hspace='5' vspace='1'></td>\n");
	document.write("<td align='left' width='100%'>");
	document.write("<span class=\"noti_data_petit\">" + avuipetit() + "&nbsp;</span><br>\n");
	document.write("<img src= '/homeub/noticies/img/not_ico.gif' width='14' height='16' alt='Icona notícia' title='Icona notícia'>&nbsp;");
	// document.write("<a href=\"javascript:void('nul');\" class=\"noti_titol_gran_img\" onClick=\"window.open('" + contenido_noticias_total[pos_noticia][2] + "','','scrollbars=yes,resizable=yes,width=800,height=600')\">" + contenido_noticias_total[pos_noticia][0] + "</a>");
	document.write("<a href=\"javascript:void('nul');\" class=\"noti_titol_gran_img\" onClick=\"window.open('" + contenido_noticias_total[pos_noticia][2] + paramsWindowOpen + contenido_noticias_total[pos_noticia][0] + "</a>");
	document.write("</td></tr>\n");
	document.write("<tr>");
	document.write("<td class=\"noti_resum_img\" colspan='2'>" + contenido_noticias_total[pos_noticia][1] + "</td>\n" );
	document.write("</table>\n");
	document.write("</td></tr>\n");
}

/*
versio img
Retorna les files necessaries per a la noticia sense imatge per a la noticia pos_noticia
*/
function noti_sense_img(pos_noticia) {
	document.write("<tr><td>\n");
	document.write("<table width='100%' border='0' summary='Notícia sense imatge'>\n");
	document.write("<tr>");
	document.write("<td width='14' height='16'><img src= '/homeub/noticies/img/not_ico.gif' width='14' height='16' alt='Icona notícia' title='Icona notícia'></td>\n");
	document.write("<td>");
	document.write("<a href=\"javascript:void('nul');\" class=\"noti_titol_petit_img\" onClick=\"window.open('" + contenido_noticias_total[pos_noticia][2] + paramsWindowOpen + contenido_noticias_total[pos_noticia][0] + "</a>");
    document.write("</td></tr>\n");
	document.write("</table>\n");
	document.write("</tr></td>\n");

}

/*
versio img
retorna una taula amb totes les noticies. El numero de noticies que retorna es quantes_notices.
quantes_img és el numero de noticies amb imatge.
*/
function totes_noti_img(quantes_noticies,quantes_img){
	var MAXIM_NOTICIES = 15;
	var MAXIM_NOTICIES_IMG =3;
	var noticies_mostra = quantes_noticies;
	var imatges_mostra = quantes_img;
	if (noticies_mostra > MAXIM_NOTICIES) {
		noticies_mostra = MAXIM_NOTICIES;
	}
	if (imatges_mostra > MAXIM_NOTICIES_IMG) {
		imatges_mostra = MAXIM_NOTICIES_IMG;
	}
	
	document.write("<table width='100%' border='0'>\n");
	for (var i=0;i<imatges_mostra;i++){
		noti_amb_img(i);
	}
	document.write("<tr><td>&nbsp;</td></tr>\n");
	for(var j=i;j<noticies_mostra;j++){
		noti_sense_img(j);
	}
	document.write("</table>\n");
}

/*
retorna la data d'avui en format curt dd/mm/aaaa
*/
function avuipetit() {
	var dataAvui = new Date();
	var meuDia = dataAvui.getDate(); 
	var meuMes = dataAvui.getMonth()+1; 
	var meuAny  = dataAvui.getFullYear();
	var mevaData = meuDia+"/"+meuMes+"/"+meuAny;
	return mevaData;
}
/*
retorna la ratio que correspon de reducció. Mai amplia.
*/
function normalitza_tamany(mida_orig, mida_maxima) {
	var ratio = 0;
	if (mida_orig <= mida_maxima) {
		ratio = 1;
	} else {
		ratio = mida_maxima / mida_orig;
	}
	return ratio;
}

/*
retorna la mida màxima de la foto en pixeles. Retornara l'ample o l'alt en funció del parametre:
	. w --> ample de la foto
	. h --> alt de la foto
*/
function tamany_maxim_foto(dim){
	var width_maxim = 100;
	var height_maxim = 100;
	var retorna = 0;
	if (dim == 'w') {
		retorna = width_maxim;
	}
	if (dim == 'h') {
		retorna = height_maxim;
	}
	return retorna;
}


