/*

	----------------------------------------------------------------------------------------------------
	Accessible News Slider
	----------------------------------------------------------------------------------------------------
	
	Author:
	Brian Reindel
	
	Author URL:
	http://blog.reindel.com

	License:
	Unrestricted. This script is free for both personal and commercial use.

        Patched by Alexander Menk <a.menk@netz98.de>, 2011-09-06
        Info: Display images centered

	Patched by Marc Becker <m.becker@netz98.de>
	Info: Not hidding slider arrows, just adding a class
	
	Updated by Marc Scherer <m.scherer@netz98.de>
	Info: added 'reload on slide' image function

*/

jQuery.fn.accessNews = function( settings ) {
	settings = jQuery.extend({
        headline : "Top Stories",
        speed : "normal",
		slideBy : 2
    }, settings);
    return this.each(function() {
		jQuery.fn.accessNews.run( jQuery( this ), settings );
    });
};

jQuery.fn.accessNews.run = function( $this, settings ) {
	jQuery( ".javascript_css", $this ).addClass('not-active');
	var ul = jQuery( "ul:eq(0)", $this );
	var li = ul.children();
	var liItems = li.length;
	var liWidth = parseInt( jQuery( li[0] ).css("width") );
	ul.css( "width", ( liItems * liWidth ) );

        // center images
	var maskWidth = jQuery('.catalogslider-mask').width();
        var ulWidth = ul.width();
        if ( maskWidth > ulWidth ) {
            ofsLeft = Math.round( (maskWidth - ulWidth) / 2 );
            ul.css('left', ofsLeft);
        }

	if ( liItems > settings.slideBy ) {
		var $next = jQuery( ".next > a", $this ).removeAttr('href');
		var $back = jQuery( ".back > a", $this ).removeAttr('href');
		var animating = false;
                
                if (liItems >= settings.displayAmount) {
		$next.live("click",function(event) {
			if ( !animating ) {
				animating = true;
				offsetLeft = parseInt( ul.css( "left" ) ) - ( liWidth * settings.slideBy );
				if ( offsetLeft + ul.width() > 0 ) {
					$back.removeClass('not-active');
					ul.animate({
						left: offsetLeft
					}, settings.speed, function() {
						if ( parseInt( ul.css( "left" ) ) + ul.width() <= liWidth * settings.slideBy ) {
							$next.addClass('not-active');
						}
						animating = false;
						
						reloadImages($this);
					});
				} else {
					animating = false;
				}
			}
			return false;
		});
                }
		$back.live("click", function(event) {
                        event.preventDefault();
			if ( !animating ) {
				animating = true;
				offsetRight = parseInt( ul.css( "left" ) ) + ( liWidth * settings.slideBy );
				if ( offsetRight + ul.width() <= ul.width() ) {
					$next.removeClass('not-active');
					ul.animate({
						left: offsetRight
					}, settings.speed, function() {
						if ( parseInt( ul.css( "left" ) ) == 0 ) {
							$back.addClass('not-active');
						}
						animating = false;
						
						reloadImages($this);
					});
				} else {
					animating = false;
				}
			}
			return false;
		});
		jQuery( ".view_all > a, .skip_to_news > a", $this ).click(function() {
			var skip_to_news = ( jQuery( this ).html() == "Skip to News" );
			if ( jQuery( this ).html() == "view all" || skip_to_news ) {
				ul.css( "width", "auto" ).css( "left", "0" );
				$next.addClass('not-active');
				$back.addClass('not-active');
				if ( !skip_to_news ) {
					jQuery( this ).html( "view less" );
				}
			} else {
				if ( !skip_to_news ) {
					jQuery( this ).html( "view all" );
				}
				ul.css( "width", ( liItems * liWidth ) );
				$next.removeClass('not-active');
			}
			return false;
		});
	}
	reloadImages($this);
	
};

function reloadImages(elem) {
	
	var sliderPos = jQuery(elem).find('ul').position().left;
	var maskWidth = jQuery('.catalogslider-mask').width();
	var count = 0;
	
	jQuery(elem).find('li').each(function() {
		var item = jQuery(this);
		var i = 0;
		
		if ((item.position().left + sliderPos) >= 0) {
			var itemId = item.attr('id');
			count++;
			
			if (window.imageCollection) {
				if (item.find('img').attr('src') !== imageCollection.getImage(itemId)) {
					item.find('img').attr('src', imageCollection.getImage(itemId));
				}
			}
			
			if(count > 4) {
				return false;
			}
		}
	});
	
};
