/*
DDSlideShow v1.0 for jQuery

Plugin jQuery per creare uno slideshow

Uso:
$().DDSlideShow({id: 'gal1',
				 idcontenitore: 'cont',
				 resize_contenitore: true,
				 animations: 'rand',
				 mostra_titolo: false,
				 event: 5000,
				 play_on_load: false,
				 stop_on_manual_change: true});
				 
id: identificativo della gallery
idcontenitore: id del div che conterrą le imagini
resize_contenitore: se si vuole adattare il contenitore alla foto
animations: tipo di animazione
mostra_titolo: se mostrare titolo e descrizione della foto
event: se un numero (1000 = 1s) farą lo slideshow automatico
play_on_load: se si vuole far partire lo slide al caricamento della pagina
stop_on_manual_change: stoppare lo slide al cambio manuale delle immagini

$().DDSlideShowStopTime(id); stoppa il cambio automatico delle immagini
$().DDSlideShowPlayTime(id); avvia lo slide automatico
$().DDSlideShowNextImage(id); immagine successiva
$().DDSlideShowPrevImage(id); immagine precedente
$().DDSlideShowGetDescription(id); restituisce titolo e descrizione del'imagine
$().DDSlideShowGetUrl(id); restituisce l'url
$().DDSlideShowGetId(id); restituisce l'id

-----------------------------------------------------------------------------------------------------------
DDSlideShow v1.0 for jQuery
Copyright (C) 2009  Digital Daigor(info.digitaldaigor@gmail.com)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

For donations:
https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=3NLGJVKN8NHBE&lc=IT&item_name=http%3a%2f%2ftrash%21&currency_code=EUR&bn=PP%2dDonationsBF%3abtn_donate_SM%2egif%3aNonHosted
*/

DDSlideShowImmagini = new Array();
DDSlideShowImmagineAct = new Array();
DDSlideShowImmagineZIndex = 100;
DDSlideShowOption = new Array();

if(jQuery) (function(){
	
	jQuery.fn.extend({
		
		DDSlideShow: function(handler)
		{
			
			DDSlideShowOption[handler['id']] = new Array();
			DDSlideShowOption[handler['id']]['play_anim'] = (handler['play_on_load'])?1:0;
			DDSlideShowOption[handler['id']]['handler'] = handler;
			DDSlideShowImmagineAct[handler['id']] = '0';
			
			var DDSlideShowLastImage = $('img[gallery="'+handler['id']+'"]:last').attr('src');
			DDSlideShowImmagini[handler['id']] = new Array();
			DDSlideShowimg_n = 0;
			$('img[gallery="'+handler['id']+'"]').each( function(){
				
				DDSlideShowImmagini[handler['id']][DDSlideShowimg_n] = new Array();
				DDSlideShowImmagini[handler['id']][DDSlideShowimg_n]['src'] =    $(this).attr('src');
				DDSlideShowImmagini[handler['id']][DDSlideShowimg_n]['w'] =      $(this).css('width');
				DDSlideShowImmagini[handler['id']][DDSlideShowimg_n]['h'] =      $(this).css('height');
				DDSlideShowImmagini[handler['id']][DDSlideShowimg_n]['class'] =  $(this).attr('class');
				DDSlideShowImmagini[handler['id']][DDSlideShowimg_n]['style'] =  $(this).attr('style');
				DDSlideShowImmagini[handler['id']][DDSlideShowimg_n]['id'] =     $(this).attr('id');
				DDSlideShowImmagini[handler['id']][DDSlideShowimg_n]['title'] =  $(this).attr('title');
				DDSlideShowImmagini[handler['id']][DDSlideShowimg_n]['alt'] =    $(this).attr('alt');
				DDSlideShowImmagini[handler['id']][DDSlideShowimg_n]['action'] = $(this).attr('action');
				$(this).remove();
				
				if(DDSlideShowLastImage == DDSlideShowImmagini[handler['id']][DDSlideShowimg_n]['src'])
				{
					$().DDSlideShowCrea(handler);
				}
				DDSlideShowimg_n++;
			});
		},
		DDSlideShowCrea: function(handler)
		{
			html = '';

			for(i=0;i<DDSlideShowImmagini[handler['id']].length;i++)
			{
				if(i==0)
				{
					p = DDSlideShowImmagini[handler['id']].length-1;
					s = i+1;
					d = '';
				}
				else if(i == DDSlideShowImmagini[handler['id']].length-1)
				{
					p = i-1;
					s = 0;
					d = ' display:none;';
				}
				else
				{
					p = i-1;
					s = i+1;
					d = ' display:none;';
				}

				if(DDSlideShowImmagini[handler['id']][i]['title'].length > 0 && DDSlideShowImmagini[handler['id']][i]['alt'].length > 0)
				{
					titolo = '<b>'+DDSlideShowImmagini[handler['id']][i]['title']+'</b><br />'+DDSlideShowImmagini[handler['id']][i]['alt'];
				}
				else
				{
					titolo = (DDSlideShowImmagini[handler['id']][i]['title'].length > 0)?DDSlideShowImmagini[handler['id']][i]['title']:DDSlideShowImmagini[handler['id']][i]['alt'];
				}
				
				if(titolo.length>0 && handler['mostra_titolo'] == true)
				{
					titolo = '<div class="img_titolo" style="position:absolute; overflow:show; bottom: 0px;"><table cellspacing="0" cellpaDDSlideShowing="0" border="0" class="img_titolo" height="30"><tr><td>'+titolo+'</td></tr></table></div>';
				}
				else
				{
					titolo = '';
				}
				
				html += '<div s="'+s+'" p="'+p+'" style="position:absolute; display: none; '+d+'" id_img="'+handler['id']+'_'+i+'" class="'+DDSlideShowImmagini[handler['id']][i]['class']+'"><table cellspacing="0" cellpadding="0" border="0" height="100%" width="100%"><tr><td valign="middle" align="center"><img id="'+DDSlideShowImmagini[handler['id']][i]['id']+'" src="'+DDSlideShowImmagini[handler['id']][i]['src']+'" />'+titolo+'</td></tr></table></div>'; 
			}
			$(html).appendTo('#'+handler['idcontenitore']);
			
			for(i=0;i<DDSlideShowImmagini[handler['id']].length;i++)
			{
				animation = handler['animations'];
				
				if(handler['event']*0 != 0)
				{
					$('div[id_img="'+handler['id']+'_'+i+'"]').bind('click',function(){$(this).DDSlideShowChangeImage(handler);});
				}
				else
				{
					if(i==0){$().DDSlideShowChangeTime(handler);}
				}

				$('#'+handler['idcontenitore']).attr({act: $(this).attr('s')});
			}
		},
		DDSlideShowRandomizeAnimation: function(handler){
			switch(Math.floor(Math.random()*6))
			{
				case 0:
					$(this).DDSlideShowSlideBottom(handler);
					break;
				case 1:
					$(this).DDSlideShowSlideLeft(handler);
					break;
				case 2:
					$(this).DDSlideShowSlideRight(handler);
					break;
				case 3:
					$(this).DDSlideShowSlideTop(handler);
					break;
				default:
					$(this).DDSlideShowFade(handler);
					break;
			}
		},
		DDSlideShowChangeTime: function(handler)
		{
			if(DDSlideShowOption[handler['id']]['play_anim'] == 1)
			{
				if(animation == 'fade'){$('div[id_img="'+handler['id']+'_'+DDSlideShowImmagineAct[handler['id']]+'"]').DDSlideShowFade(handler);}
				else if(animation == 'slide_l'){$('div[id_img="'+handler['id']+'_'+DDSlideShowImmagineAct[handler['id']]+'"]').DDSlideShowSlideLeft(handler);}
				else if(animation == 'slide_r'){$('div[id_img="'+handler['id']+'_'+DDSlideShowImmagineAct[handler['id']]+'"]').DDSlideShowSlideRight(handler);}
				else if(animation == 'slide_t'){$('div[id_img="'+handler['id']+'_'+DDSlideShowImmagineAct[handler['id']]+'"]').DDSlideShowSlideTop(handler);}
				else if(animation == 'slide_b'){$('div[id_img="'+handler['id']+'_'+DDSlideShowImmagineAct[handler['id']]+'"]').DDSlideShowSlideBottom(handler);}
				else{$('div[id_img="'+handler['id']+'_'+DDSlideShowImmagineAct[handler['id']]+'"]').DDSlideShowRandomizeAnimation(handler);}
			}
			
			setTimeout(function(){$().DDSlideShowChangeTime(handler);},handler['event'])
		},
		DDSlideShowChangeImage: function(handler)
		{
			if(animation == 'fade'){$(this).DDSlideShowFade(handler);}
			else if(animation == 'slide_l'){$(this).DDSlideShowSlideLeft(handler);}
			else if(animation == 'slide_r'){$(this).DDSlideShowSlideRight(handler);}
			else if(animation == 'slide_t'){$(this).DDSlideShowSlideTop(handler);}
			else if(animation == 'slide_b'){$(this).DDSlideShowSlideBottom(handler);}
			else{$(this).DDSlideShowRandomizeAnimation(handler);}
		},
		DDSlideShowStopTime: function(id)
		{
			DDSlideShowOption[id]['play_anim'] = 0;
		},
		DDSlideShowPlayTime: function(id)
		{
			DDSlideShowOption[id]['play_anim'] = 1;
		},
		DDSlideShowNextImage: function(id)
		{
			if(DDSlideShowOption[id]['handler']['event']*0 == 0)//stop_on_manual_change
			{
				if(DDSlideShowOption[id]['handler']['stop_on_manual_change'] == true)
				{
					DDSlideShowOption[id]['play_anim'] = 0;
				}
			}
			$('div[id_img="'+id+'_'+DDSlideShowImmagineAct[id]+'"]').DDSlideShowChangeImage(DDSlideShowOption[id]['handler']);//DDSlideShowOption[id]['play_anim'] = 1;
		},
		DDSlideShowPrevImage: function(id)
		{
			if(!(DDSlideShowOption[id]['handler']['event']*0 != 0))//stop_on_manual_change
			{
				if(DDSlideShowOption[id]['handler']['stop_on_manual_change'] == true)
				{
					DDSlideShowOption[id]['play_anim'] = 0;
				}
			}
			/*alert($('div[id_img="'+id+'_'+$('div[id_img="'+id+'_'+DDSlideShowImmagineAct[id]+'"]').attr('p')+'"]').attr('p'));
			alert($('div[id_img="'+id+'_'+DDSlideShowImmagineAct[id]+'"]').attr('p'));
			alert(DDSlideShowImmagineAct[id]);*/
			$('div[id_img="'+id+'_'+$('div[id_img="'+id+'_'+$('div[id_img="'+id+'_'+DDSlideShowImmagineAct[id]+'"]').attr('p')+'"]').attr('p')+'"]').DDSlideShowChangeImage(DDSlideShowOption[id]['handler']);
		},
		DDSlideShowGetDescription: function(id)
		{
			return {title: DDSlideShowImmagini[id][DDSlideShowImmagineAct[id]]['title'], alt: DDSlideShowImmagini[id][DDSlideShowImmagineAct[id]]['alt']};
		},
		DDSlideShowGetUrl: function(id)
		{
			return {src: DDSlideShowImmagini[id][DDSlideShowImmagineAct[id]]['src']};
		},
		DDSlideShowGetId: function(id)
		{
			return {id: DDSlideShowImmagini[id][DDSlideShowImmagineAct[id]]['id']};
		},
		DDSlideShowGetAction: function(id)
		{
			return {action: DDSlideShowImmagini[id][DDSlideShowImmagineAct[id]]['action']};
		},
		DDSlideShowRisezeCont: function(handler,this_)
		{
			if(handler['resize_contenitore'] == true)
			{
				$('#'+handler['idcontenitore']).animate({
					height:$('div[id_img="'+handler['id']+'_'+$(this_).attr('s')+'"]').height()+'px',
					width:$('div[id_img="'+handler['id']+'_'+$(this_).attr('s')+'"]').width()+'px'
				},100);
			}
		},
		/***************************************************
		*                                                  *
		*                _________________                 *
		*                |EFFETTI DI FADE|                 *
		*                                                  *
		*                                                  *
		***************************************************/
		DDSlideShowFade: function(handler)
		{
			/*$(this).fadeOut(2000);
			$('div[id_img="'+handler['id']+'_'+$(this).attr('p')+'"]').fadeOut(2000);
			$('div[id_img="'+handler['id']+'_'+$('div[id_img="'+handler['id']+'_'+$(this).attr('p')+'"]').attr('p')+'"]').fadeOut(2000);
			*/
			$('div[id_img^="'+handler['id']+'_"][id_img!="'+handler['id']+'_'+$(this).attr('s')+'"]').each(function(){
				
				if($(this).css('top') == '0px' && $(this).css('left') == '0px')
				{
					$(this).fadeOut(2000);
				}
			});
			
			$().DDSlideShowRisezeCont(handler,this);

			$('div[id_img="'+handler['id']+'_'+$(this).attr('s')+'"]').css({'z-index':DDSlideShowImmagineZIndex+10,top: '0px', left: '0px' });
			$('div[id_img="'+handler['id']+'_'+$(this).attr('s')+'"]').fadeIn(2000);
			DDSlideShowImmagineAct[handler['id']] = $(this).attr('s');
		},
		DDSlideShowSlideLeft: function(handler)
		{
			/*$(this).animate({left: '-'+$(this).width()},1000);
			$(this).fadeOut();
			$('div[id_img="'+handler['id']+'_'+$(this).attr('p')+'"]').animate({left: '-'+$('div[id_img="'+handler['id']+'_'+$(this).attr('p')+'"]').width()},1000);
			$('div[id_img="'+handler['id']+'_'+$(this).attr('p')+'"]').fadeOut();
			$('div[id_img="'+handler['id']+'_'+$('div[id_img="'+handler['id']+'_'+$(this).attr('s')+'"]').attr('s')+'"]').animate({left: '-'+$('div[id_img="'+handler['id']+'_'+$(this).attr('p')+'"]').width()},1000);
			$('div[id_img="'+handler['id']+'_'+$('div[id_img="'+handler['id']+'_'+$(this).attr('s')+'"]').attr('s')+'"]').fadeOut();
			*/
			$('div[id_img^="'+handler['id']+'_"][id_img!="'+handler['id']+'_'+$(this).attr('s')+'"]').each(function(){
				
				if($(this).css('top') == '0px' && $(this).css('left') == '0px')
				{
					$(this).animate({left: '-'+$(this).width()},1000);
					$(this).fadeOut();
				}
			});
			
			$().DDSlideShowRisezeCont(handler,this);
				
			$('div[id_img="'+handler['id']+'_'+$(this).attr('s')+'"]').fadeIn();
			$('div[id_img="'+handler['id']+'_'+$(this).attr('s')+'"]').css({'z-index':DDSlideShowImmagineZIndex+10,left: $('div[id_img="'+handler['id']+'_'+$(this).attr('s')+'"]').css('width'), top: '0px'});
			$('div[id_img="'+handler['id']+'_'+$(this).attr('s')+'"]').animate({left: '0px'},1000);
			DDSlideShowImmagineAct[handler['id']] = $(this).attr('s');
		},
		DDSlideShowSlideRight: function(handler)
		{
			/*$(this).animate({left: $('div[id_img="'+handler['id']+'_'+$(this).attr('s')+'"]').width()+'px'},1000);
			$(this).fadeOut();
			$('div[id_img="'+handler['id']+'_'+$(this).attr('p')+'"]').animate({left: $('div[id_img="'+handler['id']+'_'+$('div[id_img="'+handler['id']+'_'+$(this).attr('p')+'"]').attr('s')+'"]').width()+'px'},1000);
			$('div[id_img="'+handler['id']+'_'+$(this).attr('p')+'"]').fadeOut();
			$('div[id_img="'+handler['id']+'_'+$('div[id_img="'+handler['id']+'_'+$(this).attr('p')+'"]').attr('p')+'"]').animate({left: $('div[id_img="'+handler['id']+'_'+$('div[id_img="'+handler['id']+'_'+$(this).attr('p')+'"]').attr('s')+'"]').width()+'px'},1000);
			$('div[id_img="'+handler['id']+'_'+$('div[id_img="'+handler['id']+'_'+$(this).attr('p')+'"]').attr('p')+'"]').fadeOut();
			*/
			$('div[id_img^="'+handler['id']+'_"][id_img!="'+handler['id']+'_'+$(this).attr('s')+'"]').each(function(){
				
				if($(this).css('top') == '0px' && $(this).css('left') == '0px')
				{
					$(this).animate({left: $('div[id_img="'+handler['id']+'_'+$(this).attr('s')+'"]').width()+'px'},1000);
					$(this).fadeOut();
				}
			});
			
			$().DDSlideShowRisezeCont(handler,this);
				
			$('div[id_img="'+handler['id']+'_'+$(this).attr('s')+'"]').fadeIn();
			$('div[id_img="'+handler['id']+'_'+$(this).attr('s')+'"]').css({'z-index':DDSlideShowImmagineZIndex+10,left: '-'+$('div[id_img="'+handler['id']+'_'+$(this).attr('s')+'"]').css('width'),top: '0px'});
			$('div[id_img="'+handler['id']+'_'+$(this).attr('s')+'"]').animate({left: '0px'},1000);
			DDSlideShowImmagineAct[handler['id']] = $(this).attr('s');
		},
		DDSlideShowSlideTop: function(handler)
		{
			/*this).animate({top: '-'+$(this).height()+'px'},1000);
			$(this).fadeOut();
			$('div[id_img="'+handler['id']+'_'+$(this).attr('p')+'"]').animate({top: '-'+$('div[id_img="'+handler['id']+'_'+$(this).attr('p')+'"]').height()+'px'},1000);
			$('div[id_img="'+handler['id']+'_'+$(this).attr('p')+'"]').fadeOut();
			$('div[id_img="'+handler['id']+'_'+$('div[id_img="'+handler['id']+'_'+$(this).attr('p')+'"]').attr('p')+'"]').animate({top: '-'+$('div[id_img="'+handler['id']+'_'+$(this).attr('p')+'"]').height()+'px'},1000);
			$('div[id_img="'+handler['id']+'_'+$('div[id_img="'+handler['id']+'_'+$(this).attr('p')+'"]').attr('p')+'"]').fadeOut();
			*/
			$('div[id_img^="'+handler['id']+'_"][id_img!="'+handler['id']+'_'+$(this).attr('s')+'"]').each(function(){
				
				if($(this).css('top') == '0px' && $(this).css('left') == '0px')
				{
					$(this).animate({top: '-'+$(this).height()+'px'},1000);
					$(this).fadeOut();
				}
			});
			
			$().DDSlideShowRisezeCont(handler,this);
			
			$('div[id_img="'+handler['id']+'_'+$(this).attr('s')+'"]').fadeIn();
			$('div[id_img="'+handler['id']+'_'+$(this).attr('s')+'"]').css({'z-index':DDSlideShowImmagineZIndex+10,top: $('#'+handler['idcontenitore']).css('height'),left: '0px'});
			$('div[id_img="'+handler['id']+'_'+$(this).attr('s')+'"]').animate({top: '0px'},1000);
			DDSlideShowImmagineAct[handler['id']] = $(this).attr('s');
		},
		DDSlideShowSlideBottom: function(handler)
		{
			/*$(this).animate({top: $('div[id_img="'+handler['id']+'_'+$(this).attr('s')+'"]').height()+'px'},1000);
			$(this).fadeOut();
			$('div[id_img="'+handler['id']+'_'+$(this).attr('p')+'"]').animate({top: $('div[id_img="'+handler['id']+'_'+$(this).attr('s')+'"]').height()+'px'},1000);
			$('div[id_img="'+handler['id']+'_'+$(this).attr('p')+'"]').fadeOut();
			$('div[id_img="'+handler['id']+'_'+$('div[id_img="'+handler['id']+'_'+$(this).attr('p')+'"]').attr('p')+'"]').animate({top: $('div[id_img="'+handler['id']+'_'+$(this).attr('s')+'"]').height()+'px'},1000);
			$('div[id_img="'+handler['id']+'_'+$('div[id_img="'+handler['id']+'_'+$(this).attr('p')+'"]').attr('p')+'"]').fadeOut();
			*/
			$('div[id_img^="'+handler['id']+'_"][id_img!="'+handler['id']+'_'+$(this).attr('s')+'"]').each(function(){
				
				if($(this).css('top') == '0px' && $(this).css('left') == '0px')
				{
					$(this).animate({top: $('div[id_img="'+handler['id']+'_'+$(this).attr('s')+'"]').height()+'px'},1000);
					$(this).fadeOut();
				}
			});
			
			$().DDSlideShowRisezeCont(handler,this);
				
			$('div[id_img="'+handler['id']+'_'+$(this).attr('s')+'"]').fadeIn();
			$('div[id_img="'+handler['id']+'_'+$(this).attr('s')+'"]').css({'z-index':DDSlideShowImmagineZIndex+10,top: '-'+$('#'+handler['idcontenitore']).css('height'),left: '0px'});
			$('div[id_img="'+handler['id']+'_'+$(this).attr('s')+'"]').animate({top: '0px'},1000);
			DDSlideShowImmagineAct[handler['id']] = $(this).attr('s');
		}
		
	});
})(jQuery);


