/****************************************************/
/*  BILLBOARD SCROLL JS FOR ASTHMA.COM HOME PAGE 	*/
/*  8-13-09 by Guy Shahar 							*/
/****************************************************/

$().ready(function(){
	$(document).pngFix(); 	// fix ie 6 png issue

	var current_pos, increment, speed;
	var num_elements = $("ul.scroller li").size() - 1;	// number of scrolling elements
	
	// CONTROL VARIABLES
	current_pos = 0;			// current scroll id
	increment = 1002;			// horizontal pixels to scroll (should be set to width of image)
	speed = 300;				// scroll speed in milliseconds (smaller value = faster)
	auto_scroll_on = true;		// set to true to autoscroll, otherwise false
	auto_scroll_speed = 500;	// speed for auto scroll (if you want it different than click scroll)
	auto_scroll_delay = 6000;	// ms delay between each auto scroll
	auto_scroll_delay_second_cycle = 10000;	// ms delay to switch to after the first cycle of images.

	// DUPLICATE FIRST AND LAST ELEMENTS FOR CAROUSEL ILLUSION
	var first = $("ul.scroller li:first").clone();
	var last = $("ul.scroller li:last").clone();	
	$("ul.scroller").prepend(last);
	$("ul.scroller").append(first);
	
	$("#prev").click(function(){
		if(auto_scroll_on){
			clearInterval(scrolling_now);
		}
		goto_value = prev_position();
		$("ul.scroller").stop().animate({
			marginLeft: goto_value
		},speed);
	});
	
	$("#next").click(function(){
		if(auto_scroll_on){
			clearInterval(scrolling_now);
		}
		goto_value = next_position();
		$("ul.scroller").stop().animate({
			marginLeft: goto_value
		},speed);			
	});
	
	if(auto_scroll_on) launch_auto_scroll();
	
	function prev_position(){ // returns next position to animate to, making appropriate changes as necessary
		if(current_pos == 0){ // if at end, perform a 'magic replace' to mimic carousel effect
			current_pos = num_elements;
			$("ul.scroller").css({'margin-left' : -((current_pos+1) * increment)});
			return -(current_pos * increment) + 'px';		
		}
		newpos = -(current_pos * increment) + increment;
		current_pos--;
		return newpos + 'px';
	}
	function next_position(){ // returns next position to animate to, making appropriate changes as necessary
		if(current_pos == num_elements){ // if at end, perform a 'magic replace' to mimic carousel effect
			current_pos = 0;
			$("ul.scroller").css({'margin-left' : increment + 'px'});
			return '0px';
		}
		newpos = -(current_pos * increment) - increment;
		current_pos++;
		return newpos + 'px';
	}

	// AUTO-SCROLL FUNCTIONALITY
	var scrolling_now;  // store scrolling interval function	
	function launch_auto_scroll(){
		scrolling_now = setInterval(auto_scroll,auto_scroll_delay);
	}
	var auto_scroll_counter = 0;
	function auto_scroll(){
		if(auto_scroll_counter == num_elements){ // if first cycle complete, change delay
			clearInterval(scrolling_now);
			scrolling_now = setInterval(auto_scroll,auto_scroll_delay_second_cycle)
		}
		goto_value = next_position();
		$("ul.scroller").stop().animate({marginLeft: goto_value},auto_scroll_speed);
		auto_scroll_counter++;
	}
		   
});
