/* __________________________________________________________________
		Name: piroBox v.1.1
		Date: february 2009
		Use: just  another gallery.
		Autor: Diego Valobra (http://www.pirolab.it),(http://www.diegovalobra.com)
		Version: 1.1
		Licence: CC-BY-SA http://creativecommons.org/licenses/by-sa/2.5/it/
_______________________________________________________________________________*/

/*_____________________ HTML FOR THE GALLERY _____________________*/

$(document).ready(function(){
$('body').append(
		'<!-- :::::::: PIROBOX :::::::::: -->'+
		'<div class="pre"></div>'+
		'<div class="bg_thumbs"></div>'+
		'<div id="gallery" class="thumbs">'+
		'<div class="all">'+
		'<div class="t_l"></div>'+
		'<div class="t_r"></div>'+
		'<div class="middle_l"></div>'+
		'<div class="middle_r"></div>'+
		'<div class="t_l_b"></div>'+
		'<div class="t_r_b"></div>'+
		'<div class="img_box" title="close">'+
		'</div>'+
		'<span class="thumbs_close" title="close"></span>'+
		'</div>'+
		'</div>'+
		'<!-- :::::::: END PIROBOX :::::::::: -->'
		);	
});



(function($) {

	$.fn.piroBox = function(opt) {
		opt = jQuery.extend({
			border: 0, 
			mySpeed: null,
			open_speed : 500,
			close_speed : 500,
			bg_alpha: 0.5,
			pathLoader : null, 
			gallery : null, 
			gallery_li : null,
			single : null,
			next_class : null,
			previous_class : null,
			padding: '0',
			gloabal : true
		}, opt);


return this.each(function() {
		
	

		/*___________________ CLOSE FUNCTION ______________________________*/
		function closeIt() {
				$('.pre').hide();
				$('.caption').remove();				
				$('li.begin').remove();
				$('li.end').remove();
				$('.thumbs_close').css('display','none');
				$(opt.next_class+','+opt.previous_class).css({'visibility':'hidden'});
				$((opt.gallery_li)).removeClass('start');
				$((opt.gallery_li)).removeClass('back');
				$('.box_next_in, .box_previous_in,.box_next,.box_previous ').css('display','none');
				$('.loader').fadeTo(300,0);
				$('.loader').queue(function(){
					$('.bg_thumbs').fadeTo(500,0);
					$('.img_box img').remove();
					$('.img_box ').queue(function(){
					$('.all').css({
					'top':'50%',
					'height':'80px',
					'width':'80px',
					'marginLeft':'-45px',
					'marginTop':'-40px',
					'visibility':'hidden',
					'padding':'10px'
					});
					$('.img_box').css({
					'height':'50px',
					'width' : '50px',
					'visibility':'hidden'
					}).removeClass('unloader');
					$('.bg_thumbs').hide().css('visibility','hidden');
					$('.thumbs').hide();
					$('.img_box').dequeue();
					});
				$('.loader').dequeue().remove('<div class="loader"><span style=" background:' + opt.pathLoader + ' "></span></div>');
				});
				$('.img_box img, .thumbs_close').fadeTo(400,0);			
				$('.img_box img').queue(function(){
					$('.img_box').animate({
					height : '50px' ,
					width : '50px' 
					},50).css('visibility','hidden'); 												 
					$('.all').animate({
					top:'50%',
					height : '80px' ,
					width : '80px' , 
					marginLeft : '-45px',
					marginTop : '-40px',
					padding : '10px'
					},(opt.close_speed)); 												     
					$('.img_box img').remove();
					$('.img_box').removeClass('unloader');
					$('.bg_thumbs').fadeTo(500,0);
						$('.all').queue(function(){
							$('.bg_thumbs,.thumbs_close').hide().css('visibility','hidden');
							$('.thumbs').css('display','none');	
						$('.all').css('visibility','hidden').dequeue()
				
						});
				$('.img_box img').dequeue();
				});
		}
		/*___________________	END FUNCTION CLOSE     ________________________*/	

		/*___________________	START PIROBOX	    ________________________*/

			var next_out = $('.box_next').width();
			var idPiro = $(this).attr('id');			
			var b_size = 0;
				  if($.browser.msie && $.browser.version < 7)   {
					$('.img_box').css('padding','2px');
					} else {
						$('.img_box').css('padding','2px');
						(opt.mySpeed);
					}
					
				$('.bg_thumbs, .thumbs, .thumbs_close ').hide();
			$(window).resize(function(){
				var new_w_bg = $(document).height();
				$('.bg_thumbs').css({'visibility':'visible','height':+ new_w_bg +'px'});				  
			});	
			var w_bg = $(document).height();
			$('.bg_thumbs').css({'visibility':'hidden','height':+ w_bg +'px'});
		/*___________________	LAUNCH GALLERY     ________________________*/
		
			$(opt.single).bind('click',function() {	
				$('.pre').append('<div class="loader"><span style=" background:' + opt.pathLoader + ' "></span></div>').hide();
				$('.all').prepend('<div class="caption"><p title="caption"></p></div>');
				$('.caption').css({'opacity':'0','visibility':'hidden'});
				$(opt.next_class+','+opt.previous_class).css({'visibility':'hidden'});

		/*___________________SINGLE, NEXT AND PREVIOUS PREPARE    ________________________*/				
					$('.img_box img').remove('img');
					$(window).resize(function(){
						var new_w_bg = $(document).height();
						var w_W = $(window).width();
						$('.bg_thumbs').css({'visibility':'visible','height':+ new_w_bg +'px','width':w_W+'px'});
					});
					var w_bg = $(document).height();
					$('.pre').css('visibility','visible').show();
					var pathImg = $(this).attr('href');
					var titleImg = $(this).attr('title');
					var myImg = new Image(); 
					$(myImg).load(function() {
						var imgH = myImg.height;
						var imgW = myImg.width;	
						var w_H = $(window).height();
						var w_W = $(window).width();
						$('#' + idPiro + ' .img_box').append(this);
						
						if(imgH+100 > w_H || imgW+100 > w_W){
								var new_img_W = imgW;
								var new_img_H = imgH;
								var _x = (imgW + 100)/w_W;
								var _y = (imgH + 100)/w_H;

								if ( _y > _x ){
								new_img_W = Math.round(imgW * (0.9/_y));
								new_img_H = Math.round(imgH * (0.9/_y));
								} else {
								new_img_W = Math.round(imgW * (0.9/_x));
								new_img_H = Math.round(imgH * (0.9/_x));
								}
								imgH += new_img_H;
								imgW += new_img_W;
								$('.thumbs').show();
								//alert(w_bg);
								$('.bg_thumbs').show().css({'opacity':'0','visibility':'visible','height':+ w_bg +'px'}).fadeTo(300,(opt.bg_alpha));
								$('.img_box img').css('visibility','hidden').hide();
								$('.all').css({'visibility':'visible'}).animate({
									height : (new_img_H) + 'px' ,
									width : (new_img_W) + 'px' , 
									marginLeft : '-' +((new_img_W)/2) +'px',
									marginTop : '-' +((new_img_H)/2+20 ) +'px'
								},(opt.open_speed));
								$('.all').queue(function(){
								$('.img_box').css({
									visibility :'visible',
									height : (new_img_H) + 'px' ,
									width : (new_img_W) + 'px' 
								});
								$('.img_box').queue(function(){
									$(myImg).height(new_img_H).width(new_img_W).css('opacity',0);
									$('.img_box img').css('visibility','visible').show().fadeTo(300,1);
									$('.img_box ').addClass('unloader');
									$('.loader').remove('<div class="loader"><span style=" background:'+ opt.pathLoader + ' "></span></div>');
								$('.img_box').dequeue()
									if(titleImg == ""){							
										//$('.caption').css('visibility','hidden');
										$('.caption p').html(pathImg);
										//alert(pathImg);
										var caption_h = $('.caption').height();										
										$('.all').animate({
											height :imgH+ (30) + 'px'
										},300);
										$('.all').queue(function(){
										$('.caption').css({'visibility':'visible','display':'block'}).fadeTo(600,1);
										$('.thumbs_close').show().css({'opacity':'0','visibility':'visible'}).fadeTo(300,1);
										$('.unloader, .thumbs_close').bind('click',function(){closeIt();});	
										$('.all').dequeue()
										});
									}else{
										$('.caption p').html(titleImg);
										var caption_h = $('.caption').height();										
										$('.all').animate({
											height :new_img_H+ (caption_h+10) + 'px'
										},300);
										$('.all').queue(function(){
										$('.caption').css({'visibility':'visible','width':+ new_img_W+'px'}).fadeTo(400,1);
										$('.thumbs_close').show().css({'opacity':'0','visibility':'visible'}).fadeTo(200,1);
										$('.unloader, .thumbs_close').bind('click',function(){closeIt();});	
										$('.all').dequeue()
										});
									}
								});
									if($(opt.next_class).is('.next_in')){
										$('.box_next_in ,.box_previous_in ').css('display','block');
										$('.box_next,.box_previous ').css('display','none');
										$(opt.next_class +','+ opt.previous_class).css({'visibility':'visible'});
									}else if($(opt.next_class).is('.next')) {
										$('.box_next_in, .box_previous_in ').css('display','none');
										$('.box_next,.box_previous ').css('display','block');
										$(opt.next_class +','+ opt.previous_class).css({'visibility':'visible'});										
									}
								$('.all').dequeue();
								});		
								
						} else {
										
								$('.thumbs').show();
								//alert(w_bg);
								$('.bg_thumbs').show().css({'opacity':'0','visibility':'visible','height':+ w_bg +'px'}).fadeTo(300,(opt.bg_alpha));
								$('.img_box img').css('visibility','hidden').hide();
								$('.all').css({'visibility':'visible'}).animate({
									height : (imgH)+ 'px' ,
									width : (imgW) + 'px' , 
									marginLeft : '-' +((imgW)/2) +'px',
									marginTop : '-' +((imgH)/2+10) +'px'
								},(opt.open_speed));
								$('.all').queue(function(){
								$('.img_box').css({
									visibility :'visible',
									height : (imgH) + 'px' ,
									width : (imgW) + 'px' 
								});
								$('.img_box').queue(function(){
									$(myImg).height(imgH).width(imgW).css('opacity',0);
									$('.img_box img').css('visibility','visible').show().fadeTo(300,1);
									$('.img_box ').addClass('unloader');
									$('.loader').remove(' <div class="loader"><span style=" background:' + opt.pathLoader + ' "></span></div>');								
								$('.img_box').dequeue()
								
									if(titleImg == ""){							
										//$('.caption').css('visibility','hidden');
										$('.caption p').html(pathImg);
										//alert(pathImg);
										var caption_h = $('.caption').height();										
										$('.all').animate({
											height :imgH+ (30) + 'px'
										},300);
										$('.all').queue(function(){
										$('.caption').css({'visibility':'visible','display':'block'}).fadeTo(600,1);
										$('.thumbs_close').show().css({'opacity':'0','visibility':'visible'}).fadeTo(300,1);
										$('.unloader, .thumbs_close').bind('click',function(){closeIt();});	
										$('.all').dequeue()
										});
									}else{
										$('.caption p').html(titleImg);
										var caption_h = $('.caption').height();										
										$('.all').animate({
											height :imgH+ (caption_h+10) + 'px'
										},300);
										$('.all').queue(function(){
										$('.caption').css({'visibility':'visible','width':+ imgW+'px'}).fadeTo(400,1);
										$('.thumbs_close').show().css({'opacity':'0','visibility':'visible'}).fadeTo(200,1);
										$('.unloader, .thumbs_close').bind('click',function(){closeIt();});	
										$('.all').dequeue()
										});
									}
		
								});							
								$('.all').dequeue();

								});	
						  }

					});
				$(myImg).attr('src', pathImg);
				return false;
			});
	
		});
	}   
})(jQuery);


