Colloquio.Controls.TagFilter=Class.create(Colloquio.Views.CoreView,Colloquio.Observable,{CLASS:'TagFilter',BLANK_TAG:'',initialize:function($super,tags,value)
{$super(Colloquio.Dom.newNode(Colloquio.Html.Tag.DIV,{cssClass:this.CLASS}));this.tags=tags;this._buildView();if(value!=undefined)this.tagSelector.value=value;},getTag:function()
{return this.tagSelector.value;},getSelectElement:function()
{return this.tagSelector;},setTags:function(tags)
{this.element.update();this.tags=tags;this._buildView();},_buildView:function()
{this.tagSelector=Colloquio.Dom.newNode(Colloquio.Html.Tag.SELECT,{name:Colloquio.Controls.TagFilter.SELECT_ELEMENT_NAME});this.tagSelector.observe('change',this._tagChanged.bind(this));this.tagSelector.appendChild(Colloquio.Dom.newNode(Colloquio.Html.Tag.OPTION,this.BLANK_TAG,{value:this.BLANK_TAG}));this.tags.each(function(tag){this.tagSelector.appendChild(Colloquio.Dom.newNode(Colloquio.Html.Tag.OPTION,tag,{value:tag}));}.bind(this));this.element.appendChild(this.tagSelector);},_tagChanged:function()
{this._notifyObservers();}});Colloquio.Controls.TagFilter.SELECT_ELEMENT_NAME='tags[]';Colloquio.Controls.TagFilterBuilder=Class.create(Colloquio.Views.CoreView,{CLASS:'TagFilterBuilder',MAX_TAG_FILTERS:3,initialize:function($super,element,updateElement,url,allTags,options)
{$super(element,this.CLASS);this._setOptions({filterTags:[],remainingTags:[],titleTemplate:null},options);if(this.options.titleTemplate)this.titleTemplate=new Template(this.options.titleTemplate);this.updateElement=$(updateElement);this.url=url;this.allTags=allTags;this.currentTags=null;this.tagFilters=[];this.baseTagFilters=[];this.tags=[]
this.baseTags=[];if(this.options.filterTags.length>0)
{this.tags=this.options.filterTags;this.baseTags=this.tags.clone();this._addTags();this.baseTagFilters=this.tagFilters.clone();}
else if(this._getTagsFromHash())
{this._updateFromHash();}
Colloquio.LocationManager.getInstance().addObserver(this);this._buildView();if(this.options.remainingTags.length>0)
this.tagFilters.last().setTags(this.options.remainingTags.minus(this.tags));},_buildView:function()
{var child=this.element.down();while(child)
{var next=child.next();child.remove();child=next;}
if(((this.tagFilters.length==0)||(this.tagFilters.last().getTag()!=''))&&this.tagFilters.length<this.MAX_TAG_FILTERS)
this._addTagFilter();this.tagFilters.each(function(tf){this.element.appendChild(tf.getElement());}.bind(this));},update:function(model)
{if(model==Colloquio.LocationManager.getInstance())
{this._updateFromHash();}
else
{this.tags=this._getTagsFromTagFilters();this._updateElement(true);}
this._buildView();},_updateFromHash:function()
{var newTags=this._getTagsFromHash();if(newTags!=undefined)
{if(!(newTags instanceof Array))newTags=[newTags];if((this.tags.minus(newTags).length==0)&&(newTags.minus(this.tags).length==0))
return;else
this.tags=newTags;this.tagFilters.clear();this._addTags();}
else
{if((this.tags.minus(this.baseTags).length==0)&&(this.baseTags.minus(this.tags).length==0))
return;else
{this.tags=this.baseTags.clone();this.tagFilters=this.baseTagFilters.clone();}}
this._updateElement();},_updateElement:function(updateHash)
{var params=Form.serializeElements(this.getElementsFromTagFilters());if(updateHash)Colloquio.LocationManager.getInstance().setHash(params);if(params=='')
params=Colloquio.LocationManager.getInstance().getRequestParams();new Ajax.Request(this.url,{method:'get',parameters:params,evalScripts:true,onComplete:this._onRequestComplete.bind(this)});if(this.titleTemplate)
{var tagsArr=this._getTagsFromHash();var tags=tagsArr instanceof Array?tagsArr.join(', '):tagsArr;Colloquio.Browser.setTitle(this.titleTemplate.evaluate({tags:tags}))}},_onRequestComplete:function(transport)
{this.updateElement.update(transport.responseJSON.content);this.currentTags=$A(transport.responseJSON.tags);if(this.tagFilters.last().getTag()==''&&this.tagFilters.length>1)
this.tagFilters.last().setTags(this.currentTags.minus(this.tags));},_addTags:function()
{this.tags.each(function(t){this._addTagFilter(t);}.bind(this));},_addTagFilter:function(tag)
{var tf=new Colloquio.Controls.TagFilter(this.allTags.minus(this._getTagsFromTagFilters()),tag);tf.addObserver(this);this.tagFilters.push(tf);},_getTagsFromHash:function()
{return Colloquio.LocationManager.getInstance().getHashParams(Colloquio.Controls.TagFilter.SELECT_ELEMENT_NAME);},_getTagsFromTagFilters:function()
{return this.tagFilters.map(function(tf){return tf.getTag()!=''?tf.getTag():null;}).compact();},getElementsFromTagFilters:function()
{return this.tagFilters.map(function(tf){return tf.getTag()!=''?tf.getSelectElement():null;}).compact();}});