(function($){
    if(!$.b8){
        $.b8 = new Object();
    };
    
    $.b8.castingSlider = function(el, options){
        // To avoid scope issues, use 'base' instead of 'this'
        // to reference this class from internal events and functions.
        var base = this;
        
        // Access to jQuery and DOM versions of element
        base.$el = $(el);
        base.el = el;
        base.t = null;
        base.nextSlidePos = '-=605';
        
        // Add a reverse reference to the DOM object
        base.$el.data("b8.castingSlider", base);
        
        base.init = function(){
            
            base.options = $.extend({},$.b8.castingSlider.defaultOptions, options);
            
            // Put your initialization code here
            
            // register link handler
						$('.kh-link').bind('click', function(e) {
							$('.kh-link').removeClass('selected');
							$(e.target).addClass('selected');
							base.selectPage($(e.target).attr('rel'));
						});
						
						// register next link
						$('.kh-link-more').bind('click', function(e) {
							base.slideNext('.kh-page2');
						});
						
            
            // clone first image in every carousel after the last to enable endless scrolling 
            $('.kh-page1 ul li:first').clone().appendTo('.kh-page1 ul');
            $('.kh-page2 ul li:first').clone().appendTo('.kh-page2 ul');
            
            base.selectPage('kh-page1');
        };
        
        
        base.slideNext = function(pageID) {
        	$(pageID + ' ul').animate({marginLeft:'-=605'}, 500, 'swing', function() {
						// check if last slide is reached
						if ($(pageID + ' ul').css('margin-left') == (($(pageID + ' ul li').length - 1) * 605 * -1) + 'px') {
							$(pageID + ' ul').css({'marginLeft':'0'});
						} else {
	        		base.nextSlidePos = '-=605';
	        	}
					})
        };
        
        base.selectPage = function(pageID){
        	switch(pageID) {
        	case 'kh-page1':
        		$('.kh-page1').css({display:'block'});
        		$('.kh-page2').css({display:'none'});
        		$('.kh-page3').css({display:'none'});
        		
        		$('.kh-page1 ul').css({marginLeft:'0'});
        		$('.kh-page2 ul').css({marginLeft:'0'});

        		base.nextSlidePos = '-=605';
        		
        		if(base.t) {
        			clearInterval(base.t);
        			base.t = null;
        		}
        		
            base.t = setInterval(function(){
            	$('.kh-page1 ul').animate({marginLeft:base.nextSlidePos}, 500, 'swing', function() {
								// check if last slide is reached
								if ($('.kh-page1 ul').css('margin-left') == (($('.kh-page1 ul li').length - 1) * 605 * -1) + 'px') {
									$('.kh-page1 ul').css({'marginLeft':'0'});
								} else {
			        		base.nextSlidePos = '-=605';
								}
							})
            }, 4000);
        		
        		break;
        			
        	case 'kh-page2':
        		$('.kh-page1').css({display:'none'});
        		$('.kh-page2').css({display:'block'});
        		$('.kh-page3').css({display:'none'});

        		$('.kh-page1 ul').css({marginLeft:'0'});
        		$('.kh-page2 ul').css({marginLeft:'0'});
        		
        		base.nextSlidePos = '-=605';

        		if(base.t) {
        			clearInterval(base.t);
        			base.t = null;
        		}
        		
        		break;
        		
        	case 'kh-page3':
        		$('.kh-page1').css({display:'none'});
        		$('.kh-page2').css({display:'none'});
        		$('.kh-page3').css({display:'block'});

        		$('.kh-page1 ul').css({marginLeft:'0'});
        		$('.kh-page2 ul').css({marginLeft:'0'});
        		
        		base.nextSlidePos = '-=605';

        		if(base.t) {
        			clearInterval(base.t);
        			base.t = null;
        		}

        		break;
        		
        	}

        };
        
        // Run initializer
        base.init();
    };
    
    $.b8.castingSlider.defaultOptions = {
        interval: 3
    };
    
    $.fn.b8_castingSlider = function(options){
        return this.each(function(){
            (new $.b8.castingSlider(this, options));
        });
    };
    
})(jQuery);

