/**
 * Slide teaser
 */
(function ($) {    
    $.fn.slideTeaser = function (options) {
        var defaults = { 
            'interval' : 0
        };
        var settings = $.extend({}, defaults, options);
        var interval = null;
        var current = null;
        
        return this.each(function () {
        	var $container = $(this).find('.container');
        	var $panels = $(this).find('.container > div');

        	// calculate a new width for the container (so it holds all panels)
            $container.css('width', $panels[0].offsetWidth * $panels.length);
            var $scroll = $(this).find('.scroll').css('overflow', 'hidden');

        	// handle nav selection
        	function selectNav() {
        	    $(this).parents('ul').find('li').removeClass('selected');
             	$(this).parent().addClass('selected');
             	current = $(this).parent();
        	}

        	// go find the navigation link that has this target and select the nav
        	function trigger(data) {
        	    var el = $('.slideteaser .navigation').find('a[href$="' + data.id + '"]').get(0);
        	    selectNav.call(el);
            }

            $(this).find('.navigation li:first').addClass('selected');
            $(this).find('.navigation li a').click(function() {
                clearInterval(interval);
            });

        	// offset is used to move to *exactly* the right place, since I'm using
        	// padding on my example, I need to subtract the amount of padding to
        	// the offset.  Try removing this to get a good idea of the effect
        	var offset = parseInt($container.css('paddingTop')) * -1;

        	var scrollOptions = {
        	  target: $scroll, // the element that has the overflow

        	  // can be a selector which will be relative to the target
        	  items: $panels,

        	  navigation: '.navigation a',

        	  // allow the scroll effect to run both directions
        	  axis: 'x',

        	  onAfter: trigger, // our final callback

        	  offset: offset,

        	  // duration of the sliding effect
        	  duration: 400,

        	  // easing - can be used with the easing plugin: 
        	  // http://gsgd.co.uk/sandbox/jquery/easing/
        	  easing: 'swing'
        	};


        	// apply serialScroll to the slider - we chose this plugin because it 
        	// supports// the indexed next and previous scroll along with hooking 
        	// in to our navigation.
        	$(this).serialScroll(scrollOptions);
        	
        	if (settings.interval > 0) {
        	    interval = setInterval(function() {
        	        $scroll.trigger('next');
        	    }, (settings.interval * 1000));
        	}
        });
    };    
})(jQuery); 
 
/**
 * Newsticker
 */
$(function() {
	if ($('.newsticker').length < 1) return;
    var $panels = $('.newsticker ul > li');
	var index = 0;
	setInterval(function() {
	        var oldindex = index;
	        index++;
	        if (index >= $panels.length) index = 0;
	        $($panels[oldindex]).fadeOut(function() {
    	        $($panels[index]).fadeIn();
	        });
	    }, 6000);
});