  $(document).ready(function(){
 	if($('#four_news').is('div')){
		//writeNews();
		$("#four_news").html($("#4news").html());

	}
 	if($('.dinamycNews').is('div')){
		writeNewsList();
	}
	if($('.dinamycCalendar').is('div')){
		writeEventList();
	}
	if($("#newsDetail").is("div")){
 		getNewsDetail();
 	}
 	if($("#pubDetail").is("div")){
	 	getPubDetail();
 	}
 	if($("#comDetail").is("div")){
	 	getComDetail();
 	}
	if($("#eventiDetail").is("div")){
 	getEventiDetail();
 	}
 		if($('.box').is('div')){
			if($('.calendar').is('div')){
				writeCalendarHp();

			}
	}
  });


function changePage(current,last){
  $(".pagina").css({display:"none"});
  $("#page-"+current).css({display:"block"});

  if(current!=1){
    $(".prev").html('<a href="#" onclick="javascript:changePage('+(current-1)+','+last+');"><img src="/www.actionaid.it/img/pag_prev.gif"/></a>');
  }else{
    $(".prev").html('');
  }
  if(current!=last)  {
    $(".next").html('<a href="#" onclick="javascript:changePage('+(current+1)+','+last+');"><img src="/www.actionaid.it/img/pag_next.gif"/></a>');
  }else{
    $(".next").html('');
  }
  //$("#pager_result_news_btm ul li").removeClass("on");
  $("#paging ul").find('li').each(function(){
  $(this).removeClass('pag_on');
  });
  $("#paging ul").find('li').each(function(){
  $(this).removeClass('pag_on');
  });
  $(".pager"+current).addClass("pag_on");
}


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 getNewsDetail(){
var html = '';
var id = GetUrlParam('IDNEWS');
  $.ajax({
       type: "GET",
       url: sRootPath+"it/XML/NEWS/data.xml",
       dataType: 'xml',
       success: function(msg){
             if (id!='') {
                 var news = $(msg).find("Item").filter("[@id="+id+"]")
                 titolo = $("titolo", news).text();
                 titolo = titolo.replace(/(<([^>]+)>)/ig,"");
                 data = $("data", news).text();
                 testo = $("testo ", news).text();
                 html+='<span class="red_title">'+data+' '+titolo+'</span>';
         	     html+='<div>'+testo+'</div>';
                $("#newsDetail").html(html);
              }
      }
       });
}
function getPubDetail(){
var html = '';
var id = GetUrlParam('IDPUBBLICAZIONE');
  $.ajax({
       type: "GET",
       url: sRootPath+"it/XML/PUBBLICAZIONI/data.xml",
       dataType: 'xml',
       success: function(msg){
             if (id!='') {
                 var news = $(msg).find("Item").filter("[@id="+id+"]")
                 titolo = $("titolo", news).text();
                 titolo = titolo.replace(/(<([^>]+)>)/ig,"");
                 data = $("data", news).text();
                 testo = $("testo ", news).text();
                 html+='<span class="red_title">'+data+' '+titolo+'</span>';
         	     html+='<div>'+testo+'</div>';
                $("#pubDetail").html(html);
              }
      }
       });
}
function getComDetail(){
var html = '';
var id = GetUrlParam('IDCOMUNICATO');
  $.ajax({
       type: "GET",
       url: sRootPath+"it/XML/COMUNICATI_STAMPA/data.xml",
       dataType: 'xml',
       success: function(msg){
             if (id!='') {
                 var news = $(msg).find("Item").filter("[@id="+id+"]")
                 titolo = $("titolo", news).text();
                 titolo = titolo.replace(/(<([^>]+)>)/ig,"");
                 data = $("data", news).text();
                 testo = $("testo ", news).text();
                 html+='<span class="red_title">'+data+' '+titolo+'</span>';
         	     html+='<div>'+testo+'</div>';
                $("#comDetail").html(html);
              }
      }
       });
}

function getEventiDetail(){
var html = '';
var id = GetUrlParam('IDEVENTO');
  $.ajax({
       type: "GET",
       url: sRootPath+"it/XML/EVENTI/data.xml",
       dataType: 'xml',
       success: function(msg){
             if (id!='') {
                 var news = $(msg).find("Item").filter("[@id="+id+"]")
                 titolo = $("titolo", news).text();
                 titolo = titolo.replace(/(<([^>]+)>)/ig,"");

                 data = $("data", news).text();
                 cat = $("categoria", news).text();
                 testo = $("testo ", news).text();
                 html+='<span class="red_title">'+data+' '+titolo+' '+cat+'</span>';
         	     html+='<div>'+testo+'</div>';
                $("#eventiDetail").html(html);
              }
      }
       });
}

function showOn(mese){

	var current = $('#calendar_archive').find('a.on');
	current.removeClass('on');

	var newsc = $('#calendar_archive').find('a.m'+mese);
	newsc.addClass('on');



}

function viewCalendar(type,anno,mese){
//	alert(type);
	var m='';
	if(mese=='-'){
	var count=0;
		$("#"+anno+" p a").each(function(){
		count++;
			if(count==1){
				m=$(this).attr('class');
				}
		});
		mese=m.substring(1,m.length);
		if(mese.endsWith(" on")){
			mese=mese.substring(0,m.length-4);
		}
		$("#paging").hide();
		$("#"+type+"List").hide();
		$(".calendar").hide();
		$("#"+type+"Calendar").show();
		$(".anno").hide();
		$("#"+anno).show();
		$("."+mese+"-"+anno).show();
		showOn(mese);
	}
	else if(!isNaN(anno)){
		$("#paging").hide();
		$("#"+type+"List").hide();
		$(".calendar").hide();
		$("#"+type+"Calendar").show();
		$(".anno").hide();
		$("#"+anno).show();
		$("."+mese+"-"+anno).show();
		showOn(mese);
	}
}
/*function viewPubCalendar(anno,mese){
	if(!isNaN(anno)){
		$("#paging").hide();
		$("#pubList").hide();
		$(".calendar").hide();
		$("#pubCalendar").show();
		$("."+mese+"-"+anno).show();
		showOn(mese);
	}
}
function viewComCalendar(anno,mese){
	if(!isNaN(anno)){
		$("#paging").hide();
		$("#comList").hide();
		$(".calendar").hide();
		$("#comCalendar").show();
		$("."+mese+"-"+anno).show();
		showOn(mese);
	}
}

*/
function viewEventsCategory(cat){
	if(cat!='all'){
		$("#paging").hide();
		$("#calendario-paging").hide();
		$(".calendar").hide();
		$("#calendario-nopaging").show();
		$("."+cat).show();
	}else{
		$("#calendario-nopaging").hide();
		$(".calendar").hide();
		$("#calendario-paging").show();
		$("#paging").show();
	}
}

function writeNews(){

	var html = '<ul id="news_list">';
    var i = 0;
	$.ajax({
			 type: "GET",
			 url: sRootPath+"it/XML/NEWS/data.xml",
			 dataType: 'xml',
			 success: function(msg){
					   $(msg).find("Item").each(function(){
					   					id = $(this).attr('id');
					   					data= $("data", this).text();
									  // 	titolo = $("titolo", this).text();
									   	thumbnail = $("img_pic", this).text();
										testobreve = $("abstract", this).text();
									//	titolo = titolo.replace(/(<([^>]+)>)/ig,"");
										if (i<4) {
											html = html + '<li>';
											html = html + '<a href="/it/media_center/news/newsDetail.html?IDNEWS=' + id + ' ">';
											if (thumbnail!='/filemanager/cms_actionad/.BlankImage.jpg') {
												html = html + '<img src="' + thumbnail + '" width="150" height="100" />';
											}
											html=html+'<div><span class="red_title">'+data+'</span>';
											html = html +'<p>'+ handleCutTxtDynamic(testobreve)+'</p>';
											html = html + '</div></a></li>';
										}
										i++;
									   })
						html = html + '</ul>';
						$("#four_news").html(html);
				 }
		   });
}


function writeCalendarHp(){
	var html = '';
    var i = 0;
	$.ajax({
			 type: "GET",
			 url: sRootPath+"it/XML/EVENTI/data.xml",
			 dataType: 'xml',
			 success: function(msg){
					   $(msg).find("Item").each(function(){
					   					id = $(this).attr('id');
					   					data= $("data", this).text();
									   	titolo = $("titolo", this).text().replace(/(<([^>]+)>)/ig,"");
											testobreve = $("abstract", this).text().replace(/(<([^>]+)>)/ig,"");
									//	titolo = titolo.replace(/(<([^>]+)>)/ig,"");
										if (i<1) {
											html = html + '<a href="/it/cosa_facciamo/eventi.html"><h2>CALENDARIO</h2></a>';
											html=html+'<div>	<span class="red_title">'+data+'</span>';
											
											html = html +'<h3>'+ handleCutTxtDynamic(titolo)+'</h3>';									
											html = html +'<p>'+ handleCutTxtDynamic(testobreve)+'</p>';									
											html = html + '<a href="/it/cosa_facciamo/eventi/eventiDetail.html?IDEVENTO==' + id + ' " class="main_link">Leggi</a></div>';
										
												$(".calendar").html(html);					

										
										}										
									i++;
										
									   })
						
				 }
		   });
}

	



function writeNewsList(){
    var html = '<div class="black_grey_big"><h2 class="box_title"><a href="/it/media_center/news.html">News</a></h2><div><dl>';
    var i = 0;
    $.ajax({
             type: "GET",
             url: "XML/NEWS/data.xml",
             dataType: 'xml',
             success: function(msg){

                   $(msg).find("Item").each(function(){
                        var id = $(this).attr('id');
                        var data= $("data", this).text();
                        var testobreve = $('<div></div>').html($("abstract", this).text()).text();

			var titolo='';
                        if (testobreve.length > 30){
                            var cutTxt = testobreve.substring(0,27);
                            var newTxt = cutTxt.lastIndexOf(' ');
                            var myTxt = cutTxt.substring(0,newTxt);
                            titolo=myTxt + "...";
                        }

                        if (i<2) {
                            html = html + '<dt><a href="/it/media_center/news/newsDetail.html?IDNEWS=' + id + ' ">'+data+'</a></dt>';
                            html=html+'<dd><h3><a href="/it/media_center/news/newsDetail.html?IDNEWS=' + id + ' ">'+titolo+'</a></h3>';
                            html = html + handleCutTxtDynamic(testobreve);
                            html = html + '</dd>';
                        }
                        i++;
                   })
                    html = html + '</dl></div></div>';
                    $(".dinamycNews").html(html);

            }
    });
}





function writeEventList(){
		var html = '<div class="black_grey_big"><h2 class="box_title"><a href="/it/cosa_facciamo/eventi.html">Calendario</a></h2><div><dl>';
    var i = 0;
	$.ajax({
			 type: "GET",
			 url: sRootPath+"it/XML/EVENTI/data.xml",
			 dataType: 'xml',
			 success: function(msg){
					   $(msg).find("Item").each(function(){
					   					id = $(this).attr('id');
					   					data= $("data", this).text();
										testobreve = $("abstract", this).text();
	  				            	    titolo = $("titolo", this).text();
        						        titolo = titolo.replace(/(<([^>]+)>)/ig,"");
										if (i<2) {
											html = html + '<dt><a href="/it/cosa_facciamo/eventi/eventiDetail.html?IDEVENTO=' + id + ' ">'+data+'</a></dt>';
											html=html+'<dd><h3><a href="/it/cosa_facciamo/eventi/eventiDetail.html?IDEVENTO=' + id + ' ">'+titolo+'</a></h3>';
											html = html + testobreve;
											html = html + '</dd>';
										}
										i++;
									   })
						html = html + '</dl></div></div>';
						$(".dinamycCalendar").html(html);
				 }
		   });
}
function handleCutTxtDynamic(txx){

    if (txx.length > 130){  
	    var oldTxt =txx.replace(/[\s\t]+/g ,' ');
		 oldTxt =txx.replace(/(<([^>]+)>)/ig,"");
	    if (oldTxt.length > 130){
     		 var cutTxt = oldTxt.substring(0,127); 		 var newTxt = cutTxt.lastIndexOf(' ');
		 var myTxt = cutTxt.substring(0,newTxt);
		 txx=myTxt + "...";
	    }
	}
return  txx;
}

String.prototype.trim = function(str){return
(this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""))}

String.prototype.endsWith = function(str)
{return (this.match(str+"$")==str)}
