// @@ GLOBAL VARIABLES

var mainDomain="jamesweston.co.uk";
var siteName="James Weston";
var postcode="AA12+2AA";

tweetmeme_source = 'james_weston';

var newWin, todoBox, testiBox, oCount=0, lastObj=0, originalContentHeight=0;
var now = new Date();
var setting = xGetCookie("menuSetting");
if (!setting) setting = "normal"; // initialisation
var pp = "file://C:/Documents and Settings/James/Documents/Websites/James Weston/jamesweston.co.uk/";
if (document.location.href.indexOf('http://')==0) pp="/"; // pathPrefix
var objInterval = 20000;
var fadeDuration = 3;
var steps = 30;
var thisObj = 1;
var ctrlOn = false;

window.onload = function() {
  if (document.location.href.indexOf('introduction') >= 0) {
    xResizeTo('testiBox',xWidth('todoBox'),xHeight('todoBox'));
    for (i=1; xGetElementById('o'+i); i++) oCount++;
    lastObj=oCount;
    thisObj=xGetCookie("objSetting");
    if (thisObj<=oCount && thisObj!=null) {
      xShow('o'+thisObj);
      } else {
        thisObj = generate(1,oCount);
        fadeIn(xGetElementById('o'+thisObj),fadeDuration,steps);
        setTimeout("xSetCookie('objSetting', "+thisObj+", daysFromNow(364));",fadeDuration*1000);
      }
    setTimeout("cycleObj("+thisObj+",1);",objInterval/1.5);
  }


// Enhanced External links
if (!document.getElementsByTagName) void(0)
else {
  var links = document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
    if (links[i].className.match("download")) {
      links[i].innerHTML = "["+links[i].innerHTML+"]";
      links[i].title = 'Click here to download';
    }
    if (links[i].className.match("popup")) {
      if (links[i].title=='') links[i].title=links[i].host;
      links[i].title += ' (opens in new window/tab)';
      links[i].onclick = function() {
        window.open(this.href);
        return false;
      }
    }
    if (links[i].className.match("image")) {
      links[i].title = 'Click for popup enlargement';
      links[i].onclick = function() {
        popupImg(this.href);
        return false;
      }
    }
    if (links[i].className.match("status")) {
      links[i].onmouseover = function() {
        window.status=this.title;
        return true;
      }
      links[i].onmouseout = function() {
        window.status='';
        return true;
      }
    }
    if (links[i].href.match(".mp3")||links[i].href.match(".m3u")) {
      links[i].onclick = function() {
        var slash = "/";
        if (this.pathname.substring(0,1)=="/") slash="";
        pageTracker._trackPageview(slash+this.pathname);
      }      
    }
  }
}
  for (var i=0; i < document.links.length; i++) if (document.links[i].pathname.lastIndexOf("/")==document.links[i].pathname.length-1 && document.links[i].protocol=='file:') document.links[i].pathname+="index.html";
// Funky Forms

  function changeBGon() {
    this.style.backgroundColor='#9EE';
  };

  function changeBGoff() {
    this.style.backgroundColor='#8CC';
  };

  with (document) {
    if (document.all && document.forms) {
      for (j=0; j<document.forms.length; j++) {
        for (i=0; i<document.forms[j].elements.length; i++) {
          switch (document.forms[j].elements[i].type) {
            case "text" :
            case "textarea" :
            case "select-one" :
            case "select-multiple" : {
              document.forms[j].elements[i].style.backgroundColor='#8CC';
              document.forms[j].elements[i].onfocus = changeBGon;
              document.forms[j].elements[i].onblur = changeBGoff;
              break;
            }
            default : { break; }
          } // switch
        } // for i
      } // for j
    } // if
  } // with

} // function window.onload


// if (xIE4Up) document.writeln('<link rel="stylesheet" href="'+pp+'css/IE4Up.css" type="text/css" media="screen">');


function generate(x, y) { // creates a random number between two given integers

  var range = y - x + 1;
  return (Math.floor(Math.random() * range) + x);

}; // END generate()

function fadeIn(obj,dur) {
  xHide(obj);
  if (xIE4Up) {
    obj.style.filter="blendTrans(duration="+dur+")";
    obj.filters.blendTrans.Apply();
    xShow(obj);
  } else {
    obj.style.opacity=0;
    xShow(obj);
    for (i=1,opa=obj.style.opacity; (opa=(1/steps)*i)<=1; i++) setTimeout("xGetElementById('o"+thisObj+"').style.opacity='"+opa+"';",Math.round(opa*(dur*1000)));
  }
  if (xIE4Up) obj.filters.blendTrans.Play();
}

function fadeOut(obj,dur) {
  xShow(obj);
  if (xIE4Up) {
    obj.style.filter="blendTrans(duration="+dur+")";
    obj.filters.blendTrans.Apply();
    xHide(obj);
  } else {
    for (i=1,opa=0; (opa=(1/steps)*i)<=1; i++) setTimeout("xGetElementById('o"+thisObj+"').style.opacity='"+(1-opa)+"';",Math.round(opa*(dur*1000)));
    setTimeout("xHide('o"+thisObj+"')",dur*1000);
  }
  if (xIE4Up) obj.filters.blendTrans.Play();
}

// Other functions =================================

function changeMenu() {
  setting = (!setting || setting=="normal") ? "abstract" : "normal";
  xSetCookie("menuSetting", setting, daysFromNow(365));
  window.location.reload();
}

function popupPhotos(doc) {
  remoteWin=window.open(doc,"photos","scrollbars=1,width=700,height=500,resizable=1");
  remoteWin.focus();
}

xDeleteCookie("votedPNP");
xDeleteCookie("testiSetting");

function daysFromNow(days) {
  delete now;
  now = new Date();
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    now.setTime(now.getTime() - skew);
  // above 'fix' from DocJS.com, for older browsers I think :S
  now.setTime(now.getTime() + days * 24 * 60 * 60 * 1000);
  return now;
}

var FavOrBM = (window.sidebar) ? 'Bookmark' : 'Favourite';

function addToFavorites(title, url) {
  title = (title) ? title : document.title;
  url = (url) ? url : document.location.href;
  if (window.sidebar) window.sidebar.addPanel(title, url,"")
  else if (window.external) window.external.AddFavorite(url, title)
  else if (window.opera && window.print) {
    var mbm = document.createElement('a');
    mbm.setAttribute('rel','sidebar');
    mbm.setAttribute('href',url);
    mbm.setAttribute('title',title);
    mbm.click();
  }
}

// xLoadScript r1, Copyright 2001-2007 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL

function xLoadScript(url)
{
  if (document.createElement && document.getElementsByTagName) {
    var s = document.createElement('script');
    var h = document.getElementsByTagName('head');
    if (s && h.length) {
      s.src = url;
      h[0].appendChild(s);
    }
  }
}
