/*  Image Slides CLASS
 *  (c) 2008 Moises Beltran
/*--------------------------------------------------------------------------*/

var ImageSlides = Base.extend({
  explorer: (BrowserDetect.browser == "Explorer" ? true : false),
  constructor: function(){
  },
  init: function(fingernail,medium,large){
    this.utils = new Utilities();
    this.ic = new TagCreator();
    //preload images
    this.thumbnail_images = this.utils.preload_images(fingernail);
    this.regular_images = this.utils.preload_images(medium);
    this.regular_images_popup = large;
    if(this.regular_images_popup.length < 1) this.popup = false
    /* Start Code */
    this.change_image(0);
    pit = $(this.thumbnail_id)
    
    if(this.thumbnail_images.length == 2) pit.css("margin-left", " 37px");

    lis = ""
    for(var i=0;i<this.thumbnail_images.length;i++){
      img_opts = Array(), a_opts = Array(), li_opts = Array();
      if(this.explorer){li_opts['style'] = "float: left; margin: 0;padding: 0;" };
      img_opts['src'] = this.thumbnail_images[i].src;
      img_opts['size'] = this.fingernail_wxh;
      a_opts['href'] = "#";
      a_opts['onclick'] = this.var_name+".change_image("+i+"); return false;";
      if(i >= this.show_preview) li_opts['style'] = "display: none";
      lis += this.ic.li_a_image(img_opts,a_opts,li_opts);
    }
    thumbnails = (fingernail.length <= this.show_preview ? lis : this.arrow(0)+lis+this.arrow(1))

    pit.append(thumbnails)
  },
  /* Creates the arrows */
  arrow: function(indx){
    img_opts = Array(), a_opts = Array(), li_opts = Array()
    img_opts['src'] = this.arrows[indx];
    img_opts['size'] = this.arrow_wxh;
    a_opts['href'] = "#";
    a_opts['onclick'] = this.var_name+".shift_images("+indx+"); return false;";
    li_opts['class'] = this.arrow_classes[indx];
    if(this.explorer){li_opts['style'] = "float: left; margin: 0;padding: 0;" };
    return this.ic.li_a(this.ic.image(img_opts),a_opts,li_opts);
  },
  change_image: function(id){
    this.current_index = id;
    $(this.disp_id).attr("src",this.regular_images[id].src);
    return false;
  },
  large_image: function(){
    var large_popup = this.regular_images_popup[this.current_index];
    image_popup(large_popup);
  },
  image_cache: function(){
    this.image_items = Array();
    this.image_positions = Array();
    var image_footer_images = $(this.thumbnail_id).find('li')
    //alert(this.thumbnail_id+" :: "+image_footer_images.length)
    for(var i=0;i<image_footer_images.length;++i)
    {
      if(image_footer_images[i].className != '') continue;
      this.image_items.push(image_footer_images[i]);
      if(image_footer_images[i].style.display=='none') this.image_positions.push(0);
      else
      {
        this.image_positions.push(1);
        this.image_total += 1;
      }
    }    
  },
  //image changing stuff
  shift_images: function(indx){
    if(!this.image_positions && !this.image_items){ this.image_cache(); }
    if(indx == 1){ if(this.image_positions[this.image_positions.length -1] != 1) this.image_positions = this.utils.shift_array(this.image_positions,1) }
    else if(indx == 0){ if(this.image_positions[0] != 1)  this.image_positions = this.utils.shift_array(this.image_positions,-1) }
    //alert(this.image_total +" :: "+ this.image_positions.length)
    if(this.image_total == this.image_positions.length) var cold_start = true
    //alert(cold_start)
    for(var i=0;i<this.image_items.length;i++){
      if(cold_start)
      {
        if(i >= this.image_footer_show) this.image_items[i].style.display='none';
      }
      else
      {
        if(this.image_positions[i] == 0) this.image_items[i].style.display='none';
        else
          this.image_items[i].style.display='';
      }
    }
    return false;
  },
  /* Start Config */
  utils: false,
  ic: false,
  image_positions: false,
  image_items: false,
  image_total: 0,
  var_name: "IMAGE_ROLLER",
  disp_id: '#main-image-slide',
  thumbnail_id: '#main-image-thumbnails',
  current_index: 0,
  thumbnail_images: false,
  regular_images: false, 
  regular_images_popup: false,
  show_preview: 3,
  popup: true,
  //arrows: Array('images/arrow_previous.gif','images/arrow_next.gif'),
  arrow_classes: Array('previous-thumbnail','next-thumbnail'),
  arrows: Array('http://www.cporotarytools.com/images/arrow_previous.gif','http://www.cporotarytools.com/images/arrow_next.gif'),
  //configure expected image sizes
  arrow_wxh: "11x24",
  fingernail_wxh: "38x37"
  /**/


});

/* Helper Function */

var IMAGE_ROLLER = false;
function load_image_slideshow(fingers,medium,large)
{
  IMAGE_ROLLER = new ImageSlides();
  IMAGE_ROLLER.init(fingers,medium,large);
}
