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;}};;var ncSplash=Class.create();ncSplash.prototype={initialize:function(splashContainer,carousels)
{this.splashContainer=$(splashContainer);this.carousels=carousels;this.carouselStarted=true;this.addEventListeners();},addEventListeners:function()
{this.imageAnchors=this.splashContainer.select('a');this.ncFeatureList=this.splashContainer.select('div.ncFeature');this.overlays=this.splashContainer.select('div.overlay');this.ncFeatureList.each(function(anchor){Event.observe(anchor,'mouseover',this.hover.bindAsEventListener(this));Event.observe(anchor,'mouseout',this.hoverOut.bindAsEventListener(this));},this);this.overlays.each(function(overlay){Event.observe(overlay,'mouseover',this.hover.bindAsEventListener(this));},this);},hover:function(e)
{this.selected=Event.element(e).up('div.ncFeature');if(this.selected!=undefined){this.stopCarousels();for(var i=0;i<this.ncFeatureList.length;i++){var overlay=this.ncFeatureList[i].down('.overlay');if(this.ncFeatureList[i]==this.selected){this.ncFeatureList[i].addClassName('active');if(overlay.visible())
overlay.hide();}else{if(this.ncFeatureList[i].hasClassName('active'))
this.ncFeatureList[i].removeClassName('active');if(!overlay.visible())
overlay.show();}}}else{if(!this.carouselStarted){this.carousel.startCarousel();this.carouselStarted=true;}}},hoverOut:function(e)
{var mouseOut=e.relatedTarget;if(mouseOut!=null&&!mouseOut.descendantOf('ncFeatures')){this.restartCarousels();}
for(var i=0;i<this.ncFeatureList.length;i++){var overlay=this.ncFeatureList[i].down('.overlay');if(this.ncFeatureList[i].hasClassName('active'))
this.ncFeatureList[i].removeClassName('active');if(overlay.visible())
overlay.hide();}},restartCarousels:function(){this.carousels.each(function(element){element.restartCarousel();});},stopCarousels:function(){this.carousels.each(function(element){element.stopCarousel();});}};var ncCannabisQuiz=Class.create();ncCannabisQuiz.prototype={initialize:function(options)
{this.options=Object.extend({force:false,source:'popup'},options||{});this.cookieHandler=new JSONCookies();if(this.cookieHandler.accepted()){this.quiz=this.cookieHandler.get('cannabisQuiz');this.quiz=this.quiz||{complete:false};if(this.quiz.complete!==false){this.showWidget();}}else{return false;}
this.built=false;if(!this.options.force&&this.quiz.complete){return false;}
this.scaled=false;this.build();},showWidget:function()
{var cannabisQuiz=$('ncCannabisQuizWidget');if(cannabisQuiz){if(!cannabisQuiz.visible()){new Effect.Parallel([new Effect.BlindDown(cannabisQuiz,{sync:true}),new Effect.Appear(cannabisQuiz,{sync:true})],{duration:0.6});}
cannabisQuiz.down('a').observe('click',this.fire.bindAsEventListener(this));}},build:function()
{this.container=new Element('div',{id:'ncCannabisQuiz',className:'ncPopup'});var pageSize=getPageSize();this.overlay=new Element('div',{className:'overlay'});this.overlay.setStyle({width:'100%',height:pageSize[1]+'px'}).hide();this.quizContainer=new Element('div',{className:'container'}).hide();var wrapper=new Element('div',{className:'wrapper'});this.formContainer=new Element('div',{className:'content-container'});wrapper.insert(this.formContainer);this.quizContainer.insert(wrapper);this.container.insert(this.overlay);this.container.insert(this.quizContainer);this.optIn=true;$(document.body).insert({bottom:this.container});new Ajax.Updater(this.formContainer,_nc.baseUrl+'includes/forms/cannabis-quiz.php',{onComplete:function()
{this.formObj=this.formContainer.down('form');this.built=true;var optOutButtons=this.formObj.select('button');if(optOutButtons){optOutButtons.invoke('observe','click',this.optOut.bindAsEventListener(this));}
this.overlay.observe('click',this.optOut.bindAsEventListener(this));this.formContainer.up('.wrapper').observe('click',this.optOut.bindAsEventListener(this));this.formObj.observe('submit',this.submit.bindAsEventListener(this));this.show();this.infoContainer=$('cannabisQuiz-info');}.bind(this)});},fire:function(event)
{event.stop();this.optIn=true;this.options.force=true;if(!this.built){this.build();}else{this.show();}},show:function()
{this.overlay.hide();this.quizContainer.hide();this.container.show();this.formObj.select('.error').invoke('remove');this.formObj.setStyle({height:'auto'});this.formObj.reset();new Effect.Parallel([new Effect.Appear(this.overlay,{sync:true,to:0.5}),new Effect.Appear(this.quizContainer,{sync:true}),new Effect.ScrollTo('ncMain')],{duration:0.5});},clear:function()
{this.formObj.reset();this.formObj.select('a.mark').invoke('remove');if(selected=this.formObj.down('li.selected')){selected.removeClassName('selected').setStyle({backgroundColor:'#ffffff',marginRight:'30px',paddingRight:'0px'});}
if(next=this.formObj.down('li.next')){next.removeClassName('next');}
if(closeButton=this.formObj.down('button.close')){closeButton.remove();}
if(veil=this.formObj.down('div.veil')){veil.remove();}
this.formObj.select('input[type="submit"], button').invoke('show');this.infoContainer.hide();this.infoContainer.down('.topics').update();this.formContainer.setStyle({width:'440px'});this.formContainer.select('span.correct-answer').invoke('remove');this.scaled=false;},submit:function(event)
{event.stop();this.formObj.down('div.submit').addClassName('waiting');this.formObj.request({onSuccess:function(t,json)
{this.formObj.down('div.submit').removeClassName('waiting');this.formObj.select('button, input[type="submit"]').invoke('hide');var closeButton=new Element('button',{className:'close'}).insert('Close');this.formObj.down('div.submit').insert(closeButton);closeButton.observe('click',this.close.bindAsEventListener(this));$H(json).each(function(answer){var mark=new Element('a',{href:'#'+answer.key,rel:answer.key,className:'mark'}).hide();if(answer.value.correct){var image=new Element('img',{src:_nc.baseUrl+'images/widgets/correct.gif',alt:'correct'});mark.addClassName('correct');}else{var image=new Element('img',{src:_nc.baseUrl+'images/widgets/wrong.gif',alt:'wrong'});mark.addClassName('wrong');}
image.setStyle({width:'46px',height:'41px'});if(answer.value.noAnswer){this.formObj.down('input[name="'+answer.key+'"]').up('.question').insert(new Element('span',{className:'correct-answer'}).insert('The correct answer was <strong>'+answer.value.correctAnswer+'</strong>'));}
var furtherContainer=new Element('div',{className:'further-info'});var linklist=new Element('ul');$H(answer.value.links).each(function(link){var li=new Element('li');var a=new Element('a',{href:link.key}).insert(link.value);li.insert(a);linklist.insert(li);a.observe('click',function(event){event.stop();window.open(a.readAttribute('href'));})});furtherContainer.insert(new Element('p').insert('For further information:')).insert(linklist);mark.insert(image);mark.insert('<span>more info &raquo;</span>');var listItem=this.formObj.down('input[name="'+answer.key+'"]').up('li');listItem.insert(mark);var information=new Element('div',{className:'topic '+answer.key}).hide().insert(answer.value.information);information.insert(furtherContainer);this.infoContainer.down().insert(information);(function(){var liHeight=listItem.getHeight();var markHeight=mark.getHeight();var top=Math.round((liHeight/2)-(markHeight/2));mark.setStyle({top:top+'px'});mark.observe('click',this.moreInfo.bindAsEventListener(this));new Effect.Appear(mark,{duration:0.5});}.bind(this)).defer();},this);}.bind(this)})},moreInfo:function(event)
{event.stop();if(this.currentLink==event.findElement('a')){return false;}
this.currentLink=event.findElement('a');if(!this.scaled){this.currentTopic=this.infoContainer.down('.'+this.currentLink.readAttribute('rel')).show();var currentWidth=this.formContainer.getWidth();var newWidth=960;var scalePercentage=(newWidth/currentWidth)*100;new Effect.Scale(this.formContainer,scalePercentage,{scaleY:false,scaleContent:false,duration:0.5,afterFinish:function(){new Effect.Appear(this.infoContainer,{duration:0.5});}.bind(this)});this.veil=new Element('div',{className:'veil'});this.veil.setStyle({width:'1px',left:'-1px'});this.infoContainer.down().insert(this.veil);this.currentElement=this.currentLink.up('li');this.select();var listHeight=this.currentElement.up('ol').getHeight();['paddingTop','paddingBottom','borderTopWidth','borderBottomWidth'].each(function(style){var padding=parseInt(this.infoContainer.getStyle(style));if(!isNaN(padding)){listHeight=listHeight-padding;}},this);this.infoContainer.setStyle({height:listHeight+'px'});this.scaled=true;}else{this.deselect();this.changeTopics();this.currentElement=this.currentLink.up('li');this.select();}},select:function()
{this.currentElement.addClassName('selected');if(!this.currentElement.hasClassName('last')){this.currentElement.next('li').addClassName('next');}
new Effect.Morph(this.currentElement,{style:{paddingRight:'30px',marginRight:'0px',backgroundColor:'#f0f0f0'},duration:0.5,afterFinish:function(){var liHeight=this.currentElement.getHeight();liHeight=liHeight-2;var positionedOffset=this.currentElement.positionedOffset();var topOffset=positionedOffset.top;this.veil.setStyle({top:topOffset+'px',height:liHeight+'px'}).show();}.bind(this)});},deselect:function()
{this.currentElement.removeClassName('selected');if(!this.currentElement.hasClassName('last')){this.currentElement.next('li').removeClassName('next');}
if(veil=this.currentElement.down('div.veil')){veil.remove();}
new Effect.Morph(this.currentElement,{style:{paddingRight:'0px',marginRight:'30px',backgroundColor:'#ffffff'},duration:0.5,beforeStart:function(effect){this.veil.hide();effect.element.setStyle({borderRightWidth:'1px'});}.bind(this),afterFinish:function(effect){effect.element.setStyle({borderRightWidth:'0px'});}});},changeTopics:function()
{var previousTopic=this.currentTopic;var topicWidth=previousTopic.getWidth();previousTopic.absolutize();previousTopic.setStyle({zIndex:1000});this.currentTopic=this.infoContainer.down('.'+this.currentLink.readAttribute('rel')).show();new Effect.Fade(previousTopic,{duration:0.5,afterFinish:function(){previousTopic.setStyle({zIndex:900});}});},optOut:function(event)
{if(!event.element().hasClassName('wrapper')&&event.element().tagName.toLowerCase()!='button'){return false;}
event.stop();this.optIn=false;this.quiz.complete=true;this.cookieHandler.set('cannabisQuiz',this.quiz,365);this.close();this.showWidget();},close:function()
{new Effect.Fade(this.container,{duration:0.5,afterFinish:function(effect){this.clear();if(Prototype.Browser.IE){this.container.remove();this.built=false;}}.bind(this)});}}
