var legCor = '';
$(function() {
    
    $('.selecao_produto .form_l input').change(function() {
	    $('.form_l label').removeClass('active');
    	$(this).prev().addClass('active');
        legCor = $(this).prev().text();
	});
        
    $('.selecao_produto .form_l label').hover(function(){
        $('.legend').text($(this).text()).stop().fadeTo('fast', 1);
    },function(){
        $('.legend').text(legCor);
    });
    
    $('.figure_html5').cycle({
		fx: 'fade'
	});
	
    $('#g').attr('checked', 'checked');
    $('.input_tamanho').bind('change', function(){
        $('[name=id_produto]').val($(this).attr('ref'));

        $('#cores_pequeno, #cores_grande').toggle();
        
        if($(this).attr('id') == 'p'){
            var op = $('#cores_grande :checked').removeAttr('checked').attr('id').replace('g_', 'p_');
            $('#cores_pequeno :checked').removeAttr('checked');
            if($('#'+op).length){
               $('#'+op).attr('checked', 'checked').trigger('change'); 
            }else{
                $('#cores_pequeno input').eq(0).attr('checked', 'checked').trigger('change');
            }
            $('.tamanho_form').css('background-position', 'left bottom');
        }else{
            var op = $('#cores_pequeno :checked').removeAttr('checked').attr('id').replace('p_', 'g_');
            $('#cores_grande :checked').removeAttr('checked');
            if($('#'+op).length){
               $('#'+op).attr('checked', 'checked').trigger('change'); 
            }else{
                $('#cores_grande input').eq(0).attr('checked', 'checked').trigger('change');
            }
             
            $('.tamanho_form').css('background-position', '0 0');
        }
    });
    $('#cores_grande input').eq(0).attr('checked', 'checked').trigger('change').prev().trigger('mouseover');
});

$.fn.charCounter = function (max, settings) {
		max = max || 100;
		settings = $.extend({
			container: "<span></span>",
			classname: "charcounter",
			format: "%1",
			pulse: false,
			delay: 0
		}, settings);
		var p, timeout;
		
		function count(el, container) {
			el = $(el);
			if (el.val().length > max) {
			    el.val(el.val().substring(0, max));
			    if (settings.pulse && !p) {
			    	pulse(container, true);
			    };
			};
			if (settings.delay > 0) {
				if (timeout) {
					window.clearTimeout(timeout);
				}
				timeout = window.setTimeout(function () {
					container.html(settings.format.replace(/%1/, (max - el.val().length)));
				}, settings.delay);
			} else {
				container.html(settings.format.replace(/%1/, (max - el.val().length)));
			}
		};
		
		function pulse(el, again) {
			if (p) {
				window.clearTimeout(p);
				p = null;
			};
			el.animate({ opacity: 0.1 }, 100, function () {
				$(this).animate({ opacity: 1.0 }, 100);
			});
			if (again) {
				p = window.setTimeout(function () { pulse(el) }, 200);
			};
		};
		
		return this.each(function () {
			var container = (!settings.container.match(/^<.+>$/)) 
				? $(settings.container) 
				: $(settings.container)
					.insertAfter(this)
					.addClass(settings.classname);
			$(this)
				.bind("keydown", function () { count(this, container); })
				.bind("keypress", function () { count(this, container); })
				.bind("keyup", function () { count(this, container); })
				.bind("focus", function () { count(this, container); })
				.bind("mouseover", function () { count(this, container); })
				.bind("mouseout", function () { count(this, container); })
				.bind("paste", function () { 
					var me = this;
					setTimeout(function () { count(me, container); }, 10);
				});
			if (this.addEventListener) {
				this.addEventListener('input', function () { count(this, container); }, false);
			};
			count(this, container);
		});
	};
jQuery.fn.focusInOut = function(params) {
	var options = {
		
		textIn: '',
		textOut: ''

	}
	op = jQuery.extend(options, params);

   return this.each(function(){
		//initializing variables
		var $self = jQuery(this);
		 $self.attr('textIn',op.textIn);
		 $self.attr('textOut',op.textOut);
		 if($self.val()=='')
		 $self.val(op.textIn);
		 $self.focus(function (){ if(jQuery.trim($(this).val()) == jQuery.trim($(this).attr('textIn'))) $(this).val(jQuery.trim($(this).attr('textOut')))});
		 $self.blur(function (){ if(jQuery.trim($(this).val()) == jQuery.trim($(this).attr('textOut'))) $(this).val(jQuery.trim($(this).attr('textIn')))});
   });
};

$(document).ready(function() {
	$('.box2, .box1, .listaProdutos li').prepend('<div class="box_bg_bottom"><span class="bg_left"></span><span class="bg_right"></span></div>');
	
	$('#frmsendMailing').submit(function(){
		var filtro = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
		if(!filtro.test($('#txtMailing').val())){
			$('.msg',this).html('<span></span>E-mail inválido!').addClass('erro').removeClass('sucesso').css('display','block');
		}else{
			$.post($(this).attr('action'),$(this).serializeArray(),function(data){

			});
			$('.msg',this).text('Cadastrado com sucesso!').addClass('sucesso').removeClass('erro').css('display','block');
			$('input[type=text]',this).val('');
		}
		return false;
	});
	 $('a[rel*=facebox]').facebox();
});



