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

if(!ibi.YupNope){ibi.YupNope={};};if(!ibi.YupNope.Tags){ibi.YupNope.Tags={};};if(!ibi.YupNope.Templates){ibi.YupNope.Templates={};};ibi.YupNope.Templates.Tags={TagList:'<ul xmlns="http://www.w3.org/1999/xhtml" class="tagList" id="%(idPrefix)tagList" style="font-size: 100%">%(tagList)</ul>',Tag:'<li id="%(idPrefix)Tag.%(Id)" class="thingTag">\
 <span id="%(idPrefix)TagHandle.%(Id)" class="tagHandle">\
 <img alt="%(Name)Tag" src="/static/images/tag_%(tagColor).gif"/>\
 </span> <span><a href="javascript: filterThingListByTag(%(Id));">%(Name)</a></span>%(removeButton)</li>',FilterTagText:': filtered on <img src="/static/images/tag_%(TagColor).gif" style="position: relative; top: 2px;"/>%(Name)',RemoveButton:'<img src="/static/images/bullet_delete.gif" alt="Remove Tag" \
title="Remove Tag" class="icon" onclick="removeTag(%(TagId), %(ThingId))"/>',NoTags:'<div id="%(idPrefix)NoTags">None</div>'};function showTagPopup(){var el=$('tagPopup');bringToFront(el);if(!el.visible()){new Effect.BlindDown(el,{duration:0.25});}
if(!ibi.YupNope.Tags.tagHandler||ibi.YupNope.Tags.tagHandler.systemTagsLoaded){getSystemTags();}}
function hideTagPopup(){var el=$('tagPopup');if(el&&el.visible()){new Effect.BlindUp(el,{duration:0.25});}}
function getSystemTags(){if(ibi.YupNope.Tags.tagHandler==null){ibi.YupNope.Tags.tagHandler=new ibi.YupNope.TagHandler(ibi.YupNope.Templates.Tags);}
var dest=$('systemTagsDiv');var errMsg=ibi.YupNope.Strings.GetTags.SystemError;var idPrefix='system.';ibi.YupNope.Tags.tagHandler.getSystemTags(function(content){dest.updateXHTML(content);dest.show();ibi.YupNope.Cache.SystemTagsList.each(function(tag){new Draggable(idPrefix+"Tag."+tag.Id,{revert:true,handle:$(idPrefix+'TagHandle.'+tag.Id)});});},function(){showTagError(dest,errMsg);},idPrefix);}
function lookupTagId(tagName,callback){assertTagHandler();ibi.YupNope.Tags.tagHandler.getTagId(tagName,callback);}
function showTagError(dest,errMsg){showGlobalError(errMsg);dest.update(ibi.YupNope.Strings.GetTags.NoTags);dest.addClassName("tagError");dest.show();}
function buildThingTags(thingId,onSuccess){onSuccess=onSuccess||Prototype.emptyFunction;if($('tagContainer.Content.'+thingId).innerHTML.empty()){var dest=$('tagContainer.Content.'+thingId);var idPrefix="thing"+thingId+".";var tagData=ibi.YupNope.thingListLoader.getTagData(thingId);assertTagHandler();ibi.YupNope.Tags.tagHandler.getThingTagList(thingId,tagData,idPrefix,function(content){dest.updateXHTML(content);$('thingTagsProgress.'+thingId).hide();tagData.tags.each(function(tagId){var elId=idPrefix+'TagHandle.'+tagId;$(elId).setStyle({cursor:"default",top:"5px"});});onSuccess();},function(){showThingTagError(dest,thingId);$('thingTagsProgress.'+thingId).hide();});}}
function showThingTagError(dest,thingId){dest.update("N/A");$('thingTagsProgress.'+thingId).hide();}
function tagThing(thingId,tagId){if($('tagContainer.Content.'+thingId).innerHTML.empty()){buildThingTags(thingId,function(){tagThing(thingId,tagId);});return;}
var tagData=ibi.YupNope.thingListLoader.getTagData(thingId);if(tagData.tags.member(tagId)){return;}
var tagIdPrefix="thing"+thingId+".";var tagList=$(tagIdPrefix+"tagList");var statBlock=$('thingStatBlock.'+thingId);var progEl=$('thingTagsProgress.'+thingId);var none=$(tagIdPrefix+"NoTags");showStatBlock(thingId,true);progEl.show();tagList.hide();if(none)none.hide();var consts=ibi.Phix.YupNope.Things.Const.Tag;ibi.YupNope.Tags.tagHandler.addTag(thingId,tagId,tagIdPrefix,tagData.canRemove,function(content,json){if(json.Status==consts.Status.Success){new Insertion.Bottom(tagList,content);tagData.tags.push(tagId);}else if(json.Status==consts.Status.MemberNotOwner){showGlobalError(ibi.YupNope.Strings.Tag.NotOwner);if(none)none.show();}else{showGlobalError(ibi.YupNope.Strings.Tag.Error);if(none)none.show();}
progEl.hide();tagList.show();},function(){showGlobalError(ibi.YupNope.Strings.Tag.Error);progEl.hide();tagList.show();if(none)none.show();},function(){showGlobalError(ibi.YupNope.Strings.Tag.NotLoggedIn);if(ibi.Ajax.ExpectedLoggedIn){window.location.reload();}
else{showLoginForm();progEl.hide();tagList.show();}});}
function removeTag(tagId,thingId){var tagIdPrefix="thing"+thingId+".";var tagList=$(tagIdPrefix+"tagList");var progEl=$('thingTagsProgress.'+thingId);tagList.hide();progEl.show();ibi.YupNope.Tags.tagHandler.removeTag(thingId,tagId,function(){var tagData=ibi.YupNope.thingListLoader.getTagData(thingId);tagData.tags=tagData.tags.without(tagId);$(tagIdPrefix+'Tag.'+tagId).remove();if(tagList.empty()){var none=$(tagIdPrefix+'NoTags');if(none){none.show();}else{var content=ibi.Template.parse(ibi.YupNope.Templates.Tags.NoTags,{"idPrefix":tagIdPrefix});tagList.insert({after:content});}}
progEl.hide();tagList.show();},function(){showGlobalError(ibi.YupNope.Strings.RemoveTag.Error);progEl.hide();tagList.show();});}
function filterThingListByTag(tagId){if(ibi.YupNope.thingListLoader){if(ibi.YupNope.thingListLoader.getMode()==ibi.YupNope.Const.ListLoadMode.Search){showGlobalError(ibi.YupNope.Strings.Tag.NoSearchFilter);return;}
setFilterTagText(tagId);ibi.YupNope.thingListLoader.setTagFilter(tagId);ibi.YupNope.thingListLoader.reloadList();}}
function setFilterTagText(tagId){var namespace={"Id":tagId,"Name":ibi.YupNope.Tags.tagHandler.getTagName(tagId),"TagColor":ibi.YupNope.Tags.tagHandler.getTagColor(tagId)};var filterTagText=ibi.Template.parse(ibi.YupNope.Templates.Tags.FilterTagText,namespace);$('filterTagText').update(filterTagText);$('filterTag').show();}
function removeFilterTag(){hideFilterTagText();ibi.YupNope.thingListLoader.setTagFilter(null);ibi.YupNope.thingListLoader.reloadList();}
function hideFilterTagText(){$('filterTag').hide();}
function assertTagHandler(){if(ibi.YupNope.Tags.tagHandler==null){ibi.YupNope.Tags.tagHandler=new ibi.YupNope.TagHandler(ibi.YupNope.Templates.Tags);}}