var $flashTimer;
var $id = 0;
var $count = 0;
var $lastColor = "magenta";
var globalObject;

function clearBorders( thisNumber)
{
	$name = "";
	for (i = 1; i <= thisNumber; i++)
	{
		$name = 'pic' +i;
		if ( document.getElementById($name) )
		{ 
			$el = document.getElementById($name);
			$el.style.border='0px';
			$el.style.padding='4px';
		}
	}
}

function addBorder( id, thisNumber ) {
	clearBorders( thisNumber );
	$name = 'pic' +id;

	if ( document.getElementById($name) )
	{ 
		$el = document.getElementById($name);
		if ( $lastColor == "yellow" )
		{
			$el.style.border='4px solid magenta';
			$lastColor = "magenta";
		}
		else
		{
			$el.style.border ='4px solid yellow';
			$lastColor = "yellow";
		}
		$el.style.padding = '0px';
	}
}

function flashBorder(){  
	if ( $count > 0 )
	{
		$count = 1;
		$name = 'pic' + $id;
		if ( document.getElementById($name) )
		{ 
			$el = document.getElementById($name);
			if ($lastColor == "yellow") 
			{ 
				$el.style.borderColor = "magenta";
				$lastColor = "magenta";
			}
			else 
			{
				$el.style.borderColor = "yellow";
				$lastColor = "yellow";
			} 
		}
	}
	else
	{
		$count = 1;
		return;
	}
}



function slide(src, text, attr) {
  this.src = src;
  this.attr = attr;
  if (document.images) {
    this.image = new Image();
  }
  this.loaded = false;
  this.text = text;

  //--------------------------------------------------
  this.load = function() {
    if (!document.images) { return; }
    if (!this.loaded) {
      this.image.src = this.src;
      this.loaded = true;
    }
  }
}

//==================================================
// slideshow object
//==================================================
function slideshow( slideshowname ) {
  this.name = slideshowname;
  this.repeat = true;
  this.image;
  this.timeout = 4000;
  this.slides = new Array();
  this.current = 0;
  this.timeoutid = 0;
	$flashtimer = setInterval("flashBorder()",1500);


  this.add_slide = function(slide) {
    var i = this.slides.length;
    slide.load();
    this.slides[i] = slide;
  }

  //--------------------------------------------------
  this.play = function() {
    this.pause();
    globalObject = this;
   this.timeoutid = setTimeout( 'globalObject.loop()', this.timeout);
  }

  //--------------------------------------------------
  this.pause = function() {
    if (this.timeoutid != 0) {
      clearTimeout(this.timeoutid);
      this.timeoutid = 0;
    }
  }

  //--------------------------------------------------
  this.update = function() {
    if (! this.valid_image()) { return; }
    var slide = this.slides[ this.current ];
    var dofilter = false;
    if (this.image &&
        typeof this.image.filters != 'undefined' &&
        typeof this.image.filters[0] != 'undefined') {
      dofilter = true;
    }
    slide.load();

    if (dofilter) {
      if (slide.filter &&
          this.image.style &&
          this.image.style.filter) {
        this.image.style.filter = slide.filter;
      }
      this.image.filters[0].Apply();
    }
    this.image.src = slide.image.src;
    if (dofilter) {
      this.image.filters[0].Play();
    }
	addBorder( this.current + 1, this.slides.length);
	$id = this.current + 1;
	$count = 0;
	flashBorder();
   	if ( document.getElementById("slide_text") )
	{ 
		$el = document.getElementById("slide_text");
		$el.innerHTML = slide.text;
	}
  }

  //--------------------------------------------------
  this.goto_slide = function(n) {
  	this.pause();
    if (n == -1) {
      n = this.slides.length - 1;
    }
  
    if (n < this.slides.length && n >= 0) {
      this.current = n;
    }
    this.update();
  }

  //--------------------------------------------------
  this.next = function() {
    if (this.current < this.slides.length - 1) {
      this.current++;
    } else if (this.repeat) {
      this.current = 0;
    }
    this.update();
  }

  //--------------------------------------------------
  this.save_position = function(cookiename) {
    if (!cookiename) {
      cookiename = this.name + '_slideshow';
    }
    document.cookie = cookiename + '=' + this.current;
  }


  //--------------------------------------------------
  this.restore_position = function(cookiename) {
    //Get cookie code by Shelley Powers
  
    if (!cookiename) {
      cookiename = this.name + '_slideshow';
    }
  
    var search = cookiename + "=";
  
    if (document.cookie.length > 0) {
      offset = document.cookie.indexOf(search);
      // if cookie exists
      if (offset != -1) { 
        offset += search.length;
        // set index of beginning of value
        end = document.cookie.indexOf(";", offset);
        // set index of end of cookie value
        if (end == -1) {end = document.cookie.length};
        this.current = parseInt(unescape(document.cookie.substring(offset, end)));
        }
     }
  }

  //--------------------------------------------------
  this.loop = function() {
    // Make sure the next slide image has finished loading
    if (this.current < this.slides.length - 1) {
      next_slide = this.slides[this.current + 1];
      if (next_slide.image.complete == null || next_slide.image.complete) {
        this.next();
      }
    } else { // we're at the last slide
      this.next();
    }
    // Keep playing the slideshow
    this.play( );
  }


  //--------------------------------------------------
  this.valid_image = function() {
    // Returns 1 if a valid image has been set for the slideshow
  
    if (!this.image)
    {
      return false;
    }
    else {
      return true;
    }
  }
}

