/**
 * C4 News Ticker 2.0
 */

var ticker=function(){var options,animateDuration,pauseDuration,count,current,paused,$ticker,$items,$nav,$fader,pauseTimer;function init(options){options=options||{};ticker.options=options;ticker.animateDuration=350;ticker.pauseDuration=3000;ticker.count=0;ticker.current=0;ticker.paused=false;if($(ticker.options.ticker).length==1){makeTicker();}}
function makeTicker(){ticker.$ticker=$(ticker.options.ticker);ticker.count=$(ticker.options.items).hide().filter(':eq(0)').show(0).add(ticker.options.items).length-1;if(ticker.count>0){ticker.$items=$(ticker.options.items);ticker.$nav=$('<div class="controlNav"><ul><li class="previous" title="Previous"><span class="displace">Previous</span></li><li class="pause" title="Start/Pause"><span class="displace">Start/Pause</span></li><li class="next" title="Next"><span class="displace">Next</span></li></ul></div>').appendTo(ticker.$ticker);$('.previous',ticker.$nav).click(function(){previous();});$('.next',ticker.$nav).click(function(){next();});$('.pause',ticker.$nav).click(function(){if(!ticker.paused){scrollPause();}
else{scrollPlay();}});ticker.pauseTimer=setTimeout(function(){startAuto();},ticker.pauseDuration);}}
function startAuto(){goTo('auto');}
function stopAuto(){clearTimeout(ticker.pauseTimer);ticker.$items.each(function(){$(this).stop();hideShow(ticker.current);});}
function scrollPause(){ticker.paused=true;ticker.$nav.find('.pause').removeClass('pause').addClass('play');stopAuto();}
function scrollPlay(){ticker.paused=false;ticker.$nav.find('.play').removeClass('play').addClass('pause');startAuto();}
function next(){scrollPause();goTo('next');}
function previous(){scrollPause();goTo('prev');}
function goTo(dir){var from,to;if(dir=='prev'){to=ticker.current-1;from=ticker.current;if(to<0){to=ticker.count;from=0;}}else{if(dir=='next'||dir=='auto'){to=ticker.current+1;from=ticker.current;if(to>ticker.count){to=0;from=ticker.count;}}}
if(dir=='auto'){animate(from,to);}else{hideShow(to);}}
function animate(from,to){clearTimeout(ticker.pauseTimer);$(ticker.$items[to]).css({top:'26px'});$(ticker.$items[from]).animate({top:'-26px'},ticker.animateDuration,function(){$(ticker.$items[from]).hide();ticker.current=to;$(ticker.$items[to]).show();$(ticker.$items[to]).animate({top:0},ticker.animateDuration,function(){ticker.pauseTimer=setTimeout(function(){goTo('auto');},ticker.pauseDuration);})})}
function hideShow(to){ticker.$items.hide();ticker.current=to;$(ticker.$items[to]).css({top:0}).show();}
return{init:init};}();$(document).ready(function(){ticker.init({'ticker':'#ticker','items':'#ticker dd'});});
