/**
 * @author thespiritx
 */
(function($){$.fn.slideshow = function(){ 
        //Set options
        options = {
			'speed': 6000,
			'caption': 'slide',
			'opacity': 0.7
		};
        if (this.attr("rel")) {
            var indOptions = this.attr("rel").split(',');
            for (var n in indOptions) {
                var s = indOptions[n].split(':');
                options[s[0]] = s[1];
            }
        }
        
        //Check for ID; if none set
        var selector = '';
        if (this.attr("id")) {
            selector = this.attr("id");
        }
        else {
            selector = "slideshow_" + Math.floor(Math.random() * 1000)
            this.attr("id", selector);
        }
        selector = '#' + selector;
        
        //Set the opacity of all images to 0
        $(selector + ' a').css({
            opacity: 0.0
        });
        
        //Get the first image and display it (set it to full opacity)
        $(selector + ' a:first').css({
            opacity: 1.0
        });
        
        //Set the caption background to semi-transparent
        $(selector + ' .caption').css({
            opacity: options['opacity']
        });
        
		//Set buttons for quick access
		
        //Resize the width of the caption according to the image width
        //$(selector+' .caption').css({width: '160px'});
        
		var title = $('<h5/>').html($(selector + ' a:first').find('img').attr('title')).css('opacity','1');
		var caption = $('<div/>').html($(selector + ' a:first').find('img').attr('rel')).css('opacity','1');
		
        //Get the caption of the first image from REL attribute and display it
		$(selector + ' .description').html();
		$(selector + ' .description').append(title);
		$(selector + ' .description').append(caption);
        $(selector + ' .description').animate({
            opacity: options['opacity']
        }, 400);
		
		
		title = $(selector + ' a:first').find('img').attr('title');
		
		// Requires Shadowbox and function sb()
		var media = '';
		// Set Up Number Links to progress in slideshow
		$(selector + ' .numbers .links').html('');
		$(selector + ' a').each(function(key, value){
			var number = $('<div/>').addClass('msbutton_'+key).html(Number(key)+1);
			if(Number(key) == 0) number.addClass('current');
			number.click(function(){
				var key = $(this).attr('class').replace('msbutton_','');
				var ss = $(this).closest('.slideshow');
				$(ss).slideshowskipto(key);
			});
			$(selector + ' .numbers .links').append(number);
		});
		if ($(selector + ' a:first').attr('rel')) {
			/*
			$(selector + ' .media').css({
				'display': 'block',
				'opacity': '.5',
				'filter': 'alpha(opacity=50)'
			})
			*/;
			mediaAttr = $(selector + ' a:first').attr('rel').split(';');
			file = (mediaAttr[0]=='video')?mediaAttr[1]:mediaAttr[1]+'&image='+$(selector + ' a:first img').attr('src');
			/*
			$(selector + ' .media').attr('rel','title::'+title+';file::'+file);
			*/
			$(selector + ' .playtext').css({
				'display': 'block',
				'opacity': '1',
				'filter': 'alpha(opacity=100)'
			});
			var mopimg = ['<img src="/wgte/images/','video','.png" alt="" class="msImg"/>'];
			if (mediaAttr[0].toLowerCase() == 'audio') {
				mopimg[1] = "audio";
			}
			mopimg = mopimg.join('');
			$(selector + ' .playtext').html('<a href="#">Play&nbsp;'+mediaAttr[0].slice(0,1).toUpperCase() + mediaAttr[0].slice(1) + ' ' + mopimg + '</a>');
			$(selector + ' .playtext').attr('rel','title::'+title+';file::'+file);
		}
		/*
		$(selector + ' .media').click(function(){
			var a = $(this).attr('rel').split(';');
			var moptions = {
				title: '',
				file: ''
			}
			$(a).each(function(key,value){
				var b = value.split('::');
				moptions[b[0]] = b[1];
			});
			sb(moptions['title'],moptions['file']);
		});
		*/
		$(selector + ' .playtext').click(function(){
			var a = $(this).attr('rel').split(';');
			var moptions = {
				title: '',
				file: ''
			}
			$(a).each(function(key,value){
				var b = value.split('::');
				moptions[b[0]] = b[1];
			});
			sb(moptions['title'],moptions['file']);
		});
        
        //Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
		if(typeof document.intervals == 'undefined') document.intervals = [];
		document.intervals[selector] = setInterval("$('" + selector + "').slideshowmove()", options['speed']);
        //$(this).rotate = setInterval("$('" + selector + "').slideshowmove()", options['speed']);
        
        return this;
    }
    $.fn.slideshowmove = function(){
    
        var key=typeof arguments[0] != 'undefined'?arguments[0]:false, selector = '';
        
        if (this.attr("id")) {
            selector = '#' + this.attr("id");
        }
		
		if(typeof sb == 'undefined') sb = {halt: false};
		
		if (!sb.halt) {
		
			//Set options
			var options = {
				'caption': 'slide',
				'opacity': 0.7
			};
			if (this.attr("rel")) {
				var indOptions = this.attr("rel").split(',');
				for (var n in indOptions) {
					var s = indOptions[n].split(':');
					options[s[0]] = s[1];
				}
			}
			
			//if no IMGs have the show class, grab the first image
			var current = ($(selector + ' a.show') ? $(selector + ' a.show') : $(selector + ' a:first'));
			
			//Get next image, if it reached the end of the slideshow, rotate it back to the first image
			if(key){
				var next = $(selector + ' a.msitem_' + key);
			}else{
				var next = ((current.next().length) ? ((current.next().hasClass('caption')) ? $(selector + ' a:first') : current.next()) : $(selector + ' a:first'));
			}
			
			//Get next image caption
			var title = $('<h5/>').html(next.find('img').attr('title')).css('opacity','1');
			var caption = $('<div/>').html(next.find('img').attr('rel')).css('opacity','1');
			
			//This part requires Shadowbox and the custom function sb(name,media)
			var media = '';
			if (next.attr('rel')) {
				mediaAttr = next.attr('rel').split(';');
				text_title = next.find('img').attr('title');
				file = (mediaAttr[0]=='video')?mediaAttr[1]:mediaAttr[1]+'&image='+next.find('img').attr('src');
				$(selector + ' .media').attr('rel','title::'+text_title+';file::'+file);
				$(selector + ' .playtext').attr('rel','title::'+text_title+';file::'+file);
			}
			
			//De-Highlight old number; Highlight the new number
			$(selector + ' .numbers .links div').removeClass('current');
			var key = next.attr('class').replace('msitem_','');
			$(selector + ' .numbers .links div.msbutton_'+key).addClass('current');
			
			//Set the fade in effect for the next image, show class has higher z-index
			next.css({
				opacity: 0.0
			}).addClass('show').animate({
				opacity: 1.0
			}, 1000);
			
			//Hide the current image
			current.animate({
				opacity: 0.0
			}, 1000).removeClass('show');
			
			//Set the opacity to 0 and width to 1px
			switch (options['caption']) {
				case 'fade':
					$(selector + ' .caption').animate({
						opacity: 0.0
					}, 300);
					break;
				case 'slide':
				default:
					$(selector + ' .caption').animate({
						opacity: 0.0
					}, {
						queue: false,
						duration: 0
					}).animate({
						width: '1px'
					}, {
						queue: true,
						duration: 300
					});
			}
			
			if (current.attr('rel')) {
				/*
				$(selector + ' .media').animate({
					opacity: 0.0
				}, 300);
				*/
				$(selector + ' .playtext').animate({
					opacity: 0.0
				}, 300);
			}
			
			//Animate the caption, opacity to 0.7 and width to 160px, a slide up effect
			switch (options['caption']) {
				case 'fade':
					$(selector + ' .caption').animate({
						opacity: options['opacity']
					}, 300);
					break;
				case 'slide':
				default:
					$(selector + ' .caption').animate({
						opacity: options['opacity']
					}, 100).animate({
						width: '160px'
					}, 500);
			}
			
			if (next.attr('rel')) {
				/*
				$(selector + ' .media').css({
					'display': 'block'
				});
				$(selector + ' .media').animate({
					opacity: .5
				}, 300);
				*/
				var moptions = next.attr('rel').split(';');
				var mopimg = ['<img src="/wgte/images/','video','.png" alt=""/>'];
				if (moptions[0].toLowerCase() == 'audio') {
					mopimg[1] = "audio";
				}
				mopimg = mopimg.join('');
				$(selector + ' .playtext').html('<a href="#">Play&nbsp;'+moptions[0].slice(0,1).toUpperCase() + moptions[0].slice(1) + ' ' + mopimg + '</a>');
				$(selector + ' .playtext').css({
					'display': 'block'
				});
				$(selector + ' .playtext').animate({
					opacity: 1
				}, 300);
			}
			else {
				/*
				$(selector + ' .media').css({
					'display': 'none'
				});
				*/
				$(selector + ' .playtext').css({
					'display': 'none'
				});
			}
			
			
			//Display the description
			$(selector + ' .description').html('');
			$(selector + ' .description').append(title);
			$(selector + ' .description').append(caption);
			
		}
		
		return this;
    }
	$.fn.slideshowskipto = function(){
		var key=typeof arguments[0]!='undefined'?arguments[0]:false, selector = '';
		var selector = '';
        
        if (this.attr("id")) {
            selector = '#' + this.attr("id");
        }
		
		var options = {
			'speed': 6000,
			'caption': 'slide',
			'opacity': 0.7
		};
        if ($(this).attr("rel")) {
            var indOptions = $(this).attr("rel").split(',');
            for (var n in indOptions) {
                var s = indOptions[n].split(':');
                options[s[0]] = s[1];
            }
        }
		
		clearInterval(document.intervals[selector]);
		$(this).slideshowmove(key);
		document.intervals[selector]= setInterval("$('" + selector + "').slideshowmove()", options['speed']);
		
		return this;
	}
})(jQuery);
$(document).ready(function(){
    $('.slideshow').each(function(key, value){
        $(value).slideshow();
    });
});

