/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

function echeck(str) {
		var at="@";
		var dot=".";
		var lat= str.indexOf(at);
		var lstr= str.length;
		var ldot= str.indexOf(dot);
		if (str.indexOf(at)==-1){
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false;
		 }

 		 return true;				
	}



function newsletter(email){
	if (email == '') {
		alert(s_news_empty);
	}
	else if(!echeck(email)){
		alert(s_news_not_valid);
	}
	else {
	$.ajax({
		   type: 'POST',
		   url: '/dinamic/newsletter.php',
		   dataType: 'json',
		   data: {'email': email},
		   success: function (msg) {
			   if (msg.result) {
				  $('<div class="sumir"><p></p></div>').prependTo("#newsletter");
				  $('.sumir p').html(s_news_ok);
				  $('.sumir')
					  .fadeIn('slow')
					  .fadeOut(5000, function() {$(this).remove()});
			  } else {
				  alert(eval(msg.erro));
			  }
			   }
		   });
	}
}

function switchTab(tabEvent, tab) {
	var aba = tabEvent.parent();
	$('#abas').children().removeClass('selected');
	aba.addClass('selected');
	$('.aba').css('display','none');
	$('#'+tab).css('display', 'block');
}


function countdown(year, month, day, hour, minute, format)
         {
			 var Today = new Date();
			 var Todays_Year = Today.getFullYear() - 2000;
			 var Todays_Month = Today.getMonth();
			 
			 //Convert both today's date and the target date into miliseconds.
			 Todays_Date = (new Date(Todays_Year, Todays_Month, Today.getDate(),
									 Today.getHours(), Today.getMinutes(), Today.getSeconds())).getTime();
			 Target_Date = (new Date(year, month - 1, day, hour, minute, 00)).getTime();
			 
			 //Find their difference, and convert that into seconds.
			 Time_Left = Math.round((Target_Date - Todays_Date) / 1000);
			 
			 if(Time_Left < 0)
				Time_Left = 0;
			 
			 switch(format)
				   {
				   case 0:
						//The simplest way to display the time left.
						document.all.countdown.innerHTML = Time_Left + ' seconds';
						break;
				   case 1:
						//More datailed.
					   var  days = Math.floor(Time_Left / (60 * 60 * 24));
	
						Time_Left %= (60 * 60 * 24);
						var hours = Math.floor(Time_Left / (60 * 60));
						Time_Left %= (60 * 60);
						var minutes = Math.floor(Time_Left / 60);
						Time_Left %= 60;
						var seconds = Time_Left;
						
						var dps = 's'; 
						var hps = 's'; 
						var mps = 's'; 
						var sps = 's';
						//ps is short for plural suffix.
						if(days == 1) dps ='';
						if(hours == 1) hps ='';
						if(minutes == 1) mps ='';
						if(seconds == 1) sps ='';
						
						$('#days').html(days + s_dia + dps);
						$('#hours').html(hours + 'h') ;
						$('#minutes').html(minutes + 'm');
						$('#seconds').html(seconds + 's');
						break;
				   default: 
						document.all.countdown.innerHTML = Time_Left + ' seconds';
				   }
				   
			 //Recursive call, keeps the clock ticking.
			 setTimeout('countdown(' + year + ',' + month + ',' + day + ',' + hour + ',' + minute + ',' + format + ');', 1000);
         }


function init() {
	if ($('countdown') != null)
		countdown(9, 11, 19, 18, 0, 1);
	$("a.iframe").fancybox({ frameWidth: 575, frameHeight: 465 });
	
	$("a#single_image").fancybox({ frameWidth: 800, frameHeight: 600 });
	
	//Newsletter
	$('.email').val(s_news_input)
				.focus(
					  function() {
						  $(this).val('');
						  }
					  )
				.blur(
					  function() {
						  if ($(this).val() == '') {
							  $(this).val(s_news_input);
							  }
						  }
					  )
	;
}
