/* $Id: bm.js 3203 2008-08-07 15:46:15Z josh $ */
var BM = {
  onDOM : { // for legacy implementations and custom javascript add-ins
    addEvent : function (func) {
      document.observe('dom:loaded', func);
    }
  },
  initPage : function() {
    BM.updateSubNav();
    BM.setCommentFields();
    BM.popExternalLinks();
  },
  popExternalLinks : function() {
    if (window.name == 'newsite') { //prevent ext links from replacing page
      window.id = ( window.name =
        'win' + (Math.floor(Math.random() * 99999999999) + 1).toString()
      ); 
    }
    Event.observe($$('body')[0], 'click', BM.clickExternal);
  },
  clickExternal : function(event) { // pop new window and give focus for target
      var e = Event.element(event);
      var link = e.tagName=='A' ? e : e.up('a');
      if(link && link.target == 'newsite') {
        if (!BM.newWin(link)) { // returns false on success for onclick
          Event.stop(event); return false;
        }
        return true; // popup blocked, just allow regular behavior
      }
  },
  newWinStyle : {
    'newsite' : [
      '', //fill to force target name
      'scrollbars=yes,resizable=yes,status=yes,toolbar=yes,'
      + 'location=yes,menubar=yes,directories=yes,personalbar=yes'
    ],
    'popup' : [
      '', //fill to force target name
      'width=550,height=350,scrollbars=yes,resizable=yes,status=yes,toolbar=yes,'
      + 'location=yes,menubar=yes,directories=yes,personalbar=yes'
    ]
  },
  newWin : function(link,styleName,customStyle) {
    var targ = link.target || 'newsite';
    if (!styleName) styleName = targ;
    var attr = customStyle || BM.newWinStyle[styleName] || BM.newWinStyle.newsite;
    targ = attr[0] || targ;
    var newWin;
    var newWin;
    try { //ie6 can choke if window already exists
        newWin = window.open(link.href, targ, attr[1]);
    }
    catch (err) {
        link.target = '_blank';
        return true;
    }

    try { //ie6 can choke if window already exists
        newWin = window.open(link.href, targ, attr[1]);
    }
    catch (err) {
        link.target = '_blank';
        return true;
    }
    if (!newWin) {
        return true; //pop-up blocker, let the link open via target
    }
    newWin.focus();
    return false;
  },
  activeSubNav : {}, // for subnavigation highlighting
  updateSubNav : function() {
    $H(BM.activeSubNav).keys().each( function(slug) {
      $$('li.bmn_sec-' + slug).each( function(li) {
        Element.addClassName(li,'bmn_active');
      });
    });
  },
  dispTip : function(tips) {
    if (!tips || !tips.length) return;
    var i = (60 / tips.length);
    var d= new Date(); var s= d.getSeconds();
    var show = Math.floor(s/i);
    if (show >= tips.length) show = show -1;
    $$('div.bmw_tip' + tips[show]).each(function(tip) {
      tip.style.display = 'block';
    });
  },
  setCommentFields : function() {
    if (!BM.cName || !BM.cEmail || !BM.cURL) return;
    var nlen = 7; //bmcomm length
    var i = 0;
    var c= document.cookie;
    var value;
    while (i < c.length) {
      var j = i + nlen;
      if (c.substring(i,j) == 'bmComm=') {
        var endstr = c.indexOf(';',j);
        if (endstr < 0) endstr = c.length;
        value = unescape(c.substring(j,endstr));
        break;
      }
      i = c.indexOf(' ',i) + 1;
      if (i==0) break;
    }
    if (!value) return;
    var vals = value.split(':::');
    $(BM.cName).value = vals[0];
    $(BM.cEmail).value = vals[1];
    $(BM.cURL).value = vals[2];
    $('bmf_comment_remember').checked = true;
  },
  saveCommentCookie : function() {
    if (!BM.cName || !BM.cEmail || !BM.cURL) return;
    var name = $F(BM.cName), email = $F(BM.cEmail), url = $F(BM.cURL);
    if ($('bmf_comment_remember').checked) {
      var expDate = new Date();
      //safari has a setdate bug that limits number of days
      expDate.setDate(expDate.getDate() + 96);
      expDate.setDate(expDate.getDate() + 96);
      document.cookie = 'bmComm=' + escape(name+':::'+email+':::'+url)
        + '; expires=' + expDate.toGMTString();
    }
    else {
      document.cookie = 'bmComm=;expires=Thu, 01-Jan-70 00:00:01 GMT';
    }
  },
  removeEdit : function() {
    document.cookie = 'BigMediumEdit=;expires=Thu, 01-Jan-70 00:00:01 GMT;path=/';
    $$('p.bmEditPanel').invoke('remove');
    return false;
  },
  Feed : {
    build : function() {
      if(!$('bmjsForm')){ return }
      var id = BM.Feed.newID(),cfld = $('bmjsCount');
      var count = cfld.value.replace(/\D+/g,'');
      if (!count){count=5}
      cfld.value=count;
      var targ=($F('bmjsWin')||''),desc=($F('bmjsDesc')||''),img=($F('bmjsImage')||'');
      var url = $F('bmjsFeed')+';count='+count+';desc='+desc+';image='+img
        +';target='+targ+';id='+id;
      var styles = BM.Feed.styles(id);
      $('bmjsCode').innerHTML='&lt;!--start news gadget -->\n'
        + styles + '&lt;script type="text/javascript" id="'+id+'_scr">\n'
        + '(function(){\nvar id="'+id+'",d=document.createElement("div");d.id=id;\n'
        + 'var me=document.getElementById(id+"_scr");me.parentNode.insertBefore(d,me);\n'
        + 'var s=document.createElement("script");s.type="text/javascript";s.charset="utf-8";\n'
        + 's.src="'+url+'";\nsetTimeout(function(){d.appendChild(s);d=me=s=null;},1);'
        + '})();\n&lt;/script>\n&lt;!--end news gadget -->';
      $('bmjsSample').innerHTML='<div id="'+id+'"><\/div>';
      var s=$('bmjsScript'); if (s){s.remove()}
      s=document.createElement("script");
      s.type="text/javascript";s.charset="utf-8";s.id='bmjsScript';s.src=url;
      document.getElementsByTagName('head')[0].appendChild(s);
    },
    newID : function() { return id='bm'+(new Date).getTime(); },
    styles : function(id) {
      return '&lt;style type="text/css">\n'
        + '#'+id+' div.bmfeedItem{margin-bottom:1em;overflow:hidden;zoom:1}\n'
        + '#'+id+' img{float:left;margin-right:5px;}\n'
        + '#'+id+' div > p:first-child{margin-top:0}\n'
        + '#'+id+' div > p:last-child{margin-bottom:0}\n'
        + '&lt;\/style>';
    }
  }
};
document.observe('dom:loaded',BM.initPage);

