/*

	----------------------------------------------------------------------------------------------------
	Accessible News Slider
	----------------------------------------------------------------------------------------------------
	
	Author:
	Brian Reindel
	Editted By:
	Greg Beaven - MeTTro
	
	Author URL:
	http://blog.reindel.com

	License:
	Unrestricted. This script is free for both personal and commercial use.

*/

jQuery.fn.initiateSlide = function( settings ) {
	settings = jQuery.extend({
        headline : "test",
        speed : "normal",
		slideBy : 2,
		slideDirection : "horizontal", //vertical, horizontal
		viewAllTab : false,
		recursiveScroll : false,
		disableButtonsAtEnd : false //disable the buttons once there's no more items
    }, settings);
    return this.each(function() {
		jQuery.fn.initiateSlide.run( jQuery( this ), settings );
    });
};
jQuery.fn.initiateSlide.run = function( $this, settings ) {
	jQuery( ".javascript_css", $this ).css( "display", "none" );
	var ul = jQuery( "ul:eq(0)", $this );
	var li = ul.children("li");
	//alert(li.length);
	var int_c = 0;
	var visibleItemsTotal = Math.round(jQuery( ".content_surround", $this ).height() / li.height());
	//alert(visibleItemsTotal);
	var totalAllowableClicks = li.length - visibleItemsTotal;
	
	if ( li.length > settings.slideBy ) {
		var $next = jQuery( ".next > a", $this );
		var $back = jQuery( ".back > a", $this );
		
		
		
		if (settings.slideDirection == "vertical") {
			var directionValue = "height";
			var str_direction = "top";
		} else {
			var directionValue = "width";
			var str_direction = "left";
			
		}
		
		var tmp = directionValue.slice(0,1).toUpperCase() + directionValue.slice(1);
		var obj = jQuery( li[0] );
		eval("var li"+tmp+"=obj."+directionValue+"();");
		var animating = false;
		eval("ul.css( directionValue, ( li.length * li"+tmp+" ) );");	
		
		$next.click(function() {
			
			if (int_c < totalAllowableClicks) {
				
				//alert(int_c + ' <= ' + totalAllowableClicks);
				if ( !animating ) {
					animating = true;
					
					offsetLeft = parseInt( ul.css( str_direction ) ) - (eval("li" + tmp) * settings.slideBy );
					
					if ( offsetLeft + ul[directionValue]() > 0 ) {
						$back.css( "display", "block" );
						
						if (settings.slideDirection != "vertical") {
							ul.animate({
								left: offsetLeft
							}, settings.speed, function() {
	
								if (settings.disableButtonsAtEnd) {
									if ( parseInt( ul.css( str_direction ) ) + ul[directionValue]() <= eval("li" + tmp) * settings.slideBy ) {
										alert( parseInt( ul.css( str_direction ) ) + ul[directionValue]() + ' <= ' + eval("li" + tmp) * settings.slideBy);
										//$next.css( "display", "none" );
									}
								}
								animating = false;
							});
						
						} else {
							
							ul.animate({
								top: offsetLeft
							}, settings.speed, function() {
								
								if (settings.disableButtonsAtEnd) {
									if ( parseInt( ul.css( str_direction ) ) + ul[directionValue]() <= eval("li" + tmp) * settings.slideBy ) {
										//$next.css( "display", "none" );
									}
								}
								animating = false;
							});
						}	
						
					} else {
						animating = false;
					}
				}
				int_c++;
			}
			return false;
		});
		$back.click(function() {
			
			if (int_c > 0) {
				int_c--;
			}
			
			//alert(int_c + ' <= ' + totalAllowableClicks);
			if ( !animating ) {
				animating = true;	
				offsetRight = parseInt( ul.css( str_direction ) ) + ( eval("li" + tmp) * settings.slideBy );
				if ( offsetRight + ul[directionValue]() <= ul[directionValue]() ) {
					$next.css( "display", "block" );
					
					if (settings.slideDirection != "vertical") {
					
						ul.animate({
							left: offsetRight
						}, settings.speed, function() {
							if (settings.disableButtonsAtEnd) {
								if ( parseInt( ul.css( str_direction ) ) == 0 ) {
									$back.css( "display", "none" );
								}
							}
							animating = false;
						});
						
					} else {
						
						ul.animate({
							top: offsetRight
						}, settings.speed, function() {
							if (settings.disableButtonsAtEnd) {
								if ( parseInt( ul.css( str_direction ) ) == 0 ) {
									$back.css( "display", "none" );
								}
							}
							animating = false;
						});
						
					}
					
				} else {
					animating = false;
				}
			}
			return false;
		});
		
		if (settings.viewAllTab) {
		
			$next.css( "display", "block" )
				.parent().after( [ "<p class=\"view_all\">", settings.headline, " - ", li.length, " total ( <a href=\"#\">view all</a> )</p>" ].join( "" ) );
			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( directionValue, "auto" ).css( str_direction, "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( directionValue, ( li.length * obj_dynamic_vars["li" + tmp] ) );
					$next.css( "display", "block" );
				}
				return false;
			});
		}
	}
	else {
		var $next = jQuery( ".next > a", $this );
		var $back = jQuery( ".back > a", $this );
		
		//Disable button clicks as it does not need to scroll
		$next.click(function() {
			return false;
		});

		$back.click(function() {
			return false;
		});
	}
};