var popup;
var selectedPDF='';
var selectedURL='consultazione_zip.jsp';
var selectedURL_ricerca='consultazione_ricerca_zip.jsp';
var indexPDF=null;
var position=0;



function ridimensiona(offset){
 var myHeight=document.body.clientHeight;
 if(document.getElementById("notizie")){
     document.getElementById("notizie").style.height=(myHeight+offset)+"px";
 }
 if(document.getElementById("indice_notizie")){
     document.getElementById("indice_notizie").style.height=(myHeight+offset)+"px";
 }
 if(document.getElementById("notizie_iframe")){
     document.getElementById("notizie_iframe").style.height=(myHeight+offset)+"px";
 }
}

function apriPopup(conferma,url,nome_popup,width,height){
    var status=false;
    if(conferma!=null&&conferma!=""){
       if(confirm(conferma)){
           status=true;
       }
    }
    else{
       status=true;
    }
    if(status){
          if(popup!=null&&!popup.closed)
              popup.close();
          popup=window.open(url,nome_popup,"width="+width+", height="+height+", resizable=yes, scrollbars=yes");
          popup.focus();
    }
    return false;
}

function loadRicerca(){
    if(window.opener){
         if(window.opener.document.getElementById('parole_chiave_ricerca')){
              document.getElementById('parole_chiave_ricerca').value=window.opener.document.getElementById('parole_chiave_ricerca').innerHTML;
         }
         if(window.opener.document.getElementById('testata_ricerca')){
              document.getElementById('testata_ricerca').value=window.opener.document.getElementById('testata_ricerca').innerHTML;
         }
         if(window.opener.document.getElementById('data_ricerca')){
              document.getElementById('data_ricerca').value=window.opener.document.getElementById('data_ricerca').innerHTML;
              document.getElementById('tipo_data_1').checked=true;
         }
         if(window.opener.document.getElementById('data_da_ricerca')&&window.opener.document.getElementById('data_a_ricerca')){
              document.getElementById('data_da_ricerca').value=window.opener.document.getElementById('data_da_ricerca').innerHTML;
              document.getElementById('data_a_ricerca').value=window.opener.document.getElementById('data_a_ricerca').innerHTML;
              document.getElementById('tipo_data_2').checked=true;
         }
         if(window.opener.document.getElementById('data_variabile_ricerca')){
              document.getElementById('data_variabile_ricerca').value=window.opener.document.getElementById('data_variabile_ricerca').innerHTML;
              document.getElementById('tipo_data_3').checked=true;
         } 
         if(window.opener.document.getElementById('ordine_ricerca')){
              document.getElementById('ordine_ricerca').value=window.opener.document.getElementById('ordine_ricerca').innerHTML;
         }
         if(window.opener.document.getElementById('ordine_desc_ricerca')){				 
              document.getElementById('ordine_desc_ricerca').checked=true;
         }
         else{
              if(document.getElementById('ordine_ricerca').value=='Default'){
                   document.getElementById('ordine_desc_ricerca').checked=true;
              }
              else{
                   document.getElementById('ordine_desc_ricerca').checked=false;
              }
         }		 
         
    }   
    return false;
}

function changeSelectedPDF(){ 
    selectedPDF='';
    if(document.getElementById('esporta_rassegna')&&document.getElementById('esporta_rassegna').check){
         for(var i=0;i<document.getElementById('esporta_rassegna').check.length;i++){
              if(document.getElementById('esporta_rassegna').check[i].checked==true){
                    selectedPDF+='&check='+document.getElementById('esporta_rassegna').check[i].value;
              }
         }
    }

    return false;
}

function changeSelectedURL(url){ 
    if(document.getElementById('esporta_rassegna')&&document.getElementById('submit_esporta_rassegna')){
         selectedURL=url;
         document.getElementById('submit_esporta_rassegna').click();
    }
    return false;
}

function changeSelectedURL_ricerca(url){ 
    if(document.getElementById('esporta_rassegna')&&document.getElementById('submit_esporta_rassegna')){
         selectedURL_ricerca=url;
         document.getElementById('submit_esporta_rassegna').click();
    }
    return false;
}

function cambiaPDF(data,id){
 if(document.getElementById("indice_notizie")){
     for(i=0;i<document.getElementById("indice_notizie").getElementsByTagName("div").length;i++){
          document.getElementById("indice_notizie").getElementsByTagName("div")[i].className='';
     }
 }
 if(document.getElementById("notizia_"+id)){
     document.getElementById("notizia_"+id).className='selected';
 }
 if(document.getElementById("notizie_iframe_pdf")){
     document.getElementById("notizie_iframe_pdf").src='consultazione_pdf.jsp?data='+data+'&id='+id;
 }
 return false;
}

function nextPDF(offset){
 if(document.getElementById("indice_notizie")){
     for(i=0;i<document.getElementById("indice_notizie").getElementsByTagName("div").length;i++){
          if(document.getElementById("indice_notizie").getElementsByTagName("div")[i].className=='selected'){
              offset+=i;
          }
     }
     if(offset>=0&&document.getElementById("indice_notizie").getElementsByTagName("div")[offset]){
          for(i=0;i<document.getElementById("indice_notizie").getElementsByTagName("div").length;i++){
                document.getElementById("indice_notizie").getElementsByTagName("div")[i].className='';
         }
         document.getElementById("indice_notizie").getElementsByTagName("div")[offset].className='selected';
         document.getElementById("indice_notizie").getElementsByTagName("div")[offset].getElementsByTagName("button")[0].click();
     }
     else{
         alert('Non ci sono altri articoli disponibili');
     }
 }
 return false;
}

function mostraIndice(){
 if(document.getElementById("td_indice_notizie")){
     if(document.getElementById("td_indice_notizie").style.width=='30%'){
          document.getElementById("td_indice_notizie").style.width='0px'
          document.getElementById("indice_notizie").style.width='0px'
     }
     else{
          document.getElementById("td_indice_notizie").style.width='30%';
          document.getElementById("indice_notizie").style.width='100%'
     }
 }
 return false;
}

function attivaJQuery(){
    $("#loading").ajaxStart(function(){
        $(this).show();
    });
    $("#loading").ajaxStop(function(){
        $(this).hide();
    });

    $('.categoria_drag').Draggable(
        {
         zIndex: 	1000,
         ghosting:	true,
         opacity: 	0.7,
         revert: true,
         fx: 500,
         onStart:function () 
           {  
			$(this).parent('td').parent('tr').addClass('selected');			
			$('.notizia').DroppableDestroy();
            $('.categoria').DroppableDestroy();
			$('.categoria[@id_categoria!='+$(this).parents("tr").attr("id_categoria")+'][@id_categoria!=-1]').Droppable(
                {
                 accept : 'categoria_drag', 
                 activeclass: 'dropzoneactive', 
                 hoverclass:	'dropzonehover',
                 onDrop:	function (drag) 
                   {
                   $.ajax(
                    {url:'mod_posizione_categoria.jsp',
                    data:'id_categoria1='+$(this).attr("id_categoria")+'&id_categoria2='+$(drag).parents("tr").attr("id_categoria"),
                    type: 'POST',
                    success:function(){window.location.reload();}
                   }
                   );
                    return false;
                   },
                 tolerance: 'pointer'
                }
            );
           },
		 onStop:function () 
           {  
			$(this).parent('td').parent('tr').removeClass('selected');
           }
        }
    );
	
	
    $('.titolo_drag').Draggable(
        {
         zIndex: 	1000,
         ghosting:	true,
         opacity: 	0.7,
         revert: true,
         fx: 500,
         onStart:function () 
           {	
			ordine_notizie(this,true);			
            $('.notizia').DroppableDestroy();
            $('.categoria').DroppableDestroy();            
            $('.categoria').Droppable(
                {
                 accept : 'titolo_drag', 
                 hoverclass:	'dropzonehover',
                 onDrop:	function (drag) 
                   {
					var params="";
					$('.notizia.selected').each(function(){
						params+='&id_notizia2='+$(this).attr('id_notizia');
						params+='&pos='+$(this).attr('position');
					});
                    $.ajax(
                     {url:'mod_categoria_notizia.jsp',
                     data:'id_categoria='+$(this).attr("id_categoria")+'&id_notizia1='+$(this).attr("id_notizia")+params,
                     type: 'POST',
                     success:function(){window.location.reload();}
                     }
                    );
                    return false;
                   },
                 tolerance: 'pointer'
                }
            );
            $('.notizia:not(.selected)').Droppable(
                {
                 accept : 'titolo_drag', 
                 hoverclass:	'dropzonehover',
                 onDrop:	function (drag) 
                   {var url='mod_posizione_notizia.jsp';
					var params="";
					var id_categoria=$(this).attr("id_categoria");
					$('.notizia.selected').each(function(){
						params+='&id_notizia2='+$(this).attr('id_notizia');
						params+='&pos='+$(this).attr('position');
						if($(this).attr('id_categoria')!=id_categoria){
							url='mod_categoria_notizia.jsp';
						}
					});
                    $.ajax(
                     {url: url,
                     data:'id_categoria='+$(this).attr("id_categoria")+'&id_notizia1='+$(this).attr("id_notizia")+params,
                     type: 'POST',
                     success:function(){window.location.reload();}
                    }
                    );                   
                    return false;
                   },
                 tolerance: 'pointer'
                }
            ); 			
           },
		onStop:function () 
           {
			   if($('.notizia.selected').length==1){
					ordine_notizie(this,false);
			   }
		   }
        }
    );
}


function controlla_file(selected,id_checkbox){
 if(selected.value!=null && !selected.value==""){
      if(selected.value.toLowerCase().indexOf(".pdf")==-1){
          alert("Il file con la copertina non e' in formato PDF");
          document.getElementById(id_checkbox).checked="true";
      }
      else{
          document.getElementById(id_checkbox).checked="";
      }
 }
 if(selected.value==null || selected.value==""){
      document.getElementById(id_checkbox).checked="true";
 }

}

function ordine_notizie (selected,force) {
	if(force){
		$(selected).parent('td').parent('tr').addClass('selected');
	}
	else{
		$(selected).parent('td').parent('tr').toggleClass('selected');
	}
	var immagine=$(selected).parent('td').parent('tr').find('.select img');	
	if($(selected).parent('td').parent('tr').attr('class').match('selected')){	
		if(!$(selected).parent('td').parent('tr').attr('position')){
			position++;
			$(selected).parent('td').parent('tr').attr('position',position);
			$(selected).parent('td').parent('tr').find('b').html(position);
		}		
		immagine.attr('src','../images/selezionato.gif');
	}else{				
		if(position!=$(selected).parent('td').parent('tr').attr('position')){
			$('.notizia.selected').each(function(){
				if(Math.abs($(this).attr('position'))>Math.abs($(selected).parent('td').parent('tr').attr('position'))){
					$(this).attr('position',$(this).attr('position')-1);
					$(this).find('b').html($(this).attr('position'));
				}
			});
		}
		position--;
		$(selected).parent('td').parent('tr').removeAttr('position');
		$(selected).parent('td').parent('tr').find('b').html('');
		immagine.attr('src','../images/seleziona.gif');
	}	
}