/*
 * Katakomben Spots - v1.0 - 2011-06-08
 * http://www.ifox.nl
 * Author - Tim de Vos
*/
(function($) {
	$.fn.katslide = function(options) {
		var opts = $.extend({}, $.fn.katslide.defaults, options);
		return this.each(function(i) {
			var $this 				= $(this);
			$this.opts 				= $.extend({}, opts);
			$this.opts.current		= 0;
			$this.opts.targetpos	= 0;
			$this.opts.navdisabled	= true;
			$this.opts.to			= null;
			$this.opts.vimeoPlayers = Array();
			
			
			// ADD GLOW SPAN
			$glow = $('<span class="glow"></span>').hide();
			if ( $.browser.msie ) {
				$glow.addClass('ie');
			}
			$glow.appendTo($(this));
			$.fn.katslide.glow($this, $glow); // START GLOWING..
			
			// CLOSE ACTION
			$this.find('.flag a.close').bind('click', function(){
				$.fn.katslide.pauseCurrentMovie($this);
				$.fn.katslide.hideflag($this);
			});
			
			// ADD NAVIGATION?
			$this.opts.slides = $this.find('.flag .slideshow li').length;
			$this.opts.slideshowwidth = 0;
			
			if($this.opts.slides > 1){
				
				$this.find('.flag .slideshow li').each(function(i){
					$this.opts.slideshowwidth += $(this).width();
				});
				$this.find('.flag .slideshow li').each(function(i){
					$clone = $(this).clone();
					$clone.addClass('clone').prependTo($this.find('.slideshow ul'));
					$clone.find('.video').attr('title', $clone.find('.video').attr('title') + '-clone-1');
				});
				
				$this.find('.flag .slideshow li:not(.clone)').each(function(i){
					$clone = $(this).clone();
					$clone.addClass('clone').appendTo($this.find('.slideshow ul'));
					$clone.find('.video').attr('title', $clone.find('.video').attr('title') + '-clone-2');
				});
				
				$this.find('.video').each(function(){
					$(this).html('<iframe src="http://player.vimeo.com/video/'+$(this).attr('rel')+'?title=0&amp;byline=0&amp;portrait=0&amp;api=1&amp;player_id='+$(this).attr('title')+'" id="'+$(this).attr('title')+'" width="100%" height="100%" frameborder="0"></iframe>');
				})	
					
				$('<a class="left">&nbsp;</a>').appendTo($this.find('.flag'));
				$('<a class="right">&nbsp;</a>').appendTo($this.find('.flag'));
				$this.find('.flag .right').click(function(){
					$.fn.katslide.slide($this, 1);					  
				});
				$this.find('.flag .left').click(function(){
					$.fn.katslide.slide($this, -1);					  
				});
			}
			
			
			
			// SHOW INFO TEXT
			$this.find('a.info').bind('click', function(){	
				$this.opts.navdisabled	= true;
				$.fn.katslide.hidenav($this)
														
				$this.find('.text').show();					
				$max = $this.find('.slideshow li').height() - 50;
				if($this.find('.text .scroll').height() > $max){
					$this.find('.text .scroll').css({'height':$max+'px'}).jScrollPane();
				}
				
				$this.find('.text').css('opacity',0).fadeTo(300, 1, function(){
					$(this).css('filter','');										   
				});
			});
			
			// HIDE INFO TEXT
			$this.find('a.closetext').bind('click', function(){
				$this.opts.navdisabled	= false;
				//$.fn.katslide.shownav($this)
														
				$this.find('.text').css('opacity',1).fadeTo(300, 0, function(){
					$(this).hide().css('filter','');										   
				});
			});
			
			
			// HOVER STATE
			$this.find('a.open').hover(function(){
				$this.find('.glow').stop().fadeTo(300, $this.opts.glowopacitymax, function(){});
				$this.addClass('hovered');
			},function(){
				$.fn.katslide.glow($this, $this.find('.glow'));
				$this.removeClass('hovered');
			});
			
			// OPEN FLAG
			$this.find('a.open').click(function(){
				$.fn.katslide.showflag($this, $(this).parent());
			});
			
			$this.find('.flag').hover(
				function(){
					$.fn.katslide.shownav($this);
				}, 
				function(){
					$.fn.katslide.hidenav($this)
				}
			);

		});
	};
	
	$.fn.katslide.shownav = function($this){
		if($this.opts.navdisabled == false){
			$this.find('a.left, a.right').stop().fadeTo(300,1, function(){});
		}
	};
	$.fn.katslide.hidenav = function($this){
		$this.find('a.left, a.right').stop().fadeTo(300,0, function(){	
			$(this).hide();
		});
	};
	
	$.fn.katslide.slidearrange = function($this){
		currentpos = parseInt($this.find('.slideshow ul').css('left'));
		change = false;
		if(currentpos  < -2 * $this.opts.slideshowwidth){
			currentpos += $this.opts.slideshowwidth;
			$this.opts.targetpos += $this.opts.slideshowwidth;
			change = true;
		}
		
		if(currentpos  > -$this.opts.slideshowwidth){
			currentpos -= $this.opts.slideshowwidth;
			$this.opts.targetpos -= $this.opts.slideshowwidth;
			change = true;
		}
		
		if(change){
			$this.find('.slideshow ul').stop().css('left', currentpos)
			$.fn.katslide.slide($this, 0);
		}
	}
	$.fn.katslide.pauseCurrentMovie = function($this){	
		$nr = $this.opts.current + ($this.find('.slideshow ul li').length/3);
		$iframe = $this.find('.slideshow ul li:eq('+$nr+') iframe');
		if($iframe.length > 0){
			froogaloop = $f($iframe.attr('id'));
			froogaloop.api('pause');
		}
	}
	
	$.fn.katslide.slide = function($this, $amount){	
		$.fn.katslide.pauseCurrentMovie($this);
		$this.opts.current += $amount;
		
		if($this.opts.current >= $this.opts.slides){
			$this.opts.current = 0;
		}
		if($this.opts.current < 0){
			$this.opts.current = $this.opts.slides - 1;
		}
		
			
		
		$this.opts.targetpos -= $amount * $this.find('.slideshow li').width();
		
		clearInterval($this.opts.to);
		$this.opts.to = setInterval(function(){ $.fn.katslide.slidearrange($this); }, 20);
		$this.find('.slideshow ul').stop().animate({'left':$this.opts.targetpos  +'px'},400, function(){
			clearInterval($this.opts.to);
		});
	};
	
	$.fn.katslide.resetslider = function($this){
		$.fn.katslide.pauseCurrentMovie($this);
		$this.find('.text').hide().css('filter','');	
		$this.find('.flag, a.left, a.right').css('filter','');
		$this.opts.current = 0;
		$this.opts.navdisabled	= true;
		$this.opts.targetpos = -$this.opts.slideshowwidth;
		$this.find('.slideshow ul').css({'left':$this.opts.targetpos+'px'});
	},
	
	$.fn.katslide.showflag = function($this){
		if(!$this.hasClass('active')){
			// ADD FANCYBOX PLUGIN
			$this.find("a[href$='.jpg']").fancybox({overlayColor:'#000000', 'titlePosition':'inside'});	
			
			$.fn.katslide.resetslider($this);
			
			// CLOSE OTHER ACTIVE SLIDER
			$('.points li.active').each(function(){
				$.fn.katslide.hideflag($(this));
			});
			
			$this.addClass('active');
			leftpos 	= ($this.find('.flag').width()-13) / -2;
			toppos 		= -$this.find('.flag').height() + 9;
			overlapy 	= $this.offset().top + toppos - 40;
			overlapx	= $this.offset().left + leftpos - 40;
			newy = parseInt($('.drag').css('top'));
			newx = parseInt($('.drag').css('left'));
			
			if(overlapy < 0){ newy = (newy-overlapy); }
			if(overlapx < 0){ newx = (newx-overlapx); }
			
			$('.drag').animate({'top':newy+'px','left':newx+'px'},200);
			
			$this.find('.flag').css('marginTop','9px');
			$this.find('a.close').css('bottom','0px');	
			$this.find('.flag').css({left:leftpos+'px', top:toppos+'px'}).stop().fadeTo(500,1, function(){
				
				$(this).css('filter','');															
				$this.find('a.close').animate({'bottom':'-9px'},200);																
				$this.find('.flag').animate({'marginTop':'0px'},200, function(){
					setTimeout(function(){
						$this.opts.navdisabled	= false;
						$.fn.katslide.shownav($this);
					},100);
				});							
			});
		}
	};
	
	$.fn.katslide.hideflag = function($this){
		$this.removeClass('active').find('.flag').stop().fadeTo(300,0, function(){
			$(this).hide();
		});
	};
	
	$.fn.katslide.glow = function($this, $punt){
		if($punt.hasClass('on')){
			$punt.removeClass('on').fadeTo(getRandom($this.opts.glowtimeoutmin, $this.opts.glowtimeoutmax), 0, function(){	
				$.fn.katslide.glow($this, $(this));
			});
		}else{
			$punt.addClass('on').fadeTo(getRandom($this.opts.glowtimeoutmin, $this.opts.glowtimeoutmax), (getRandom(($this.opts.glowopacitymin*100),($this.opts.glowopacitymax*100))/100), function(){
				$.fn.katslide.glow($this, $(this));
			});
		}
	};
	
	$.fn.katslide.defaults = {
		glowtimeoutmin : 1000,
		glowtimeoutmax : 1600,
		glowopacitymin : 0.2,
		glowopacitymax : 0.4
	};
	
})(jQuery);
