var button_count = 6;
var urls;
var iLastTimer;
var iNowPos;
var iNowSP;
var img_buf;
var loaded_flag;

var auto_change_timer_id = -1;
var auto_change_now_index = 1;
var auto_change_now_step = -1;
var mouse_select_index = 0;
var iNowOverMode;

var iLastIndex = 1;
var iNowIndex = 1;

var img1;
var img2;
var bnr1tag;
var bnr2tag;
var bnr1link;
var bnr2link;

var btn;

var num = 0; 
var v = 100;


var iMainImageChangeDel = 0;

var  auto_change_timer_reps = 0;
function on_mouse_move_fn()
{
   auto_change_timer_reps = 1
}

function init_f()
{
  urls = new Array();
  var i;
  for(i = 1; i <= 6; i++) {
    var link = getElement('b'+i+'link');
    if(link) {
      urls[i-1] = link.href;
      button_count = i;
    }
  }
  
  iLastTimer = new Array(button_count+1);
  iNowPos = new Array(button_count+1);
  iNowSP = new Array(button_count+1);
  img_buf = new Array(button_count+1);
  loaded_flag = new Array(button_count+1);
  iNowOverMode = new Array(button_count+1);
  btn = new Array(button_count+1);
  
    bnr1tag = getElement('bnr1');
    bnr2tag = getElement('bnr2');

  bnr1link = getElement('bnr1link');
  bnr2link = getElement('bnr2link');

  img1 = getElement('bnr1img');
  img2 = getElement('bnr2img');
  img1.onmousemove = on_mouse_move_fn;
  img2.onmousemove = on_mouse_move_fn;

    bnr2tag.style.top= bnr1tag.style.top;
    bnr2tag.style.left=bnr1tag.style.left;

  var s;
  for(var i = 1; i < button_count+1; i++) {
    loaded_flag[i] = 0;
    btn[i] = getElement('b'+i);
    iLastTimer[i] = -1;
    iNowPos[i] = 5;
    iNowSP[i] = 1;
    iNowOverMode[i] = 0;
    
    s = getElement('b'+i+'link');
    if(s) {
      s.href =urls[i-1];
    }
  }

  for(var i = 1; i < button_count+1; i++) {
    img_buf[i] = document.createElement("img");
    img_buf[i].id = i;
    img_buf[i].onload = function() {loaded_flag[this.id]=1;};
    img_buf[i].src = base_file_name+i+'.jpg?af'+clear_cash_str;
  }
  img1.src = base_file_name+'1'+'.jpg?af'+clear_cash_str;
  img2.src = base_file_name+'1'+'.jpg?af'+clear_cash_str;

  fn();
  StartAutoChangeTimer();
}

function image_update()
{
  img1.src= img_buf[iLastIndex].src; 
  bnr1link.href = urls[iNowIndex-1];
  img1.useMap = "topSlide" + iNowIndex + "map";

  img2.src= img_buf[iNowIndex].src; 
  bnr2link.href = urls[iNowIndex-1];
  bnr1tag.style.filter='alpha(opacity=100)';  
  bnr1tag.style.mozOpacity = 1.0;
  bnr1tag.style.opacity=1.0; 
}

var iAtciveTimer = -1;
function fn() 
{ 
  v-=25;
    if(v <= 0) { 
        v = 100; 
    iAtciveTimer = -1;
        bnr1tag.style.filter='alpha(opacity=0)';  
        bnr1tag.style.mozOpacity = 0.0;  
        bnr1tag.style.opacity=0.0; 
    img1.src= img_buf[iNowIndex].src; 
    img1.usemap = "topSlide" + iNowIndex + "map";
    } else { 
        bnr1tag.style.filter='alpha(opacity='+v+')';  
        bnr1tag.style.mozOpacity = v/100;  
        bnr1tag.style.opacity=v/100; 
     iAtciveTimer = setTimeout('fn()',100);
    } 
} 


  function getElement(id){
    if(document.getElementById)
      return document.getElementById(id);
    else if(document.all)
      return document.all(id); 
  return null;
  }


function repTimer(this_id)
{
  if(iLastTimer[this_id] != -1) {
    clearTimeout(iLastTimer[this_id]);
    iLastTimer[this_id] = -1;
  }
  var obj = btn[this_id];
  if(obj) {
    if(iNowPos[this_id] < 20){
      iNowPos[this_id]+=iNowSP[this_id];
      iNowSP[this_id]-=1;
      obj.style.marginLeft =iNowPos[this_id] + "px";
      iLastTimer[this_id] = setTimeout("repTimer('"+this_id+"')", 100);
    }

  }
}

function repRevTimer(this_id)
{
  if(iLastTimer[this_id] != -1) {
    clearTimeout(iLastTimer[this_id]);
    iLastTimer[this_id] = -1;
  }
  var obj = btn[this_id];
  if(obj) {
    if(iNowPos[this_id] > 5+1){
      iNowPos[this_id]-=iNowSP[this_id];
      if(iNowPos[this_id] <= 5) {
        iNowPos[this_id] = 5;
      }
      obj.style.marginLeft = iNowPos[this_id] + "px";
      iLastTimer[this_id] = setTimeout("repRevTimer('"+this_id+"')", 100);
    } else {
      iNowOverMode[this_id] = 0;
    }
  }
}

var stat_pre_timer=4000;

function StartAutoChangeTimer()
{
  for(var i = 1; i < button_count+1; i++) {
    if(loaded_flag[i] == 0) {
      auto_change_timer_id = setTimeout("StartAutoChangeTimer()",100);
      return ;
    }
  }
	stat_pre_timer=4000;
  AutoChangeTimer();
}

function AutoChangeTimer()
{
  
  if(auto_change_timer_reps <= 0)
  {
    var flag = 1;
    var i;
    for(i = 1; i < button_count+1; i++) {
      if(auto_change_now_index != i) {
        if(iNowOverMode[i] != 0) {
          flag = 0;
        }
      }
    }
    if(auto_change_now_index == mouse_select_index)
    {
      flag = 0;
    }
    if(flag == 1) {
      if(auto_change_now_step == -1) {
        auto_change_now_step = 0;
        overDo(auto_change_now_index);
      }
      if(auto_change_now_step == 3) {
        outDo(auto_change_now_index);
        auto_change_now_step = -1;
        auto_change_now_index++;
        if(auto_change_now_index >= button_count+1) {
          auto_change_now_index = 1;
        }
        overDo(auto_change_now_index);
      }
      auto_change_now_step++;
    }
  }
  auto_change_timer_id = setTimeout("AutoChangeTimer()",1000+auto_change_timer_reps*2000+stat_pre_timer);
	stat_pre_timer=0;
  auto_change_timer_reps = 0;
}

//var mouse_enter_dely_timer_id = -1;

function outMousecheck()
{
  if(iMainImageChangeDel != 0) {
  	clearTimeout(iMainImageChangeDel);
  	iMainImageChangeDel = 0;
  	//overDo(iLastIndex);
  }
}

function overMouse(this_id) {
  if(auto_change_timer_id != -1) {
    clearTimeout(auto_change_timer_id);
    //overDo(this_id);
  }
//
//  if(mouse_enter_dely_timer_id != -1) {
//    return;
//  }
//  mouse_enter_dely_timer_id = setTimeout("overMouse_Dely("+this_id+")",100);
//
//}
//
//
//function overMouse_Dely(this_id) {
//   mouse_enter_dely_timer_id = -1;

  mouse_select_index = this_id;
  if(auto_change_now_index != this_id) {
    if(auto_change_now_step >= 1 && auto_change_now_step <= 3) {
      outDo(auto_change_now_index);
    }
  }

  if(iNowIndex != this_id) {
    outMouse(iNowIndex);
  }

  auto_change_now_step = 0;

  if(iMainImageChangeDel != 0) {
  	clearTimeout(iMainImageChangeDel);
  }

  iMainImageChangeDel = setTimeout("overDo("+this_id+")",220);
}
function overDo(this_id) {
	iMainImageChangeDel = 0;

  iNowSP[this_id] = 8;
  iNowOverMode[this_id] = 1;
  
  
  iLastIndex = iNowIndex;
  iNowIndex = this_id;

  var  obj = getElement('MainImage');
  if(obj) {
    obj.style.backgroundImage = "url('"+base_file_name+this_id+".jpg?af'+clear_cash_str)";
  }
  repTimer(this_id);

  if(iLastIndex == this_id) {
    return;
  }
  v = 100;

  image_update();
  if(iAtciveTimer == -1) {
    iAtciveTimer =setTimeout('fn()',200);
  }
}
function outMouse(this_id) {
//  if(mouse_enter_dely_timer_id != -1) {
//    clearTimeout(mouse_enter_dely_timer_id);
//    mouse_enter_dely_timer_id = -1;
//  } 
  if(iMainImageChangeDel != 0) {
  	clearTimeout(iMainImageChangeDel);
  }

  outDo(this_id);
  if(mouse_select_index == this_id) {
    mouse_select_index =0;
  }
}
function outDo(this_id) {
  iNowSP[this_id] =8;

  repRevTimer(this_id);
  
}


