var IS_IE = (navigator.appName=="Microsoft Internet Explorer");

var sliders = new Array();

function cookie_value(key){ // a fortune
	keypos = document.cookie.indexOf(key);
	result = "";
	if(keypos != -1){
		result = document.cookie.substr(keypos+key.length+1, document.cookie.length);
		endpos = result.indexOf(";");
		if(endpos != -1){
			result = result.substr(0, endpos);
		}
	}
	return (result);
}

function scroll_this(aName){
  sliders[aName].scroll_iframe();
}

function TSlider(AName){
  sliders[AName]=this;// keep a list of sliders
  this.name = AName;
  this.iframe = window.frames[this.name];
  window.frames[this.name] = this;
  this.iframe.slider = this;
  
  this.slide_interval = 0;
  this.slide_increment = 1;
  this.slide_speed = 10; // ms delay per frame change 
   
  this.save_cookies = function(){
    if(this.slide_increment==0){
      this.slide_increment=1;
    }
//    setCookie(this.name+'scroll_pos', scroll_left(this.iframe), 1);
//    setCookie(this.name+'slide_increment', this.slide_increment, 1);
    document.cookie = this.name+'scroll_pos='+scroll_left(this.iframe);
    document.cookie = this.name+'slide_increment='+this.slide_increment;    
  }
  
  this.load_cookies = function(){    
    scroll_pos = cookie_value(this.name+"scroll_pos");
    //alert(scroll_pos);
    this.iframe.scrollTo(scroll_pos, 0);
    this.slide_increment = cookie_value(this.name+"slide_increment");
    if(this.slide_increment==0){
      this.slide_increment=1;
    }
  }

  this.scroll_iframe = function(){
    //top.document.title="(this:"+this+")(this.slider:"+this.slider+")(this==window:"+(this==window)+")(this.name:"+this.name+")(this.slide_increment:"+this.slide_increment+")(this.iframe:"+this.iframe+")(this.test:"+this.test+")";   
   
    this.iframe.scrollBy(this.slide_increment, 0);
    if((scroll_left(this.iframe)==0) || (scroll_left(this.iframe) == (scroll_width(this.iframe) - client_width(this.iframe)))){
      this.slide_increment = this.slide_increment * -1;
    }
    this.save_cookies();
  }  
 
  this.play_slide = function(){
    if(this.slide_interval==0){
      this.slide_interval = setInterval("scroll_this(\""+this.name+"\");", 10);
    }
  }
   
  this.stop_slide = function(){    
    if(this.slide_interval!=0){
      clearInterval(this.slide_interval);
      this.slide_interval=0;
    }
  }
  
  this.restart_slide = function(){
    //alert("restart_slide");
    this.iframe.scrollTo(0,0);
  }
  
  this.pause_slide = function(){
    if(this.slide_interval==0){
      this.play_slide();
    }
    else{
      this.stop_slide();
    }
  }
  
  this.restore_slide = function(){
    // load the position from cookie and play
    this.load_cookies();
    this.play_slide();
  }
  
  this.write_gallery_buttons = function(){
    // control buttons                             
    document.write("<div style='left:0px;' id='"+this.name+"_controls' class='controls' name='controls'>");
    document.write("<img id='"+this.name+"_restart' name='"+this.name+"_restart' alt='goto start' onmouseover='document.images[\""+this.name+"_restart\"].src=\"restart02.jpg\"' onmouseout='document.images[\""+this.name+"_restart\"].src=\"restart01.jpg\"' class='fake_link2' src='restart01.jpg'>");
    document.write("<img id='"+this.name+"_pause' name='"+this.name+"_pause' alt='pause' onmouseover='document.images[\""+this.name+"_pause\"].src=\"pause02.jpg\"' onmouseout='document.images[\""+this.name+"_pause\"].src=\"pause01.jpg\"' class='fake_link2' src='pause01.jpg'>");
    document.write("<img id='"+this.name+"_play' name='"+this.name+"_play' alt='play' onmouseover='document.images[\""+this.name+"_play\"].src=\"play02.jpg\"' onmouseout='document.images[\""+this.name+"_play\"].src=\"play01.jpg\"' class='fake_link2' src='play01.jpg'>");
    document.write("</div>");    
    
    restart = document.getElementById(this.name+"_restart");
    restart.slider = this;
    restart.onclick=function(){
      this.slider.restart_slide();
    }
    
    pause = document.getElementById(this.name+"_pause");
    pause.slider = this;
    pause.onclick=function(){
      this.slider.pause_slide();
    }
    
    play = document.getElementById(this.name+"_play");
    play.slider = this;
    play.onclick=function(){
      this.slider.play_slide();
    }
  }
  
}

function scroll_left(AWindow){
  if((AWindow.document) && (AWindow.document.body) && (AWindow.document.body.scrollLeft)){
    return(AWindow.document.body.scrollLeft);
  }
  else if(document.documentElement.scrollLeft){
    return(document.documentElement.scrollLeft);
  }
  else{
    return(0);
  }
} 

function scroll_width(AWindow){
  if(AWindow.document.body.scrollLeft){
    return(AWindow.document.body.scrollWidth);
  }
  else if(document.documentElement.scrollWidth){
    return(document.documentElement.scrollWidth);
  }
  else{
    return(0);
  }
} 

function client_width(AWindow){
  if(AWindow.document.body.clientWidth){
    return(AWindow.document.body.clientWidth);
  }
  else if(document.documentElement.clientWidth){
    return(document.documentElement.clientWidth);
  }
  else{
    return(0);
  }
} 


function preload(AImage){
  NewImage = new Image();
  NewImage.src = AImage;
}

function preload_controls(){
  preload('play01.jpg');
  preload('play02.jpg');
  preload('pause01.jpg');
  preload('pause02.jpg');
  preload('restart01.jpg');
  preload('restart02.jpg');
}


function do_show_label(ALabel, AShow){
  label_div = document.getElementById(ALabel);
  if(AShow){
    label_div.style.visibility = "visible";
  }
  else{
    label_div.style.visibility = "hidden";
  }
}

function show_label(ALabel){
  this.do_show_label(ALabel, true);
}

function hide_label(ALabel){
  this.do_show_label(ALabel, false);
}

function current_gallery_id(aCurrentImage){
  for(iImage=0; iImage<AGallery.image_url_list.length; iImage++){
    if(AGallery.image_url_list[iImage]==aCurrentImage){
      result=iImage;
      break;
    }
  }
  return(result);
}

function get_gallery_url(a_id){
  AImage = AGallery.image_url_list[a_id];
  AAltText = AGallery.alt_text_list[a_id];
  result = 'showimage.php?image='+AImage+'&alt='+AAltText+'&width='+AGallery.show_width+'&height='+AGallery.show_height+'&borders='+AGallery.show_borders+'&script='+AGallery.gallery_script+'&buttons=true';
  return(result);
}

function prev_gallery_show(aCurrentImage){
  next_id=current_gallery_id(aCurrentImage)-1;

  if(next_id<0){
    next_id=AGallery.image_url_list.length-1;
  }
  
  link = get_gallery_url(next_id);
  
  document.location=link;
}

function next_gallery_show(aCurrentImage){
  next_id=current_gallery_id(aCurrentImage)+1;

  if(next_id>=AGallery.image_url_list.length){
    next_id=0;
  }
  
  link = get_gallery_url(next_id);
  
  document.location=link;
}

function TGallery(aMaxWidth, aMaxHeight, aShowWidth, aShowHeight, aShowBorders, aGalleryScript){

  this.load_count = 0;
  this.num_images = 0;
  this.image_list = new Array(); 
  this.image_url_list = new Array(); 
  this.alt_text_list = new Array(); 
  
  this.gallery_script = aGalleryScript;
  
  this.max_width = aMaxWidth;
  this.max_height = aMaxHeight;

  this.show_width = aShowWidth;
  this.show_height = aShowHeight;
  this.show_borders = aShowBorders;
  
  this.html = "";

  this.inc_load = function(){
    this.load_count++;
    if(this.load_count==this.num_images){
  //    play_slide();
    }
  } 
  
  this.preload_inc = function(AImage){
    NewImage = new Image();
    NewImage.onload=this.inc_load;
    NewImage.src = AImage;
  }
  
  this.gallery_src = function(AImage, ADegrees){
    return('gtphoto.php?image='+AImage+'&width='+this.max_width+'&height='+this.max_height+'&degrees='+ADegrees+'&start=FFFFFF&end=C0C0C0&borderColor=000000');
  }
  
  this.write = function(){
    this.preload_gallery_images();
    document.write(this.gallery_start()+this.html+this.gallery_end());
  }
  
  this.add_image = function(AImage, ADegrees, AAltText){
    result = "";
    img_src = this.gallery_src(AImage, ADegrees);
    
    this.image_list[this.num_images] = img_src;
    
    this.image_url_list[this.num_images] = AImage;
    this.alt_text_list[this.num_images] = AAltText;
    
    this.num_images++;
    
    link = 'showimage.php?image='+AImage+'&alt='+AAltText+'&width='+this.show_width+'&height='+this.show_height+'&borders='+this.show_borders+'&script='+this.gallery_script+'&buttons=true';
    
    result+=('<td>');
    if(AAltText!=""){
      result+=('<div ');
      
      result+=('onmouseover="show_label(\'label'+AImage+'\');" onmouseout="hide_label(\'label'+AImage+'\');" ');
  
      result+=('onclick="top.location=\''+link+'\'" id="label'+AImage+'" class="gallery_label">');
      result+=(AAltText);
      result+=('</div>'); 
    }
    result+=('<a target="_top" href="'+link+'">');
    result+=('<img class="gallery_img"');
    if(AAltText!=""){
      result+=('onmouseover="show_label(\'label'+AImage+'\');" onmouseout="hide_label(\'label'+AImage+'\');" ');
    }
    result+=('alt="'+AAltText+'" src="'+img_src+'">'); 
    result+=('</a>'); 
    result+=('</td>');
    this.html += result;
  }
  
  this.preload_gallery_images = function(){
    for(i=0; i<this.num_images; i++){
      this.preload_inc(this.image_list[i]);
    }
  }
 
  this.gallery_start = function(){
    result=("<table class='spacer'><tr>");
    return(result);
  }
  
  this.gallery_end = function (){
    result=("</tr><table>");
    return(result);
  }

}
