var maxNewsPage = 3;

$(document).ready(function(){
	/*
	if($("#news_hp").is("div")){
		getNewsHp();
	}
	*/
	if($("#listnews").is("div")){
		//PROD : "/it/News/data.xml"
		//SVIL : "/WorkingFolder/deploy/Bancomat/it/News/data.xml"
		//SVIL : "/WorkingFolder/deploy/Bancomat/it/Press_e_Media/listanews/dettaglionews.html"
		if (langXml == 'it') {
			getArchive(sRootPath + 'it/News/data.xml',sRootPath + 'it/cdi/listanews.html','#listnews',sRootPath + 'it/cdi/listanews/dettaglionews.html');
		} else {
			getArchive(sRootPath + 'it/News/data.xml',sRootPath + 'it/cdi/listanews.html','#listnews',sRootPath + 'it/cdi/listanews/dettaglionews.html');
		}
	}
	if($("#listpress").is("div")){
		//PROD : "/it/Eventi/data.xml"
		//SVIL : "/WorkingFolder/deploy/Bancomat/it/Eventi/data.xml"
		//SVIL : "/WorkingFolder/deploy/Bancomat/it/Press_e_Media/listaeventi/dettaglioeventi.html"
		if (langXml == 'it') {
			getArchive(sRootPath + 'it/Rassegna_Stampa/data.xml',sRootPath + 'it/cdi/listapress.html','#listpress',sRootPath + 'it/cdi/listapress/dettagliopress.html');
		} else {
			getArchive(sRootPath + 'it/Rassegna_Stampa/data.xml',sRootPath + 'it/cdi/listapress.html','#listpress',sRootPath + 'it/cdi/listapress/dettagliopress.html');
		}
	}
	if($("#listcomun").is("div")){
		//PROD : "/it/Eventi/data.xml"
		//SVIL : "/WorkingFolder/deploy/Bancomat/it/Eventi/data.xml"
		//SVIL : "/WorkingFolder/deploy/Bancomat/it/Press_e_Media/listaeventi/dettaglioeventi.html"
		if (langXml == 'it') {
			getArchive(sRootPath + 'it/Comunicati/data.xml',sRootPath + 'it/cdi/listacomun.html','#listcomun',sRootPath + 'it/cdi/listacomun/dettagliocomun.html');
		} else {
			getArchive(sRootPath + 'it/Comunicati/data.xml',sRootPath + 'it/cdi/listacomin.html','#listcomin',sRootPath + 'it/cdi/listacomun/dettagliocomun.html');
		}
	}
	if($("#listmeeting").is("div")){
		//PROD : "/it/Eventi/data.xml"
		//SVIL : "/WorkingFolder/deploy/Bancomat/it/Eventi/data.xml"
		//SVIL : "/WorkingFolder/deploy/Bancomat/it/Press_e_Media/listaeventi/dettaglioeventi.html"
		if (langXml == 'it') {
			getArchive(sRootPath + 'it/Meeting/data.xml',sRootPath + 'it/nostriservizi/medici/medicalp/listameet.html','#listmeeting',sRootPath + 'it/nostriservizi/medici/medicalp/listameet/dettagliomeet.html');
		} else {
			getArchive(sRootPath + 'it/Meeting/data.xml',sRootPath + 'it/nostriservizi/medici/medicalp/listameet.html','#listmeeting',sRootPath + 'it/nostriservizi/medici/medicalp/listameet/dettagliomeet.html');
		}
	}
	if($("#listecm").is("div")){
		//PROD : "/it/Eventi/data.xml"
		//SVIL : "/WorkingFolder/deploy/Bancomat/it/Eventi/data.xml"
		//SVIL : "/WorkingFolder/deploy/Bancomat/it/Press_e_Media/listaeventi/dettaglioeventi.html"
		if (langXml == 'it') {
			getArchive(sRootPath + 'it/ECM/data.xml',sRootPath + 'it/nostriservizi/medici/medicalp/listaecm.html','#listecm',sRootPath + 'it/nostriservizi/medici/medicalp/listaecm/dettaglioecm.html');
		} else {
			getArchive(sRootPath + 'it/ECM/data.xml',sRootPath + 'it/nostriservizi/medici/medicalp/listaecm.html','#listecm',sRootPath + 'it/nostriservizi/medici/medicalp/listaecm/dettaglioecm.html');
		}
	}

	if($("#detailnews").is("div")){
		if (langXml == 'it') {
			getDetail(sRootPath + 'it/News/data.xml','detailnews','ULTIME NEWS',sRootPath + 'it/cdi/listanews/dettaglionews.html');
		} else {
			getDetail(sRootPath + 'it/News/data.xml','detailnews','ULTIME NEWS',sRootPath + 'it/cdi/listanews/dettaglionews.html');
		}
	}
	if($("#detailpress").is("div")){
		if (langXml == 'it') {
			getDetail(sRootPath + 'it/Rassegna_Stampa/data.xml','detailpress','ULTIME DALLA RASSEGNA STAMPA',sRootPath + 'it/cdi/listapress/dettagliopress.html');
		} else {
			getDetail(sRootPath + 'it/Rassegna_Stampa/data.xml','detailpress','ULTIME DALLA RASSEGNA STAMPA',sRootPath + 'it/cdi/listapress/dettagliopress.html');
		}
	}
	if($("#detailcomun").is("div")){
		if (langXml == 'it') {
			getDetail(sRootPath + 'it/Comunicati/data.xml','detailcomun','ULTIMI COMUNICATI',sRootPath + 'it/cdi/listacomun/dettagliocomun.html');
		} else {
			getDetail(sRootPath + 'it/Comunicati/data.xml','detailcomun','ULTIMI COMUNICATI',sRootPath + 'it/cdi/listacomun/dettagliocomun.html');
		}
	}
	if($("#detailmeeting").is("div")){
		if (langXml == 'it') {
			getDetail(sRootPath + 'it/Meeting/data.xml','detailmeeting','ULTIMI APPUNTAMENTI E MEETING',sRootPath + 'it/nostriservizi/medici/medicalp/listameet/dettagliomeet.html');
		} else {
			getDetail(sRootPath + 'it/Meeting/data.xml','detailmeeting','ULTIMI APPUNTAMENTI E MEETING',sRootPath + 'it/nostriservizi/medici/medicalp/listameet/dettagliomeet.html');
		}
	}
	if($("#detailecm").is("div")){
		if (langXml == 'it') {
			getDetail(sRootPath + 'it/ECM/data.xml','detailecm','ECM Formazione al CDI',sRootPath + 'it/nostriservizi/medici/medicalp/listaecm/dettaglioecm.html');
		} else {
			getDetail(sRootPath + 'it/ECM/data.xml','detailecm','ECM Formazione al CDI',sRootPath + 'it/nostriservizi/medici/medicalp/listaecm/dettaglioecm.html');
		}
	}
});

function GetUrlParam( paramName )
{
	var oRegex = new RegExp( '[\?&]' + paramName + '=([^&]+)', 'i' ) ;
	var oMatch = oRegex.exec( window.top.location.search ) ;

	if ( oMatch && oMatch.length > 1 )
		return decodeURIComponent( oMatch[1] ) ;
	else
		return '' ;
}

function getArchive(pathData, listpage, obj, pathEnter){
	//alert("load");

	var page = GetUrlParam('page');
	if (page.length == 0) {
		page = 1;
	}

	var archive = GetUrlParam('archive');
	if (archive.length == 0) {
		archive = 0;
	}

	// Cast a value;
	page = page * 1;

	var maxNews = page * maxNewsPage;
	var minNews = maxNews - maxNewsPage;

	var html = '<dl class="dl-news">';
    var i = 0;

	$.ajax({
			 type: "GET",
			 url: pathData,
			 dataType: 'xml',
			 success: function(msg){
					   $(msg).find("Item").each(function(){


					   					var id = $(this).attr('id');
									   	var titolo = $("Titolo", this).text();
									   	var thumbnail = $("Thumbnail", this).text();
									   	var data = $("Data", this).text();
										var testobreve = $("TestoBreve", this).text();

										if ((archive>0) && (data.indexOf(''+archive)>=0) || (archive==0)) {
											i++;
											if ((i<=maxNews) && (i>minNews)) {

												var giorno = data.split("/")[0];
												var mese = data.split("/")[1];
												var anno = data.split("/")[2];
												if (anno.length==4) {
													anno = anno.substring(2,4);
												}
												giorno = (giorno.length == 2) ? (giorno=giorno) : (giorno="0"+giorno);
												mese = (mese.length == 2) ? (mese=mese) : (mese="0"+mese);
												anno = (anno.length == 2) ? (anno=anno) : (anno="0"+anno);


												html = html + '<dt class="data"><span class="day">' + giorno + '</span> <span class="month">' + mese + '</span> <span class="year"> '+ anno + '</span> </dt>';
												html = html + '<dt class="subtitle"><a href="' + pathEnter + '?page=' + page + '&idnews=' + id + '">'
												html = html + titolo;
												html = html + '</a></dt>';
												html = html + '<dd>';
												html = html + testobreve;
												html = html + '</dd>';
											}
										}
										//alert(titolo + '-' + thumbnail + '-' + data);
									   })

						html = html + '</dl>';
						// Paginazione
						if (i>maxNewsPage) {
							var numPages = Math.ceil(i / maxNewsPage);

							html = html + '<div class="paginazione"><a class="rewind" hreF="' + listpage + '?page=1&archive=' + archive + '"></a>';
							if (page>1) {
								html = html + '<a class="revplay" href="' + listpage + '?page=' + (page - 1) + '&archive=' + archive + '"></a>';
							} else {
								html = html + '<div class="revplay"></div>';
							}

							var j=1;
							for (j=1;j<=numPages;j++) {
								if ((j) == (page)) {
									html = html + '<a class="pag_on" href="' + listpage + '?page=' + j +'&archive=' + archive + '">' + j + '</a>';
								} else {
									html = html + '<a href="' + listpage + '?page=' + j +'&archive=' + archive + '">' + j + '</a>';
								}
							}

							if (page<numPages) {
								html = html + '<a class="play" href="' + listpage + '?page=' + (page + 1) + '&archive=' + archive + '"></a>';
							} else {
								html = html + '<div class="play"></div>';
							}
							html = html + '<a class="forward" href="' + listpage + '?page=' + numPages + '&archive=' + archive + '"></a>';
							html = html + '</div><!-- newsPager -->';

						}

						// Stampa l'AJAX
						$(obj).parent().append(html);

				 }

		   });
}

function getDetail(pathData, obj, titleLast, pathEnter){
	//alert("load");

	var html = '<dl class="dl-news">';

	var id = GetUrlParam('idnews');
	var page = GetUrlParam('page');

	$.ajax({
			 type: "GET",
			 url: pathData,
			 dataType: 'xml',
			 success: function(msg){
			 			if (id!='') {
				 		    var news = $(msg).find("Item").filter("[id="+id+"]")

						   	var titolo = $("Titolo", news).text();
						   	var data = $("Data", news).text();
							var testo = $("Testo", news).text();

							var giorno = data.split("/")[0];
							var mese = data.split("/")[1];
							var anno = data.split("/")[2];
							if (anno.length==4) {
								anno = anno.substring(2,4);
							}
							giorno = (giorno.length == 2) ? (giorno=giorno) : (giorno="0"+giorno);
							mese = (mese.length == 2) ? (mese=mese) : (mese="0"+mese);
							anno = (anno.length == 2) ? (anno=anno) : (anno="0"+anno);

							html = html + '<dt class="data"><span class="day">' + giorno + '</span> <span class="month">' + mese + '</span> <span class="year"> '+ anno + '</span> </dt>';
							html = html + '<dt class="subtitle"><a href="' + pathEnter + '?page=' + page + '&idnews=' + id + '">'
							html = html + titolo;
							html = html + '</a></dt>';
							html = html + '<dd>';
							html = html + testo;
							html = html + '</dd>';
						} else {

							var news = $(msg).find("Item").get(0);

						   	var titolo = $("Titolo", news).text();
						   	var data = $("Data", news).text();
							var testo = $("Testo", news).text();

							var giorno = data.split("/")[0];
							var mese = data.split("/")[1];
							var anno = data.split("/")[2];
							if (anno.length==4) {
								anno = anno.substring(2,4);
							}

							giorno = (giorno.length == 2) ? (giorno=giorno) : (giorno="0"+giorno);
							mese = (mese.length == 2) ? (mese=mese) : (mese="0"+mese);
							anno = (anno.length == 2) ? (anno=anno) : (anno="0"+anno);

							html = html + '<dt class="data"><span class="day">' + giorno + '</span> <span class="month">' + mese + '</span> <span class="year"> '+ anno + '</span> </dt>';
							html = html + '<dt class="subtitle"><a href="' + pathEnter + '?page=' + page + '&idnews=' + id + '">'
							html = html + titolo;
							html = html + '</a></dt>';
							html = html + '<dd>';
							html = html + testo;
							html = html + '</dd>';
						}

						//alert(titolo + '-' + thumbnail + '-' + data);

						//html = html + '</ul><div id="paging"><p id="btn_bar"></p></div>';


						html = html + '<br/>';
						html = html + '<h3>' + titleLast + '</h3>';
						html = html + '<br/>';

						i = 0;

						$(msg).find("Item").each(function(){
					   					i++;
					   					var id = $(this).attr('id');
									   	var titolo = $("Titolo", this).text();
									   	var thumbnail = $("Thumbnail", this).text();
									   	var data = $("Data", this).text();
										var testobreve = $("TestoBreve", this).text();

										var giorno = data.split("/")[0];
										var mese = data.split("/")[1];
										var anno = data.split("/")[2];
										if (anno.length==4) {
											anno = anno.substring(2,4);
										}
										giorno = (giorno.length == 2) ? (giorno=giorno) : (giorno="0"+giorno);
										mese = (mese.length == 2) ? (mese=mese) : (mese="0"+mese);
										anno = (anno.length == 2) ? (anno=anno) : (anno="0"+anno);

										if (i<=3) {

											html = html + '<dt class="data"><span class="day">' + giorno + '</span> <span class="month">' + mese + '</span> <span class="year"> '+ anno + '</span> </dt>';
											html = html + '<dt class="subtitle"><a href="' + pathEnter + '?page=' + page + '&idnews=' + id + '">'
											html = html + titolo;
											html = html + '</a></dt>';
											html = html + '<dd>';
											html = html + testobreve;
											html = html + '</dd>';
										}
										//alert(titolo + '-' + thumbnail + '-' + data);
									   })

						html = html + '</dl>';

						$("#"+ obj).parent().append(html);

						/*
						// Gestione di lightbox per le news/eventi
  						$('#gallery a').lightBox({
												overlayBgColor: '#FFF',
												overlayOpacity: 0.6,
												imageLoading: '/www.bancomat.it/img/lightbox/loading.gif',
												imageBtnClose: '/www.bancomat.it/img/lightbox/close.gif',
												imageBtnPrev: '/www.bancomat.it/img/lightbox/prev.gif',
												imageBtnNext: '/www.bancomat.it/img/lightbox/next.gif',
												imageBlank: '/www.bancomat.it/img/lightbox/lightbox-blank.gif',
												containerResizeSpeed: 350,
												txtImage: 'Immagine',
												txtOf: 'di'
											   });
						*/

				 },
			  complete: function(msg){
			  				/*
							var videoUrl = $('.show-video').find('a').attr('href');
							var imageUrl = $('.show-video').find('img').attr('src');

							$('.show-video').flash({
													width:     470,
													height:    352,
													autoplay:  true,
													params:    {bgcolor : '#000000',scale:'noscale', allowfullscreen:'true', wmode: 'transparent'},
													attrs:     {bgcolor : '#000000',scale:'noscale', allowfullscreen:'true', wmode: 'transparent'},
													swf:       '/www.avanziholding.com/swf/player.swf',
													flashvars:  {file: videoUrl, image: imageUrl, height : 470, width :352,frontcolor :"ffffff",backcolor : "000000",lightcolor : "FFFFFF",overstretch : false,bufferlength : 5,usefullscreen : true},
													caption:   false
											});
							//$('#show-video').find('a').remove();
							//$('#show-video').find('img').remove();
							*/
			    }

		   });
}
