// Global Javascript file for Lancaster

//version:   1.0
//author:    Paul Boag
//email:     paul.boag@headscape.co.uk
//website:   http://www.headscape.co.uk

// Updated by Kris McKay
// rewritten to use jQuery which runs smoother.. old method was causing flash pre-rolls to reset
// 2009-08-07

// Updated by Kris McKay
// now affects all images within a set div ID and uses the image's align property to set the class
// 2007-03-22

if (typeof(jQuery) != 'object')
{
  // jQuery is not loaded  
  var head= document.getElementsByTagName('head')[0];
  var script= document.createElement('script');
  script.type= 'text/javascript';
  script.src= '/scripts/jquery-1.2.1.min.js';
  head.appendChild(script);
}

function toUpper(word)
{
  var pattern = /(\w)(\w*)/;
  var parts = word.match(pattern);
  var firstLetter = parts[1].toUpperCase();
  var restOfWord = parts[2].toLowerCase();
  word = firstLetter + restOfWord;
  return word;
}

//Function for converting title tags on images into caption
function addCaption(xID)
{
  $('#'+xID+' img').each(
    function(i)
    {
      if($(this).attr('title') == "")	return;
      var imagecaption = $(this).attr('title');
      var classname = ($(this).attr("align")) ? 'img'+toUpper($(this).attr("align")) : 'img'+toUpper('left');
      $(this).wrap('<div style="width: '+$(this).attr('width')+'px" class="'+classname+'"></div>');
      if(imagecaption && imagecaption != "") $(this).after('<p>'+imagecaption+'</p><span class="spareSpan"></span>');
      else $(this).after('<span class="spareSpan"></span>');
      $(this).addClass("imgCaption");
      $(this).attr("vspace","0");
      $(this).attr("hspace","0");
      $(this).attr("border","0");
      $(this).removeAttr("align");
    });
}

// Runs all the listed functions on the loading of the window

window.onload=function(){
	addCaption("vsCaption");
}
