
var autoFilterId=0;var isUserLoggedOn=false;var imagesPath='../../Content/Images/';var activeStream=null;var activeArticles;var activeTimeOutId=null;var lastRefresh;function ActiveStream(allowEdit,id,orDataStream){this.allowEdit=allowEdit;this.id=id;this.dataStream=orDataStream;this.isOnlyIdKnown=function(){return(this.dataStream==undefined||this.dataStream==null);}}
$(document).ready(function(){if(autoFilterId!=0)
runDataStream(autoFilterId);$('#storyHeaderControls img').click(function(){$(this).siblings().each(function(){$(this).attr('src','{0}{1}Off.gif'.format(imagesPath,$(this).attr('action')));});$(this).attr('src','{0}{1}On.gif'.format(imagesPath,$(this).attr('action')));});RoosstEvents.registerLogOn(function(userName){getView('/Home/UserDataStreamsControl',function(data){$('#userDataStreams').html(data)});isUserLoggedOn=true;});RoosstEvents.registerDataStreamEdited(function(dataStream,box){function autoSaveResponder(dataStream){var dataStream=dataStream;this.handleResponse=function(response){if(response.success){redrawUserStreamsControl();if(activeStream!=null&&activeStream.dataStream==dataStream)
activeStream.dataStream.id=response.id;}else{alert('There was a problem saving your search: '+response.errorMessage);}}};if(isUserLoggedOn)
RoosstProxy.controllerAction('DataStream/Save',new autoSaveResponder(dataStream).handleResponse,function(){},$.compactJSON(dataStream));runDataStream(null,dataStream,true);box.hide();});RoosstEvents.registerBasketToggle(function(hash,removed){$('#mainVidi img[basketHash]').each(function(){if($(this).attr('basketHash')==hash){$(this).attr('src',getVidiBasketStateImg(!removed));$(this).attr('basketState',(!removed).toString())}});});});function redrawUserStreamsControl(){getView('/Home/UserDataStreamsControl',function(data){$('#userDataStreams').html(data)});}
function notifyStreamLoading(){$('#storyHeaderText h1').text('Loading, please wait...');}
function handleStreamLoadSuccess(data){handleLoadSuccess(data.d,true);}
function handleBasketLoadSuccess(data){handleLoadSuccess(data,false);}
function handleLoadSuccess(rssSet,autoRefresh){$('#mainVidi').show();$('#getStartedDiv').show();handleRefreshAction('mainVidi',true,autoRefresh);var style=new Roosst.VidiStyle();Roosst.initialise('mainVidi',rssSet,new Roosst.VidiSettings(7,false,5000,'94px'));}
function handleStreamLoadFail(){$('#storyHeaderText h1').text('There was an error executing your request.');$('#storyHeaderText h2').text('Please retry.');$('#getStartedDiv').show();}
function loadBasket(){if(activeTimeOutId!=null)
window.clearTimeout(activeTimeOutId);activeStream=null;configureToolbar();notifyStreamLoading();$('#storyHeader').show();$('#welcome').hide();RoosstProxy.controllerAction('Home/LoadBasket',handleBasketLoadSuccess,handleStreamLoadFail);}
function runDataStream(id,orDataStream,allowEdit){if(activeTimeOutId!=null)
window.clearTimeout(activeTimeOutId);notifyStreamLoading();$('#storyHeader').show();$('#welcome').hide();$('#getStartedDiv').hide();allowEdit=(allowEdit==undefined||allowEdit==null)?false:allowEdit;if(orDataStream!=undefined&&orDataStream!=null){activeStream=new ActiveStream(allowEdit,null,orDataStream);RoosstProxy.executeStream(orDataStream,handleStreamLoadSuccess,handleStreamLoadFail);}
else{activeStream=new ActiveStream(allowEdit,id);RoosstProxy.executeStreamByID(id,handleStreamLoadSuccess,handleStreamLoadFail);}
configureToolbar();}
function formatTime(date){return'{0}:{1}'.format(date.getHours(),zeroPad(date.getMinutes(),2));}
function handleRefreshAction(vidiId,dataLoaded,autoRefresh){if(dataLoaded){Roosst.resume(vidiId);$('#storyHeaderControls img[action="play"]').attr('src','{0}PlayOn.gif'.format(imagesPath));}
var now=new Date();var nextRefresh=new Date(now);var DELAY=180000;nextRefresh.setMilliseconds(now.getMilliseconds()+DELAY);var previousRefesh;if(!dataLoaded&&lastRefresh)
previousRefesh=lastRefresh;else
previousRefesh=now;if(autoRefresh){$('#storyHeaderText h2').text('Loaded at {0}, next refresh {1}.'.format(formatTime(previousRefesh),formatTime(nextRefresh)));if(dataLoaded)
lastRefresh=now;function runner(vidiId){var _vidiId=vidiId;this.run=function(){activeTimeOutId=window.setTimeout(function(){autoLoadFreshData(_vidiId);},DELAY);}}
new runner(vidiId).run();}else{$('#storyHeaderText h2').text('Loaded at {0}.'.format(formatTime(previousRefesh)));}}
function loadFreshData(vidiId){$('#storyHeaderText h2').text('Loading.');$('#vidiControls li').siblings('[action]').each(function(){$(this).find('img').attr('src','{0}{1}Off.jpg'.format(imagesPath,$(this).attr('action')));});var success=function(data){handleRefreshAction(vidiId,true,true);Roosst.resupply(vidiId,data.d);};var failure=function(){$('#storyHeaderText h2').text('Latest stories could not currently be loaded.');};if(activeStream.isOnlyIdKnown())
RoosstProxy.executeStreamByID(activeStream.id,success,failure);else
RoosstProxy.executeStream(activeStream.dataStream,success,failure);}
function autoLoadFreshData(vidiId){if(!Roosst.isPaused(vidiId))
loadFreshData(vidiId);else
handleRefreshAction(vidiId,null,true);}
function manualLoadFreshData(vidiId){if(activeTimeOutId)
window.clearTimeout(activeTimeOutId);activeTimeOutId=null;loadFreshData(vidiId);}
function createReadFunction(rssItem){return function(){try{var description=rssItem.description==null?'':rssItem.description;var jContent=$('<div class="readDialog"><a href="'+rssItem.link+'" target="blank">'+rssItem.title+'</a><p>'+description+'</p></div>');jContent.find('script').remove();var previewWindow=new Boxy(jContent,{title:rssItem.title,unloadOnHide:true,clickToFront:true,show:false});var windowSize=previewWindow.getSize();var resize=false;if(windowSize[0]>$(window).width()*0.9){windowSize[0]=$(window).width()*0.9;resize=true;}
if(windowSize[1]>$(window).height()*0.9){windowSize[1]=$(window).height()*0.9;resize=true;}
previewWindow.show();if(resize)
previewWindow.resize(windowSize[0],windowSize[1]);}
catch(err){alert(err);}};}
function openItem(url){window.open(url);}
function configureToolbar(){if(activeStream!=null&&activeStream.allowEdit)
$('#tbbEditSearch').show();else
$('#tbbEditSearch').hide();}
function launchExplorer(){function rendered(box){RoosstExplorer.setup(activeArticles,box.getSize()[1]);}
DialogLoader.display('Home/ExplorerControl','Explorer',{cache:true,rendered:rendered,width:$(window).width()*0.9,height:$(window).height()*0.9});}
function redrawBasketSummary(){getView('/Home/BasketSummaryControl',function(data){$('#basketSummary').html(data).corner();});}
function addToBasket(hash){for(var i=0;i<activeArticles.length;i++){if(activeArticles[i].basketHash==hash){var shallow=jQuery.extend({},activeArticles[i]);shallow.channel=null;RoosstProxy.controllerAction('Home/AddToBasket',redrawBasketSummary,null,$.compactJSON(shallow));break;}}
updateBasketFlag(hash,true);RoosstEvents.raiseBasketToggle(hash,false);}
function updateBasketFlag(hash,val){for(var i=0;i<activeArticles.length;i++){if(activeArticles[i].basketHash==hash){activeArticles[i].isInBasket=val;}}}
function removeFromBasket(hash){RoosstProxy.controllerAction('Home/RemoveFromBasket',redrawBasketSummary,null,{hash:hash});updateBasketFlag(hash,false);RoosstEvents.raiseBasketToggle(hash,true);}
function toggleBasket(){var hash=$(this).attr('basketHash');if($(this).attr('basketState')=='false')
addToBasket(hash);else
removeFromBasket(hash);}
function getVidiBasketStateImg(isInBasket){return'{0}Content/Images/EggBasket{1}42.gif'.format(TLD,isInBasket?'On':'Off');}
Roosst.intialising=function(vidiId,rssSetName,articles){activeArticles=articles;$('#storyHeaderText h1').text(rssSetName);var j=$('#rssSetSummary');j.empty();j.append('<a href="javascript:launchExplorer();"><img src="{0}Content/Images/Map32.gif" title="Explore Stories" /></a> {1} active stories. <a href="javascript:launchExplorer();">Explore them.</a>'.format(TLD,articles.length));j.show().corner();}
Roosst.populateItemDivOverride=function(div,rssItem,vidiStyle,alt){var readFunction=createReadFunction(rssItem);var summary=Roosst.summarize(rssItem);var date=formatItemDate(utcToLocal(new Date(rssItem.pubDateUtcTime)));var jContentDiv=$('<div class="roosstItemStoryText"><h1>{0}</h1><h2>{1} - {2}</h2><p>{3}</p>'.format(rssItem.title,date,rssItem.channel.title,summary));jContentDiv.find('h1,h2,p').click(readFunction);jContentDiv.hover(function(){$(this).addClass('highlightedContent');},function(){$(this).removeClass('highlightedContent');});var jThumbNailImg
var imgUrl=null;if(rssItem.thumbnail){imgUrl=rssItem.thumbnail.url;}else{var jImg=jContentDiv.find('img');if(jImg.length>0)
imgUrl=jImg.eq(0).attr('src');}
if(imgUrl!=null){jThumbNailImg=$('<img class="roosstItemStoryPic" src="{0}" title="{1}" />'.format(imgUrl,rssItem.title));jThumbNailImg.click(readFunction);}
else
jThumbNailImg=$('<div class="roosstItemStoryPic"></div>');var jGoThereDiv=$('<div class="roosstItemStoryLinks" ><img basketHash="{0}" basketState="{1}" src="{2}" /><img src="{3}Content/Images/PlainGoArrow.gif" title="Go There" onclick="openItem(\'{4}\');" /></div>'.format(rssItem.basketHash,rssItem.isInBasket,getVidiBasketStateImg(rssItem.isInBasket),TLD,rssItem.link));jGoThereDiv.find('img[basketState]').click(toggleBasket);var jItemLimiter=$('<div class="roosstItemLimiter">');jItemLimiter.append(jThumbNailImg[0]);jItemLimiter.append(jGoThereDiv[0]);jItemLimiter.append(jContentDiv[0]);div.className='roosstItem';div.appendChild(jItemLimiter[0]);}
function editCurrentSearch(){if(activeStream.isOnlyIdKnown())
displaySearch(null,activeStream.id);else
displaySearch(activeStream.dataStream);}
