
if(!document.getElementById && document.all)
document.getElementById = function(id) {return document.all[id];}

var popWinUrlArray=[['/tribe/tribes/nyangatom/locho.htm',636,600]] // very ugly way of implementing, but I can't work out a away of passing the var from flash

function popWinFromFlash(popIndex) {
	bbcjs.win.open(popWinUrlArray[popIndex][0], popWinUrlArray[popIndex][1], popWinUrlArray[popIndex][2]); return false;	
}

function toggleView(linkObj, dispID) {
  // toggles the hidden
  dispObj = document.getElementById(dispID);
  toggleClass(dispObj,'hidden');
  linkObj.innerHTML = (hasClass(dispObj,'hidden')) ? 'View &#187;' : 'Close &#171;';
}

function togglePostcard(postcardID){
	//toggles the display of main content (using toggleView) and also toggles description.
	linkObj = document.getElementById('view_postcard'+postcardID);
	toggleView(linkObj, 'postcard'+postcardID);
	//new get the decription element and toggle that.  should be the reverse of the main content.
	dispObj = document.getElementById('postcard_desc'+postcardID);
	toggleClass(dispObj,'hidden');
}

function toggleLocation(locationID){
	//toggles the display of main content (using toggleView) and also toggles description.
	linkObj = document.getElementById('view_on_location'+locationID);
	toggleView(linkObj, 'on_location'+locationID);
	//new get the decription element and toggle that.  should be the reverse of the main content.
	dispObj = document.getElementById('on_location_desc'+locationID);
	toggleClass(dispObj,'hidden');
}

function toggleReport(reportID){
	//toggles the display of main content (using toggleView) and also toggles description.
	linkObj = document.getElementById('view_report'+reportID);
	toggleView(linkObj, 'report'+reportID);
	//new get the decription element and toggle that.  should be the reverse of the main content.
	dispObj = document.getElementById('report_desc'+reportID);
	toggleClass(dispObj,'hidden');
}


function toggleFlash(lnkObj,flashID,noFlashID,flashHtml,noFlashHtml) {
  if (hasClass(document.getElementById(flashID), 'hidden')) {
		setTimeout("removeClass(document.getElementById('" + flashID + "'), 'hidden')",10);
		addClass(document.getElementById(noFlashID), 'hidden');
	  lnkObj.innerHTML = flashHtml;
	}
	else {
		addClass(document.getElementById(flashID), 'hidden');
		setTimeout("removeClass(document.getElementById('" + noFlashID + "'), 'hidden')", 10);
		lnkObj.innerHTML = noFlashHtml;
	}
}

function toggleClass(domObj, className) {
  if (hasClass(domObj, className)) removeClass(domObj, className);
  else addClass(domObj, className);
}

function addClass(obj, className) { // adds the passed className to the passed DOm object
  if (obj && obj.id) obj.className = (obj.className == '') ? className : obj.className + ' ' + className;
}

function removeClass(obj, className) { // replaces the passed classname with an empty string
  if (obj && obj.id) obj.className = obj.className.replace(new RegExp('^'+className+'| '+className),'');
}

function hasClass(obj, className) {
  // function returns true if the class for the passed DOM objct contains the passed class
  return (obj && obj.id) ? new RegExp('^'+className+'| '+className).test(obj.className) : false;
}

// window.onfocus = function() { alert('hi'); }
window.onMove = function() { alert('move'); }

// slideshow functions
function slideshow(objName){

  // slideshow adds html nodes to the dom of the including page
  // has two canvases, driven by two timed events
  // the _current_, which we preload with the image we want to show _next_
  // the _next_ is the canvas that we can see
  // fade is the timer that fades the current canvas into view
  // swap is the timer that you want to see current canvas
  // playing is swapping on a timer
  // stepping one frame
  this.id = '';
  this.className = '';
  this.captionID = '';
  this.name = objName;
  this.isOpera = (navigator.userAgent.indexOf("Opera") != -1);
  this.isOperaOK = ((navigator.userAgent.indexOf("Opera 7.") != -1) || (navigator.userAgent.indexOf("Opera 8.") != -1));

  // the current canvas is the one behind - the one we want to be hidden and preloading the _next image
  this.currentCanvas = 1;
  // the index in img[] of the next slide which we set up when we swap
  this.currentIndex = 0;
  this.nextIndex = 0;
  // opacity of the current slide
  this.opacity = [0,0];
  
  this.fadeInterval = [null,null];
  this.swapInterval = null;
  
  // path to a prepended common web image directory
  this.imgPath = '';
  //   
  this.imgs = [];
  this.alt = [];
  this.links = [];
  this.onclicks = [];
  this.caps = [];
  
  //this.width = '';
  //this.height = '';
  // img will fill max div dimensions that you set in your css
  this.loop = 1;
  this.random = false;
  this.forward = 1;
  this.time = 5000;
  this.debug = false;
  this.oldHtml = '';
  
  this.str_err = '';
  
  this.capObj = function() {
    return document.getElementById(this.captionID);
  }

  /* begin slide rendering methods */
  this.valid = function (){
    //validate and check for errors
    //check for no image array or image tag id errors
    this.imgs.length < 2 ? this.str_err += "- No images array defined or too few images in array.\n" : '';
    this.id == "" ? this.str_err += "* No slide ID tag defined.\n" : '';
    this.className == "" ? this.str_err += "- No slide CSS class name defined.\n" : '';
    
    //set properties for object support
    if (document.getElementById && !document.getElementById(this.id)) // slide ID error
      this.str_err += "- Incorrect ID defined in slide tag or no slide tag defined.\n";
    
    //show error alert if any
    if (this.str_err != ""){
      alert("Slideshow Error:\n" + this.str_err);
      this.stop();
      return false;
    }
    else{
      return true;
    }    
  }

  this.slider = function () {
  
    // creates divs for swapping then applying opacity filters, applies CSS class
    slideElem = document.getElementById(this.id);
    slideElem.innerHTML = '';
    
    if (this.debug){
      nameElem = document.createElement("DIV");
      slideElem.appendChild(nameElem);
      nameElem.id = this.name + 'DebugLabel0';
      nameElem.className = 'debugLabel';
      nameElem.innerHTML = 'Canvas0 : 0 : 0 : 1';
    }
    
    if (this.debug){
      nameElem = document.createElement("DIV");
      slideElem.appendChild(nameElem);
      nameElem.id = this.name + 'DebugLabel1';
      nameElem.className = 'debugLabel';
      nameElem.innerHTML = 'Canvas1 : 0 : 0 : 0';
    }

    // create div, link and img nodes for first canvas
    var canvasElemDiv0 = document.createElement("DIV");
    slideElem.appendChild(canvasElemDiv0);
    canvasElemDiv0.id = this.name + 'Canvas0';
    canvasElemDiv0.className = this.className;
    canvasElemDiv0.style.zIndex = 1; // override css
    
    var canvasElemLink0 = document.createElement("A");
    canvasElemDiv0.appendChild(canvasElemLink0);
    canvasElemLink0.id = this.name + 'Link0';
    
    var canvasElemImg0 = document.createElement("IMG");
    canvasElemLink0.appendChild(canvasElemImg0);
    canvasElemImg0.id = this.name + 'Img0';
    
    // create div, link and img nodes for second canvas
    var canvasElemDiv1 = document.createElement("DIV");
    slideElem.appendChild(canvasElemDiv1);
    canvasElemDiv1.id = this.name + 'Canvas1';
    canvasElemDiv1.className = this.className;
    canvasElemDiv1.style.zIndex = 0; // override css

    var canvasElemLink1 = document.createElement("A");
    canvasElemDiv1.appendChild(canvasElemLink1);
    canvasElemLink1.id = this.name + 'Link1';
    
    var canvasElemImg1 = document.createElement("IMG");
    canvasElemLink1.appendChild(canvasElemImg1);
    canvasElemImg1.id = this.name + 'Img1';
    
    canvasElemImg0.src = this.imgPath + this.imgs[0];
    canvasElemImg1.src = this.imgPath + this.imgs[1];
    //canvasElemImg1.src = '/f/t.gif';  // standrd bbc spacer location

    if (document.images) {
      var cache = new Array();
      for (i=0;i<this.imgs.length;i++) {
        cache[i] = new Image();
        cache[i].src = this.imgPath + this.imgs[i];
      }
    }
    
    // clear the current canvas
    this.setOpacity(this.currentCanvas, 0);
    this.setOpacity(this.currentCanvas==0?1:0, 100);
    this.setNext();
    this.setCanvas();
  }

  this.nonSlider = function () {

    //document.write('<a href="'+this.links[0]+'"><img name="defaultslide" src="'+this.imgs[0]+'" alt="'+this.alt[0]+'" border="0" /></a>');
    slideElem = document.getElementById(this.id);
    var canvasElemLink = document.createElement("A");
    slideElem.appendChild(canvasElemLink);
    canvasElemLink.id = this.name + 'Link';
    if (this.links.length >= 1)
      canvasElemLink.href = this.links[0];
    var canvasElemImg = document.createElement("IMG");
    canvasElemLink.appendChild(canvasElemImg);
    canvasElemImg.id = this.name + 'Img';
    if (this.imgs.length >= 1) {
      canvasElemImg.src = this.imgPath + this.imgs[0];
      canvasElemImg.width = this.width;
      canvasElemImg.height = this.height;
    }
    
  }
  
  this.randomise = function () {
    // run through the array, swap the current with a random element.
    var curr, rnd, tmpEl;
    for (var curr = 0; curr < this.imgs.length; curr++) {
      rnd = Math.floor(Math.random() * this.imgs.length);
      if (this.links.length > 0)    { tmpEl = this.links[curr]; this.links[curr] = this.links[rnd]; this.links[rnd] = tmpEl; }
      if (this.imgs.length > 0)     { tmpEl = this.imgs[curr]; this.imgs[curr] = this.imgs[rnd]; this.imgs[rnd] = tmpEl; }
      if (this.alt.length > 0)      { tmpEl = this.alt[curr]; this.alt[curr] = this.alt[rnd]; this.alt[rnd] = tmpEl; }
      if (this.onclicks.length > 0) { tmpEl = this.onclicks[curr]; this.onclicks[curr] = this.onclicks[rnd]; this.onclicks[rnd] = tmpEl; }
      if (this.caps.length > 0)     { tmpEl = this.caps[curr]; this.caps[curr] = this.caps[rnd]; this.caps[rnd] = tmpEl; }
    }
  }

  this.render = function () {
    // decides whether to render transitioning slideshow or not (first image only)
    if (!this.valid())
      this.nonSlider();
    else {
      if (this.random) { this.randomise(); }
      this.slider();
    }
  }
  /* end slide rendering methods */
  
  /* begin slide canvas methods */
  this.setOpacity = function (whichCanvas,num) {
    // sets opacity of passed canvas to num 0 < 100
    var obj = document.getElementById(this.name + 'Canvas' + whichCanvas);
    if (obj) {
      if (typeof(obj.filters) != 'undefined') {obj.filters.alpha.opacity=num;}
      if (typeof(obj.style.MozOpacity) != 'undefined') {obj.style.MozOpacity=num/100;}
      if (typeof(obj.style.opacity) != 'undefined') {obj.style.opacity=num/100;}
      if (typeof(obj.style.setProperty) != 'undefined') {obj.style.setProperty('-khtml-opacity',num/100,null);}
    }
    if (this.debug) {
      this.msg('setOpacity on canvas ' + whichCanvas + ': ' + num);
      this.label('Canvas' + whichCanvas + ': ' + this.nextIndex + ' : ' + num + ' : ' + obj.style.zIndex + ' : ' + this.fadeInterval[whichCanvas], whichCanvas);
    }
  }

  this.setCanvas = function() {
    // actually set the current canvas' dom with the next images properties as attrs
    // set the attributes on the canvas we are prepping for the next slide
    this.debug?this.msg('setting img[' + this.nextIndex + '] up on canvas' + this.currentCanvas):'';
    linkObj = document.getElementById(this.name+'Link'+this.currentCanvas);
    linkObj.href = ((this.links.length > 0) && linkObj) ? this.link[this.nextIndex] : '#';
    linkObj.onclick = ((this.links.length > 0) && linkObj && (this.onclicks.length > 0)) ? this.onclicks[this.nextIndex] : 'return false;';
    imgObj = document.getElementById(this.name+'Img'+this.currentCanvas);
    imgObj.src = imgObj ? this.imgPath + this.imgs[this.nextIndex] : '';
    imgObj.alt = (imgObj && (this.alt.length > 0)) ? this.alt[this.nextIndex] : '';
    //this.setCaption();  // apply the current caption
    /*
    // loop? cancel interval if we are at the start or end again
    if ((this.loop==0) && ((this.forward&&this.nextIndex==0)||(!this.forward&&this.nextIndex==this.imgs.length-1))) {
      clearInterval(this.swapInterval);
      this.debug?this.msg('kill loop? ' + ((this.loop==0) && ((this.nextIndex==0)||(this.nextIndex==this.imgs.length-1))))
    }
    */
  }

  this.setNext = function (frame) {
    this.currentIndex = this.nextIndex;
    if (typeof(frame) == 'undefined') { // work it out form the direction
      this.nextIndex = (this.forward)
        ? ( (this.nextIndex < this.imgs.length-1) ? this.nextIndex+1 : 0 )
        : ( (this.nextIndex > 0) ? this.nextIndex-1 : this.imgs.length-1 )
      ;
    }
    else
      this.nextIndex = frame;
      
    this.debug?this.msg('current img ' + this.currentIndex + '\n'):'';
  }
  
  this.swap = function () {
    // slideshow worker process. called on a timeout or interval basis. always preps the next slide.
    this.debug?this.msg('swap fired : curr = ' + this.name+'Canvas'+this.currentCanvas):'';
    // the next here us the one that currently is not the canvas
    var next = document.getElementById(this.name+'Canvas'+((this.currentCanvas==1)?0:1));
    // the curr here us the current canvas (ie not shown)
    var curr = document.getElementById(this.name+'Canvas'+this.currentCanvas);
    
    if (this.valid() && next && curr) {
    
      // bring the next canvas to the back
      curr.style.zIndex=1;
      next.style.zIndex=0;
      
      this.debug?this.msg('canvas swapped & fading .. \n'):'';
      // set the fade-in interval
      // this.fadeInterval[this.currentCanvas] = window.setInterval(this.name+'.fade()',50);
      this.fade();
      
    }
  }
  
  this.fade = function () {
    // timed event called on a short interval to fade in the current prepped canvas 
    if (this.opacity[this.currentCanvas]<=90) {
      // if current opacity is not yet full stength, increase opacity of current carnvas
      this.opacity[this.currentCanvas]+=10;
      this.setOpacity(this.currentCanvas,this.opacity[this.currentCanvas]);
      this.setOpacity(this.currentCanvas==0?1:0,100-this.opacity[this.currentCanvas]);
      this.fadeInterval[this.currentCanvas] = window.setTimeout(this.name+'.fade()',50);
    }
    else {
      // the canvas is faded in - stop incrementing
      this.debug?this.msg('fade complete on canvas' + this.currentCanvas):'';

      // swap the canvas for prepping the next slide and calc index of next image
      clearTimeout(this.fadeInterval[0]);
      clearTimeout(this.fadeInterval[1]);
      
      this.currentCanvas = (this.currentCanvas == 0) ? 1 : 0;
      // reset the opacity for next canvas
      this.opacity[this.currentCanvas] = 0;
      this.setOpacity(this.currentCanvas,this.opacity[this.currentCanvas]);

      // sets caption if declared correctly
      if (this.capObj() && this.caps[this.currentIndex])
        this.capObj().innerHTML = this.caps[this.nextIndex];

      // now, prep the next canvas then wait for next timeout to call a swap
      this.setNext();     // now change canvas and calc next index
      this.setCanvas();   // prep the next slide
    }
  }

  /* end slide canvas method */

  /* begin slider control methods */

  this.play = function (num){
    if (typeof(num)=='undefined') num = 0;
    if (!isNaN(parseInt(num))) {
      this.stop();
      // prep the next slide
      if (!this.forward) {
        this.forward = true;
        this.setNext();
        this.setNext();
        this.setCanvas();
      }
      // if given any number > zero, play one frame only
      if (num!=0) {
        this.debug?this.msg('play one frame'):'';
        this.swapInterval = setTimeout(this.name + ".swap()",this.time);
        if (this.capObj()) {
          this.oldHtml = this.capObj().innerHTML;
          this.capObj().innerHTML = 'Please wait while next image loads ...';
          setTimeout(this.name+'.capObj().innerHTML = ' + this.name+'.oldHtml',1000);
        }
      }
      else {
        this.debug?this.msg('play on'):'';
        this.swapInterval = window.setInterval(this.name + ".swap()",this.time);
        if (this.capObj()) {
          this.oldHtml = this.capObj().innerHTML;
          this.capObj().innerHTML = 'Playing ...';
          setTimeout(this.name+'.capObj().innerHTML = ' + this.name+'.oldHtml',1000);
        }
      }
    }
  }

  this.reverse = function (num){
    if (typeof(num)=='undefined') num = 0;
    var oldHtml = '';
    if (!isNaN(parseInt(num))) {
      this.stop();
      // prep the next slide
      if (this.forward) {
        this.forward = false;
        this.setNext();
        this.setNext();
        this.setCanvas();
      }
      if (num!=0) {
        this.debug?this.msg('reverse one frame'):'';
        this.swapInterval = setTimeout(this.name + ".swap()",this.time);
        if (this.capObj()) {
          this.oldHtml = this.capObj().innerHTML;
          this.capObj().innerHTML = 'Please wait while previous image loads ...';
          setTimeout(this.name+'.capObj().innerHTML = ' + this.name+'.oldHtml',1000);
        }
      }
      else {
        this.debug?this.msg('reverse on'):'';
        this.swapInterval = window.setInterval(this.name + ".swap()",this.time);
        if (this.capObj()) {
          this.oldHtml = this.capObj().innerHTML;
          this.capObj().innerHTML = 'Playing in reverse ...';
          setTimeout(this.name+'.capObj().innerHTML = ' + this.name+'.oldHtml',1000);
        }
      }
    }
  }

  this.jump = function (num){
    this.stop();
    this.setNext(num-1);
    this.setCanvas();   // prep the next slide
    this.debug?this.msg('jump'+num):'';
    if (this.forward) { this.play(1); } else { this.reverse(1); }
    
    if (this.capObj() && this.caps[this.nextIndex]) {
      this.oldHtml = this.capObj().innerHTML;
      this.capObj().innerHTML = 'Image ' + num + ' loading ...';
      setTimeout(this.name+'.capObj().innerHTML = ' + this.name+'.oldHtml',1000);
    }

  }

  this.stop = function (showMsg){
    if (typeof(showMsg=='undefined')) showMsg = false;
    //clears interval if it exists
    this.swapInterval ? clearInterval(this.swapInterval) : null;
    this.debug?this.msg('stopped'):'';
    
    if (this.capObj() && showMsg) {
      this.oldHtml = this.capObj().innerHTML;
      this.capObj().innerHTML = 'Stopped';
      setTimeout(this.name+'.capObj().innerHTML = ' + this.name+'.oldHtml',1000);
    }
  }

  /* end slider control methods */
  this.msg = function (msg) {
    //var txtOP = document.getElementById('txtOP'+this.currentCanvas)
    var txtOP = document.getElementById('txtOP0')
    if (txtOP) txtOP.value = msg + '\n' + txtOP.value;
  }

  this.label = function (msg, whichCanvas) {
    //var txtOP = document.getElementById('txtOP'+this.currentCanvas)
    var txtMsg = document.getElementById(this.name + 'DebugLabel' + whichCanvas);
    if (txtMsg) txtMsg.innerHTML = msg;
  }
}

function createSlideShow(name) {
  if (eval('typeof(window.'+name+') == "undefined"')) {
    eval("window."+name+" = new slideshow('"+name+"');");
    return eval(name);
  }
  return false;
}