// xCookie r2, Copyright 2009 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL
var xCookie = {
  get: function(name) {
    var c = document.cookie.match(new RegExp('(^|;)\\s*' + name + '=([^;\\s]*)'));  
    return ((c && c.length >= 3) ? unescape(c[2]) : null);  
  },
  set: function(name, value, days, path, domain, secure) {
    if (days) {
      var d = new Date();
      d.setTime(d.getTime() + (days * 8.64e7)); // now + days in milliseconds
    }
    document.cookie = name + '=' + escape(value) +
      (days ? ('; expires=' + d.toGMTString()) : '') +
      '; path=' + (path || '/') +
      (domain ? ('; domain=' + domain) : '') +
      (secure ? '; secure' : '');
  },
  del: function(name, path, domain) {
    this.set(name, '', -1, path, domain); // sets expiry to now - 1 day
  }
};


var mainDomain="jamesweston.co.uk";
var siteName="James Weston";

var gaPageName="";

tweetmeme_source = 'james_weston';

var newWin, todoBox, testiBox, oCount=0, lastObj=0, originalContentHeight=0;
var now = new Date();
// alert(xCookie.get("menuSetting"));
var setting = (xCookie.get("menuSetting")) ? xCookie.get("menuSetting") : 'normal';
var ctrlOn = false;


//window.onload = function() {
//jQuery(document).ready(function($) {
$(document).ready(function() {

  function drawNsqs(n) {

    var lastNumSqs = 11;
    var sqsArr = new Array();
    var picArr = new Array();
    var sqs = '';

    for (i=0; i<lastNumSqs; i++) sqsArr[i] = i;

    for (i=0, tmpIndex=generate(0, sqsArr.length-1); i<n; i++, tmpIndex=generate(0, sqsArr.length-1)) {
      picArr[i] = sqsArr[tmpIndex];
      sqsArr.splice(tmpIndex,1);
    }

  if (picArr.length == n) {
    for (i=0, sqs="", j=9; i<n; i++, j--) sqs+='<img class="a'+j+'0" border="0" src="'+document.location.href.substring(0,document.location.href.lastIndexOf('jamesweston.co.uk/'))+'jamesweston.co.uk/images/gui/sqs/'+picArr[i]+'.gif" width="56" height="56">';
    return sqs;
  } else { return false; }
}; // END drawNsqs()

$('#sqs').html(drawNsqs(9));
$('html.ie7 body.mixes #player, html.ie6 body.mixes #player').width($('body.mixes #player .widget').width()+$('body.mixes #player .options').width()+10);

// 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')) {
      var buyMethod = (document.location.hash=='#buy' && links[i].className.match('affiliate')) ? 'buy' : 'download';
      links[i].innerHTML = '['+buyMethod+']';
      links[i].title = 'Click here to '+buyMethod;
      if (document.location.hash=='#buy') $('#buy').html('Buy tracks featured in this DJ set');
    }
    if (links[i].className.match("popup")) {
      if (links[i].title=='') links[i].title=links[i].hostname;
      links[i].title += ' (opens in new window/tab)';
      links[i].onclick = function() {
        if (this.className.match("social")) {
          var slash = "/";
          if (this.pathname.substring(0,1)=="/") slash="";
          try { pageTracker._trackPageview("/ext/social"+slash+this.pathname); } catch(err) {};
        }
        if (this.className.match("affiliate")) try { pageTracker._trackPageview("/ext/affiliate/"+this.hostname.replace(/\./g,"_")); } catch(err) {};
        window.open(this.href);
        return false;
      }
    }
    if (links[i].className.match("image")) {
      links[i].title = 'Click for popup enlargement';
      links[i].onclick = function() {
        var slash = "/";
        if (this.pathname.substring(0,1)=="/") slash="";
        try { pageTracker._trackPageview("/popup/img"+slash+this.pathname); } catch(err) {};
        remoteWin = window.open("","JWphoto","resizable=1,width=200,height=200,status=1,scrollbars=1");
        remoteWin.document.open('text/html', 'replace');
        remoteWin.document.write('<html style="overflow-y:auto;"><head><meta http-equiv="imagetoolbar" content="no"><title>'+siteName+'</title><link rel="stylesheet" type="text/css" href="'+document.styleSheets[0].href+'"><!--[if IE 7]><style type="text/css">body{overflow-y:auto;}</style><![endif]--></head><body style="margin:15px 0;font-size:11px;" onload="var iW=document.popupImg.width+40, iH=document.popupImg.height+150; if (iW > screen.width) iW=screen.width-100; if (iH > screen.height) iH=screen.height-80; resizeTo(iW,iH); window.focus();"><div align="center"><img border="0" src="'+this.href+'" name="popupImg"><br><br>[<a href="javascript:window.close()">close window</a>]</div></body></html>');
        remoteWin.document.close();
        return false;
      }
    }
    if (links[i].className.match("status")) {
      links[i].onmouseover = function() {
        window.status='http://'+this.hostname+'/';
        return true;
      }
      links[i].onmouseout = function() {
        window.status='';
        return true;
      }
    }
    if (links[i].href.match(/\.(mp3|m3u|pdf|doc|txt|xls)/)) {
      links[i].onclick = function() {
        var slash = "/";
        var myPath = this.pathname;
        if (this.href.match(/soundcloud\.com/)) myPath.replace("james-weston","ext/soundcloud");
        if (this.href.match(/simpleproductions\.co\.uk/)) myPath.replace("download","ext/simple");
        if (myPath.substring(0,1)=="/") slash="";
        try { pageTracker._trackPageview(slash+myPath); } catch(err) {};
      }
    }
    if (links[i].className.match("object")) {
      links[i].innerHTML = 'click here to listen in a popout player';
      links[i].onclick = function() {
        remoteWin = window.open("","JWobject","resizable=1,width=300,height=300,status=1,scrollbars=0");
        remoteWin.document.open('text/html', 'replace');
        remoteWin.document.write('<html style="overflow-y:auto;"><head><base target="_blank"><title>Player: '+$("h2").html()+'</title><link rel="stylesheet" type="text/css" href="'+document.styleSheets[0].href+'"><!--[if IE 7]><style type="text/css">body{overflow-y:auto;}</style><![endif]--></head><body><object width="300" height="300"><param name="movie" value="http://player.soundcloud.com/player.swf?url='+escape(this.href)+'+&amp;auto_play=false&amp;player_type=artwork&amp;color=779966"></param><param name="allowscriptaccess" value="always"></param><embed allowscriptaccess="always" width="300" height="300" src="http://player.soundcloud.com/player.swf?url='+escape(this.href)+'&amp;auto_play=false&amp;player_type=artwork&amp;color=779966" type="application/x-shockwave-flash"></embed></object></body></html>');
        remoteWin.document.close();
        return false;
      }
    }
  }
}
  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";

}); // function window.onload


/* @@@@@ popup window to Image size and wrap in HTML @@@@@

function popupImg(img) {
  remoteWin = window.open("","photo","resizable=1,width=200,height=200,status=1,scrollbars=1");
  with (remoteWin.document) {
    open('text/html', 'replace');
    write('<html style="overflow-y:auto;"><head><meta http-equiv="imagetoolbar" content="no"><title>'+siteName+'</title><link rel="stylesheet" type="text/css" href="http://www.'+mainDomain+'/css/default-1.css"><!--[if IE 7]><style type="text/css">body{overflow-y:auto;}</style><![endif]--></head><body style="margin:15px 0;font-size:11px;" onload="var iW=document.popupImg.width+40, iH=document.popupImg.height+120; if (iW > screen.width) iW=screen.width-100; if (iH > screen.height) iH=screen.height-100; resizeTo(iW,iH); window.focus();"><div align="center"><img border="0" src="' + img + '" name="popupImg"><br><br>[<a href="javascript:window.close()">close window</a>]</div></body></html>');
    close();
  }
} */


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()

// Other functions =================================

function changeMenu(init) {
  setting = (!setting || setting=="normal") ? "abstract" : "normal";
  $('#navImg').attr('src',$('#navImg').attr('src').substring(0,$('#navImg').attr('src').lastIndexOf('_')+1)+setting+'.gif');
  xCookie.set("menuSetting", setting, daysFromNow(365));
}

function popupPhotos(doc) {
  remoteWin=window.open(doc,"photos","scrollbars=1,width=700,height=500,resizable=1");
  remoteWin.focus();
}

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);
    }
  }
}


