// Full Photo Viewer - Library for viewing full size photos on the article.

function FullPhotoViewerObject(){
  
  // playing state of slideshow
  this.state = 'paused';
  
  // Number of seconds delay for slideshow
  this.delay = 5;
  
  this.js_interval_id = 0;
  
  /* This function will start the viewer and load the current photo */
  this.start = function(thumbId,cLink){
    this.switchSelects('hidden');
    this.p = $(thumbId).getElementsByTagName('img');
    this.c = 0;
    if(cLink != undefined){
      cImg = cLink.getElementsByTagName('img');
      cImg = cImg[0];
      //this.thumb = cImg;
    }
    for(i=0;i<this.p.length;i++){
     // if (/current/i.test(this.p[i].className)){this.c=i;break;}
     if (cImg == this.p[i]){this.c=i;}
    }
    
    // show layers
    m = $('fpv_m');
    if(m == undefined){m = this.genHTML();}
    
    this.set(); // set values
    
    var pageSize = getPageSize();
    var pageScroll = getPageScroll();
    $('fpv_o').style.height = pageSize[1] +"px";
    Element.show('fpv_o');
    // set location of fpv_m
    m.style.top = (pageScroll[1] + (pageSize[3] / 15))+'px';
    m.style.left = (pageScroll[0] + ((pageSize[2]-820)/2))+'px';
    Element.show(m);
  }

  this.next = function(){
    this.c = (this.c < (this.p.length-1)) ? this.c+1 : 0;
    this.set();
  }
  
  this.prev = function(){
    this.c = (this.c == 0) ? this.p.length-1 : this.c-1;
    this.set();
  }

  this.play = function(delay){
    if(delay != undefined){this.delay = delay;}
    if (this.state == 'playing'){
      this.stop_slideshow();
    }else{
      this.start_slideshow();
    }
  }

  this.start_slideshow = function(){
    this.next();
    this.state = 'playing';
    this.js_interval_id = setInterval('fpv.next()',(this.delay*1000));
     $('fpv_play_pause').innerHTML = 'Pause Slideshow';
  }

  this.stop_slideshow = function(){
    this.state = 'paused';
    clearInterval(this.js_interval_id);
    $('fpv_play_pause').innerHTML = 'Play Slideshow';
  }
  
  this.quit = function(){
    this.stop_slideshow(); // if playing stop
    Element.hide('fpv_m');
    Element.hide('fpv_o');
    this.switchSelects('visible');
  }
  
  this.set = function(){ // fill in data and photo
    $('fpv_t').innerHTML = this.p.length;
    $('fpv_c').innerHTML = this.c+1;
    $('fpv_i').src = this.src();
    cap = (this.p[this.c].title.length) ? this.p[this.c].title : '';
    $('fpv_caption').innerHTML = cap;
  }
  
  this.src = function(){
    return this.p[this.c].src.replace('_140.jpg','_800.jpg');
  }
  
  this.div = function(id,content,hide){
    out = '<div id="'+id+'"';
    if(hide != undefined){out += ' style="display:none;"';}
    out += '>';
    if(content != undefined){out += content;}
    return out+'</div>';
  }

  this.genHTML = function(){
    b = document.getElementsByTagName('body').item(0);
    new Insertion.Bottom(b, this.div('fpv_o','',true));
    m_c = '<table><tr>';
    m_c += '<td id="fpv_status"><span id="fpv_c">1</span> of <span id="fpv_t">1</span></td>';
    m_c += '<td id="fpv_ctl_l"><a href="JavaScript:fpv.prev();">&#0171; Previous</a></td>';
    m_c += '<td id="fpv_ctl_c"><a href="JavaScript:fpv.play();" id="fpv_play_pause">Play Slideshow</a></td>';
    m_c += '<td id="fpv_ctl_r"><a href="JavaScript:fpv.next();">Next &#0187;</a></td>';
    m_c += '<td id="fpv_close"><a href="JavaScript:fpv.quit();">Close</a></td>';
    m_c += '</tr></table>';
    m_c += '<img id="fpv_i" src="'+this.src()+'" width="800" height="600" onclick="fpv.quit();" />';
    m_c += '<div id="fpv_caption"></div>';
    new Insertion.Bottom(b, this.div('fpv_m',m_c,true));
    o = $('fpv_o');
    o.onclick = function(){fpv.quit();return false;}
    return $('fpv_m');
  }
  
  this.switchSelects = function(visible_hidden){
    // also hiding flash hdr object
    $('mPhoto').style.visibility = visible_hidden;
  	sels = document.getElementsByTagName("select");
  	for (i=0;i<sels.length;i++){sels[i].style.visibility = visible_hidden;}
  }
  
  
}
var fpv = new FullPhotoViewerObject();

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){
	var yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}
	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}
