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

if(!ibi.YupNope){ibi.YupNope={};}
ibi.YupNope.Cache={};ibi.YupNope.Const={};function showLoginForm(){if($('loginFormDiv')&&!$('loginFormDiv').visible()){if(Prototype.Browser.IE){$('loginFormDiv').setStyle({position:'absolute'});}
new Effect.Fade('headerLinksLoggedOut',{duration:0.5});new Effect.SlideDown('loginFormDiv',{duration:0.5,afterFinish:function(){if($F('loginUser').empty()){Field.focus('loginUser');}}});}}
function hideLoginForm(){if($('loginFormDiv').visible()){$('loginUser').value="";$('loginPass').value="";$('loginError').hide();new Effect.SlideUp('loginFormDiv',{duration:0.5});new Effect.Appear('headerLinksLoggedOut',{duration:0.5});}}
function doLogin(){if($('loginFormDiv').visible()==true){if($F('loginPass').empty()||$F('loginUser').empty()){showLoginError("Please fill in all fields in the form above.");}
else{$('login-form').hide();$('loginResult').hide();$('loginError').hide();$('loginProgress').show();new ibi.Ajax.Updater('loginResult',"/login",{asynchronous:true,evalScripts:true,ignoreLoggedIn:true,parameters:Form.serialize('login-form'),onSuccess:function(request){$('loginProgress').hide();$('loginResult').show();},onFailure:function(request){showLoginError(ibi.YupNope.Strings.GeneralError);}});}}}
function showLoginError(msg){$('login-form').show();$('loginProgress').hide();$('loginResult').hide();var errorDivEl=$('loginError');errorDivEl.update(msg);if(!Prototype.Browser.IE){new Effect.Shake('loginFormDiv','10');}
if(errorDivEl.visible()==false){new Effect.BlindDown(errorDivEl,{duration:0.5});}
new Effect.Highlight(errorDivEl,{startcolor:'#ff5a79',duration:2});}
function showLoggedInHeaderLinks(){var outEl=$('headerLinksLoggedOut');if(outEl){outEl.hide();}
$('headerLinksLoggedIn').show();}
function ToggleBlurb(){var blurb=$('Blurb');if(blurb.visible()){new Effect.BlindUp(blurb,{duration:0.5});}
else{new Effect.BlindDown(blurb,{duration:0.5});}}
function showAccountMenu(){if(Prototype.Browser.IE){$('accountMenuDiv').setStyle({position:'absolute'});}
new Effect.Fade('headerLinksLoggedIn');new Effect.BlindDown('accountMenuDiv',{duration:0.5});}
function hideAccountMenu(){new Effect.Appear('headerLinksLoggedIn');new Effect.BlindUp('accountMenuDiv',{duration:0.5});}
function showOtherListsPopup(){var el=$('otherListsPopup');bringToFront(el);if(!el.visible()){new Effect.BlindDown(el,{duration:0.25});}}
function hideOtherListsPopup(){var el=$('otherListsPopup');if(el&&el.visible()){new Effect.BlindUp(el,{duration:0.25});}}
function bringToFront(popup){var zindex=80;['otherListsPopup','tagPopup'].each(function(id,index){var el=$(id);if(el){el.setStyle({zIndex:zindex++});}});popup.setStyle({zIndex:zindex++});}
function setupPopup(popup){var element=$(popup+'Popup');new Draggable(element,{revert:false,handle:$(popup+'PopupHandle')});var linkEl=$(popup+'PopupLink');if(linkEl){var pos=Position.cumulativeOffset(linkEl);var newx=pos[0]-element.getWidth()+16;var newy=pos[1]+20;element.setStyle({'left':newx+"px",'top':newy+"px"});}}
function showGlobalError(text){var divEl=$("globalErrorDiv");var contentEl=$("globalErrorContent");if(Prototype.Browser.IE){divEl.setStyle({position:'absolute',top:document.documentElement.scrollTop+"px"});}
contentEl.update(text);if(!divEl.visible()){new Effect.BlindDown(divEl);new PeriodicalExecuter(function(pe){pe.stop();if(divEl.visible()){new Effect.BlindUp(divEl);}},5);}}
function hideGlobalError(){new Effect.SlideUp('globalErrorDiv',{duration:0.25});}
if(!ibi.YupNope.Voting){ibi.YupNope.Voting={};}
function doVote(thingId,vote){var butEl=$("ThingButtons."+thingId);var progEl=$("ThingProgress."+thingId);progEl.show();butEl.hide();if(ibi.YupNope.VisibleStatBlock&&!(ibi.YupNope.VisibleStatBlock==$('thingStatBlock. '+thingId))){showStatBlock(thingId,true);}
var consts=ibi.Phix.YupNope.Things.Const.AddOrVote;new ibi.Ajax.Request(consts.uriPath,{parameters:consts.Arg.ThingId+"="+thingId
+"&"+consts.Arg.Vote+"="+vote,onFailure:function(){showGlobalError(ibi.YupNope.Strings.Vote.Error);progEl.hide();butEl.show();},onSuccess:function(transport){try{var json=transport.responseText.evalJSON();if(json.Status==consts.Status.Success){var voteButtons='';if(vote==1){voteButtons=ibi.Template.parse(ibi.YupNope.Templates.yupButtonTmpl,json.Thing);}else{voteButtons=ibi.Template.parse(ibi.YupNope.Templates.nopeButtonTmpl,json.Thing);}
butEl.updateXHTML(voteButtons);progEl.hide();butEl.show();updateThingData(json.Thing);if(ibi.YupNope.Voting.Callback){ibi.YupNope.Voting.Callback();}}else{showGlobalError(ibi.YupNope.Strings.Vote.Error);reloadCurrentList()}}catch(e){ibi.log(e);}}});turnOffAutoRefresh();}
function reloadCurrentList()
{if(ibi.YupNope.thingListLoader!=null){ibi.YupNope.thingListLoader.reloadList();}}
function updateThingData(thing){$('thingScore.'+thing.Id).update(thing.Score);$('minScore.'+thing.Id).update(thing.MinScore);$('maxScore.'+thing.Id).update(thing.MaxScore);$('lastVoteTime.'+thing.Id).update(new Date(thing.LastVoteTime*1000).toLocaleString());$('votesYup.'+thing.Id).update(thing.VotesYup);$('votesNope.'+thing.Id).update(thing.VotesNope);var total=thing.VotesYup+thing.VotesNope;$('totalVotes.'+thing.Id).update(total);$('percYup.'+thing.Id).update((thing.VotesYup/total*100).toFixed(2));$('percNope.'+
thing.Id).update((thing.VotesNope/total*100).toFixed(2));}
ibi.YupNope.lastObserveFunc=Prototype.EmptyFunction;function showVoteChangeConfirm(thingid,voteStatus,pos){var voteForm=$('voteChangeConfirm');voteForm.hide();$('voteChangeText1').hide();$('voteChangeText0').hide();$('voteChangeText'+voteStatus).show();var changeButton=$('ChangeVoteButton');if(ibi.YupNope.lastObserveFunc!=Prototype.EmptyFunction){Event.stopObserving(changeButton,'click',ibi.YupNope.lastObserveFunc);}
ibi.YupNope.lastObserveFunc=function(){doVote(thingid,voteStatus==1?0:1);hideVoteChangeConfirm();}
changeButton.observe('click',ibi.YupNope.lastObserveFunc);if(voteStatus==1){changeButton.value='Change To Nope';}else{changeButton.value='Change To Yup';}
voteForm.setStyle({top:pos[1]+"px",left:pos[0]+"px"});new Effect.BlindDown(voteForm,{direction:'top-right',duration:0.25});}
function hideVoteChangeConfirm(){var el=$('voteChangeConfirm');if(el.visible()){new Effect.BlindUp(el,{duration:0.25,direction:'top-right'});}}
function changeVote(thingid,vote){hideVoteChangeConfirm();doVote(thingid,vote);}
function deleteThing(thingId){var progEl=$("ThingDeleteProgress."+thingId);var butEl=$("ThingDelete."+thingId);butEl.hide();progEl.show();var consts=ibi.Phix.YupNope.Things.Const.Delete;new ibi.Ajax.Request(consts.uriPath,{asynchronous:true,parameters:consts.Arg.ThingId+"="+thingId,onFailure:function(){showGlobalError(ibi.YupNope.Strings.Delete.Error);progEl.hide();butEl.show();},onSuccess:function(transport){progEl.hide();var json=transport.responseText.evalJSON();if(json.Status==ibi.Phix.YupNope.Things.Const.Delete.Status.Success){if(ibi.YupNope.DeleteCallback){ibi.YupNope.DeleteCallback(thingId);}}
else if(json.Status==ibi.Phix.YupNope.Things.Const.Delete.Status.HasVotes){showGlobalError(ibi.YupNope.Strings.Delete.HasVotes);$('ThingDelete.'+thingId).hide();}else{showGlobalError(ibi.YupNope.Strings.Delete.Error);$('ThingDelete.'+thingId).hide();}}});}
function onThingDeleteSuccess(thingId){new Effect.Highlight("ThingDiv."+thingId,{startcolor:"#FFD387"});new Effect.DropOut("ThingDiv."+thingId,{duration:1.5});}
ibi.YupNope.VisibleStatBlock=null;function showStatBlock(thingId,fancy){var statBlock=$('thingStatBlock.'+thingId);if(statBlock.visible()){return;}
if(ibi.YupNope.VisibleStatBlock){ibi.YupNope.VisibleStatBlock.hide();}
if(Prototype.Browser.IE){statBlock.setStyle({'margin-left':'0','z-index':'200','position':'relative','top':'-5px'});}
ibi.YupNope.VisibleStatBlock=statBlock;if(fancy){new Effect.SlideDown(statBlock,{duration:0.5});}else{statBlock.show();}
buildThingTags(thingId);}
function hideStatBlock(thingId,fancy){var el=$('thingStatBlock.'+thingId);if(el.visible()){if(fancy){new Effect.SlideUp(el,{duration:0.5});}else{el.hide();}}
ibi.YupNope.VisibleStatBlock=null;}
function turnOnAutoRefresh(refreshFunc){if(ibi.YupNope.thingListLoader){ibi.YupNope.thingListLoader.startAutoRefresh(refreshFunc);}}
function turnOffAutoRefresh(){if(ibi.YupNope.thingListLoader){ibi.YupNope.thingListLoader.stopAutoRefresh();}}
function setDebug(srcId){if($('debugDiv')){$('debugDiv').update($(srcId).innerHTML);}}