/*
  Author: Moises Beltran
  Usage: See any example.
*/
//holds instance of IMAGE_ROLLER
function image_popup(image){
  OpenWindow=window.open("", "Image", "resizable=1,height=300, width=300,toolbar=no,scrollbars="+scroll+",menubar=no");
  OpenWindow.document.write("<html><head><title>Large Image</title>")
  OpenWindow.document.write("<script language='javascript'>\n\
			var temp=self.location.href.split(\"?\");\n\
			var picUrl = (temp.length>1)?temp[1]:\"\";\n\
			var NS = (navigator.appName==\"Netscape\")?true:false;\n\
			function scaletofit() {\n\
				iWidth = (NS)?window.innerWidth:document.body.clientWidth;\n\
				iHeight = (NS)?window.innerHeight:document.body.clientHeight;\n\
				iWidth = document.images[0].width - iWidth;\n\
				iHeight = document.images[0].height - iHeight + 29;\n\
				window.resizeBy(iWidth, iHeight-1);\n\
				self.focus();\n\
			};\n\
		</script>\n ")
  OpenWindow.document.write("<head><body onLoad='scaletofit()'>")
  OpenWindow.document.write("<img src='"+image+"' name=imgobj>")
  OpenWindow.document.write("</body></html>")
  OpenWindow.document.close()
}

var IMAGE_ROLLER = false 

function IMAGE_SLIDESHOW()
{
  this.shift_array = function(pos,dir)
  {
    var start = (dir > 0 ? 0 : 1);
    var stop = (dir > 0 ? pos.length-1 : pos.length)
    var pos_new = new Array()
    if(dir > 0) pos_new.push(pos[pos.length-1]);
    for(var i=start;i<stop;i++) pos_new.push(pos[i])
    if(dir < 0) pos_new.push(pos[0])
    return pos_new
  }

  this.pre_load_images = function(image_array)
  {
    all_images = new Array()
    for(var i =0;i<image_array.length;++i)
    {
      img = new Image();
      img.src = image_array[i]
      all_images.push(img)
    }
    return all_images
  }
  
  this.poe = false;
  //OH! Prototype. Exclude all methods added by Array.Proto
  this.PrototypeObjectExtensions = function(){
    poe = ''
    for (var i in Array.prototype) poe += i+'|';
    this.poe = new RegExp('('+poe+'null|_)')
  }
  this.parse_opts = function(orig_opts){
    if(this.poe == false) this.PrototypeObjectExtensions();
    opts = '';
    for (var i in orig_opts) {
      if(i.match(this.poe) == null) opts += i+'="'+orig_opts[i]+'"';
    }
    return opts;
  }
  this.li = function(content,li_opts){
    return '<li '+this.parse_opts(li_opts)+'>'+content+'</li>';
  }
  
  this.a = function(content,a_opts){
    return '<a '+this.parse_opts(a_opts)+'>'+content+'</a>';
  }

  this.image = function(img_opts) {
    img_opts['border'] = 0;
    if(img_opts['size']) {
      size = img_opts['size'].split("x");
      img_opts['width'] = size[0];
      img_opts['height'] = size[1];
      img_opts['size'] = null;
    }
    return '<img '+this.parse_opts(img_opts)+'>';
  }
  
  this.li_a = function(content,a_opts,li_opts) {
    return this.li(this.a(content,a_opts),li_opts);
  }
  
  this.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];
    return this.li_a(this.image(img_opts),a_opts,li_opts);
  }

  this.li_a_image = function(image_opts,a_opts,li_opts) {
    return this.li_a(this.image(image_opts),a_opts,li_opts);
  }

  this.change_image = function(id)
  { 
    this.current_index = id;
    $(this.disp_id).attr("src",this.regular_images[id].src);
  }
  
  this.large_image = function(){
    var large_popup = this.regular_images_popup[this.current_index];
    image_popup(large_popup);
  }
  //image changing stuff
  this.shift_images = function(indx){
    var items = new Array();
    var pos = new Array(); //hold positions
    var sum_tot = 0;

    var image_footer_images = $(this.thumnail_id).find('li')

    for(var i=0;i<image_footer_images.length;++i)
    {
      if(image_footer_images[i].className != '') continue;
      items.push(image_footer_images[i]);
      if(image_footer_images[i].style.display=='none')pos.push(0);
      else
      {
        pos.push(1);
        sum_tot += 1;
      }
    }

    if(indx == 1){ if(pos[pos.length -1] != 1) pos = this.shift_array(pos,1) }
    else if(indx == 0){ if(pos[0] != 1)  pos = this.shift_array(pos,-1) }  
    if(sum_tot == pos.length) var cold_start = true
    for(var i=0;i<items.length;i++){
      if(cold_start)
      {
        if(i >= this.image_footer_show) items[i].style.display='none';
      }
      else
      {
        if(pos[i] == 0) items[i].style.display='none';
        else
          items[i].style.display='';
      }
    }
  }

  //define names and element IDs
  this.var_name = "IMAGE_ROLLER";
  this.disp_id = '#main-image-slide';
  this.thumnail_id = '#main-image-thumbnails';
  
  
  this.current_index = 0
  //preload images
  this.thumbnail_images = this.pre_load_images(arguments[0]);
  this.regular_images = this.pre_load_images(arguments[1]);
  this.regular_images_popup = arguments[2];

  //set misc config
  this.show_preview = 3;
  this.popup = true
  if(this.regular_images_popup.length < 1) this.popup = false
   
//   this.arrows = Array('arrow_previous.gif','arrow_next.gif')
  this.arrow_classes = Array('previous-thumbnail','next-thumbnail')
  this.arrows = Array('/cporotarytools/images/arrow_previous.gif','/cporotarytools/images/arrow_next.gif')
  //configure expected image sizes
  this.arrow_wxh = "11x24";
  this.fingernail_wxh = "38x37";
    
  //set image src
  this.change_image(0);

  pit = $(this.thumnail_id)
  lis = ""
  for(var i=0;i<this.thumbnail_images.length;i++){
    img_opts = Array(), a_opts = Array(), li_opts = Array();
    
    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.li_a_image(img_opts,a_opts,li_opts);
  }
  thumbnails = (arguments[0].length <= this.show_preview ? lis : this.arrow(0)+lis+this.arrow(1))
  pit.append(thumbnails)
}

function load_image_slideshow(fingers,medium,large)
{
  IMAGE_ROLLER = new IMAGE_SLIDESHOW(fingers,medium,large)
}
