// Core Javascript routines for Siege-engine.com

// Copyright (C) 2007, 2008 by Eric Ludlam
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING.  If not, write to
// the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301, USA.

var animextension = '.jpg';

// From http://www.devx.com/webdev/Article/20947
function MM_preloadImages() 
{ 
  //v3.0
  var d=document; 

  if(d.images)
    { 
      if(!d.MM_p)
	d.MM_p = new Array();
   
      var i,j=d.MM_p.length,a=MM_preloadImages.arguments; 
      for(i=0; i<a.length; i++)
        {
	  if (a[i].indexOf("#")!=0)
	    {
              d.MM_p[j]=new Image; 
              d.MM_p[j++].src=a[i];
	    }
        }
    }
}

function MM_preload_range(prefix, max, min)
{
  // Preload a range of similarly named images.

  if (arguments.length == 2) {
    min = 1;
  }

  var i;
  for(i=min; i<=max; i++) {
    MM_preloadImages(my_imagename(prefix, i));
  }

}

//
// Image dragging routine.  Called from my_DragFunc in each html file.
//
// To do a movie scrollbar, just put the word "thumb" into the name of
// the thumb on the scroller.  (such as "thumb-1" or "thumb-p1".
//
// That thumb must then have a "movie" slot, which is an object containing
// all the info needed to run a move.

function my_core_DragFunc(dd)
{
    if (dd.obj.name.indexOf('thumb') >= 0)
    {
       var thumb = dd.obj;

       var thumbpos = thumb.x-thumb.defx;

       var track = thumb.movie.track;

       var max = thumb.movie.max;

       var frame = (parseInt(((thumbpos+thumb.w/2)/track.div)*max));

       my_img_drift(thumb.movie, frame);
    }
}

function my_movie(track, img, prefix)
{
  var da = arguments;

  var movie = new Object();

  // Input
  movie.track = track;
  movie.img = img;
  movie.imgprefix = prefix;
  movie.max = 0; // placeholder

  // Calculated stuff
  movie.prevx = 0;
  movie.indexarray = new Array();

  return movie;
}

function my_movie_indicies_range(movie, start, end)
{
  var i, idx=0;
  for(i = start; i <= end; i++, idx++) {
    movie.indexarray[idx] = i;
  }
  movie.max = movie.indexarray.length-1;
}

function my_movie_indicies_arbitrary()
{
  var da = arguments;
  movie = da[0];
  
  var i;
  for(i=0; i<(da.length-1); i++) {
    movie.indexarray[i] = da[i+1];
  }
  movie.max = movie.indexarray.length-1;
}

function my_setup_slider(thumb)
{
  var track = thumb.movie.track;

  var img = thumb.movie.img;

  var imgname = img.name;

  var anchor = dd.elements[imgname + "numbersanchor"];

  track.resizeTo(anchor.w, 35);

  thumb.moveTo(track.x, track.y);
  thumb.setZ(track.z+1);
  track.addChild(thumb);
  thumb.defx = track.x; 
  thumb.maxoffr = anchor.w-thumb.w;
  track.div = thumb.maxoffr;
}

//
// These two image routines are designed for animators, where images
// captured out of video frames can be flipped between.
//
function my_imagename(movie, frame)
{
  var prefix = movie.imgprefix;
  var imgidx = 1;

  if ( movie.indexarray ) {
    imgidx = movie.indexarray[frame];
  }
  var frameidx = imgidx.toString(10);

  if (frameidx.length == 1) {
    var zero = "0";
  } else {
    var zero = "";
  }
  return prefix + zero + frameidx + animextension;
}

function my_img_drift(movie, endframe)
{
  // Drift frames between the current frame and ENDFRAME for the named
  // image IMG using the filename prefix PREFIX.

  startframe = movie.prevx;
  img = movie.img;

  if (startframe == endframe) return;

  var dir = 1;

  if (startframe > endframe) {
    dir = -1;
  }

  prefix = movie.imageprefix;

  for( i=startframe; i != endframe; i=i+dir) {
    img.swapImage(my_imagename(movie, i));
  }
  img.swapImage(my_imagename(movie,endframe));

  movie.prevx = endframe;
}

//
// Movie playing timeout functions
//
// Assume movies have a corresponding slider next to them.
//
// THUMB - the slider thumb
// TRACK - the slider track
// IMG - The movie image we are drifting through
// IMGPREFIX - The file name prefix for the image.
// MAX - the maximum frame number.
var my_play_timeout_length = 100;
var my_active_timeout = null;
var my_active_thumb = null;

function my_play_timeout() 
{
  var thumb = my_active_thumb;
  var track = thumb.movie.track;
  var img = thumb.movie.img;
  var imgprefix = thumb.movie.imageprefix;
  var max = thumb.movie.max;

  var space = (track.div+thumb.w)/max;

  thumb.moveTo(track.x+space*thumb.movie.prevx, track.y);
  my_img_drift(thumb.movie, thumb.movie.prevx+1);

  if (thumb.movie.prevx < max) {
    my_active_timeout = my_active_timeout = window.setTimeout(my_play_timeout, my_play_timeout_length, thumb);
  } else {
    my_active_thumb = null;
  }
}

function my_play(thumb)
{
  var track = thumb.movie.track;
  var img = thumb.movie.img;
  var imgprefix = thumb.movie.imageprefix;
  var max = thumb.movie.max;

  thumb.moveTo(track.x, track.y);
  my_img_drift(thumb.movie, 0);

  if (my_active_timeout) {
    window.clearTimeout(my_active_timeout);
  }

  my_active_thumb = thumb;
  my_active_timeout = window.setTimeout(my_play_timeout, my_play_timeout_length);
}



