/*
	Flashbox v1.0.1 - The ultimate Lightbox clone
	by Jason Hendry (flash) & Abhi Beckert (javascript)
	Inspired by slimbox by Christophe Beyls, which was inspired by Lightbox v2 by Lokesh Dhakar.
	
	usage: flashboxInit();
*/
var flashboxGalleryImages;
var flashboxGalleryImagesStr;

function flashboxInit()
{
  var docLinks = document.links;
  flashboxGalleryImages = [];
  flashboxGalleryImagesStr = '[1';
  
	for (var i = 0; i < docLinks.length; i++) {
	  var elem = docLinks[i];
	  
		if (!elem.rel || !elem.rel.match(/^lightbox/i))
		  continue;
		
    imageRec = {
	    'index': flashboxGalleryImages.length,
	    'url': elem.href,
	    'caption': elem.title
	  };
    elem.onclick = function() { return flashboxGalleryLinkOnClick(this); };
    
    // make sure we don't already have this url
    var didFind = false;
    for (var j = 0; j < flashboxGalleryImages.length; j++) {
      if (flashboxGalleryImages[j].url == imageRec.url) didFind = true;
    }
    if (didFind) continue;
		
		flashboxGalleryImages.push(imageRec);		
		flashboxGalleryImagesStr += ',{"index":'+imageRec.index+',"url":"'+imageRec.url+'","caption":"'+ imageRec.caption+'"}';
		
	}
}

function flashboxGalleryLinkOnClick(linkEl)
{
  // find image rec for this link
  var imageRec;
  for (i = 0; i < flashboxGalleryImages.length; i++) {
    if (flashboxGalleryImages[i]['url'] != linkEl.href)
      continue;    
    idx = flashboxGalleryImages[i].index;
    break;
  }
    
  fElem = document.createElement('div');
  fElem.id = 'flashcontent';
  fElem.style.width='100%';
  fElem.style.height='100%';
  fElem.style.top=0;
  fElem.style.left=0;
  fElem.style.position='fixed';
  document.body.appendChild(fElem);  
  
  var so = new SWFObject("scripts/flashbox.swf", "pattern", "100%", "100%", "8", "#000000");
  so.addVariable("current", idx);
  so.addVariable("displayoptions", escape('{"DarkColor":"#000000","DarkAlpha":80,"AnimationSpeed":2}'));
  so.addVariable("images", escape(flashboxGalleryImagesStr)+']');
  
  so.addParam("wmode", "transparent");
  so.write("flashcontent");
  
  if(fElem.innerHTML == '') return true; 
  
  if (flashboxIsIE6()) {
    fElem.style.position='absolute';
    flashboxIE6Scroll();
    window.onscroll = flashboxIE6Scroll;
  }
  
  if (flashboxIsIE()) {
    // TODO: Make nicer
    flashVars = "current="+idx;
    flashVars += "&displayoptions="+escape('{"DarkColor":"#000000","DarkAlpha":60,"AnimationSpeed":2}');
    flashVars += "&images="+escape(flashboxGalleryImagesStr)+']';
    html = fElem.outerHTML.replace('<PARAM NAME="FlashVars" VALUE="">', '<PARAM NAME="FlashVars" VALUE="'+flashVars+'">')
    fElem.outerHTML = html;  // IE6 Activate SWF
  }
  return false;
}

function flashboxIE6Scroll() {
  scrollElem = document.getElementById('flashcontent');
  if(!scrollElem) return;
  scrollElem.style.top = document.documentElement.scrollTop;
  scrollElem.style.left = document.documentElement.scrollLeft;
  document.recalc();  
}

flashboxInit(); // TODO: make this happen onload

function flashboxClose() {
  flashElem = document.getElementById('flashcontent');
  flashElem.parentNode.removeChild(flashElem);
}

function flashboxIsIE() { return (navigator.userAgent.match(/MSIE /)); }
function flashboxIsIE6() { return (navigator.userAgent.match(/MSIE 6\./)); }