﻿(function ($) {
	$.fn.extend({
		slideshow: function (options) {
			var defaults = {
				displaytime: 6000,
				fadespeed: 1000,
				clickfadespeed: 300,
				width: 795,
				height: 330,
				styleWrapper: true,
				navigation: false,
				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;
				var slideBg = $('#slider-bg');
				setup();

				function setup() {
					images = $('img', obj);
					total = images.length - 1;
					obj.css({
						'display': 'block',
						'position': 'relative',
						'overflow': 'hidden',
						'background': '#fff',
						'width': o.width + 'px',
						'height': o.height + 'px'
					});
					$('#news0').addClass('highlight');
					if (o.navigation) {
						obj.append('<div id="slidenav" class="' + o.navPosition + '"></div>');
						slidenav = $('#slidenav');
					}
					if (o.startimage == 'random') {
						startindex = Math.floor(Math.random() * (total + 1));
					} else {
						startindex = 0;
					}
					slideBg.css({ 'top': '-500px' });
					images.each(function (index) {

						$(this).css({
							'position': 'absolute',
							'top': '0',
							'left': '0',
							'display': (index == startindex) ? 'block' : 'none'
						}).attr('ref', index);

						if (typeof ($(this).attr('url')) != 'undefined' && $(this).attr('url') != '') {
							$(this).css({ 'cursor': 'pointer' }).click(function () {
								if ($(this).attr('url').substring(0, 7) == 'http://' || $(this).attr('url').substring(0, 8) == 'https://') {
									window.open($(this).attr('url'));
								} else {
									window.location = $(this).attr('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>');
							}
						}
					});
					if (o.navigation) {
						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();
					});
					if (o.navigation) {
						slidenav.find('label[for!=' + current + ']').removeClass('active');
						slidenav.find('label[for=' + current + ']').addClass('active');
					}
					var slideBgPos = '-500px';

					
					$('.slider-links .lnk').removeClass('highlight');
					if (current == 1) {
						slideBg.animate({ 'top': '-407px' }, 500,function(){$('#news' + current).addClass('highlight')});
					} else if (current == 2) {
						slideBg.animate({ 'top': '-317px' }, 500,function(){$('#news' + current).addClass('highlight')});
					} else {
						slideBg.animate({ 'top': '-500px' }, 250,function(){$('#news' + current).addClass('highlight')});
					}
					
				}

				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;
					});
					if (o.navigation) {
						slidenav.find('label[for!=' + index + ']').removeClass('active');
						slidenav.find('label[for=' + index + ']').addClass('active');
					}
					current = index;
					initiateInterval();
				}
			});
		}
	});
})(jQuery);
