/*
  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 = {};
  
  for (var i = 0; i < docLinks.length; i++) {
    var elem = docLinks[i];
    
    if (!elem.rel || !elem.rel.match(/^lightbox/i))
      continue;
    
    gallery = elem.rel.replace(/^lightbox\[(.*)\]/i, '$1');
    
    if(!flashboxGalleryImages[gallery])
      flashboxGalleryImages[gallery] = new Array();
    if(!flashboxGalleryImagesStr[gallery])
      flashboxGalleryImagesStr[gallery] = '[1';
      
    imageRec = {
      'index': flashboxGalleryImages[gallery].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[gallery].length; j++) {
      if (flashboxGalleryImages[gallery][j].url == imageRec.url) didFind = true;
    }
    if (didFind) continue;
    flashboxGalleryImages[gallery].push(imageRec);
    flashboxGalleryImagesStr[gallery] += ',{"index":'+imageRec.index+',"url":"'+imageRec.url+'","caption":"'+ imageRec.caption+'"}';
    
  }
}

function flashboxGalleryLinkOnClick(linkEl)
{
  // find image rec for this link
  var imageRec;
  gallery = linkEl.rel.replace(/^lightbox\[(.*)\]/i, '$1');
  for (i = 0; i < flashboxGalleryImages[gallery].length; i++) {
    if (flashboxGalleryImages[gallery][i]['url'] != linkEl.href)
      continue;    
    idx = flashboxGalleryImages[gallery][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("/wp-content/themes/karumbapoint/scripts/flashbox.swf", "pattern", "100%", "100%", "8", "#FFFFFF");
  so.addVariable("current", idx);
  so.addVariable("displayoptions", escape('{"DarkColor":"#000000","DarkAlpha":80,"AnimationSpeed":2}'));
  so.addVariable("images", escape(flashboxGalleryImagesStr[gallery])+']');
  
  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[gallery])+']';
    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\./)); }
