var tv=function(){var base="/wordpress/wp-content/themes/jonmiles.co.uk/css/theme/funky/images/";var timeoutNoise;var frame=0;var ready=false;var previousChannel=-1;var currentChannel=-1;var nextChannel=-1;var cache={};var animNoise=function(){if(currentChannel===0){frame++;var position="0 0";if(frame%2==1){position="0 -161px"}$tv.css({backgroundImage:"url("+base+"channel0.png)",backgroundPosition:position});timeoutNoise=setTimeout(animNoise,100)}};var preloaded=function(data){if(typeof(data)=="undefined"||data.loaded==1){ready=true;cache[nextChannel]=true}else{preload()}};var preload=function(){ready=false;if(cache[nextChannel]){preloaded()}else{jQuery.preload(["channel"+nextChannel],{base:base,ext:".png",onFinish:preloaded,enforceCache:true})}};var changeChannel=function(){if(!ready){setTimeout(changeChannel,5000)}else{currentChannel=nextChannel;if(currentChannel===0){timeoutNoise=setTimeout(animNoise,100)}else{clearTimeout(timeoutNoise)}$tv.css("backgroundImage","url("+base+"channel"+currentChannel+".png)");pickChannel()}};var pickChannel=function(){var wait=5000;if(tv.settings.scrollTop!==0&&jQuery(window).scrollTop()>tv.settings.scrollTop){setTimeout(pickChannel,wait)}else{if(currentChannel===0){if(tv.settings.random){do{nextChannel=Math.floor(Math.random()*tv.settings.channels)+1}while(nextChannel==previousChannel)}else{nextChannel=(previousChannel+1)%(tv.settings.channels+1);if(nextChannel===0){nextChannel++}}wait=Math.floor(Math.random()*1500+500)}else{nextChannel=0;previousChannel=currentChannel}preload();setTimeout(changeChannel,wait)}};return{settings:{channels:3,random:false,scrollTop:259},init:function(){$wrapper=jQuery("#innerwrapper").append("<div class='tv'></div>");$tv=jQuery(".tv",$wrapper);pickChannel()}}}();jQuery(function(){tv.init()});