//    @@@@@@@@@@@@@@  @@@@@@@@@@@@@@  @@@@@@@@@     @@@@@@  @@@@@  @@@@@@@@@@@@  
//    @@@@@@@@@@@@@@  @@@@@@@@@@@@@@  @@@@@@@@@@    @@@@@@  @@@@@  @@@@@@@@@@@@ 
//    @@@@@           @@@@@@          @@@@@@@@@@@   @@@@@@         @@@@    @@@@  
//    @@@@@  @@@@@@@  @@@@@@@@@@@@@@  @@@@@@ @@@@@  @@@@@@  @@@@@  @@@@    @@@@ 
//    @@@@@  @@ @@@@  @@@@@@@@@@@@@@  @@@@@@  @@@@@ @@@@@@  @@@@@  @@@@@@@@@@@@ 
//    @@@@@     @@@@  @@@@@@          @@@@@@   @@@@@@@@@@@  @@@@@  @@@@@@@@@@@@
//    @@@@@@@@@@@@@@  @@@@@@@@@@@@@@  @@@@@@    @@@@@@@@@@  @@@@@  @@@@    @@@@  
//    @@@@@@@@@@@@@@  @@@@@@@@@@@@@@  @@@@@@     @@@@@@@@@  @@@@@  @@@@    @@@@
//
//	  Example usage:
//		$('#galeria').galeria({
//			at: '-thumbanail'
//			af: '-full'
//		})
//
//		created by Renan Veroneze, Gênia Web Developer, created in 24-03-11
// 													   all rigths reserved.

jQuery.fn.extend({
	galeria:function(options){
		defaults = {at: '-thumb', af: '-full', wt:67, st: 42, li:'#/foto-'}
		options = jQuery.extend(defaults, options);
		first = true;
		id = document.URL.split(options.li);
		id = ((id[1] == null) ? 0 : eval(id[1]-1));
		var w = eval((this.width() - options.st)), widths = 0, c = 0, box = '', top = '<div class="set">', bottom = '</div>';
		var sets = [];
		this.find('img').each(function(i){
			widths = widths + options.wt;
			if(widths > w){
				box += bottom+top;
				sets.push(widths-options.wt)
				widths = options.wt;
			}
			box += '<div class="thumbjo" style="background-image:url('+$(this).attr('src')+')">';
			box += '<img id="'+eval(i+1)+'" src="'+$(this).attr('src')+'" title="'+$(this).attr('title')+'"/>';
			box += '</div>';
		}).remove();
		this.append($('<div id="foto-grande" />').append($('<img/>')).append($('<div id="legenda" />')));
		this.append($('<div id="thumb" />').append($('<div id="slider" />').append(top+box+bottom)));		
		$('#thumb').width(w).before($('<div id="setaesquerda" />')).after($('<div id="setadireita" />'));
		
		$('#setaesquerda').click(function(){if(c !=0){c--;$('#slider').animate({left:'+='+eval(sets[c])+'px'},2000);}});	
		$('#setadireita').click(function(){if(c < sets.length){$('#slider').animate({left:'-='+eval(sets[c])+'px'},2000);c++;}});	
		$('#slider img').click(function(){
			src = $(this).attr('src').split(options.at).join(options.af);
			legend = $(this).attr('title');
			(!first)?window.location = '#/foto-'+$(this).attr('id'):first = false;
			$('#foto-grande').find('img').each(function(){
				$(this).fadeOut(function(){
					$(this).attr('src',src).bind('load',function(){
						$('#foto-grande').width($(this).width()).css({'left':'50%','margin-left':'-'+eval($(this).width()/2)+'px'});
						$('#foto-grande').height($(this).height());
						(legend != '')?$('#legenda').show().text(legend):$('#legenda').hide();
						$(this).fadeIn();						
					});
				});
			});
		}).eq(id).click();	
	}
});
