jQuery(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;
	});
	
	$('#cForm, #mForm').bind('submit', function() {
		var form = $(this);
		$(this).ajaxSubmit({
			beforeSubmit: function() {
				$('div#log_res').hide();
				$(".loading").show();
				form.find("input[type=text], textarea").css('border', '1px solid #cccccc');
			},
			success: function(response) {
				var php = response.split('<|>');
				$('div#log_res').html(php[0]);
				$(".loading").hide();
				$('div#log_res').show();
				form.find("input[name="+php[1]+"], textarea[name="+php[1]+"]").css('border', '1px solid #ef8b68').focus();
				if(php[1]=='OKK-OKK') {
					form.clearForm().hide();
					$('div#log_res').css('margin-left', 0);
					$('div#log_res').css('text-align', 'center');
				}
			}
		});
		return false; // <-- important!
	});
	
	$('.piro_close').live('click', function(){
		$('#piroForm').find("input[type=text], textarea").css('border', '1px solid #cccccc');
		$('#piroForm').find("#log_res").html('');
	});
	
	$('span.arrow').each(function(){
		if($(this).hasClass('closed'))
			$(this).parent('li').find('ul').hide();
	});
	
	$('li span.line a').live('click', function(){
		// Explode url
		var urlparts = $(this).attr('href').split('/');
		// Make selected
		if($(this).hasClass('current'))
			$(this).removeClass('current');
		else
			$(this).addClass('current');
		// Get parent <li> AND this <a>
		var li = $(this).parents('li:first');
		var a = $(this);
		// Get child cats of selected cat
		$.post('http://' + urlparts[2] + '/catalogo_printcats.php', {catid: $(this).attr('id')}, function(response){
			li.find('ul span.arrow').hide();
			
			// Toogle arrow
			if(response.length > 0)
			{
				if(li.find('span.arrow:first').hasClass('closed'))
					li.find('span.arrow:first').removeClass('closed').addClass('opened');
				else
					li.find('span.arrow:first').removeClass('opened').addClass('closed');
			}
			
			// Check if exists <ul> child
			if(li.find('ul').length > 0)
			{
				li.find('ul:first').slideToggle('fast', function(){
					li.find('ul span.arrow').show();
				});
			}else{
				// check if response has <ul>
				if(response.length > 0)
				{
					li.append(response);
					
					li.find('ul span.arrow').slideDown('fast');
				}else{
					top.location = a.attr('href');
				}
			}
			return false;
		});
		return false;
	});
    
    $('.imgsbox').cycle({ fx: 'fade', pager: '#pager', timeout: 7000 });
    
	$('.tip').CreateBubblePopup();
	
	$('.tip').mouseover(function(){
		$(this).ShowBubblePopup({
			innerHtml: $(this).closest('a').next('div').html(),
			themeName: 	'all-black',
			themePath: 	'/includes/imgs',
			alwaysVisible: true,
			openingSpeed: 250,
			selectable: true
		});
	});
	
	$().piroBox_ext({
		piro_speed : 700,
		bg_alpha : 0.1,
		piro_scroll : true //pirobox always positioned at the center of the page
    });
	
	$('.refresh').live('click', function(){
		$(this).parent('div').find('img').attr('src', $(this).parent('div').find('img').attr('src') + '?' + Math.random());
		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;
}
