
var Produtos = {
		filtrosUL : $('#filtro'),
		
		produtosUL : $('#list-produtos'),
		produtosBalaoElem: $('.balao-info-produto.tooltip', this.produtosUL),
		
		Slide : {
			scrollItems : ( $('#section-produtos').is('.produtos-filtrados') ) ? 3 : 5,
			init : function () {
				Produtos.produtosUL.jcarousel({
					scroll: this.scrollItems,		
					easing: 'easeOutExpo', // Back Bounce Circ Expo Quad
					animation: 1500
				});
			}
		},
		
		Tooltips : {

			init : function() {
				Produtos.produtosBalaoElem.hide();
				this.bindHover();
			},
			
			bindHover : function() {
				Produtos.produtosBalaoElem.each(function(){
					var $this = $(this);
					$this.parent().hover(function() {
						$this.show();									 
					},function() {
						$this.hide();	
					});
				});
			}
		},
		
		Filtros : { 
			init : function() {
			
				if ( ! $('#section-produtos').is('.produtos-filtrados') )
				{
					Produtos.Filtros.hideOptions();
				}
			
				$('.options select', Produtos.filtrosUL).hide().each(function() {
					Produtos.Filtros.extractOptions($(this));
				});
					
				$('#filtro label').bind('click',function(){
					if ( ! $('#filtro .fechar').is(':visible') )
					{
						Produtos.Filtros.showOptions();
					}
				});
				
				$('#section-produtos .fechar').bind('click', function(){
					Produtos.Filtros.hideOptions();
					$('select', Produtos.filtrosUL).selectOptions('');
				});
			},
			
			hideOptions : function () {
				$('#section-produtos').find('.fechar, .set, .options').hide().end();
			},
			
			showOptions : function() {
				$('#section-produtos').find('.fechar, .set').show().end().find('.options').fadeIn().end();
			},

			extractOptions : function(elemento) {
				var eleParent = elemento.parent();
				$('option', elemento).each(function(index, option){
					if ( $(option).val() != '' )
					{
					    $("<a />", {
					       href:"javascript: void(0)",
					       text: ( elemento.attr('id') == 'filtro_cores' ) ? option.value : option.innerHTML,
					       rel: option.value,
			    		   title: ( elemento.attr('id') == 'filtro_cores' ) ? option.innerHTML : option.value
                        })
                        .addClass(( elemento.attr('id') == 'filtro_cores' ) ? 'imgrpl color' : '')
					    .bind('click', function(){
					    	if ( $(this).is('.selected') ) 
					    	{
					    		$(this).removeClass('selected');
					    		$(elemento).selectOptions('');
					    	}
					    	else
					    	{
					    		$(this).parent().find('a').removeClass('selected').end().end().addClass('selected');
					    		$(elemento).selectOptions(option.value);
					    	}
					    })
					    .insertAfter(elemento);
					}
				});
				
				Produtos.Filtros.checkOptions(elemento);
			},
			
			checkOptions : function(elemento) {
				$(elemento).each(function(){
					$('a[rel='+$(this).val()+']', $(this).parent()).addClass('selected');
				});
			}
		},
		
		_init : function() {
			try {		
				var testProdutos = Produtos.produtosUL.find('li').length;
				this.Filtros.init();
				this.Tooltips.init();
				if ( testProdutos > 0) this.Slide.init();
			} catch (e) {
				//console.log('Error: ' + e.description);
			}
		}
	};

(function ($) { 
		
	$.fn.reset = function() {
		return this.each(function() {
			$(this).is('form') && this.reset();
		});
	};
		  
	$.fn.replaceColors = function() {
		return this.each(function(){
			$(this).css('background',$(this).html());					  
		});
	};
	
	$.fn.replaceButtons = function() {
		return this.each(function(){
			var b = $(this);
			var tt = b.text() || b.val();
			
			if ($(this).is(':submit, :reset, :button')) {
				b = $('<a>').text(tt).insertAfter(this).addClass(this.className).attr('id',this.id).css({cursor:'pointer'});
				$(this).remove();
				
				b.bind('click', function() {
					var form = $(this).parents('form');				
					switch (true) 
					{
						case ($(this).is('.submit')):
							form.submit();
							break;
						case ($(this).is('.reset')):
							form.reset();
							break;							
					}
				});
			}
		});
	};
	
	$.fn.selectOptions = function(value, clear)
	{
		var v = value;
		var vT = typeof(value);
		// handle arrays
		if(vT == "object" && v.constructor == Array)
		{
			var $this = this;
			$.each(v, function()
				{
	      				$this.selectOptions(this, clear);
	    			}
			);
		};
		var c = clear || false;
		
		// has to be a string or regular expression (object in IE, function in Firefox)
		if(vT != "string" && vT != "function" && vT != "object") return this;
		this.each(
			function()
			{
				if(this.nodeName.toLowerCase() != "select") return this;
				// get options
				var o = this.options;
				// get number of options
				var oL = o.length;
				for(var i = 0; i<oL; i++)
				{
					if(v.constructor == RegExp)
					{
						if(o[i].value.match(v))
						{
							o[i].selected = true;
						}
						else if(c)
						{
							o[i].selected = false;
						}
					}
					else
					{
						if(o[i].value == v)
						{
							o[i].selected = true;
						}
						else if(c)
						{
							o[i].selected = false;
						}
					}
				}
			}
		);
		return this;
	};	
	
	$.fn.equalHeights = function(px) {
		$(this).each(function(){
			var currentTallest = 0;
			$(this).children().each(function(i){
				if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
			});
			//if (!px || !Number.prototype.pxToEm) currentTallest = currentTallest.pxToEm(); //use ems unless px is specified
			// for ie6, set height since min-height isn't supported
			if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({'height': currentTallest}); }
			$(this).children().css({'min-height': currentTallest}); 
		});
		return this;
	};	

	$.fn.vAlign = function(type) {
		var type = ( typeof type == "undefined" || type == null ) ? 'middle' : type;

		return this.each(function(i){
			var ah = $(this).height();
			var ph = $(this).parent().height();
			var mh = ( type == 'bottom' ) ? (ph-ah) : (ph - ah) / 2;
			$(this).css('margin-top', mh);
		});
	};
	
})(jQuery);
