function placeBanner(idSrc, idDest, first){
	var divBannerContainerSrc = document.getElementById(idSrc);
	var divBannerContainerDest = document.getElementById(idDest);
	if(!first)
		divBannerContainerDest.appendChild(divBannerContainerSrc);
	else{
		//htmlSrc = divBannerContainerSrc.innerHTML;
		if(document.getElementById('testo___Frame'))
			return;
	}	
	divBannerContainerSrc.style.display = "block";
	divBannerContainerDest.style.display = "block";
}

function Linka(blocco){
    var destinazione = blocco.getElementsByTagName('a');
    if(destinazione[0] && destinazione[0].href!=""){
      location.href=destinazione[0].href;
    }
}
function RollOn(blocco){
    var titolo = blocco.getElementsByTagName('a');
    if(titolo[0] && titolo[0].href!=""){
      titolo[0].style.textDecoration="underline";
      blocco.style.cursor="pointer";
    }
}
function RollOut(blocco){
    var titolo = blocco.getElementsByTagName('a');
    if(titolo[0] && titolo[0].href!=""){
      titolo[0].style.textDecoration="none";
    }
}
function RandomNumber(){
    var randomnumber=Math.floor(Math.random()*16);
    return randomnumber;
}
function RandomWeight(){
    var links = document.getElementById('Leonardo_TagCloud').getElementsByTagName('a');
    for(var i=0;i<links.length;i++){
		links[i].className = 'Peso-'+RandomNumber();
	}
}

function showDiv2(divObj){
	if(divObj)
		$(divObj).slideDown();
}

function Leonardo_Ricerca(){
    nptValue = document.getElementById('Leonardo_nptCerca').value;
    if(nptValue && nptValue!="Cerca..."){
        return true;
    }else{
        alert('Inserire almeno una parola chiave da ricercare');
        document.getElementById('Leonardo_nptCerca').value = "Cerca...";
        document.getElementById('Leonardo_nptCerca').focus();
        return false;
    }
}


function cornerImages(){
	//$(".Round").corners("30px");
	$('.Rounded').corners("10px");

}


function ShowInfo(link){
    link.blur();
    nextDIV = node_after(link.parentNode.parentNode);
    if (nextDIV.style.display == 'none') {
      $(nextDIV).slideDown('slow');
      link.innerHTML = 'Nascondi';
      link.parentNode.parentNode.parentNode.className = '';
    } else {
      $(nextDIV).slideUp();
      link.innerHTML = 'Visualizza';
      link.parentNode.parentNode.parentNode.className = 'closed';
    }
}


/**
 * Determine whether a node's text content is entirely whitespace.
 *
 * @param nod  A node implementing the |CharacterData| interface (i.e.,
 *             a |Text|, |Comment|, or |CDATASection| node
 * @return     True if all of the text content of |nod| is whitespace,
 *             otherwise false.
 */
function is_all_ws( nod )
{
  // Use ECMA-262 Edition 3 String and RegExp features
  return !(/[^\t\n\r ]/.test(nod.data));
}


/**
 * Determine if a node should be ignored by the iterator functions.
 *
 * @param nod  An object implementing the DOM1 |Node| interface.
 * @return     true if the node is:
 *                1) A |Text| node that is all whitespace
 *                2) A |Comment| node
 *             and otherwise false.
 */

function is_ignorable( nod )
{
  return ( nod.nodeType == 8) || // A comment node
         ( (nod.nodeType == 3) && is_all_ws(nod) ); // a text node, all ws
}

/**
 * Version of |previousSibling| that skips nodes that are entirely
 * whitespace or comments.  (Normally |previousSibling| is a property
 * of all DOM nodes that gives the sibling node, the node that is
 * a child of the same parent, that occurs immediately before the
 * reference node.)
 *
 * @param sib  The reference node.
 * @return     Either:
 *               1) The closest previous sibling to |sib| that is not
 *                  ignorable according to |is_ignorable|, or
 *               2) null if no such node exists.
 */
function node_before( sib )
{
  while ((sib = sib.previousSibling)) {
    if (!is_ignorable(sib)) return sib;
  }
  return null;
}

/**
 * Version of |nextSibling| that skips nodes that are entirely
 * whitespace or comments.
 *
 * @param sib  The reference node.
 * @return     Either:
 *               1) The closest next sibling to |sib| that is not
 *                  ignorable according to |is_ignorable|, or
 *               2) null if no such node exists.
 */
function node_after( sib )
{
  while ((sib = sib.nextSibling)) {
    if (!is_ignorable(sib)) return sib;
  }
  return null;
}

/**
 * Version of |lastChild| that skips nodes that are entirely
 * whitespace or comments.  (Normally |lastChild| is a property
 * of all DOM nodes that gives the last of the nodes contained
 * directly in the reference node.)
 *
 * @param sib  The reference node.
 * @return     Either:
 *               1) The last child of |sib| that is not
 *                  ignorable according to |is_ignorable|, or
 *               2) null if no such node exists.
 */
function last_child( par )
{
  var res=par.lastChild;
  while (res) {
    if (!is_ignorable(res)) return res;
    res = res.previousSibling;
  }
  return null;
}

/**
 * Version of |firstChild| that skips nodes that are entirely
 * whitespace and comments.
 *
 * @param sib  The reference node.
 * @return     Either:
 *               1) The first child of |sib| that is not
 *                  ignorable according to |is_ignorable|, or
 *               2) null if no such node exists.
 */
function first_child( par )
{
  var res=par.firstChild;
  while (res) {
    if (!is_ignorable(res)) return res;
    res = res.nextSibling;
  }
  return null;
}

/**
 * Version of |data| that doesn't include whitespace at the beginning
 * and end and normalizes all whitespace to a single space.  (Normally
 * |data| is a property of text nodes that gives the text of the node.)
 *
 * @param txt  The text node whose data should be returned
 * @return     A string giving the contents of the text node with
 *             whitespace collapsed.
 */
function data_of( txt )
{
  var data = txt.data;
  // Use ECMA-262 Edition 3 String and RegExp features
  data = data.replace(/[\t\n\r ]+/g, " ");
  if (data.charAt(0) == " ")
    data = data.substring(1, data.length);
  if (data.charAt(data.length - 1) == " ")
    data = data.substring(0, data.length - 1);
  return data;
}


//window.onload = function(){RandomWeight(0)}



function ahah(url,target,delay, function_extra, post){
	var type = "GET";
	if(window.XMLHttpRequest){
		  var req=new XMLHttpRequest();
	}else if(window.ActiveXObject){
		  var req=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if(req!=undefined){
		if(post)
			type = "POST";
		req.open(type,url,true);
		if(post)
			req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.onreadystatechange=function(){
			ahahDone(req, url,target,delay, function_extra);
		};
		
		req.send(post);
	}
}
function ahahDone(req, url,target,delay, function_extra){
	if(req.readyState==4)
	{
		if(req.status==200)
		{
			if(target)
			{
				document.getElementById(target).innerHTML='';
				document.getElementById(target).innerHTML=req.responseText;
				//$("#"+target).show();
			}
				
			if (function_extra)
				setTimeout(function_extra, 100);
		}
	}
}


function changeDivVisibility(divId){
	var div = document.getElementById(divId);
	if(div.style.display == "none" || divVisibility == 0)
	{
		divVisibility = 1;
		div.style.display = "";
	}
	else
	{
		div.style.display = "none";
	}
}


function showDiv(divId){
}


function setTooltip(objTooltipCondition, position, offset, effect, tip, direction, opacity){
	// select all desired input fields and attach tooltips to them
	$(objTooltipCondition).tooltip({
		// place tooltip on the right edge
		position: position,
	
		// a little tweaking of the position
		offset: offset,
	
		// use the built-in fadeIn/fadeOut effect
		effect: effect,
	
		// custom opacity setting
		opacity: opacity,
	
		lazy: false,
				
		// use this single tooltip element
		tip: tip
	
		}).dynamic( {
		
			// customized configuration on bottom edge
			bottom: {
			
				// slide downwards
				direction: direction,
				
				// bounce back when closed
				bounce: true
			}
		});
};	


function refreshPage(sec){	
	if(!sec)
		return;
	setTimeout( "window.location.reload()", sec*1000 );
}


function checkPrenotazioniNuove(url, tot_prenotazioni_attesa_now){
	if(!url || isNaN(tot_prenotazioni_attesa_now))
		return;
		
	$.ajax({
	   type: "GET",
	   url: url,
	   dataType: "json",
	   success: function(msg){
	    	if(msg && msg.tot_prenotazioni_attesa != tot_prenotazioni_attesa_now)
	    		window.location.reload();	
	   }
	 });
}


function getMouseCoordinate(e) {
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	
	coordinate = new Array();
	coordinate['x']=posx;
	coordinate['y']=posy;
	return coordinate;
}


function timePicker(timeStart, timeEnd, step, callback, startTime, endTime){
	if(!step)
		step = 30;
	if(!startTime)
		startTime = "0:00";
	if(!endTime)
		endTime = "23:45";
		
	// An example how the two helper functions can be used to achieve 
	// advanced functionality.
	// - Linking: When changing the first input the second input is updated and the
	//   duration is kept.
	// - Validation: If the second input has a time earlier than the firs input,
	//   an error class is added.
	
	// Use default settings
	$("#"+timeStart+", #"+timeEnd).timePicker({step:step, startTime:startTime, endTime:endTime});
	    
	// Store time used by duration.
	var oldTime = $.timePicker("#"+timeStart).getTime();
	
	// Keep the duration between the two inputs.
	$("#"+timeStart).change(function() {
		
	  if ($("#"+timeEnd).val()) { // Only update when second input has a value.
	    // Calculate duration.
	    var duration = ($.timePicker("#"+timeEnd).getTime() - oldTime);
	    var time = $.timePicker("#"+timeStart).getTime();
	    // Calculate and update the time in the second input.
	    $.timePicker("#"+timeEnd).setTime(new Date(new Date(time.getTime() + duration)));
	    oldTime = time;
	  }
	  eval(callback);
	});
	// Validate.
	$("#"+timeEnd).change(function() {
	  if($.timePicker("#"+timeStart).getTime() > $.timePicker(this).getTime()) {
	    $(this).addClass("error");
	  }
	  else {
	    $(this).removeClass("error");
	  }
	  eval(callback);
	});
}


function openBoxSelectSaleproveUserToUse(){
	$("#box").fancybox({
		'opacity'		: true,
		'width'				: '65%',
		'height'			: '100%',
		'autoScale'			: false,
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'easingIn'      	: 'easeOutBack',
		'easingOut'     	: 'easeInBack',
		'type'				: 'iframe',
		'showCloseButton'	: false,
		'hideOnOverlayClick': false,
		'centerOnScroll'	: true,
		'overlayOpacity'	: 0.35
	});
	setTimeout("$('#box').trigger('click');", 3000);
}


function closeBoxSelectSaleproveUserToUse(){
	$.fancybox.close();
}


function loadMenuNavigation() {
	/**
	* for each menu element, on mouseenter, 
	* we enlarge the image, and show both sdt_active span and 
	* sdt_wrap span. If the element has a sub menu (sdt_box),
	* then we slide it - if the element is the last one in the menu
	* we slide it to the left, otherwise to the right
	*/
    $('#sdt_menu > li').bind('mouseenter',function(){
		var $elem = $(this);
		$elem.find('img')
			 .stop(true)
			 .animate({
				'width':'150px',
				'height':'140px',
				'left':'10px'
			 },400,'easeOutBack')
			 .andSelf()
			 .find('.sdt_wrap')
		     .stop(true)
			 .animate({'top':'140px'},500,'easeOutBack')
			 .andSelf()
			 .find('.sdt_active')
		     .stop(true)
			 .animate({'height':'160px'},300,function(){
			var $sub_menu = $elem.find('.sdt_box');
			if($sub_menu.length){
				var left = '160px';
				if($elem.parent().children().length == $elem.index()+1)
					left = '-150px';
				$sub_menu.show().animate({'left':left},200);
			}	
		});
	}).bind('mouseleave',function(){
		var $elem = $(this);
		var $sub_menu = $elem.find('.sdt_box');
		if($sub_menu.length)
			$sub_menu.hide().css('left','0px');
		
		$elem.find('.sdt_active')
			 .stop(true)
			 .animate({'height':'0px'},300)
			 .andSelf().find('img')
			 .stop(true)
			 .animate({
				'width':'0px',
				'height':'0px',
				'left':'85px'},400)
			 .andSelf()
			 .find('.sdt_wrap')
			 .stop(true)
			 .animate({'top':'10px'},500);
	});
}
