/* Utilities */
function addslashes(str) { 
    return (str+'').replace(/([\\"'])/g, "\\$1").replace(/\0/g, "\\0");
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {createCookie(name,"",-1);}

function getNodeValue(node)
{
   return node.innerText || node.textContent || node.value || node.text; 
}


/* AddToAny */
YAHOO.util.Event.onDOMReady(function setupShareThis(){
  var STcoll = YAHOO.util.Dom.getElementsByClassName("communitypostinlist");
  if (STcoll.length >= 1) { // Listing page
    for (var STi = 0; STi < STcoll.length; STi++) {
      var STitemTitle = YAHOO.util.Dom.getFirstChild(YAHOO.util.Dom.getFirstChild(STcoll[STi]));
      var STfooter = YAHOO.util.Dom.getLastChild(STcoll[STi]);

     var STTarget = document.createElement("div");
     STTarget.setAttribute("style","width:175px;height:20px;float:right");
     var scpt = "<fb:share-button href=\"" + STitemTitle.href + "\" type=\"button_count\"></fb:share-button>";

     //var scpt = "<a class=\"a2a_dd\" href=\"http://www.addtoany.com/share_save\?linkurl=" + STitemTitle.href + "\"><img src=\"http://static.addtoany.com/buttons/share_save_171_16.png\" width=\"171\" height=\"16\" border=\"0\" alt=\"Share/Save/Bookmark\" /></a><script type=\"text/javascript\">" +
         "a2a_linkname=\"" + addslashes(STitemTitle.innerHTML) + "\";" +
         "a2a_linkurl=\"" + STitemTitle.href + "\";" +
         "a2a_init(\"page\");" +
         "</script>";
     STTarget.innerHTML = scpt;
     YAHOO.util.Dom.insertBefore(STTarget ,
           YAHOO.util.Dom.getFirstChild(STfooter));

    }
  }
  else { // Likely Single Entry page
    
  }


});



/* Accordion Menu */

var AccordionMenu=(function(){var YUD=YAHOO.util.Dom;var YUE=YAHOO.util.Event;var oMenuSetting={};var oMenuCache={};var dLastHoverTitle;YUD.addClass(document.documentElement,'accordion-menu-js');function getDT(e){var dEl=YUE.getTarget(e);if((e.tagName+'').toUpperCase()=='DD'){var dt=e.previousSibling;while(dt){if(dt.tagName&&dt.tagName.toUpperCase()=='DT'){break;};dt=dt.previousSibling};if(!dt||dt.tagName.toUpperCase()!='DT'){return;}else{return(dt.tagName==='DT')?dt:null};}else if(e.clientX){var found=false;while(dEl.parentNode){if(YUD.hasClass(dEl,'a-m-t')){found=true;break;};dEl=dEl.parentNode;};if(!found){return null}else{return(dEl.tagName==='DT')?dEl:null};};};function getDD(dt){if(!dt){return null;};var dd=dt.nextSibling;while(dd){if(dd.tagName&&dd.tagName.toUpperCase()=='DD'){break;};dd=dd.nextSibling;};if(!dd||dd.tagName.toUpperCase()!='DD'){return;}else{return dd};};function expand(dl,dt,dd){dl.hasAnimation+=1;YUD.addClass(dd,'a-m-d-before-expand');var oAttr={height:{from:0,to:dd.offsetHeight}};YUD.removeClass(dd,'a-m-d-before-expand');var onComplete=function(){oAnim.onComplete.unsubscribe(onComplete);oAnim.stop();YUD.removeClass(dd,'a-m-d-anim');YUD.addClass(dd,'a-m-d-expand');onComplete=null;dl.hasAnimation-=1;var dt=getDT(dd);YUD.addClass(dt,'a-m-t-expand');if(oMenuCache[dl.id]&&oMenuCache[dl.id].onOpen&&dd.style.height!=''){oMenuCache[dl.id].onOpen({dl:dl,dt:dt,dd:dd});};dd.style.height='';};var onTween=function(){if(dd.style.height){YUD.addClass(dd,'a-m-d-anim');oAnim.onTween.unsubscribe(onTween);onTween=null;dd.oAnim=null;};};if(dd.oAnim){dd.oAnim.stop();dd.oAnim=null;dl.hasAnimation-=1;};var oEaseType=YAHOO.util.Easing.easeOut;var seconds=0.5;if(oMenuCache[dl.id]){oEaseType=oMenuCache[dl.id]['easeOut']?oEaseType:YAHOO.util.Easing.easeIn;seconds=oMenuCache[dl.id]['seconds'];if(!oMenuCache[dl.id]['animation']){var oAnim={onComplete:{unsubscribe:function(){}},stop:function(){}};onComplete();return;};};var oAnim=new YAHOO.util.Anim(dd,oAttr,seconds,oEaseType);oAnim.onComplete.subscribe(onComplete);oAnim.onTween.subscribe(onTween);oAnim.animate();dd.oAnim=oAnim;};function collapse(dl,dt,dd){dl.hasAnimation+=1;YUD.addClass(dd,'a-m-d-anim');var oAttr={height:{from:dd.offsetHeight,to:0}};var onComplete=function(){oAnim.onComplete.unsubscribe(onComplete);YUD.removeClass(dd,'a-m-d-anim');YUD.removeClass(dd,'a-m-d-expand');dd.style.height='';dd.oAnim=null;onComplete=null;dl.hasAnimation-=1;var dt=getDT(dd);YUD.removeClass(dt,'a-m-t-expand');if(oMenuCache[dl.id]&&oMenuCache[dl.id].onOpen){oMenuCache[dl.id].onClose({dl:dl,dt:dt,dd:dd});};};if(dd.oAnim){dd.oAnim.stop();dd.oAnim=null;dl.hasAnimation-=1;};var oEaseType=YAHOO.util.Easing.easeOut;var seconds=0.5;if(oMenuCache[dl.id]){oEaseType=oMenuCache[dl.id]['easeOut']?oEaseType:YAHOO.util.Easing.easeIn;seconds=oMenuCache[dl.id]['seconds'];if(!oMenuCache[dl.id]['animation']){var oAnim={onComplete:{unsubscribe:function(){}},stop:function(){}};onComplete();return;};};var oAnim=new YAHOO.util.Anim(dd,oAttr,seconds,oEaseType);oAnim.onComplete.subscribe(onComplete);oAnim.animate();dd.oAnim=oAnim;};function collapseAll(dl,dt,dd){var aOtherDD=YUD.getElementsByClassName('a-m-d-expand','dd',dl);for(var i=0;i<aOtherDD.length;i++){var otherDD=aOtherDD[i];if(otherDD!=dd){collapse(dl,null,otherDD);};};}var onMenuMouseover=function(e){var dMenuTitle=getDT(e);if(!dMenuTitle){return;};if(dLastHoverTitle){YUD.removeClass(dLastHoverTitle,'a-m-t-hover');};YUD.addClass(dMenuTitle,'a-m-t-hover');dLastHoverTitle=dMenuTitle;YUE.preventDefault(e);return false;};var onMenuMouseout=function(e){var dMenuTitle=getDT(e);if(!dMenuTitle){return;};if(dLastHoverTitle&&dLastHoverTitle!=dMenuTitle){YUD.removeClass(dLastHoverTitle,'a-m-t-hover');YUD.removeClass(dLastHoverTitle,'a-m-t-down');};YUD.removeClass(dMenuTitle,'a-m-t-down');YUD.removeClass(dMenuTitle,'a-m-t-hover');dLastHoverTitle=null;YUE.preventDefault(e);return false;};var onMenuMousedown=function(e){var dMenuTitle=getDT(e);if(!dMenuTitle){return;};YUD.addClass(dMenuTitle,'a-m-t-down');YUE.preventDefault(e);return false;};var onMenuClick=function(e){var dt=getDT(e);if(!dt){return;};var dd=getDD(dt);if(!dd){return;};var dl=dt.parentNode;if(dl.hasAnimation==null){dl.hasAnimation=0;}if(dl.hasAnimation>0){return;};YUD.removeClass(dt,'a-m-t-down');if(YUD.hasClass(dd,'a-m-d-expand')){collapse(dl,dt,dd);}else
{if(oMenuCache[dl.id]&&oMenuCache[dl.id].dependent==false){}else{collapseAll(dl,dt,dd);}expand(dl,dt,dd);};YUE.preventDefault(e);return false;};YUE.on(document,'mouseover',onMenuMouseover);YUE.on(document,'mouseout',onMenuMouseout);YUE.on(document,'mousedown',onMenuMousedown);YUE.on(document,'click',onMenuClick);var oApi={openDtById:function(sId){var dt=document.getElementById(sId);if(!dt){return;};if(!YUD.hasClass(dt,'a-m-t')){return;};var dl=dt.parentNode;var dd=getDD(dt);if(dl.hasAnimation==null){dl.hasAnimation=0;};if(dl.hasAnimation>0){return;};if(YUD.hasClass(dd,'a-m-d-expand')){return;};if(oMenuCache[dl.id]&&oMenuCache[dl.id].dependent==false){}else{collapseAll(dl,dt,dd);}expand(dl,dt,dd);},closeDtById:function(sId){var dt=document.getElementById(sId);if(!dt){return;};if(!YUD.hasClass(dt,'a-m-t')){return;};var dl=dt.parentNode;var dd=getDD(dt);if(dl.hasAnimation==null){dl.hasAnimation=0;};if(dl.hasAnimation>0){return;};if(!YUD.hasClass(dd,'a-m-d-expand')){return;};collapse(dl,dt,dd);},setting:function(id,oOptions){if(!oOptions){return;};if(typeof(id)!='string'){return;};var setMunu=function(dl){dl=dl||this;dl.hasAnimation=0;oMenuCache[dl.id]={element:dl,dependent:true,onOpen:function(){},onClose:function(){},seconds:0.5,easeOut:true,openedIds:[],animation:true};oMenu=oMenuCache[dl.id];if(typeof(oOptions['animation'])=='boolean'){oMenu['animation']=!!oOptions['animation'];};if(typeof(oOptions['dependent'])=='boolean'){oMenu['dependent']=!!oOptions['dependent'];};if(typeof(oOptions['easeOut'])=='boolean'){oMenu['easeOut']=!!oOptions['easeOut'];};if(typeof(oOptions['seconds'])=='number'){oMenu['seconds']=Math.max(0,oOptions['seconds']);};if(typeof(oOptions['onOpen'])=='function'){oMenu['onOpen']=oOptions['onOpen'];};if(typeof(oOptions['onClose'])=='function'){oMenu['onClose']=oOptions['onClose'];};if(oOptions['openedIds'].shift){oMenu['openedIds']=oOptions['openedIds'];};for(var i=0;i<oMenu['openedIds'].length;i++){var sId=oMenu['openedIds'][i];var dt=document.getElementById(sId);if(dt&&dt.tagName.toUpperCase()=='DT'){var dl=dt.parentNode;var dd=getDD(dt);expand(dl,dt,dd);}else if(!dt){function onDtAvailable(){var dt=this;if(dt.tagName.toUpperCase()=='DT'){var dl=dt.parentNode;var dd=getDD(dt);expand(dl,dt,dd);};};YUE.onAvailable(sId,onDtAvailable);}};};if(document.getElementById(id)){setMunu(document.getElementById(id))}else
{YUE.onAvailable(id,setMunu);};}};return oApi;})();
/* End Accordion Menu */

/* Menu Stuff */
var menu1=new Array()
menu1[0]='<a href="/page/community/post_edit">Write a Post</a>'
menu1[1]='<a href="/page/community/dashboard">Edit a Post</a>'
menu1[2]='<a href="/page/community/blog_edit">Blog Settings</a>'
menu1[3]='<a href="/bloghelp">Blog Help</a>'
var menu2=new Array()
menu2[0]='<a href="/page/profile/edit">My Profile</a>'
menu2[1]='<a href="/page/points/view">My Points</a>'
menu2[2]='<a href="/page/user/manage?uu=/page/dashboard/view/private">Account Settings</a>'
menu2[3]='<a href="/accounthelp">Account Help</a>'
var menu3=new Array()
menu3[0]='<a href="/page/socialnet/mynetwork">My Network</a>'
menu3[1]='<a href="/page/socialnet/mymessages">My Messages</a>'
menu3[2]='<a href="/peoplehelp">People Help</a>'
var menu4=new Array()
menu4[0]='<a href="/page/group">My Groups</a>'
menu4[1]='<a href="/page/group_create">Create a Group</a>'
menu4[2]='<a href="/page/group">Find Groups</a>'
menu4[3]='<a href="/grouphelp">Groups Help</a>'
var menu5=new Array()
menu5[0]='<a href="/page/event/myevents">Manage Events</a>'
menu5[1]='<a href="/page/event/search_simple">Find Events</a>'
menu5[2]='<a href="/page/event/create">Post Event</a>'
menu5[3]='<a href="/eventhelp">Events Help</a>'

		
var menuwidth='170px' //default menu width
var menubgcolor='#eeeeff'  //menu bgcolor
var disappeardelay=250  //menu disappear speed onMouseout (in miliseconds)
var hidemenu_onclick="yes" //hide menu when user clicks within menu?
/////No further editting needed
var ie4=document.all
var ns6=document.getElementById&&!document.all
if (ie4||ns6)
document.write('<div id="dropmenudiv" style="visibility:hidden;width:'+menuwidth+';background-color:'+menubgcolor+'" onMouseover="clearhidemenu()" onMouseout="dynamichide(event)"></div>')
function getposOffset(what, offsettype){
var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
var parentEl=what.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}
function showhide(obj, e, visible, hidden, menuwidth){
if (ie4||ns6)
dropmenuobj.style.left=dropmenuobj.style.top="-500px"
if (menuwidth!=""){
dropmenuobj.widthobj=dropmenuobj.style
dropmenuobj.widthobj.width=menuwidth
}
if (e.type=="click" && obj.visibility==hidden || e.type=="mouseover")
obj.visibility=visible
else if (e.type=="click")
obj.visibility=hidden
}
function iecompattest(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}
function clearbrowseredge(obj, whichedge){
var edgeoffset=0
if (whichedge=="rightedge"){
var windowedge=ie4 && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15
dropmenuobj.contentmeasure=dropmenuobj.offsetWidth
if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
edgeoffset=dropmenuobj.contentmeasure-obj.offsetWidth
}
else{
var topedge=ie4 && !window.opera? iecompattest().scrollTop : window.pageYOffset
var windowedge=ie4 && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure){ //move up?
edgeoffset=dropmenuobj.contentmeasure+obj.offsetHeight
if ((dropmenuobj.y-topedge)<dropmenuobj.contentmeasure) //up no good either?
edgeoffset=dropmenuobj.y+obj.offsetHeight-topedge
}
}
return edgeoffset
}
function populatemenu(what){
if (ie4||ns6)
dropmenuobj.innerHTML=what.join("")
}
function dropdownmenu(obj, e, menucontents, menuwidth){
if (window.event) event.cancelBubble=true
else if (e.stopPropagation) e.stopPropagation()
clearhidemenu()
dropmenuobj=document.getElementById? document.getElementById("dropmenudiv") : dropmenudiv
populatemenu(menucontents)
if (ie4||ns6){
showhide(dropmenuobj.style, e, "visible", "hidden", menuwidth)
dropmenuobj.x=getposOffset(obj, "left")
dropmenuobj.y=getposOffset(obj, "top")
dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+"px"
dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+obj.offsetHeight+"px"
}
return clickreturnvalue()
}
function clickreturnvalue(){
if (ie4||ns6) return false
else return true
}
function contains_ns6(a, b) {
while (b.parentNode)
if ((b = b.parentNode) == a)
return true;
return false;
}
function dynamichide(e){
if (ie4&&!dropmenuobj.contains(e.toElement))
delayhidemenu()
else if (ns6&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget))
delayhidemenu()
}
function hidemenu(e){
if (typeof dropmenuobj!="undefined"){
if (ie4||ns6)
dropmenuobj.style.visibility="hidden"
}
}
function delayhidemenu(){
if (ie4||ns6)
delayhide=setTimeout("hidemenu()",disappeardelay)
}
function clearhidemenu(){
if (typeof delayhide!="undefined")
clearTimeout(delayhide)
}
if (hidemenu_onclick=="yes")
document.onclick=hidemenu;

/* End Menu Stuff */

/* Site Personalization */

function set_spud_from_qsignup()
{
  if (spud_set) {
    spud_set({field : 'email', value : document.getElementById('qemail').value });
    spud_set({field : 'zip', value : document.getElementById('qzip').value });
  }
}

// Initialize the personalization data if any

YAHOO.util.Event.onContentReady("pnhat", function () {
  var script = document.createElement("script");
  script.setAttribute("src", "/page/spud?jsonp=callback&type=getm&field=firstname,lastname,email,lastlogin,zip,lastlogout");
  script.setAttribute("type","text/javascript");
  document.body.appendChild(script);
});

function callback(spud) {
  if (spud.firstname) {
     var welcome = document.createElement("div");
     welcome.setAttribute("id","hatwelcome");
     welcome.innerHTML = "<span style=\"font-family:Palatino;font-style:italic\">Welcome back, " +
       spud.firstname + "!</span> <span style=\"font-size:smaller\">(Not " + spud.firstname + "? <a href=\"" + location.href + "\" onclick=\"eraseCookie('spud');window.location.reload();\">Click here</a>.)</span>";
     
     document.getElementById("pnhat").replaceChild(
       welcome, document.getElementById("quickjoin"));
  }
  else {
     var welcome = document.createElement("div");
     welcome.setAttribute("id","hatwelcome");
     welcome.innerHTML = "Welcome, stranger! <i>(<a href=\"/page/dashboard/private\" title=\"Click here to set up your profile and we'll greet you by name!\">Create your profile</a>)</i>";
     
     document.getElementById("pnhat").replaceChild(
       welcome, document.getElementById("quickjoin"));

  }

}

/* Site Localization */

if (location.href.indexOf("/page/unsubscribe") != -1) {
  YAHOO.util.Event.onContentReady("bannergraphic", function () {
    bannergraphic = document.getElementById("bannergraphic");
    var curState = "co";
    if (location.href.indexOf("keystoneprogress") != -1) {
          bannergraphic.src = "/page/-/ksp/kspban1.png";
      document.getElementById("mainmenu").style.display="none";
      document.getElementById("rightcolumn").style.display="none";
      document.getElementById("mainfooter").style.display="none";
      document.getElementById("pnhat").style.display="none";

    }
    else if (location.href.indexOf("granitestateprogress") != -1) {
          bannergraphic.src = "/page/-/gsp/nhban1.gif";
      document.getElementById("mainmenu").style.display="none";
      document.getElementById("rightcolumn").style.display="none";
      document.getElementById("mainfooter").style.display="none";
      document.getElementById("pnhat").style.display="none";

    }
    
  });
}

/* Insert the Facebook and Twitter buttons on signup pages
   on the signup/invite wrapper */

YAHOO.util.Event.onDOMReady(function()
{
   if (document.getElementsByTagName(
      "body")[0].className.indexOf("autofb") != -1) {
      var fbShareButton = document.createElement("fb:share-button");
      var twButton = document.createElement("img");

      fbShareButton.setAttribute("class","url");
      fbShareButton.setAttribute("type","box_count");

      fbShareButton.setAttribute("href", location.protocol + 
         "//" + location.hostname + "/page/s" + 
         location.pathname.substr(location.pathname.lastIndexOf("/")));

      twButton.setAttribute("src",
         "http://api.tweetmeme.com/imagebutton.gif?url=" + location.protocol + 
         "//" + location.hostname + "/page/s" + 
         location.pathname.substr(location.pathname.lastIndexOf("/")));

      fbShareButton.style.marginTop=fbShareButton.style.marginBottom="15px";
      fbShareButton.style.marginLeft=twButton.style.marginLeft=
         fbShareButton.style.marginRight=twButton.style.marginRight="15px";
      fbShareButton.style.cssFloat=fbShareButton.style.styleFloat=
         twButton.style.cssFloat=twButton.style.styleFloat="right";
      fbShareButton.style.clear=twButton.style.clear="both";

      /* Build the Twitter status update from a TT element if it exists,
         otherwise give it a default based on the title element */

      var tweet="";

      if (document.getElementsByTagName("tt").length == 1 &&
            getNodeValue(document.getElementsByTagName("tt")[0]).length <= 140) {
         tweet=encodeURIComponent(
            getNodeValue(document.getElementsByTagName("tt")[0]));
         createCookie(
            location.pathname.substr(location.pathname.lastIndexOf("/")),
            tweet,0);
      }
      else {
         tweet = readCookie(location.pathname.substr(
                    location.pathname.lastIndexOf("/")));

         if (tweet==null) {
            tweet="Check this out: " + "http://www.progressnowcolorado.org/page/s" +
               location.pathname.substr(location.pathname.lastIndexOf("/"));
         }
      }
 
      var twitterLink = document.createElement("a");
      twitterLink.href = "http://twitter.com/home/?status=" + tweet;   
      twitterLink.setAttribute("onclick","window.open(this.href);return(false);")        
      twitterLink.appendChild(twButton);

      /* Signups are not connect-anabled so we have to inject some stuff */

      if (location.pathname.indexOf("/page/s/") != -1) {

         /* Hidden box for Facebook */
         var fbHiddenDiv = document.createElement("div");

         fbHiddenDiv.id = "FB_HiddenContainer";
         fbHiddenDiv.style.position = "absolute";
         fbHiddenDiv.style.left = fbHiddenDiv.style.top = "-10000px";
         fbHiddenDiv.style.width = fbHiddenDiv.style.height = "0px";

         window.document.body.insertBefore(fbHiddenDiv, 
            window.document.body.firstChild);

         /* Insert the share buttons */
         document.getElementById("signupfooter").insertBefore(fbShareButton,
            document.getElementById("signupfooter").firstChild);
         document.getElementById("signupfooter").insertBefore(twitterLink,
            document.getElementById("signupfooter").firstChild);

         /* Load the Facebook Connect API */
         LazyLoad.js(["/javascript/bsd.js",
            "/page/fbconnect/constants",
            "/modules/fbconnect/javascript/fbconnect.js",
         "http://www.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php"]);

      }

      /* Invites are connect-enabled, so we just add the button */
      if (location.pathname.indexOf("/page/invite/") != -1) {
         document.getElementById("invitationpage").parentNode.insertBefore(
            fbShareButton,
            document.getElementById("invitationpage").parentNode.firstChild);

         document.getElementById("invitationpage").parentNode.insertBefore(
            twitterLink,
            document.getElementById("invitationpage").parentNode.firstChild);

      }
   }
});