// JavaScript Document

var Site = {

	start: function(){

		new Observer($('searchQuery'), showResults);
        StartClock();
		divResults = new Element( 'div', { 'id': 'divResults' }).inject( $('wrapper') );
		var myFx = new Fx.Tween(divResults);
		myFx.set('opacity','0');

		function showResults (){
			//myFx.set('opacity','0');

			if(this.value != ""){
				var jsonRequest = new Request.JSON({
					url: '/_ajax/search.php',
					onComplete: function(returnObj){

						if(returnObj.results.length){
							htmlJS ='<ul>';
							for($i=0; $i<returnObj.results.length; $i++){
								evento = returnObj.results[$i];
								htmlJS +='<li>';
								if(evento.img)
									htmlJS +='<img alt="'+evento.titulo+'" src="'+evento.img+'" />';
								htmlJS +='<h2>'+evento.titulo+'</h2>';
								htmlJS +='<a href="evento.php?id='+evento.id+'" title="">ver mais</a>';
								htmlJS +='</li>';
							}
							htmlJS +='</ul>';
						}


						divResults.setProperty('html', htmlJS);
						myFx.start('opacity', '1');


					}.bindWithEvent(this)
				}).get({'query': this.value });
			} else {
				myFx.start('opacity', '0');
			}
		};


		$('searchQuery').addEvent('blur', function(){
			myFx.start('opacity', '0');
		});
        banSlide = new bannersSlide({ wrapper: 'banners'});
		promo = new promos({ wrapper: 'eventScroller'});


        // calendar
        var data = new Date();
        callCalendar(data.getMonth()+1,data.getFullYear());

        

	}


};
function callCalendar (mes, ano){
    $('calendarWrapper').set('html','');
    var myHTMLRequest = new Request.HTML({
    url: "../_ajax/calendar.php",
    evalScripts: true,
    update:$('calendarWrapper')
//    onSuccess: function (responseTree, responseElements, responseHTML, responseJavaScript){
//        $('calendarWrapper').set('html',responseHTML);
//        var myTips = new Tips('.caltooltip');
//    }
    }).get({'month': mes,'year':ano});
};

var clockID = 0;

function UpdateClock() {
   if(clockID) {
      clearTimeout(clockID);
      clockID  = 0;
   }

   var tDate = new Date();

   var currentHours = tDate.getHours ( );
   var currentMinutes = tDate.getMinutes ( );
   var currentSeconds = tDate.getSeconds ( );

   currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
   currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;


   $('clock').setProperty('html', ""
                                   + currentHours + ":"
                                   + currentMinutes + ":"
                                   + currentSeconds );

   clockID = setTimeout("UpdateClock()", 1000);
}
function StartClock() {
   clockID = setTimeout("UpdateClock()", 500);
}

function KillClock() {
   if(clockID) {
      clearTimeout(clockID);
      clockID  = 0;
   }
}

window.addEvent('domready', Site.start);