/* 
*  Copyright 2006-2010 Dynamic Site Solutions.
*  Free use of this script is permitted for non-commercial applications,
*  subject to the requirement that this comment block be kept and not be
*  altered.  The data and executable parts of the script may be changed
*  as needed.  Dynamic Site Solutions makes no warranty regarding fitness
*  of use or correct function of the script.  Terms for use of this script
*  in commercial applications may be negotiated; for this, or for other
*  questions, contact "license-info@dynamicsitesolutions.com".
*
*  Script by: Dynamic Site Solutions -- http://www.dynamicsitesolutions.com/
*  Last Updated: 2010-03-05
*/

//IE5+/Win, Firefox, Netscape 6+, Opera 7+, Safari, Google Chrome for Windows,
// Konqueror 3, IE5/Mac, iCab 3

var isMSIE=/*@cc_on!@*/false; // http://dean.edwards.name/weblog/2007/03/sniff/
var isIEmac=false; /*@cc_on @if(@_jscript&&!(@_win32||@_win16)&& 
(@_jscript_version<5.5)) isIEmac=true; @end @*/

function isEmpty(s){return ((s=='')||/^\s*$/.test(s));}

var addBookmarkObj = {
  linkText:'Add the BPVRA website to your favourites',
  title:'Bicester Parkland View Residents Association',
  URL:'http://www.bpvra.org.uk',
  addTextLink:function(parId){
    var a=addBookmarkObj.makeLink(parId);
    if(a){
      a.appendChild(document.createTextNode(addBookmarkObj.linkText));
      return;
    }
    var cont=addBookmarkObj.getParent(parId);
    if(!cont) return;
    var elm=document.createElement('span');
    elm.appendChild(document.createTextNode(addBookmarkObj.findKeys()));
    cont.appendChild(elm);
  },
  addImageLink:function(parId,imgPath){
    if(!imgPath || isEmpty(imgPath)) return;
    var a=addBookmarkObj.makeLink(parId);
    var img=document.createElement('img');
    img.title=img.alt=a?addBookmarkObj.linkText:addBookmarkObj.findKeys();
    img.src=imgPath;
    if(!a) {
      a=addBookmarkObj.getParent(parId);
      img.style.cursor='pointer';
      img.onclick=function(){alert('After closing this, '+
        this.title.charAt(0).toLowerCase()+this.title.slice(1));}
    }
    if(a) a.appendChild(img);
  },
  makeLink:function(parId){
    var cont=addBookmarkObj.getParent(parId);
    if(!cont) return null;
    var a=document.createElement('a');
    a.href=addBookmarkObj.URL;
    if(window.external && isMSIE && !isIEmac){
      // IE4/Win generates an error when you
      // execute 'typeof(window.external.AddFavorite)'
      // In IE7 the page must be from a web server, not directly from a local 
      // file system, otherwise, you will get a permission denied error.
      // Maxthon shows 'typeof(window.external.AddFavorite)' as 'undefined'
      // even though it is defined.
      a.onclick=function(){ // IE/Win
        window.external.AddFavorite(addBookmarkObj.URL,addBookmarkObj.title);
        return false;
      }
    } else if(window.opera){ // Opera 7+
      a.title=addBookmarkObj.title;
      a.rel='sidebar';
    } else {
      return null;
    }
    return cont.appendChild(a);
  },
  getParent:function(parId){
    if(!document.getElementById || !document.createTextNode) return null;
    parId=((typeof(parId)=='string')&&!isEmpty(parId))
      ?parId:'addBookmarkContainer';
    return document.getElementById(parId)||null;
  },
  findKeys:function(){
    // user agent sniffing is bad in general, but this is one of the times 
    // when it's really necessary
    var ua=navigator.userAgent.toLowerCase(),str='';
    var isWebkit=(ua.indexOf('webkit')!=-1),isMac=(ua.indexOf('mac')!=-1);
    if(ua.indexOf('konqueror')!=-1) {
      str='CTRL + B'; // Konqueror
    } else if(window.home || isWebkit || isIEmac || isMac) {
      // Firefox, Netscape, Safari, iCab, IE5/Mac
      str=(isMac?'Command/Cmd':'CTRL')+' + D';
    }
    return ((str)?'Press '+str+' to bookmark this page.':str);
  }
}

// Note: I recommend you use a more comprehensive event management script for
// production (aka "live") pages. Most libaries, such as jQuery, include one.
// Dean Edwards' event manipulation functions is a good example. You can find
// them here: http://dean.edwards.name/weblog/2005/10/add-event2/
var LoadHandler = {
  handlers:[],
  add:function(fn){
    if(window.onload!=LoadHandler.theHandler) LoadHandler._push(window.onload);
    LoadHandler._push(fn);
    window.onload=LoadHandler.theHandler;
  },
  _push:function(fn){
    if(typeof(fn)!='function') return;
    LoadHandler.handlers[LoadHandler.handlers.length]=fn;
  },
  theHandler:function(){
    var handlers=LoadHandler.handlers,i=-1,fn;
    while(fn=handlers[++i]) fn();
  }
}

LoadHandler.add(addBookmarkObj.addTextLink);


// to make multiple links, do something like this:
/*
LoadHandler.add(function(){
  var f=addBookmarkObj.addTextLink;
  f();
  f('otherContainerID');
});
*/

// below is an example of how to make an image link with this
// the first parameter is the ID. If you pass an empty string it defaults to
// 'addBookmarkContainer'.
/*
LoadHandler.add(function(){
  addBookmarkObj.addImageLink('','/images/add-bookmark.jpg');
});
*/
