var done = 0;
var num2 = 1;
var curOpacity = 100;
var name = new Array();
var price = new Array();
var size = new Array();
var loadedPage = 0;

var check_loaded_ti;
var image_cache = new Image;


// Funcitons to change thumb on mouseover/off
function rollOver(num, current) {
    returnObjById(num).src='images/thumbs/'+num+type+'_hover.jpg';
}

function rollOut(num, current) {
  if (current<=0) {
    returnObjById(num).src='images/thumbs/'+num+type+'.jpg';
  }
}


// Extra bits for Mozilla
if (document.all) {
  isMoz = 0;
} else {
  isMoz = 1;
}

// If this is Mozilla, use our Load function
if (isMoz) {
  Image.prototype.readyState = "0";
  Image.prototype.__load__ = Image.prototype.load;
  Image.prototype.load = _Image_load;
}

//This is called when it starts loading
function _Image_load(strURL) {
    //change the readyState
    this.readyState = 1;

    //call the original load method
    this.__load__(strURL);
}

// This is called when it's finished loading
function _Image_onload() {
    //change the readyState
    this.readyState = 4;
}


function pageLoaded() {  loadedPage = 1;
}

function blank() {}

function changeImage(num, first) {
  if (!first) {  	first = false;
  }

  if (loadedPage==1) {    window.interval = setInterval("", 1);    window.clearInterval(interval);    done=0;    global_num=num;

    if (first==true) {
      returnObjById('main').src = 'images/'+global_num+type+'.jpg';
      returnObjById('caption_text_only').innerHTML = caption[global_num];
    }
    else {    	opacity_func('main', curOpacity, 0, 500);
      opacity_func('caption_text', curOpacity, 0, 500);    }

    // update all thumbs to make this one the selectd one
    for (z=1; z<30; z++) {
      if (z!=global_num) {
        obj=returnObjById("thumb"+z);
        if (obj) {
          obj.innerHTML="<a href='javascript: changeImage("+z+");' onMouseOver='rollOver("+z+", 0);' onMouseOut='rollOut("+z+", 0);'><img src='images/thumbs/"+z+type+".jpg' id='"+z+"'></a>";
        }
      }
      else {
        returnObjById('thumb'+global_num).innerHTML="<a href='javascript: changeImage("+global_num+");' onMouseOver='rollOver("+global_num+", 1);' onMouseOut='rollOut("+global_num+", 1);'><img src='images/thumbs/"+global_num+type+"_hover.jpg' id='"+global_num+"'></a>";
      }
    }

  }
}

function opacity_func(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            window.setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            window.setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = returnObjById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
    curOpacity = opacity;
    if (opacity==0 && done==0) {      image_cache = null;      image_cache = new Image;
      image_cache.src = 'images/'+global_num+type+'.jpg';
      if (isMoz) {
        image_cache.addEventListener("load", _Image_onload, false);
      }
      check_loaded_ti = window.setInterval("check_loaded()",200);
      returnObjById('caption_text_only').innerHTML = caption[global_num];
      done=1;
    }
}

function check_loaded() {
  var cachepic_obj = image_cache;
  if ( (cachepic_obj.readyState != "complete") && (cachepic_obj.readyState != 4) ){
  }
  else {    returnObjById('main').src = 'images/'+global_num+type+'.jpg';    window.clearInterval(check_loaded_ti);
    opacity_func('main', 0, 100, 500);
    opacity_func('caption_text', 0, 100, 500);  }
}
