//global object
var ixf = { 'clock' : null, 'count' : 1 }
imgIdx=1;
direction=1; //1 for forward, -1 for backwards
fadeImg=0; //flag for fade img present

ixf.getRealPosition = function()
{
	this.pos = (arguments[1] == 'x') ? arguments[0].offsetLeft : arguments[0].offsetTop;
	this.tmp = arguments[0].offsetParent;
	while(this.tmp != null)
	{
		this.pos += (arguments[1] == 'x') ? this.tmp.offsetLeft : this.tmp.offsetTop;
		this.tmp = this.tmp.offsetParent;
	}
	
	return this.pos;
};


ixf.crossfade = function() {
	fadeImg=1;
	ixf.count -= (1 / ixf.resolution);
	if(ixf.count < (1 / ixf.resolution))
	{
		clearInterval(ixf.clock);
		ixf.clock = null;
		ixf.count = 1;
		ixf.obj.src = ixf.src;
	}
	switch(ixf.type)
	{
		case 'ie' :
			ixf.obj.filters.alpha.opacity = ixf.count * 100;
			ixf.newimg.filters.alpha.opacity = (1 - ixf.count) * 100;
			break;
			
		case 'khtml' :
			ixf.obj.style.KhtmlOpacity = ixf.count;
			ixf.newimg.style.KhtmlOpacity = (1 - ixf.count);
			break;
			
		case 'moz' : 
			//restrict max opacity to prevent a visual popping effect in firefox
			ixf.obj.style.MozOpacity = (ixf.count == 1 ? 0.9999999 : ixf.count);
			ixf.newimg.style.MozOpacity = (1 - ixf.count);
			break;
			
		default : 
			//restrict max opacity to prevent a visual popping effect in firefox
			ixf.obj.style.opacity = (ixf.count == 1 ? 0.9999999 : ixf.count);
			ixf.newimg.style.opacity = (1 - ixf.count);
	}
	ixf.newimg.style.visibility = 'visible';
	ixf.newimg.style.left = ixf.getRealPosition(ixf.obj, 'x') + 'px';
	ixf.newimg.style.top = ixf.getRealPosition(ixf.obj, 'y') + 'px';
	if(ixf.count == 1)
	{
		ixf.newimg.parentNode.removeChild(ixf.newimg);
		fadeImg=0;
	}
};


function crossfade() {
	//if the timer is not already going
	if(ixf.clock == null)
	{
		if (typeof slideImage!='undefined') {
			if (menObj!='undefined') {
				menObj.className='popOff'
				menObj.innerHTML = ""
			}

			ixf.src = slideImage;
			if(typeof ixf.obj.style.opacity != 'undefined')
			{
				ixf.type = 'w3c';
			}
			else if(typeof ixf.obj.style.MozOpacity != 'undefined')
			{
				ixf.type = 'moz';
			}
			else if(typeof ixf.obj.style.KhtmlOpacity != 'undefined')
			{
				ixf.type = 'khtml';
			}
			else if(typeof ixf.obj.filters == 'object')
			{
				ixf.type = (ixf.obj.filters.length > 0 && typeof ixf.obj.filters.alpha == 'object' && typeof ixf.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
			}
			else
			{
				ixf.type = 'none';
			}
		
			if(ixf.type != 'none')
			{
				ixf.newimg = document.getElementsByTagName('body')[0].appendChild((typeof document.createElementNS != 'undefined') ? document.createElementNS('http://www.w3.org/1999/xhtml', 'img') : document.createElement('img'));
				ixf.newimg.className = 'idupe';
				ixf.newimg.src = ixf.src
				ixf.newimg.style.left = ixf.getRealPosition(ixf.obj, 'x') + 'px';
				ixf.newimg.style.top = ixf.getRealPosition(ixf.obj, 'y') + 'px';
				ixf.length = parseInt(2, 10) * 1000;	// set fade time to 2
				ixf.resolution = parseInt(2, 10) * 20;
				ixf.clock = setInterval('ixf.crossfade()', ixf.length/ixf.resolution);
			}
			else
			{
				//just do the image swap
				ixf.obj.src = ixf.src;
			}
 			if (menObj!='undefined') {
				msg='<a href="'+linkFile+'" class="title" target="mainWindow" onclick="showIframe()">'+linkName+'</a><br>'+info+'<br><a href="'+linkFile+'" class="goto" target="mainWindow" onclick="showIframe()">Go to page</a>';
				msg=msg+'<P>Slide '+slideNo+' - www.oak-barrel.com</P>'
				menObj.innerHTML=msg;
				menObj.className='popOn'
			}
		}
		include_dom(nextjs);
	}
}


function include_dom(script_filename) {		//see http://www.phpied.com/javascript-include/ and http://www.dotvoid.com/view.php?id=13
  slideNo=imgIdx+1;
  imgIdx=imgIdx+direction;
  if (imgIdx>nosSlides-1) imgIdx=0;
  if (imgIdx<0) imgIdx=nosSlides-1;
  nextjs=showName+'-slide'+imgIdx+'.js';
  var head = document.getElementsByTagName('head').item(0);
  var old=document.getElementById('lastIncludeJS');
  if (typeof slideImage!='undefined') delete(slideImage);
  if (old) head.removeChild(old); //remove last one if it exists
  jsObj = document.createElement('script');
  jsObj.setAttribute('language', 'javascript');
  jsObj.setAttribute('type', 'text/javascript');
  jsObj.setAttribute('src', script_filename);
  jsObj.setAttribute('id', 'lastIncludeJS');
  head.appendChild(jsObj);
  return false;
}


function showIframe() {
  clearInterval(slideshow);
  if (ifObj) {
	ifObj.className='itemFrame';
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //if IE
		var ieversion=new Number(RegExp.$1);
		if (ieversion>=6) {
			var isOp=window.opera?1:0;
			if (isOp==0) {
				ifObj.style.height='100%';
				ifObj.style.top='0px';
			}
		}
	}
  }
  if (menObj) {
	menObj.className='popOff';
  }
  if (typeof ctObj!='undefined') {
	ctObj.style.width='450px';
	msg='<font style="font-size: 15px">Slideshow paused:&nbsp;&nbsp;&nbsp;<a href="javascript:resumeSS()" style="font-size: 15px">Resume Slideshow</a> | <a href="javascript:closeIt()" style="font-size: 15px">Close slideshow</a></font>';
	ctObj.innerHTML=msg;
  }
  return(false);
}


function resumeSS() {
  if (ifObj) {
	ifObj.className='popOff';
  }
  if (ifObj) {
	ctObj.style.width='300px';
  }
  startIt();
}


function closeIt() {
    var lastpage="";
    var fend=-1;
    var fbegin=document.cookie.indexOf("; lastpage=");
    if (fbegin==-1)
      {
       fbegin=document.cookie.indexOf("lastpage=");
       if (fbegin==0)
         {
          fend=document.cookie.indexOf(";", fbegin);
          if (fend==-1)
           fend=document.cookie.length;
          lastpage=document.cookie.substring(fbegin + 9, fend);
          lastpage=unescape(lastpage);
         }
      } else {
            fbegin+=2;
            fend=document.cookie.indexOf(";", fbegin);
            if (fend==-1)
             fend=document.cookie.length;
            lastpage=document.cookie.substring(fbegin + 9, fend);
            lastpage=unescape(lastpage);
             }

    if (lastpage=="") self.location="../index.html";
    else self.location="../"+lastpage;
}



function startIt() {
 	if (typeof ctObj!='undefined') {
		msg='<font color="#cccccc">start</font> | ';
		if (direction==1) {
			msg=msg+'<font color="#cccccc">forward</font> | <a href="javascript:reverse()">reverse</a>';
		} else {
			msg=msg+'<a href="javascript:forward()">forward</a> | <font color="#cccccc">reverse</font>';
		}
		msg=msg+' | <a href="javascript:stopIt()">stop</a> | <a href="javascript:begining()">beginning</a> | <a href="javascript:closeIt()">exit</a>';
		ctObj.innerHTML=msg;
	}
	if (document.getElementById('slide')) {
		ixf.obj=document.getElementById('slide')
		slideshow=setInterval(crossfade, 5000);
	}
	running=1;
}


function stopIt() {
	clearInterval(slideshow);
 	if (typeof ctObj!='undefined') {
		msg='<a href="javascript:startIt()">start</a> | <a href="javascript:forward()">forward</a> | ';
		msg=msg+'<a href="javascript:reverse()">reverse</a> | <font color="#cccccc">stop</font> | <a href="javascript:begining()">beginning</a> | <a href="javascript:closeIt()">exit</a>';
		ctObj.innerHTML=msg;
	}
	running=0;
}


function changeDirection() {
	clearInterval(slideshow);
	clearInterval(ixf.clock);
	ixf.clock=null;
	if (fadeImg==1) {
		ixf.newimg.parentNode.removeChild(ixf.newimg);
		fadeImg=0;
		ixf.obj.src = ixf.src;
	}
	imgIdx=imgIdx+2*direction;
	if (imgIdx>nosSlides-1) imgIdx=0;
	if (imgIdx<0) imgIdx=nosSlides-1;
	nextjs=showName+'-slide'+imgIdx+'.js';
	include_dom(nextjs); //insert js include file as an object in header
	startIt();
}


function reverse() {
	direction=-1;
	changeDirection();
}

function forward() {
	direction=1;
	changeDirection();
}

function begining() {
	clearInterval(slideshow);
	clearInterval(ixf.clock);
	ixf.clock=null;
	if (fadeImg==1) {
		ixf.newimg.parentNode.removeChild(ixf.newimg);
		fadeImg=0;
		ixf.obj.src = ixf.src;
	}
	imgIdx=0;
	nextjs=showName+'-slide0.js';
	include_dom(nextjs); //insert js include file as an object in header
	direction=1;
	startIt();
}

function initialise() {
  //initialise variables
	if (document.getElementById('pop')) menObj=document.getElementById('pop');
	if (document.getElementById('controls')) ctObj=document.getElementById('controls');
	if (document.getElementById('itemFrame')) ifObj=document.getElementById('itemFrame');
	if (menObj!='undefined') { // display current javascript info
		msg='<a href="'+linkFile+'" class="title" target="mainWindow" onclick="showIframe()">'+linkName+'</a><br>'+info+'<br><a href="'+linkFile+'" class="goto" target="mainWindow" onclick="showIframe()">Go to page</a>';
		msg=msg+'<P>Slide 1 - www.oak-barrel.com</P>'
		menObj.innerHTML=msg;
		menObj.className='popOn'
	}
	nextjs=showName+'-slide1.js';
	jsCache = new Image;
	jsCache.src = nextjs; //cache the js include file
	include_dom(nextjs); //insert js include file as an object in header
  //end initialise
  startIt();
}
