jQuery.fn.abphoto = function () {
	return this.each (function () {
		var abphotoContainer = $(this);
		var album = $(this).attr("albumid")?$(this).attr("albumid"):0;
		var aberto = $(this).attr("aberto");
		
		// Tamanho do gadget
		var abWidth = $(this).width();
		var thumbsWidth = abWidth - 40;	
		
		// Acessa cada slide e torna eles invisívels
		var slide = 0;
		abphotoContainer.children().each(function(){
			$(this).children('a').each(function(){	
				$(this).addClass('abZoomImage').attr('rel','ab' + album);
			});
			
			if(slide){
				$(this).hide().width(thumbsWidth);
				if($.browser.msie&& $.browser.version < '9.0'){
					$(this).css('background','#5E5E5E');
				}
			}
			slide++;
		});
		
		var htmlSlides = $(this).html();
		
		// Marca o container com posicionamento relativo
		abphotoContainer.parent().css('position','relative').css('overflow','hidden');
		
		abphotoContainer.html('<div class="ab_wrapper"><div class="ab_header">&nbsp;</div><div class="ab_leftnav" id="ab' + album + 'leftnav">&nbsp;</div><div class="ab_thumbs" id="ab' + album + 'thumbs">' + htmlSlides + '</div><div class="ab_rightnav" id="ab' + album + 'rightnav">&nbsp;</div><div class="clear"></div></div>');
		
		if($.browser.msie&& $.browser.version < '9.0'){
			abphotoContainer.children(".ab_wrapper").css('background','#EEEEEE');
			abphotoContainer.children(".ab_wrapper").children(".ab_thumbs").css('background','#5E5E5E');
		}
		
		// Seta o tamanho da área central
		$(this).children().children(".ab_thumbs").width(thumbsWidth);
		
		// Seta se o estado inicial é aberto ou fechado - padrão: fechado
		if(aberto)
			abphotoContainer.children(".ab_wrapper").animate({'bottom':'0'},300,'swing');

		// Seta animação de abrir e fechar
		$(this).children().children(".ab_header").click(function(){
			var posicao = parseInt(abphotoContainer.children(".ab_wrapper").css('bottom'));
			if(posicao==0){	
				abphotoContainer.children(".ab_wrapper").animate({'bottom':'-60'},300,'swing');
			}else{
				abphotoContainer.children(".ab_wrapper").animate({'bottom':'0'},300,'swing');
			}
		});
		
		$(".abZoomImage").fancybox({
			'titlePosition'  : 'inside',
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic'	,
			'centerOnScroll': true,
			'autoScale' : true,
			'titleShow' : true,
			'padding': 0
		});
		
		$('#ab' + album + 'thumbs').cycle({
			fx: 'scrollHorz',
			timeout: 0,
			speed: 500,
			next: '#ab' + album + 'rightnav',
			prev: '#ab' + album + 'leftnav'
		});
	});
}
