/*
  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;
  var idx =0;
  for (i = 0; i < flashboxGalleryImages.length; i++) {
    if (flashboxGalleryImages[i]['url'] != linkEl.href)
      continue;    
    idx = flashboxGalleryImages[i].index;
    break;
  }
  
  var fElem = document.createElement('div');
  fElem.id = 'flashcontent-wrapper';
  fElem.style.width='100%';
  fElem.style.height='100%';
  fElem.style.top=0;
  fElem.style.left=0;
  fElem.style.position='fixed';
  
  var ftElem = document.createElement('div');
  var ftElemInner = document.createElement('div');
  ftElem.appendChild(ftElemInner);
  ftElem.id = 'flashcontent-placeholder';
  ftElem.id = 'flashcontent-placeholder-inner';
  fElem.appendChild(ftElem);
  
  document.body.appendChild(fElem);  
    
  var flashvars = {};
  flashvars.current= idx;
  flashvars.displayoptions= escape('{"DarkColor":"#000000","DarkAlpha":80,"AnimationSpeed":2}');
  flashvars.images= escape(flashboxGalleryImagesStr)+']';
  
  var params = {};
  params.wmode = "transparent";
  var attributes = {};
  attributes.id = "flashcontent-placeholder-inner";
  swfobject.embedSWF("/flash/flashbox.swf", "flashcontent-placeholder-inner", "100%", "100%", "8.0.0", false, flashvars, params, attributes);
  
  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-wrapper');
  if(!scrollElem) return;
  scrollElem.style.top = document.documentElement.scrollTop;
  scrollElem.style.left = document.documentElement.scrollLeft;
  document.recalc();  
}

flashboxInit();

function flashboxClose() {
  flashElem = document.getElementById('flashcontent-wrapper');
  flashElem1 = document.getElementById('flashcontent-placeholder');
  flashElem2 = document.getElementById('flashcontent-placeholder-inner');

  flashElem.parentNode.removeChild(flashElem);
  flashElem1.parentNode.removeChild(flashElem1);
  flashElem2.parentNode.removeChild(flashElem2);
}

function flashboxIsIE() { return (navigator.userAgent.match(/MSIE /)); }
function flashboxIsIE6() { return (navigator.userAgent.match(/MSIE 6\./)); }

