var ncSearchForm=Class.create();ncSearchForm.prototype={initialize:function(query)
{this.options=Object.extend({apiKey:false,cseIdentifier:false,searchForm:undefined,searchLabel:'Search…',resultsContainer:undefined,waitingContainer:undefined,paging:true,count:10,collapse:false,resultsTemplate:'<li#{first}><a href="#{link}"#{onclick}><span class="heading">#{htmlTitle}</span><span class="url">#{link}</span><span class="summary">#{htmlSnippet}</span></a></li>'},arguments[1]||{});this.searchForm=$(this.options.searchForm);if(!this.searchForm){return;}
this.searchField=this.searchForm.down('input.search');this.searchField.writeAttribute('autocomplete','off');this.searchField.observe('focus',this.focus.bindAsEventListener(this));this.searchField.observe('blur',this.blur.bindAsEventListener(this));this.searchField.observe('keydown',this.keyDown.bindAsEventListener(this));this.searchForm.observe('submit',this.submit.bindAsEventListener(this));this.resultsContainer=$(this.options.resultsContainer);this.resultsList=this.resultsContainer.down('ol');this.waitingContainer=$(this.options.waitingContainer);this.searchCache=$H();if(query){this.searchField.setValue(query);this.search();}else{this.searchField.setValue(this.options.searchLabel);}
if(this.searchField.getValue()==this.options.searchLabel){this.searchField.addClassName('placeholder');}},keyDown:function(e)
{if(this.observer)
clearTimeout(this.observer);switch(e.keyCode){case Event.KEY_ESC:this.searchField.value='';this.close();break;case Event.KEY_RETURN:this.submit();e.stop();break;case Event.KEY_UP:this.prev();e.stop();break;case Event.KEY_DOWN:this.next();e.stop();break;default:this.observer=setTimeout(this.search.bind(this),1000);break;}},search:function()
{if(this.searchEmpty()){this.close();}else{if(!this.resultsContainer.visible()){this.showSearch();}else{this.performSearch();}}},showSearch:function()
{new Effect.BlindDown(this.resultsContainer,{duration:0.2,afterFinish:function(){this.performSearch();}.bind(this)});},performSearch:function(startIndex)
{if(this.searchEmpty()){return;}
if(!startIndex){startIndex=1;}
var value=this.searchField.getValue();if(this.currentValue==value&&startIndex<=this.currentIndex){return;}
this.currentValue=value;var searchCache=this.searchCache.get(this.currentValue+'.startIndex.'+startIndex);if(searchCache){this.populateSearch(searchCache);return;}
var cache={items:[],start:startIndex,count:0,total:0};new Ajax.JSONRequest('https://www.googleapis.com/customsearch/v1',{callbackParamName:'callback',parameters:{key:this.options.apiKey,cx:this.options.cseIdentifier,q:this.currentValue,num:this.options.count,start:startIndex},onSuccess:function(response){if(response.status===200){cache={items:response.responseJSON.items,start:parseInt(response.responseJSON.queries.request[0].startIndex),count:parseInt(response.responseJSON.queries.request[0].count),total:parseInt(response.responseJSON.queries.request[0].totalResults)}
this.currentIndex=startIndex+cache.count-1;this.searchCache.set(this.currentValue+'.startIndex.'+startIndex,cache);this.populateSearch(cache);}else{this.searchCache.set(this.currentValue+'.startIndex.'+startIndex,cache);this.populateSearch(cache);}}.bind(this),onFailure:function(response){this.searchCache.set(this.currentValue+'.startIndex.'+startIndex,cache);this.populateSearch(cache);}.bind(this)});},populateSearch:function(response)
{var more=this.resultsList.select('li.more');if(more.size()){more.invoke('remove');}
var results=new Array();if(response.items.length>0){if(response.items.length>this.options.count){response.items.length=this.options.count;}
var first=true;var resultsTemplate=new Template(this.options.resultsTemplate);response.items.each(function(result){result.first='';if(first&&response.start==1){result.first=' class="first"';first=false;}
result.onclick='';if(result.link.match(/\.pdf$/)){result.onclick=' onclick="window.open(this.href); return false;"';}
results.push(resultsTemplate.evaluate(result));});}else{results.push('<li class="first error">No results matched your query.</li>');}
if(this.options.paging&&response.total>(response.start+response.count-1)){results.push('<li class="more"><a class="clearfix" href="#show-more" rel="'+(response.start+response.count)+'"><span>Displaying first '+(response.start+response.count-1)+' results — click to show more results…</span><img src="'+_nc.baseUrl+'images/wait.gif" /></a></li>');}
if(response.start==1){this.resultsList.update(results.join(''));}else{this.resultsList.insert(results.join(''));}
more=this.resultsList.down('li.more');if(more){wait=more.down('img').setOpacity(0);more.down('a').observe('click',function(event){event.stop();more.down('a').stopObserving();more.down('a span').hide();new Effect.Opacity(wait,{to:1,duration:0.4});this.performSearch(response.start+response.count);}.bind(this));}
if(!this.resultsList.visible()){new Effect.BlindDown(this.resultsList,{duration:0.2});}
this.waitingContainer.absolutize();this.waitingContainer.hide();},focus:function(e)
{if(this.searchField.hasClassName('placeholder')){this.searchField.value='';this.searchField.removeClassName('placeholder');}else if(this.searchField.value!=''){this.search();}},blur:function(e)
{if(this.searchEmpty()){this.searchField.value='Search for it here…';this.searchField.addClassName('placeholder');}
this.closer=setTimeout(this.close.bind(this),400);},next:function()
{if(selected=this.resultsContainer.down('li.selected')){if(next=selected.next()){next.addClassName('selected');selected.removeClassName('selected');}}else{next=this.resultsContainer.down('li');if(!next.hasClassName('error')){next.addClassName('selected');}}},prev:function()
{if(selected=this.resultsContainer.down('li.selected')){selected.removeClassName('selected');if(previous=selected.previous()){previous.addClassName('selected');}}else{previous=this.resultsContainer.down('li');if(!previous.hasClassName('error')){previous.addClassName('selected');}}},close:function()
{if(!this.options.collapse){return;}
new Effect.BlindUp(this.resultsContainer,{duration:0.2,afterFinish:this.resetList.bind(this)});},submit:function()
{clearTimeout(this.closer);if(selected=this.resultsContainer.down('li.selected')){var href=selected.down('a').href;if(href.match(/^http:\/\/ncpic\.org\.au/)&&!href.match(/\.pdf$/)){window.location.href=href;}else{window.open(href);}}else{if(this.searchField.getValue()!=this.options.searchLabel&&!this.searchEmpty()){this.searchForm.submit();}}},resetList:function()
{if(this.currentValue!=this.searchField.getValue()){this.resultsList.update('');this.resultsList.hide();this.waitingContainer.relativize();this.waitingContainer.show();}},searchEmpty:function()
{if(this.searchField.getValue().strip()=='')
return true;return false;}};;function climateSchoolsObserver()
{var animating=false;var activeContainer;$('ncContentMain').select('.info-block h4 a').invoke('observe','click',function(event){event.stop();if(animating){return false;}
var anchorObj=event.element();var currentContainer=anchorObj.up().next();var effects=[];if(activeContainer){activeContainer.previous().down('a').removeClassName('active');var opts={sync:true}
if(activeContainer==currentContainer){Object.extend(opts,{afterFinish:function(){activeContainer=false;}});}
effects.push(new Effect.BlindUp(activeContainer,opts));}
if(activeContainer!=currentContainer){anchorObj.addClassName('active');effects.push(new Effect.BlindDown(currentContainer,{sync:true,afterFinish:function(){activeContainer=currentContainer;}}));}
new Effect.Parallel(effects,{duration:0.5,beforeStart:function(){animating=true;},afterFinish:function(){animating=false;}});});}
function makingTheLinkObserver()
{var container=$('ncContentMain').down('.information-container');var currentContainer;$('ncInfoNavigation').select('a').invoke('observe','click',function(event){event.stop();var deselect=false;var anchorObj=event.element();if(anchorObj.hasClassName('active')){deselect=true;}
$('ncInfoNavigation').select('a').invoke('removeClassName','active');if(!deselect){anchorObj.addClassName('active');}
var anchorHref=anchorObj.readAttribute('href');anchorHref=anchorHref.substr(1,anchorHref.length);var infoContainer=$(anchorHref);var currentHeight=container.getHeight();if(deselect){var newHeight=1;}else{var newHeight=infoContainer.getHeight();}
if(currentHeight==0){container.setStyle({height:'1px'});currentHeight=1;}
var effects=[];var scaleY=(newHeight/currentHeight)*100;effects.push(new Effect.Scale(container,scaleY,{scaleX:false,delay:0,scaleContent:false,sync:true}));effects.push(new Effect.Appear(infoContainer,{sync:true}));if(currentContainer){effects.push(new Effect.Fade(currentContainer,{sync:true}));}
currentContainer=infoContainer;new Effect.Parallel(effects,{duration:0.7});});}
var videoControls;function makingTheLinkVodcastCloseIntroduction(introObject)
{if(introObject.visible()){new Effect.SlideUp(introObject,{duration:0.5});$('ncIntroductions').select('a.tab').first().update('Show questions');$f().resume();}}
function makingTheLinkVodcast(videoHost,playlistName)
{var video;if(document.location.hash){var hash=document.location.hash;video=hash.substring(1,hash.length);}
var controlsTemplate='<div class="control"><a class="play">play</a></div>'+'<div class="progressbar">'+'<div class="buffer"></div>'+'<div class="progress"></div>'+'</div>'+'<div class="track-wrapper">'+'<div class="track" style="width: 544px;">'+'<div class="playhead"></div>'+'</div>'+'</div>'+'<div class="time"></div>'+'<div class="mute"></div>';switch(playlistName){case'mates-help-mates':var playlist=[{url:'mhm-1-should-we-help-bree.mov',title:'Should we help Bree?',autoPlay:false},{url:'mhm-2-helping-bree.mov',title:'Helping Bree',autoPlay:false},{url:'mhm-3-should-we-help-mitch.mov',title:'Should we help Mitch?',autoPlay:false},{url:'mhm-4-helping-mitch.mov',title:'Helping Mitch',autoPlay:false}];break;case'vodcasts':var playlist=[{url:'vodcast-1-missed-opportunity.mov',title:'Missed Opportunity',autoPlay:false},{url:'vodcast-2-engaging-jason.mov',title:'Engaging Jason',autoPlay:false},{url:'vodcast-3-change-talk.mov',title:'Change Talk',autoPlay:false},{url:'vodcast-4-rolling-with-resistance.mov',title:'Rolling with Resistance',autoPlay:false}];break;}
var cacheBuster='';if(Prototype.Browser.IE){cacheBuster='?r='+(new Date()).getTime();}
var playlistObj=$('ncPlaylist');var playlistElements=playlistObj.select('a');var introductions=$('ncIntroductions').select('div.ncIntroduction');var introductionTab=$('ncIntroductions').select('a.tab');var started=false;var wasPlaying=false;var nowPlaying=false;var resumeFromZero=false;var introductionTabCountdown;$f('ncVideo',{src:_nc.baseUrl+'includes/flash/flowplayer-3.1.5.swf'+cacheBuster,wmode:'opaque'},{clip:{baseUrl:videoHost+'making-the-link',onStart:function(clip){if(_gaq){var path=clip.url.sub(/https?\:\/\/[1-9]{1,1}\.video\.ncpic\.org\.au\//,'',1);_gaq.push(['_trackEvent','Video','Play',path]);}
if(resumeFromZero){this.seek(5);this.pause();}},onResume:function(clip){if(resumeFromZero){this.seek(0);resumeFromZero=false;}
var selectedIndex=0;playlist.each(function(data,index){if(data.url==clip.url){selectedIndex=index;}});var introduction=$('ncIntroduction-'+selectedIndex);if(introduction.visible()){new Effect.SlideUp(introduction,{duration:0.5});introductionTab.first().update('Show questions');}},onBegin:function(clip){playlistElements.invoke('removeClassName','active');playlistObj.down('a[href="'+clip.baseUrl+'/'+clip.url+'"]').addClassName('active');},onBeforeBegin:function(clip){var selectedIndex=0;playlist.each(function(data,index){if(data.url==clip.url){selectedIndex=index;}});var instructions=$('ncInstructions');if(instructions.visible()){if(Prototype.Browser.IE){instructions.hide();}else{new Effect.Fade(instructions,{duration:0.5});}}
introductions.each(function(item,index){if(item.visible()){if(Prototype.Browser.IE){item.hide();}else{new Effect.SlideUp(item,{duration:0.5});}}});var introduction=$('ncIntroduction-'+selectedIndex);if(!introduction.visible()){new Effect.SlideDown(introduction,{duration:0.5,queue:'end',afterFinish:function(effect){effect.element.setStyle({height:'320px'});}});introductionTab.first().update('Hide questions');}
introductionTab.first().stopObserving('click').observe('click',function(event){event.stop();introductions.each(function(item,index){if(item.visible()&&index!==selectedIndex){new Effect.SlideUp(item,{duration:0.5});}});if(!introduction.visible()){$f().pause();new Effect.SlideDown(introduction,{duration:0.5,afterFinish:function(effect){effect.element.setStyle({height:'320px'});}});event.findElement('a').update('Hide questions');}else{event.findElement('a').update('Show questions');new Effect.SlideUp(introduction,{duration:0.5});$f().resume();}
window.clearTimeout(introductionTabCountdown);});introductionTabCountdown=makingTheLinkVodcastCloseIntroduction.delay(12,introduction);}},playlist:playlist,plugins:{controls:null}}).controls('ncControls',{template:controlsTemplate});if(Prototype.Browser.IE){$('ncInstructions').setStyle({zoom:1});$('ncIntroductions').setStyle({zoom:1});}
playlistElements.invoke('observe','click',function(event){event.stop();playlistElements.invoke('removeClassName','active');var anchor=event.findElement('a');anchor.addClassName('active');var selectedIndex=0;playlist.each(function(clip,index){if(anchor.readAttribute('href')==clip.baseUrl+'/'+clip.url){selectedIndex=index;}});$f().play(selectedIndex);wasPlaying=nowPlaying;nowPlaying=selectedIndex;if(wasPlaying!==nowPlaying){resumeFromZero=true;}});}
function matesHelpMatesDropdown(element)
{var element=$(element);var subelement=element.down('ul');element.observe('element:enter',function(){element.down('a').addClassName('active');if(!subelement.visible()){new Effect.BlindDown(subelement,{duration:0.2});}});element.observe('element:leave',function(){if(subelement.visible()){new Effect.BlindUp(subelement,{duration:0.2,afterFinish:function(){element.down('a').removeClassName('active');}});}});new ElementHover(element,{enterDelay:0});}
function showSOTGAbout(event)
{event.stop();var overlay=$('ncContent').down('.overlay');var aboutThis=$('ncContent').down('div.aboutThisProject');new Effect.Parallel([new Effect.Appear(overlay,{to:0.7,sync:true}),new Effect.Appear(aboutThis,{sync:true})],{duration:0.4});var close=aboutThis.down('button');if(!close.retrieve('observed')){close.store('observed',true);close.observe('click',function(event)
{event.stop();new Effect.Parallel([new Effect.Fade(overlay,{sync:true}),new Effect.Fade(aboutThis,{sync:true})],{duration:0.4});});}}
