$(document).ready(function(){
	
	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;
	});
	
	$('a[rel*=facebox]').facebox();
	
	$('#cForm,#nForm').bind('submit', function() {
		var form = $(this);
		var formParent = form.parents('#formHolder');
		$(this).ajaxSubmit({
			beforeSubmit: function() {
				formParent.find('#log_res').hide();
				if(formParent.find(".loading").length > 0){
					formParent.find('.button-submit').hide();
					formParent.find(".loading").show();
				}
			},
			success: function(response) {
				var php = response.split('<|>');
				formParent.find('#log_res').html(php[0]);
				if(formParent.find(".loading").length > 0){
					formParent.find('.button-submit').show();
					formParent.find(".loading").hide();
				}
				formParent.find('#log_res').show();
				form.find("input[name="+php[1]+"], textarea[name="+php[1]+"]").focus().val('');
				if(php[1]=='OKK-OKK') {
					$('.newsltr').hide();
					formParent.find('#log_res').css('margin-left', 0);
					formParent.find('#log_res').css('text-align', 'center');
					formParent.find('#log_res').css('padding-bottom', '10px');
				}
			}
		});
		return false; // <-- important!
	});
	
	$('#nlForm').bind('submit', function() {
		var form = $(this);
		var formParent = form.parents('#formHolder');
		$(this).ajaxSubmit({
			beforeSubmit: function() {
				formParent.find('#nltr_res').hide();
				if(formParent.find(".loading").length > 0){
					formParent.find('.button-submit').hide();
					formParent.find(".loading").show();
				}
			},
			success: function(response) {
				var php = response.split('<|>');
				formParent.find('#nltr_res').html(php[0]);
				form.find("input[name="+php[1]+"], textarea[name="+php[1]+"]").focus().val('');
				if(php[1]=='OKK-OKK') {
					if(php[2] != undefined){
						top.location = php[2];
					}else{
						form.clearForm().hide();
						formParent.find('#log_res').css('margin-left', 0);
						formParent.find('#log_res').css('text-align', 'center');
					}
				}
				formParent.find('#nltr_res').show();
				if(formParent.find(".loading").length > 0){
					formParent.find('.button-submit').show();
					formParent.find(".loading").hide();
				}
			}
		});
		return false; // <-- important!
	});
	
	$('.warn').click(function() {
		if(confirm("Confirma o cancelamento da subscrição?")){
			var form = $(this).parents('form:first');
			var formParent = form.parents('#formHolder');
			$(this).parents('form:first').ajaxSubmit({
			beforeSubmit: function() {
				formParent.find('#nltr_res').hide();
				if(formParent.find(".loading").length > 0){
					formParent.find('.button-submit').hide();
					formParent.find(".loading").show();
				}
			},
			success: function(response) {
				var php = response.split('<|>');
				formParent.find('#nltr_res').html(php[0]);
				form.find("input[name="+php[1]+"], textarea[name="+php[1]+"]").focus().val('');
				if(php[1]=='OKK-OKK') {
					top.location = php[2];
				}
				formParent.find('#nltr_res').show();
				if(formParent.find(".loading").length > 0){
					formParent.find('.button-submit').show();
					formParent.find(".loading").hide();
				}
			}
		});
			return false;
		}else{
			return false;
		}
	});
	
	/* autoclear function for inputs */
	$('.autoclear').live('click focus',
	function() {
	if (this.value == this.defaultValue) {
	this.value = '';
	}
	}
	);
	$('.autoclear').blur(
	function() {
	if (this.value == '') {
	this.value = this.defaultValue;
	}
	}
	);
	
	$('.paginacao a').corner();
	
	$('a.btnMap').live('click', function(){
		$('#facebox').find('.content iframe').attr('src', $(this).attr('href'));
		return false;
	});
	
	$('a.skip_intro').click(function(){
		$.post('/includes/scripts/videojs/skip.php', { skip_intro: true, url: $(this).attr('href') }, function(redirect){
			if(redirect)
				top.location = redirect;
		});
		return false;
	});
})

