﻿(function ($) {
	$.fn.extend({
		slideshow: function (options){
			var defaults = {
				displaytime: 3000,
				fadespeed: 1000,
				clickfadespeed: 300,
				width: 954,
				height: 400,
				styleWrapper: true,
				navigation: true,
				navPosition: 'br',
				effect: 'rotate',
				startimage:'0'
			};
			var options = $.extend(defaults, options);

			return this.each(function (){
				var obj = $(this);
				var o = options;
				var current = 0;
				var total = 0;
				var images;
				var slidenav;
				var clicked = false;
				setup();
			
				function setup() {
					images = $('img', obj);
					total = images.length -1;
					obj.css({
						'display': 'block',
						'position': 'relative',
						'overflow': 'visible',
						'background': '#fff',
						'width': o.width + 'px',
						'height': o.height + 'px'
					});
					var navStylePosition = 'position:absolute;z-index:1000;';
					switch(o.navPosition){
						case 'br': 	navStylePosition += 'bottom:0;right:5px;'
									break;
						case 'bl':	navStylePosition += 'bottom:0;left:5px;'
									break;
						case 'tr':	navStylePosition += 'top:0;right:5px;'
									break;
						case 'tl':	navStylePosition += 'top:0;left:5px;'
									break;
						default:	navStylePosition += 'bottom:0;right:5px;'
									break;
					}
					obj.append('<div id="slidenav" style="' + navStylePosition + '"></div>');
					slidenav = $('#slidenav');
					if(o.startimage=='random'){
						startindex = Math.floor(Math.random()*(total+1));
					}else{
						startindex = 0;
					}
					images.each(function (index){

						$(this).css({
							'position': 'absolute',
							'top': '0',
							'left': '0',
							'display': (index == startindex) ? 'block' : 'none'
						}).attr('ref',index);							
						
						if(typeof($(this).attr('data-url')) != 'undefined' && $(this).attr('url')!=''){
							$(this).css({'cursor':'pointer'}).click(function (){
								if($(this).attr('data-url').substring(0,7) == 'http://' || $(this).attr('data-url').substring(0,8) == 'https://'){
									window.open($(this).attr('url'));
								}else{
									window.location = $(this).attr('data-url');
								}
							});
						}
						
						if(o.navigation == true){
							if(index==startindex){
								slidenav.append('<label for="' + index + '" class="active">' + (index + 1) + '</label>');
							}else{
								slidenav.append('<label for="' + index + '">' + (index + 1) + '</label>');
							}
						}
					});
					slidenav.find('label').click(function () {
						if(!clicked){
							clicked = true;
							navigateTo($(this).attr('for'));
						}
					});
					initiateInterval();
				}
			
				function initiateInterval()	{
					timer = setInterval(function() {
						switch(o.effect)
						{
							case 'rotate':rotate();break;
							case 'slideleft':slideleft();break;
							default:break;
						}
					},o.displaytime);
				}

				function rotate() {
					if(current  < (total)) {current++} else {current=0}
					
					obj.find('img[ref!=' + current + ']').css({'z-index':'1'});
					obj.find('img[ref=' + current + ']').css({'z-index':'10'}).fadeIn(o.fadespeed, function (){
							obj.find('img[ref!=' + current + ']').hide();
					});
					slidenav.find('label[for!=' + current + ']').removeClass('active');
					slidenav.find('label[for=' + current + ']').addClass('active');
				}

				function navigateTo(index){
					clearInterval(timer);
					obj.find('img[ref!=' + index + ']').css({'z-index':'1'});
					obj.find('img[ref=' + index + ']').css({'z-index':'10'}).fadeIn(o.clickfadespeed, function (){
							obj.find('img[ref!=' + index + ']').hide();
							clicked = false;
					});
					slidenav.find('label[for!=' + index + ']').removeClass('active');
					slidenav.find('label[for=' + index + ']').addClass('active');
					current = index;
					initiateInterval();
				}
			});
		}
	});
})(jQuery);
