// JavaScript Document



(function($) {
		
    $.fn.hpSlideshow = function(options) {
        return this.each(function() {   
            
			var show = $.hpSlideshow(this, options);
			if (show.settings.type == "sequence") {
                show.timer = setTimeout(function() {
                    $($("#"+show.id)).goToSlide(show.active+1, 'play');
                }, show.settings.timeout);
            } 
			
			$.hpSlideshow.shows[this.id] = show;
			/*
			$(this).parent().click(function(e) {
							console.log(e);
				var id = this.id.slice(0,-8)
				var obj = $.hpSlideshow.shows[id];
				var container = $(this).find('.'+obj.settings.runningclass);
				var len = $(container).children().length;
				
				if($(e.target).hasClass(obj.settings.navClass)) {
					
					var control = $("#playControl");
					var targetSlide = new Number(e.target.hash.substring(1));
					var rel = e.target.rel;
					console.log(typeof(rel));
					if(typeof(rel) == 'string') {
						switch(rel) {
							case 'play':
								$("#playControl").attr('rel','pause').find("img").attr("src", $.hpSlideshow.iconPath.pause());
								$(container).removeClass('slideshow-stop');
						
								if(targetSlide == len) targetSlide = 0;
								$(container).goToSlide(new Number(targetSlide), 'play');

								break;
							case 'pause':
								if(!($(container).hasClass('slideshow-stop'))) $(container).addClass('slideshow-stop');
									
								$("#playControl").attr('rel','play').find("img").attr("src", $.hpSlideshow.iconPath.play());							
								break;
						}
						
					}
					else {
							
						$(container).goToSlide(targetSlide, 'pause');
						
						$("#playControl").attr('rel','play').find("img").attr("src", $.hpSlideshow.iconPath.play());
						
					}
					
					
					if(e.target.id != "playControl" && $("#playControl").attr('rel') == 'pause') {
						//clearTimeout($.hpSlideshow.shows[id].timer);
						//$.hpSlideshow.shows[id].timer = null;
						if(!($(container).hasClass('slideshow-stop'))) $(container).addClass('slideshow-stop');
							
						$(container).goToSlide(targetSlide, 'pause');
						
						$("#playControl").attr('rel','play').find("img").attr("src", $.hpSlideshow.iconPath.play());
						
					}
					
					else if(e.target.id == "playControl" && $("#playControl").attr('rel') == 'pause') {
						if(!($(container).hasClass('slideshow-stop'))) $(container).addClass('slideshow-stop');
							
						$("#playControl").attr('rel','play').find("img").attr("src", $.hpSlideshow.iconPath.play());							
					}
					
					else {
						$("#playControl").attr('rel','pause').find("img").attr("src", $.hpSlideshow.iconPath.pause());
						$(container).removeClass('slideshow-stop');
						
						if(targetSlide == len) targetSlide = 0;
						$(container).goToSlide(new Number(targetSlide), 'play');
						
						
						
					}
						$("#playControl").attr("href","#"+(targetSlide+1));
						
				
					
					return false;
				}
				else return true;
				
				else {
					if(obj.timer) {	
						clearTimeout(obj.timer);
						obj.timer = null;
					}
					else
						$.hpSlideshow.goTo(obj, obj.active, 'play');
					
				}
				
			});*/
        });
    };
	
	

    $.hpSlideshow = function(container, options) {

		var settings,elements,timer = null;
		settings = $.hpSlideshow.defaultSettings;
		
	
		var handleClick = function(e) {
			var container = $(this).parents(".navList").prev().get(0);
			var id = container.id.slice(0,-8)
			var obj = $.hpSlideshow.shows[id];
			var len = $(container).children().length;
			var active = $('.selected').get(0);
			
			var targetSlide = new Number(this.hash.substring(1));
			var rel = this.rel;
			if(isNaN(rel)) {
				switch(rel) {
					case 'play':
						$("#playControl").attr('rel','pause').find("img").attr("src", $.hpSlideshow.iconPath.pause());
						$(container).removeClass('slideshow-stop');
				
						if(targetSlide == len) targetSlide = 0;
						$(container).goToSlide(new Number(targetSlide), 'play');

						break;
					case 'pause':
						$(container).addClass('slideshow-stop');
							
						$("#playControl").attr('rel','play').attr('href', '#'+(new Number(active.hash.substring(1))+1)).find("img").attr("src", $.hpSlideshow.iconPath.play());
						clearTimeout($.hpSlideshow.shows[container.id].timer);
						break;
				}
				
			}
			else {
					
				$(container).goToSlide(targetSlide, 'pause');
				
				$("#playControl").attr('rel','play').attr("href","#"+(targetSlide+1)).find("img").attr("src", $.hpSlideshow.iconPath.play());
				
			}
			
			return false;
		};
		
        if (options)	$.extend(settings, options);
        
		
		if (settings.children === null) elements = $(container).children();
        
		else elements = $(container).children(settings.children);
        
		
		if (elements.length > 1) {
			
		    $(container).css('position', 'relative').css('height', settings.containerheight).addClass(settings.runningclass);
        
		    for (var i = 0; i < elements.length; i++) {
                $(elements[i]).css('z-index', String(elements.length-i)).css('position', 'absolute').addClass('slideshowEl').hide();
            };
		
			if(settings.navigation == 'number') {
				var nav = $('<div/>').addClass('navList').append('<ul>');
				var id = container.id+'-wrapper';
				
				if(settings.navControl) {
					var $a = $("<a href='#' id='playControl' rel='pause'><img src='"+$.hpSlideshow.iconPath.pause()+"' alt='Play/Pause'/></a>").addClass(settings.navClass).addClass('navListControl').click(handleClick);
					$('<li>').appendTo(nav.children('ul')).append($a);
				}
				
				for(var j = 0; j < elements.length; j++) {
					$a = $("<a href='#"+j+"' rel='"+j+"' id='navListLink-"+j+"'>"+(j+1)+"</a>").addClass(settings.navClass).addClass('indexLink').click(handleClick);
					$('<li>').appendTo(nav.children('ul')).append($a);
				}
				$(container).wrap('<div class="hpSlideshow-container" id="'+id+'"/>').after(nav);
				
				$('#'+id).css('width', settings.containerwidth);
			}
			
			$(elements[0]).show();
			$('.navList a.indexLink:first').addClass('selected');
			
			return {elements: elements, settings: settings, active: 0, id: container.id};
			
		}
    };
	
	$.hpSlideshow.shows = {};
	$.hpSlideshow.iconPath = {
		path: "/sitespecific/webassets/images/slideshow/",
		play: function() {
			return this.path+"play.png";
		},
		pause: function() {
			return this.path+"pause.png";
		},
		foFrward: function() {
			return this.path+"arrow-right.png";
		},
		goBack: function() {
			return this.path+"arrow-left.png";
		}
	};
	
	$.fn.goToSlide = function(n, playMode) {
		
		var show = $.hpSlideshow.shows[this[0].id];
		var speed = show.settings.speed;
		var activeEl = $(this[0]).find('li').get(show.active);
		var nextEl = $(this[0]).find('li').get(n);
		if(playMode == 'pause') speed = 0;
		
		var animate = function(prev, next, aniType, speed) {
			if (aniType == 'slide') {
				$(prev).slideUp(speed, function() {
					$('.navList').find('.selected').removeClass('selected');
					$('#navListLink-'+n).addClass('selected');
				}).removeClass('activeEl');
				$(next).slideDown(speed).addClass('activeEl');
				show.active = show.elements[n];
			} 
			else if (aniType == 'fade') {
				$(prev).fadeOut(speed, function() {
					$('.navList').find('.selected').removeClass('selected');
					$('#navListLink-'+n).addClass('selected');
				}).removeClass('activeEl');
				$(next).fadeIn(speed, function() {
					removeFilter($(this)[0]);
				}).addClass('activeEl');
				$.hpSlideshow.shows[show.id].active = n;
			} else
				alert('hpSlideshow-animationtype must either be \'slide\' or \'fade\'');	
		}
		
		if(show.active != n) {
		
			animate(activeEl, nextEl, show.settings.animationtype, show.settings.speed);
		}
			
			
		if(playMode == 'play' && !($(this[0]).hasClass('slideshow-stop'))) { $.hpSlideshow.play(this[0]); 	}
		
		else if(playMode == 'pause') {
			clearTimeout($.hpSlideshow.shows[show.id].timer);
		}
    };
	
	
	$.hpSlideshow.play = function(elements){
		var show = $.hpSlideshow.shows[elements.id];
		var next = show.active + 1;
		if(next >= show.elements.length) next = 0;
		if(!($(elements).hasClass('slideshow-stop'))) {
			$.hpSlideshow.shows[show.id].timer = setTimeout((function() {
				$(elements).goToSlide(next, 'play');
			}), show.settings.timeout);
		}
	}

	
	$.hpSlideshow.defaultSettings = {
		'animationtype':    'fade',
		'speed':            'normal',
		'type':             'sequence',
		'timeout':          2000,
		'containerheight':  'auto',
		'containerwidth':	'640px',
		'runningclass':     'hpSlideshow',
		'children':         null,
		'navigation':		'number',
		'navigationImage':	'',
		'navClass': 	'slideshow-nav',
		'navControl':	true
	};

})(jQuery);

// **** remove Opacity-Filter in ie ****
function removeFilter(element) {
	if(element.style.removeAttribute){
		element.style.removeAttribute('filter');
	}
}

