var $gGalleryCurrent = 1;
var ua = $.browser;
(function($) {
	$.fn.gGallery = function(options) {
		var opts = $.extend({}, $.fn.gGallery.defaults, options);		
		return this.each(function() {
			o = $.meta ? $.extend({}, opts, $pxs_container.data()) : opts;
				
			var $g          = $(this),
                        $gTotalHeight 	= $g.find('ul').height(),			
                        $gHeight	= $g.height(),
                        $nCols		= $gTotalHeight / $gHeight,
                        $total_thumbs	= $(o.thumbs).children().find('img').length,
                        $cCount 	= 0; 

			if ($total_thumbs <= 8)
			{ 
                            $(o.upButton).hide();
                            $(o.downButton).hide();
			}
			
			$(o.thumbs).css('height',$gTotalHeight);
			$(o.container).css('overflow','hidden');
			
			//if ( navigator.appName == "Microsoft Internet Explorer" ) {
				if (o.slideshowMode == false) {
					$(o.container).css('position', 'relative');
					$(o.thumbs).css('position','absolute').css('left', '0');
				}	
			//}
			
			$(o.upButton).bind('click', function(){
				if ($cCount > 0){
					$(o.thumbs).stop().animate({
						'top'	: '+='+ $gHeight +'px'	
					},500);
					
					$cCount--;				
				}											
				return false;		
			});
			
			

			
			$(o.downButton).bind('click',function(){
				
				if ($cCount == $nCols){$cCount = $nCols;}
				
				if ( ua.msie && ua.version == "7.0" ) {
				
					if ($cCount < ($nCols - 2)){
						$(o.thumbs).stop().animate({
							'top'	: '-='+ $gHeight +'px'	
						},500);
						
						$cCount++;
					}
					
				} else {
					
					if ($cCount < ($nCols - 1)){
						$(o.thumbs).stop().animate({
							'top'	: '-='+ $gHeight +'px'	
						},500);
						
						$cCount++;
					}
					
				}
				return false;
				
			});
			
			
			$(o.imgContainer).mouseover(function(){if (o.slideshowMode == true){pause();}});
			$(o.imgContainer).mouseout(function(){ if (o.slideshowMode == true){play();}});
			
			$(o.thumbs + ' li:first a').addClass('active-thumb');
			
			
			/*
			*	THUMBNAILS E CARREGAMENTO DOS PRODUTOS
			*	-----------------------------------------
			*	Programando a função de carregamento das imagens 
			*	e das informações do produto em geral (Título e Descrição).			
			**/
			
			
			// Para a Thumbnail atual, desabilitar a função de carregar.
			
			if (o.slideshowMode == true){ play(); }					
						
			$(o.thumbs).children('li').find('a').bind('click',function(){
				// Incrementamos a contadora para descobrir qual é a foto atual
				++$gGalleryCurrent;
				
				var $thumb	= $(this).find('img');
				
				//	Removo a classe active-thumb de todas as thumbs e 
				//	aplico somente a thumb atual
				$(o.thumbs).children('li').find('a').removeClass('active-thumb');
				
				
				// Caminho para a foto 
				var $imgN 	= $(this).attr("rel");				
				
				// Título do Produto 
				//var $titulo	= $(this).find('img').attr('alt');
				
				// Descrição do Produto
				var $desc	= $(this).find('img').attr('title');
                                
                                if (o.showPrice == true){
                                    
                                    var $splitString = $(this).find('img').attr('alt');
                                    var $splitTag    = $splitString.split('|');
                                    
                                    if ($splitTag.length == 2){
                                        
                                        var $titulo =   $splitTag[1];
                                        var $preco  =   $splitTag[0];
                                        
                                    } else {
                                        var $titulo = $(this).find('img').attr('alt');
                                        var $preco  = '';
                                    }
                                    
                                } else {
                                    var $titulo	= $(this).find('img').attr('alt');
                                    var $preco  = '';
                                }
                                
						
				// Exibindo o loader ao clicar em uma thumb
				$(o.loader).show();			
				/* 
				*	TRANSIÇÃO DAS IMAGENS E CARREGAMENTO DO PRODUTO
				*	-------------------------------------------------
				*
				*	1 - Fade na imagem Principal 
				*	2 - Removendo a imagem principal do DOM.
				*	3 - Setando a função load para esconder o loader e atribuir o valor de título
				*		e Descrição pro produto assim que a imagem for carregada.
				*	4 - Atribuindo o caminho da imagem para a imagem nova.
				* 	5 - Façamos a imagem aparecer.
				*	6 - Carregamos a imagem na div .imagemS que é o Container. 
				**/		
				
				$(o.imgContainer).find('img').fadeOut(350,function(){ // 1
					$(o.imgContainer + ' img').each(function(){
						$(o.imgContainer + ' img').remove();	
					}); // 2

					if (o.showCaptions == true)
					{
						$(o.captionContainer).animate({
							'top'	: 0
						},50);
					}
					
					$(o.captionContainer).hide();
					

					if ( navigator.appName == "Microsoft Internet Explorer" ) {
						$('<img />').attr('src', $imgN).prependTo(o.imgContainer).fadeIn(500, function(){ 
							$(o.loader).hide();
							$(o.titleContainer).text($titulo);
                                                        $(o.priceContainer).text($preco);
							$(o.descContainer).text($desc);
							$(o.captionContainer).fadeIn(350); 
						});
					} else {											
						$('<img />').attr('src', $imgN).load(function(){											
							$(o.loader).hide();
							$thumb.parent().addClass('active-thumb');
							$(o.imgContainer).prepend($(this)).hide().fadeIn(500,function(){
								$(o.titleContainer).text($titulo);
                                                                $(o.priceContainer).text($preco);
								$(o.descContainer).text($desc);
								$(o.captionContainer).fadeIn(350);
							});
						});	
					}
										
				});
				
				if (o.slideshowMode == true){ pause();play(); }				
					if (o.showCaptions == true)
					{
						$(o.captionContainer).delay(650).animate({
							'top'	: '-73px'
						},350);
					}
				
			});

				
		});
	};
	$.fn.gGallery.defaults = {
		container	: '.gallery',
		thumbs		: 'ul.colecao-miniatura',
		imgContainer	: '.imagemS',
		upButton	: '.controle-cima',
		downButton	: '.controle-baixo',
		loader		: '#loader',
		captionContainer: '.gGallery-caption',
		titleContainer	: '.g-caption-title',
                priceContainer	: '.g-caption-price',
		descContainer	: '.g-caption-desc',
                showPrice	: false,
		showCaptions	: false,
		slideshowMode	: false,
		slideTimeOut	: 5000		
	};
})(jQuery);


function switch_slides()
{	
	var $cont	= $(o.thumbs + ' li a').length;
		
	if ($gGalleryCurrent > ($cont -1))
	{
		$gGalleryCurrent = 0;
	}
	
	var $active = $(o.thumbs + ' li a.active-thumb').index();
	var $next 	= $active + $gGalleryCurrent;
	
	$(o.thumbs + ' li a').eq($next).addClass('active-thumb').click();
		 		
}

function play(){	
	playtime = setInterval('switch_slides()',o.slideTimeOut);
}

function pause(){	
	clearTimeout(playtime);
}

