// imposta il cookie sNome = sValore
// per la durata di iGiorni
function setCookie(sNome, sValore, iGiorni) {
  var dtOggi = new Date()
  var dtExpires = new Date()
  dtExpires.setTime
    (dtOggi.getTime() + 24 * iGiorni * 3600000)
  document.cookie = sNome + "=" + escape(sValore) +
    "; expires=" + dtExpires.toGMTString() +
    "; path=/;";
}

// restituisce il valore del cookie sNome
function getCookie(sNome) {
  // genera un array di coppie "Nome = Valore"
  // NOTA: i cookies sono separati da ';'
  var asCookies 		= document.cookie;
  //alert(" $$" + asCookie + "$$ ") ;
  var asCookiesArray	= asCookies.split("; ");
  // ciclo su tutti i cookies
  for (var iCnt = 0; iCnt < asCookiesArray.length; iCnt++)
  {

    // leggo singolo cookie "Nome = Valore"
    var asCookiesArray2 = asCookiesArray[iCnt].split("=");
    if (sNome == asCookiesArray2[0]) { 
      return (unescape(asCookiesArray2[1]));
    }
  }

  // SE non esiste il cookie richiesto
  return("");
}

// rimuove un cookie
function delCookie(sNome) {
  setCookie(sNome, "");
}

/********************/


function openArticle(element) {
	var article_container="";
	if ($('article_window').value == '2') {
		article_container="image_";
	} else {
		article_container="";
	}
	var article_container_cookie=getCookie("article_container");
	if (article_container_cookie=="image_article_container"){
		article_container="image_";
	} else {
		article_container="";
	}
	var id = element.className.match(/\d+/)[0].toInt()
	var article = element.className.match(/\d+/)
	var uri = 'article_container/' + article
	if (id >= 999 || article_container=="image_") uri = 'image_' + uri
	//alert (id);
	//alert (article_container);
	//alert(uri);
	$('content-frame').setAttribute('src', uri);
}

function openArticle_old(element) {
	var id = element.className.match(/\d+/)[0].toInt()
	var article = element.className.match(/\d+/)
	var uri = 'article_container/' + article
	if (id >= 999) uri = 'image_' + uri
	$('content-frame').setAttribute('src', uri);
}

function addHighlight(element) {
	var ec = element.className
	$$('#page div.' + ec).each(function(n) {
		n.addClass('selected')
		n.tween('opacity', .3)
	})
}

function removeHighlight(element) {
	$$('#page div.selected').each(function(n) {
		n.removeClass('selected')
		n.tween('opacity', .0001)
	})
}

function search() {
	var n = (document.location + '').split('/ee/')[1].split('/')
	var url = '/search_results_container/' + n[0] + '/' + n[1] + '/' + n[2] + '/' + n[3] + '/' + n[4]
	$('content-frame').setAttribute('src', url + '?query=' + $('query').value + '&query_type=all_words')
}

window.addEvent('domready', function() {
    //start the tips
	var tips = new Tips();

	$('select-date').addEvent('change', function(){
		if ($('select-date').value.match(/calendar/)) {
			$('content-frame').setAttribute('src', $('select-date').value);
		} else {
			document.location = '../../../../' + $('select-date').value + '/'
		}
	})

	if ($('select-edition')) {
		$('select-edition').addEvent('change', function(){
			document.location = '../../../../../' + $('select-edition').value + '/'
		})
	}

	$('select-section').addEvent('change', function(){
		document.location = '../' + $('select-section').value + '/'
	})

	$('select-page').addEvent('change', function(){
		document.location = '../' + $('select-page').value + '/'
	})

	$$('#page div').each(function(element) {
		SetTipStorage($(element));
		tips.attach($(element), {
			fixed: false,
			showDelay: 0,
			hideDelay: 0,
			maxTitleChars: 100
		});

		element.setOpacity(.0001)
		element.addEvent('click',     function(){ openArticle(element) })  
		element.addEvent('mouseover', function(){ addHighlight(element) })  
		element.addEvent('mouseout',  function(){ removeHighlight(element) })
	})

/*
	new Tips($$('#page div, #select-date, #select-edition, #select-section, #query, #search-button'), {
		timeOut: 700,
		maxTitleChars: 100
	})
*/

/*    SetTipStorage($$('#page div'));
    tips.attach($$('#page div'), {
        fixed: false,
        showDelay: 0,
        hideDelay: 0,
		maxTitleChars: 100
    });
*/
    SetTipStorage($('select-date'));
    tips.attach($('select-date'), {
        fixed: false,
        showDelay: 0,
        hideDelay: 0,
		maxTitleChars: 100
    });
	SetTipStorage($('select-edition'));
    tips.attach($('select-edition'), {
    	fixed: false,
   	   	showDelay: 0,
   	    hideDelay: 0,
		maxTitleChars: 100
   	 });
    SetTipStorage($('select-section'));
    tips.attach($('select-section'), {
        fixed: false,
        showDelay: 0,
        hideDelay: 0,
		maxTitleChars: 100
    });
    SetTipStorage($('query'));
    tips.attach($('query'), {
        fixed: false,
        showDelay: 0,
        hideDelay: 0,
		maxTitleChars: 100
    });
    SetTipStorage($('search-button'));
    tips.attach($('search-button'), {
        fixed: false,
        showDelay: 0,
        hideDelay: 0,
		maxTitleChars: 100
    });
    SetTipStorage($('search-adv-button'));
    tips.attach($('search-adv-button'), {
        fixed: false,
        showDelay: 0,
        hideDelay: 0,
		maxTitleChars: 100
    });
    SetTipStorage($('dowload-pdf-button'));
    tips.attach($('dowload-pdf-button'), {
        fixed: false,
        showDelay: 0,
        hideDelay: 0,
		maxTitleChars: 100
    });
    SetTipStorage($('all-pages-button'));
    tips.attach($('all-pages-button'), {
        fixed: false,
        showDelay: 0,
        hideDelay: 0,
		maxTitleChars: 100
    });
    SetTipStorage($('archive-button'));
    tips.attach($('archive-button'), {
        fixed: false,
        showDelay: 0,
        hideDelay: 0,
		maxTitleChars: 100
    });
	SetTipStorage($('first-button'));
    tips.attach($('first-button'), {
        fixed: false,
        showDelay: 0,
        hideDelay: 0,
		maxTitleChars: 100
    });
    SetTipStorage($('prev-button'));
    tips.attach($('prev-button'), {
        fixed: false,
        showDelay: 0,
        hideDelay: 0,
		maxTitleChars: 100
    });
    SetTipStorage($('next-button'));
    tips.attach($('next-button'), {
        fixed: false,
        showDelay: 0,
        hideDelay: 0,
		maxTitleChars: 100
    });
    SetTipStorage($('last-button'));
    tips.attach($('last-button'), {
        fixed: false,
        showDelay: 0,
        hideDelay: 0,
		maxTitleChars: 100
    });
    SetTipStorage($('select-page'));
    tips.attach($('select-page'), {
        fixed: false,
        showDelay: 0,
        hideDelay: 0,
		maxTitleChars: 100
    });

})


/*tips*/
/*http://www.uhleeka.com/blog/2008/11/mootoolstips-convert-v11-to-v12/*/

function SetTipStorage(element) {
    // create an array including the title at
    // position 0 and the text at position 1
    try {
		//alert(element.get('title'));
	    var content = element.get('title').split('::');   

        // use the element storage functionality 
        // to store the title and text for the 
        // specified element
        element.store('tip:title', content[0]);   
        element.store('tip:text', content[1]);
        // remove the element title
        element.title = '';
    }catch(e){
	}

}





/* BEGIN js pages scrolling */

	function pageNumber3 (pagenr) {
		var v_pagenr="00"+pagenr
		v_pagenr=v_pagenr.substring(v_pagenr.length-3,v_pagenr.length)
		return v_pagenr;
	}
		
	function findPosX(obj) {
	    var curleft = 0;
	    if(obj.offsetParent)
	        while(1) 
	        {
	          curleft += obj.offsetLeft;
	          if(!obj.offsetParent)
	            break;
	          obj = obj.offsetParent;
	        }
	    else if(obj.x)
	        curleft += obj.x;
	    return curleft;
	  }
	  
	  	var morph_open 		= false;
		var scroll_open 	= false;
		var current_elid 	= "";
		var current_background = "";

		
		function openMorph(elid){
			document.getElementById("background_"+elid).style.display = "block";
			if (morph_open==true) {
				try{
					clearTimeout(timerMorphOpen);
				} catch (e) {}
			 	colseMorphStartFast(elid);
				current_elid 	= "";
			}
			current_elid = elid;
			timerMorphOpen = setTimeout("openMorphStart(current_elid)",1000);
		}
		

		
		function colseMorph(elid){
					if(current_background != elid){
						document.getElementById("background_"+elid).style.display = "none";
					}
			if(morph_open == true && current_elid == elid){
				timerMorphOpen = setTimeout("colseMorphStart()",2000);
			}
			current_elid = "";
		}
			
		function openMorphStart(elid){
			if(morph_open == false && current_elid == elid &&  current_elid != ""){
				morph_open = true;
				document.getElementById("pageMorphBack").style.display 	=  "block";
				document.getElementById("pageMorph").style.display 		=  "block";
				document.getElementById("pageOverIdImg").style.display 	=  "block";
				var CoSc_offset_position 	=$("ContainerScroller").getOffsets();
				var CoSc_position			=$("ContainerScroller").getPosition();
				//alert (CoSc_offset_position.x +"$$"+CoSc_position.x);
				var offset_position 	=$("ContainerScroller-Main").getOffsets();
				var position			=$("ContainerScroller-Main").getPosition();
				var position_total 		= eval( eval(position.x*1) + eval(offset_position.x*-1) )
				//alert(position_total)
				var leftposition_temp 	= findPosX(document.getElementById("element_"+elid))+position_total; 
				var leftposition 		= leftposition_temp - 80;
				var leftposition1 		= leftposition_temp - 80;			
				var myFx 				= new Fx.Morph('pageMorph').set({
					    'left': leftposition
				});
				var myFx 				= new Fx.Morph('pageMorphBack').set({
					    'left': leftposition1
				});

				document.getElementById("pageOverIdNr").innerHTML 	= elid+"";
				document.getElementById("pageOverIdPdf").href		= "../"+pageNumber3(elid)+"/index.pdf"; 
				document.getElementById("pageOverIdPage").href		= "../"+pageNumber3(elid)+"/"; 
				document.getElementById("pageOverIdImg").src		= "/images/trasp.gif"; 
				document.getElementById("pageOverIdImg").src		= "../"+pageNumber3(elid)+"/page.jpg"; 
				
				var myEffect1 			= new Fx.Morph("pageOverIdImg", {duration: 600, transition: Fx.Transitions.Sine.easeOut});	
			 	myEffect1.start('.endPageOverIdImgStyle');				
				var myEffect 			= new Fx.Morph("pageMorph", {duration: 800, transition: Fx.Transitions.Sine.easeOut});
			 	myEffect.start('.endPageMorphStyle');
				var myEffect1 			= new Fx.Morph("pageMorphBack", {duration: 800, transition: Fx.Transitions.Sine.easeOut});	
			 	myEffect1.start('.endPageMorphBackStyle');
			}
		}
	
		function colseMorphStart(pageid){
				if(morph_open == true){
					var myEffect 		= new Fx.Morph("pageMorph", {duration: 300, transition: Fx.Transitions.Sine.easeOut });
		 			myEffect.start('.startPageMorphStyle');
					var myEffect1 		= new Fx.Morph("pageMorphBack", {duration: 300, transition: Fx.Transitions.Sine.easeOut });
		 			myEffect1.start('.startPageMorphBackStyle');		
					var myEffect1 		= new Fx.Morph("pageOverIdImg", {duration: 300, transition: Fx.Transitions.Sine.easeOut});	
				 	myEffect1.start('.startPageOverIdImgStyle');									
					setTimeout("hideMorph()",300)
				}
		}
		
		function colseMorphStartFast(pageid){
				if(morph_open == true && current_elid != pageid){
					morph_open 						= false;				
					var myEffect 					= new Fx.Morph("pageMorph", {duration: 100, transition: Fx.Transitions.Sine.easeOut });
		 			myEffect.start('.startPageMorphStyle');
					var myEffect1 					= new Fx.Morph("pageMorphBack", {duration: 100, transition: Fx.Transitions.Sine.easeOut });
		 			myEffect1.start('.startPageMorphBackStyle');				
					var myEffect1 					= new Fx.Morph("pageOverIdImg", {duration: 100, transition: Fx.Transitions.Sine.easeOut});	
				 	myEffect1.start('.startPageOverIdImgStyle');			
					$("pageMorph").ClassName		='.startPageMorphStyle';
					$("pageMorphBack").ClassName	='.startPageMorphBackStyle';
					$("pageOverIdImg").ClassName	='.startPageOverIdImgStyle';
					hideMorph()
				}
		}
	
		function hideMorph(pageid){
			document.getElementById("pageMorph").style.display 		=  "none";
			document.getElementById("pageMorphBack").style.display 	=  "none";
			document.getElementById("pageOverIdImg").style.display 	=  "none";
			$("pageMorph").ClassName		='.startPageMorphStyle';
			$("pageMorphBack").ClassName	='.startPageMorphBackStyle';
			$("pageOverIdImg").ClassName	='.startPageOverIdImgStyle';
		}
		
		function pageMorphOver () {
				try{
					clearTimeout(timerMorphOpen);
				} catch (e) {}
		}
	
		function pageMorphOut () {
			if(morph_open == true){
				timerMorphOpen = setTimeout("colseMorphStart()",2000);
			}
			current_elid = "";
		}
		
		//scroll
/*
		window.addEvent('domready', function(){
			var myElement = $('ContainerScroller');
			var myScroller = new Scroller(myElement, {area: 300, velocity: 0.1, fsp: 300});
			myScroller.start();
			myElement.addEvent('mouseover', function(){
						myScroller.start.bind(myScroller);
				});		
			myElement.addEvent('mouseout',  function(){
						myScroller.stop.bind(myScroller);
				});		
		}); 

*/
/* END js pages scrolling */
