/*

	----------------------------------------------------------------------------------------------------
	Accessible News Slider
	----------------------------------------------------------------------------------------------------
	
	Author:
	Brian Reindel
	
	Author URL:
	http://blog.reindel.com

	License:
	Unrestricted. This script is free for both personal and commercial use.

*/

jQuery.fn.accessNews_vertical = function( settings ) {
	settings = jQuery.extend({
        headline : "Top Stories",
        speed : "normal",
		slideBy : 2
    }, settings);
    return this.each(function() {
		jQuery.fn.accessNews_vertical.run( jQuery( this ), settings );
    });
};
jQuery.fn.accessNews_vertical.run = function( $this, settings ) {
	jQuery( ".javascript_css", $this ).css( "display", "none" );
	var ul = jQuery( "ul:eq(0)", $this );
	var li = ul.children();
	var sidebar_height = $(".sidebar_content").height();
	//console.log(sidebar_height + "px | " + li.length + " * 57px");	
	if ( li.length > settings.slideBy ) {		
		var $next = jQuery( ".next", $this );
		var $back = jQuery( ".back", $this );
		var animating = false;
		//var liHeight = jQuery( li[0] ).height();				
		//hacked		
		var liHeight = 40;
		var joboffer_margin_sum = 4;
		var joboffer_padding_inside_sum = 12;		
		var border_bottom = 1;
		var joboffer_height = border_bottom + liHeight + joboffer_padding_inside_sum + joboffer_margin_sum;			
		ul.css( "height", ( li.length * (joboffer_height) ) );	
		var notseen_height = ul.height() - joboffer_height*9;
		var sidebar_margin_outside_sum = 24;
		$next.click(function() {
		if ( (joboffer_height * li.length + sidebar_margin_outside_sum) > sidebar_height ){
			if ( !animating ) {
				animating = true;
				offsetTop = parseInt( ul.css( "top" ) ) - ( (joboffer_height) * settings.slideBy );
				var t = $next.attr("class").split(' ').slice(-1)[0];
				if ( (offsetTop + ul.height() > 0) && t!="inactive_next_down" ) {
					$back.removeClass('back back_up inactive_back inactive_back_up').addClass('back back_up');
					ul.animate({
						top: offsetTop
					}, settings.speed, function() {
						if ( parseInt( ul.css( "top" ) ) + ul.height() <= liHeight * settings.slideBy*9 ) {
							$next.removeClass('next next_down inactive_next inactive_next_down').addClass('inactive_next inactive_next_down');
						}
						animating = false;
					});
				} else {
					animating = false;
				}
			}
			return false;
		}
		});
		$back.click(function() {
			if ( !animating ) {
				animating = true;
				offsetBottom = parseInt( ul.css( "top" ) ) + ( (joboffer_height) * settings.slideBy );
				if ( offsetBottom + ul.height() <= ul.height() ) {
					$next.removeClass('next next_down inactive_next inactive_next_down').addClass('next next_down');
					ul.animate({
						top: offsetBottom
					}, settings.speed, function() {
						if ( parseInt( ul.css( "top" ) ) == 0 ) {
							$back.removeClass('back back_up inactive_back inactive_back_up').addClass('inactive_back inactive_back_up');
						}
						animating = false;
					});
				} else {
					animating = false;
				}
			}
			return false;
		});
		$next.removeClass('next next_down inactive_next inactive_next_down').addClass('next next_down')
			.parent().after("&nbsp;");
		$back.removeClass('back back_up inactive_back inactive_back_up').addClass('inactive_back inactive_back_up')
			.parent().after("&nbsp;");
		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( "height", "auto" ).css( "left", "0" );
				$next.css( "display", "none" );
				$back.css( "display", "none" );
				if ( !skip_to_news ) {
					jQuery( this ).html( "view less" );
				}
			} else {
				if ( !skip_to_news ) {
					jQuery( this ).html( "view all" );
				}
				ul.css( "height", ( li.length * liHeight ) );
				$next.css( "display", "block" );
			}
			return false;
		});
	}
};
