// ////////////////////////////////////////////////////////////////////
// SLIDESHOW
(function($) {
	$.creunaSlideshow = function(el, options) {

		var main = this;
		main.$el = $(el).addClass("creunaSlideshow");
		main.$el.data("slider", main);
		
		
		main.list = null;
		main.next = null;
		main.prev = null;
		main.dibs = null;
		main.numElements = 0;
		main.elementWidth = 0;
		main.elementMargin = 0;
		main.index = 0;
		main.indexOld = 0;
		main.active = false;
		main.animated = false;
		main.init = function( ) {
			main.options = $.extend({},$.creunaSlideshow.defaults, options);
			main.active = true;
			main.list = main.$el.find("ul:eq(0)");
               
            // find out how many li there are within list, .length = number of li within the list
			main.numElements = main.list.children().length;
			if (main.options.autoplay) {
				main.list.children(":first").clone().appendTo(main.list);
			}

			main.next = main.$el.find(main.options.next);
			main.prev = main.$el.find(main.options.prev);
			
			// if index is more than one, move to the current slide
			main.index = main.options.startIndex;
			if(main.index > 0)
			{
				main.resize();
			}
			
			if(!main.options.loop)
			{
				if(main.options.startIndex == 0){
					main.prev.hide(); // hide previous button on start
				}
				if(main.options.startIndex >= main.numElements-1 || main.numElements <= 1){
					main.next.hide(); // hide next button on start
				}
			}
			
			if(main.options.dibs)
			{
				main.dibs = main.$el.find("ul:eq(1)");
				main.dibs.empty();
				
				// main.dibs.append('<li><a href="#" class="selected"><span class="offLeft">1</span></a></li>');
				for(var i = 0; i < main.numElements; i++){
					main.dibs.append('<li><a href="#"><span class="offLeft">'+(i+1)+'</span></a></li>');
				}
				main.dibs.find("a").click(function(){
					main.setSlide($(this).parent().index());
					return false;
				}).parent().filter(":eq("+main.options.startIndex+")").children("a").addClass("selected");
			}
			else { //This else is backenders addition, for seting first displayed TwoTreeFourBanner
		        var links = $(".menuBox a");
		        links.removeClass("selected");
				var tabIndex = location.hash.substring(1, 2);
				if (!(tabIndex.length) || tabIndex >= links.length) {
					tabIndex = $("#FirstDisplayedTwoTreeFourBanner").val();
				}
		        links.filter(":eq("+tabIndex+")").addClass("selected");
				main.index = tabIndex;
			}

			if(main.next.length > 0){
				// next button clicked	
				main.next.click(function() { 
					if(main.autoplayClearInterval)
					{
						clearInterval(main.autoplayInterval);
						main.autoplayClearInterval = false;
					}
					if(main.animated || (!main.options.loop && main.index >= main.numElements - 1 ) ) return false;
					
					main.index++; // index + 1
					if(main.index >= main.numElements)
					{
						main.index = main.options.loop ? 0: main.numElements - 1;
					}
					main.setSlide(main.index);
					return false;
				});  	
			}
			
			if(main.prev.length > 0){
				// previous button clicked
				main.prev.click(function() { 
					if(main.autoplayClearInterval)
					{
						clearInterval(main.autoplayInterval);
						main.autoplayClearInterval = false;
					}
					if(main.animated || (!main.options.loop && main.index <= 0 ) ) return false;
					
					main.index--; // index - 1
					if(main.index < 0)
					{
						main.index = main.options.loop ? main.numElements - 1 : 0;
					}
					main.setSlide(main.index);
					return false;
				});	
			}
			if(main.next.length > 0 && main.prev.length > 0){
				// KEY FUNCTIONALITY
				$(window).keyup(function(e){
					if(e.keyCode == 39)
					{
						main.next.click();
					}	
					else if(e.keyCode == 37)
					{
						main.prev.click();
					}	
				});
			}
			
			main.resize();

			
			if(main.options.autoplay)
			{
				main.autoplayInterval = setInterval(function(){
					main.autoplayClearInterval = false;
					main.next.click();
					main.autoplayClearInterval = true;
				}, main.options.sliderInterval || 5000);
				main.autoplayClearInterval = true;

			}
		};
		
		main.resize = function( ) { 
			if(!main.active) return;
		
			// set to full container width
			if(main.options.fullWidth){
				main.list.css("width", main.$el.width()).children().css("width", main.$el.width());
			}
			var li = main.list.find("li:eq(0)");
			if(li.length > 0){
				main.elementWidth = li.width(); // what is the width of each li within list
				main.elementMargin = li.css("margin-right");
				main.elementMargin = Math.round(main.elementMargin.substring(0,main.elementMargin.length-2));
			}
			main.list.width((main.elementWidth+main.elementMargin)*main.numElements+"px");
			main.$el.find(".cont").width(main.elementWidth+"px").css("overflow","hidden").children("ul").children("li").width(main.elementWidth);
			
			// move to slide
			main.list.css({marginLeft: - main.index * (main.elementWidth + main.elementMargin)});
		    if (main.options.autoplay) {
			    main.list.css('width', main.list.width() + main.elementWidth + 1);
		    }
		};
		
		main.setSlide = function( num ) {
			if(!main.active) return;
			main.index = num; // set global index to current given slide index
			main.animated = true; 	
			
			// fade out buttons that can't be used
			if(!main.options.loop){
				if(main.index === main.numElements-1){
					main.next.fadeOut("fast"); // when #next is clicked, hide #next and #previous
				}else if(main.index === 0){
					main.prev.fadeOut("fast"); // when #next is clicked, hide #next and #previous
				}
			}
			
			if(main.options.dibs)
			{
				main.dibs.find(".selected").removeClass("selected");
				main.dibs.find("li").filter(":eq("+num+")").children().addClass("selected");
			}

			// animate elementWidth to the right over 1 sec
			if(main.index == 0 && main.options.autoplay && main.indexOld != 1) {
				main.list.animate({marginLeft: -main.numElements * (main.elementWidth + main.elementMargin) }, 1000, function() {
					// when the list animation is finished
					main.animated = false;
				    main.list.animate({marginLeft: - main.index * (main.elementWidth + main.elementMargin) }, 0, function() {
				    });
					// check the index
					if (main.index < main.numElements - 1)
						main.next.fadeIn("fast"); // fade in the next button with the speed: fast
					if (main.index > 0)
						main.prev.fadeIn("fast"); // fade in the previous button with the speed: fast
				});
			}
			else {
				main.list.animate({marginLeft: - main.index * (main.elementWidth + main.elementMargin) }, 1000, function() {
					// when the list animation is finished
					main.animated = false;

					// check the index
					if (main.index < main.numElements - 1)
						main.next.fadeIn("fast"); // fade in the next button with the speed: fast
					if (main.index > 0)
						main.prev.fadeIn("fast"); // fade in the previous button with the speed: fast
				});
			}
			main.indexOld = main.index;
		};
		main.init();
	};
	$.creunaSlideshow.defaults = {

		// startIndex				: 0,
		next					: ".next",
		prev					: ".previous",
		
		startIndex		: 0,
		fullWidth		: false,
		dibs			: false,
		loop			: true
	};
	
	$.fn.creunaSlideshow = function(options) {
		// initialize the slider
		if ((typeof(options)).match('object|undefined')){
			return this.each(function(i){
				(new $.creunaSlideshow(this, options));
			});
		}else if(typeof options == "string")
		{
			switch(options)
			{
				case 'resize':
					var slider = $(this).data('slider');
					if (undefined != slider)
                        slider.resize();
					
				break;
				case 'setSlide':
					var slider = $(this).data('slider');
					if (undefined != slider)
					    slider.setSlide(arguments[1]);
				break;
			}
		}
	};
})(jQuery);


