//*****************************************************************************
// Copyright 2007 Starscale Inc.

ibi.YupNope.Cache.SystemTagsLookup={};ibi.YupNope.Cache.SystemTagsList=null;ibi.YupNope.TagHandler=Class.create(({},{Const:{TagColors:["yellow","purple","red","green","pink","blue"]},m_systemTagsLoadFailed:false,initialize:function(templateHash){this.m_templateHash=templateHash;},systemTagsLoadFailed:function(){return this.m_systemTagsLoadFailed;},systemTagsLoaded:function(){return ibi.YupNope.Cache.SystemTagsList!=null;},getTagName:function(tagId){return this.prv_getTagName(tagId);},getTagId:function(tagName,callback){if(ibi.YupNope.Cache.SystemTagsList==null){this.prv_loadSystemTags(function(json){this.getTagId(tagName,callback);}.bind(this),function(){callback(null);}.bind(this));}
$H(ibi.YupNope.Cache.SystemTagsList).keys().each(function(key){var tag=ibi.YupNope.Cache.SystemTagsList[key];if(tag.Name==tagName){callback(tag.Id);throw $break;}});callback(null);},getTagColor:function(tagId){return this.Const.TagColors[tagId%this.Const.TagColors.length]},getSystemTags:function(onSuccess,onFailure,idPrefix){onSuccess=onSuccess||Prototype.emptyFunction;onFailure=onFailure||Prototype.emptyFunction;idPrefix=idPrefix||"system.";if(this.m_systemTagsLoadFailed){onFailure();return;}
if(ibi.YupNope.Cache.SystemTagsList!=null){var content=this.prv_buildTagList(ibi.YupNope.Cache.SystemTagsList,idPrefix);onSuccess(content);return;}
this.prv_loadSystemTags(function(json){var content=this.prv_buildTagList(json.Tags,idPrefix);onSuccess(content);}.bind(this),function(){this.m_systemTagsLoadFailed=true;onFailure();}.bind(this));},getThingTagList:function(thingId,tagData,idPrefix,onSuccess,onFailure)
{onSuccess=onSuccess||Prototype.emptyFunction;onFailure=onFailure||Prototype.emptyFunction;if(thingId==null||tagData==null||idPrefix==null){onFailure();return;}
if(ibi.YupNope.Cache.SystemTagsList==null){this.prv_loadSystemTags(function(){this.getThingTagList(thingId,tagData,idPrefix,onSuccess,onFailure);}.bind(this),onFailure);return;}
var tagArray=[];tagData.tags.each(function(tagId){var name=this.prv_getTagName(tagId);if(name==null){name="unknown";}
var removeButton="";if(tagData.canRemove){removeButton=ibi.Template.parse(this.m_templateHash.RemoveButton,{"TagId":tagId,"ThingId":thingId});}
tagArray.push({"Id":tagId,"Name":name,"removeButton":removeButton});}.bind(this));var content=this.prv_buildTagList(tagArray,idPrefix);onSuccess(content);},removeTag:function(thingId,tagId,onSuccess,onFailure){onSuccess=onSuccess||Prototype.emptyFunction;onFailure=onFailure||Prototype.emptyFunction;if(thingId==null||tagId==null){onFailure();return;}
var consts=ibi.Phix.YupNope.Things.Const.RemoveTag;new ibi.Ajax.Request(consts.uriPath,{parameters:consts.Arg.ThingId+"="+thingId+"&"+
consts.Arg.TagId+"="+tagId,onFailure:function(){onFailure();},onSuccess:function(transport){var json=transport.responseText.evalJSON();if(json.Status==consts.Status.Success){onSuccess();}else{onFailure();}}});},addTag:function(thingId,tagId,idPrefix,canRemove,onSuccess,onFailure,on403){onSuccess=onSuccess||Prototype.emptyFunction;onFailure=onFailure||Prototype.emptyFunction;on403=on403||Prototype.emptyFunction;if(thingId==null||tagId==null||idPrefix==null){onFailure();return;}
var consts=ibi.Phix.YupNope.Things.Const.Tag;new ibi.Ajax.Request(consts.uriPath,{parameters:consts.Arg.ThingId+"="+thingId+"&"+
consts.Arg.TagId+"="+tagId,onFailure:function(){onFailure();},onSuccess:function(transport){var json=transport.responseText.evalJSON();if(json.Status==consts.Status.Success){var namespace={"Id":tagId,"Name":this.prv_getTagName(tagId),"idPrefix":idPrefix,"tagColor":this.getTagColor(tagId),"removeButton":""};if(canRemove){namespace["removeButton"]=ibi.Template.parse(this.m_templateHash.RemoveButton,{"TagId":tagId,"ThingId":thingId});}
var content=ibi.Template.parse(this.m_templateHash.Tag,namespace);onSuccess(content,json);}else{onSuccess(null,json);}}.bind(this),on403:function(){on403();}});},prv_cacheSystemTags:function(tags){ibi.YupNope.Cache.SystemTagsList=tags;tags.each(function(tag){ibi.YupNope.Cache.SystemTagsLookup[tag.Id]=tag.Name;});},prv_buildTagList:function(tags,idPrefix){var tagListContent="";tags.each(function(tag,index){tag["tagColor"]=this.Const.TagColors[tag.Id%this.Const.TagColors.length];tag["idPrefix"]=idPrefix;if(!tag["removeButton"]){tag["removeButton"]="";}
tagListContent+=ibi.Template.parse(this.m_templateHash.Tag,tag);}.bind(this));var tagHTML='';if(tags.size()==0&&this.m_templateHash.NoTags){tagHTML+=ibi.Template.parse(this.m_templateHash.NoTags,{"idPrefix":idPrefix});}
tagHTML+=ibi.Template.parse(this.m_templateHash.TagList,{"tagList":tagListContent,"idPrefix":idPrefix});return'<div xmlns="http://www.w3.org/1999/xhtml">'+tagHTML+"</div>";},prv_getTagName:function(tagId){if(ibi.YupNope.Cache.SystemTagsLookup){return ibi.YupNope.Cache.SystemTagsLookup[tagId];}
return null;},prv_loadSystemTags:function(onSuccess,onFailure){onSuccess=onSuccess||Prototype.emptyFunction;onFailure=onFailure||Prototype.emptyFunction;var consts=ibi.Phix.YupNope.Things.Const.GetTags;new ibi.Ajax.Request(consts.uriPath,{encoding:false,onFailure:function(){onFailure();}.bind(this),onSuccess:function(transport){var json=transport.responseText.evalJSON();if(json.Status==consts.Status.Success){this.prv_cacheSystemTags(json.Tags);onSuccess(json);}
else{onFailure();}}.bind(this)});}}));