
// init
$(document).ready(function(){
  startClock("clock");
  startDate("date")
});

// functions
function toggle(id){
  var el = document.getElementById(id);
  if(el!=null){
  	if(el.style.display=='none'){
  	  el.style.display = '';
  	} else {
  	  el.style.display = 'none';
  	}
  }
}

var toggled_array = Array(), timeout = 0; 
function toggleEffect(element, effect, depth){
  setTimeout('toggleEffect_(\''+element+'\', \''+effect+'\', \''+depth+'\')',timeout);
  timeout+=500; setTimeout('timeout-=500;',500);
} function toggleEffect_(element, effect, depth){
  // check if element not empty
  var e = document.getElementById(element);
  //if(e.childNodes.length==0) return; // empty node
  if(e==null) return; // empty node
  // toggle previous elements off
  var toggled = null;
  while(element!=toggled && toggled_array.length>0 && depth<toggled_array.length) {
    //toggled = toggled_array.pop();
    toggled = toggled_array[toggled_array.length-1];
    //alert("POPED » " + toggled);
	toggleStyle(toggled, effect, {duration: 0.5});
	toggled_array.pop();
    if(element==toggled) return;
  } // toggle element on
  toggleStyle(element, effect, {duration: 0.5});
  if(depth>=toggled_array.length){
	  toggled_array.push(element); // save current element
	  //alert("PUSHED » " + element);
  }
}
function toggleStyle(id){
  var el = document.getElementById(id);
  if(el!=null){
  	if(el.style.display=='none'){
  	  //el.style.display = '';
  	  $(el).slideDown();
  	  //changeTabStyle(id+'tab','down');
  	} else {
  	  el.style.display = 'none';
  	  $(el).slideUp();
  	  //changeTabStyle(id+'tab','up');
  	}
  }
}
/*function changeTabStyle(id, direction){
  var el = document.getElementById(id);
  if(el!=null){
  	if(direction=='up'){
  	  el.style.background = 'url(themes/protravel/imagens/arrow_down_grey.gif) no-repeat top right';
  	} else {
  	  el.style.background = 'url(themes/protravel/imagens/arrow_up_grey.gif) no-repeat top right';
  	}
  }
}*/

function display(id){
  var el = document.getElementById(id);
  if(el!=null){
  	if(el.style.display=='none'){
  	  el.style.display = '';
  	}
  }
}
function hide(id){
  var el = document.getElementById(id);
  if(el!=null){
  	if(el.style.display!='none'){
  	  el.style.display = 'none';
  	}
  }
}

function startClock(id){
	updateClock(id);
	setInterval("updateClock('"+id+"')",1000);
}

function startDate(id){
	updateDate(id);
	setInterval("updateDate('"+id+"')",60000);
}

function updateClock(id){
	document.getElementById(id).innerHTML=getTime();
}

function updateDate(id){
	document.getElementById(id).innerHTML=getDate();
}

function getDateTime(){
	return getTime() + ' ' + getDate(); 
}

function getDate(){ 
	var now = new Date(); 
	return now.getFullYear()+'-'+pad(now.getMonth()+1,2)+'-'+pad(now.getDate(),2); 
}

function getTime(){ 
	var now = new Date(); 
	return pad(now.getHours(),2)+':'+pad(now.getMinutes(),2)+':'+pad(now.getSeconds(),2); 
}

function pad(str, n){ 
	var res = ''+str; 
	while(res.length<n) res='0'+res; 
	return res; 
}

