jQuery(document).ready(function($){
	
	$('.farmaciasbtn').live('click', function(){
		$('#farm_conteudo').html('<div id="loading">Por favor, aguarde!</div>');
		jQuery.facebox({ div: '#facebox_farmacias' });
		var x = $.post($(this).attr('href'),function(data){
			$('.content').find('#loading').remove();
			$('.content').html(data);
			return false;
		});
		$(document).bind('afterClose.facebox', function() {
			$('#farm_conteudo').find('#loading').remove();
			x.abort();
		});
		return false;
	});
	
	$('#tempo').html('<div id="loading"></div>');
	
	$('a[rel*=facebox]').facebox();
	
	$('a[rel*=galeria]').colorbox({ maxWidth: '500px' });
	
	var section = new Array('.entrada','.noticia'); 
	  section = section.join(',');

	  // Reset Font Size
	  var originalFontSize = $(section).css('font-size');
	  $(".treset").click(function(){
	    $(section).css('font-size', originalFontSize); 
	  });

	  // Increase Font Size
	  $(".tmais").click(function(){
	    var currentFontSize = $(section).css('font-size');
	    var currentFontSizeNum = parseFloat(currentFontSize, 10);
	    var newFontSize = currentFontSizeNum*1.2;
	    $(section).css('font-size', newFontSize);
	    return false;
	  });

	  // Decrease Font Size
	  $(".tmenos").click(function(){
	    var currentFontSize = $(section).css('font-size');
	    var currentFontSizeNum = parseFloat(currentFontSize, 10);
	    var newFontSize = currentFontSizeNum*0.8;
	    $(section).css('font-size', newFontSize);
	    return false;
	  });
	
	$('body').find('.slideshow').each(function(){
		var $este = $(this);
		$este.cycle({
				fx:     'fade',
				speed:  'slow',
				timeout: 8000,
				pager:  $este.next(),
				pagerAnchorBuilder: paginate
		});
	});
	
	function paginate(ind, el)
	{
		return '<p class="square cor' + (ind + 1) + '"><a class="cor' + (ind + 1) + '">' + el.id + '</a></p>';
	}
	
	$('#cForm').bind('submit', function() {
		$(this).ajaxSubmit({
			beforeSubmit: function() {
				$('#log_res').hide();
				$(".loading").show();
			},
			success: function(response) {
				var php = response.split('<|>');
				$('#log_res').html(php[0]);
				$(".loading").hide();
				$('#log_res').show();
				$("input[name="+php[1]+"], textarea[name="+php[1]+"]").focus();
				if(php[1]=='OKK-OKK') {
					$('#formulario').html(php[0]);
				}
			}
		});
		return false; // <-- important!
	});
	
	//When page loads...
	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content

	//On Click Event
	$("ul.tabs li").click(function() {

		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});
	
	function URLEncode( plaintext )
	{
	  // The Javascript escape and unescape functions do not correspond
	    // with what browsers actually do...
	    var SAFECHARS = "0123456789" +					// Numeric
			        "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
			        "abcdefghijklmnopqrstuvwxyz" +
			        "-_.!~*'()";					// RFC2396 Mark characters
	    var HEX = "0123456789ABCDEF";

	    var encoded = "";
	    for (var i = 0; i < plaintext.length; i++ ) {
	      var ch = plaintext.charAt(i);
	        if (ch == " ") {
	          encoded += "+";				// x-www-urlencoded, rather than %20
	      } else if (SAFECHARS.indexOf(ch) != -1) {
	          encoded += ch;
	      } else {
	          var charCode = ch.charCodeAt(0);
	        if (charCode > 255) {
	            alert( "Unicode Character '" 
	                          + ch 
	                          + "' cannot be encoded using standard RFC2396 encoding.\n" +
		                  "(URL encoding only supports 8-bit characters.)\n" +
				          "A space (+) will be substituted." );
		        encoded += "+";
	        } else {
		        encoded += "%";
		        encoded += HEX.charAt((charCode >> 4) & 0xF);
		        encoded += HEX.charAt(charCode & 0xF);
	        }
	      }
	    } // for

		return encoded;
	};
	
	$('form[name=pesquisaForm]').submit(function(){
		var form = $(this);
		$.post('http://' + $(this).attr('action').split(/\/+/g)[1] + '/pesquisa_cont.php', $(this).serialize(), function(response){
				top.location = response.replace(form.find('input[name=key]').val(), URLEncode(form.find('input[name=key]').val()));
		});
		return false;
	});
	
});

function hideText(id, valor, fixvalor){
	if(fixvalor == valor)
		id.value = '';
}

function showText(id, valor, fixvalor){
	if(fixvalor == valor && id.value == '')
		id.value = valor;
}
