function set_opacity( elem, amount )
{
/*@cc_on
    @if (@_jscript_version == 5.5)
      // This is For Internet Explorer version 5.5
      if ( amount < 0.5 )
      {
        if ( elem.style.display != "none" )
          elem.style.display = "none";
      }
      else if ( elem.style.display == "none" )
        elem.style.display = "block";
      return;
    @elif (@_jscript_version == 9.0 )
      //nothing
    @else
      // This is For Internet Explorer versions greater than 5.5 but less than 9
      elem.style.filter = 'alpha(opacity=' + Math.round( amount * 100 ) + ")";
      if ( amount == 0.0 )
        elem.style.display = "none";
      else if ( elem.style.display != "block" )
        elem.style.display = "block";
      return;
    @end
  @*/
  // for all other browsers including ie9 and above
  elem.style.opacity = amount;
  if ( amount == 0.0 )
    elem.style.display = "none";
  else if ( elem.style.display != "block" )
  {
    elem.style.display = "block";
  }
}

function fade_out( elem, time )
{
  var fade_elem = elem;
  var fade_time = time;
  var start_time = (new Date()).getTime();
  var elapsed_time = 0;

  function doit()
  {
    elapsed_time = (new Date()).getTime() - start_time;
    var opacity = 0.0;
    if ( elapsed_time < fade_time )
    {
      opacity = 1.0 - (elapsed_time / fade_time);
      setTimeout( doit, 50 );
    }
    set_opacity( fade_elem, opacity );
  }
  setTimeout( doit, 0 );
}
function fade_in( elem, time )
{
  var fade_elem = elem;
  var fade_time = time;
  var start_time = (new Date()).getTime();
  var elapsed_time = 0;

  function doit()
  {
    elapsed_time = (new Date()).getTime() - start_time;
    var opacity = 1.0;
    if ( elapsed_time < fade_time )
    {
      opacity = elapsed_time / fade_time;
      setTimeout( doit, 50 );
    }
    set_opacity( fade_elem, opacity );
  }
  setTimeout( doit, 0 );
}

var map;
var directionsPanel;
var directions;
var old_background = 0;
/*@cc_on
  @if (@_jscript_version == 5.6)
var main_panel_div;
var top_slider_div;
var contact_div;
var rhs_div;
var price_list_div;
var main_container;
  @end
  @*/

function load() {
/*@cc_on
  @if (@_jscript_version == 5.6)
  main_panel_div = document.getElementById( "main_panel" );
  top_slider_div = document.getElementById( "top_slider" );
  contact_div = document.getElementById( "contact" );
  rhs_div = document.getElementById( "rhs" );
  price_list_div = document.getElementById( "price_list" );
  main_container = document.getElementById( "main_container" );
  ie6adjust();
  @end
  @*/
  document.getElementById( "salon_slide_chooser" ).onclick = function() { launch_top_strip( 0 ); return false; };
  document.getElementById( "clients_slide_chooser" ).onclick = function() { launch_top_strip( 1 ); return false; };
  change_background();
  animate();
  launch_strip( "look4strip", 110, 660, 1100 );
  launch_strip( "look3strip", 110, 550, 1220 );
  setTimeout( "launch_top_strip( 0 );", 500 );
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    var mapcenter=new GLatLng(52.938,-1.256);
    var pincenter=new GLatLng(52.938533,-1.257802);
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(mapcenter, 15);
    map.addOverlay(new GMarker(pincenter));
    map.setMapType(G_NORMAL_MAP);
  }
}

var imagelist = [ 
  "tigi/blond.jpg",
  "tigi/brunette.jpg",
  "tigi/gent.jpg",
  "tigi/curls.jpg",
  "tigi/jack.jpg",
  "tigi/curls3.jpg"
];

function change_background()
{
  var index;
  do
  {
    index = Math.floor( Math.random() * imagelist.length );
  } while ( index == old_background );
  old_background = index;
  document.getElementById( "price_list" ).style.backgroundImage='url("' + imagelist[index] + '")';
}

function clear_background()
{
  document.getElementById( "price_list" ).style.backgroundImage='none';
}

var process_list = [ ];
var elapsed_time = 0;
function sort_processes( a, b )
{
  return a.when - b.when;
} 
function animate()
{
  var start_time = (new Date()).getTime();
  setInterval( function()
  {
    process_list.sort( sort_processes );
    elapsed_time = (new Date()).getTime() - start_time;
    while ( process_list.length > 0 && process_list[0].when <= elapsed_time )
    {
      process_list.shift().fn();
    }
  }, 40 );
}

var top_strips = [
  { id: "salon_slider", repeat: 2042, timer:null, chooser_id:"salon_slide_chooser" },
  { id: "clients_slider", repeat: 2755, timer:null, chooser_id:"clients_slide_chooser" }
];
var cur_top_strip = -1;

function launch_top_strip( strip_num )
{
  if ( cur_top_strip == strip_num )
    return;
  if ( cur_top_strip != -1 )
  {
    fade_out( document.getElementById( top_strips[cur_top_strip].id ), 1000 )
    document.getElementById( top_strips[cur_top_strip].chooser_id ).className="slide_chooser";
    stop_strip( top_strips[cur_top_strip].id );
  }
  cur_top_strip = strip_num;
  fade_in( document.getElementById( top_strips[strip_num].id ), 1000 )
  document.getElementById( top_strips[strip_num].chooser_id ).className="active_slide_chooser";
  launch_strip( top_strips[strip_num].id, 2, top_strips[strip_num].repeat, 40 );
}

function launch_strip( given_div_id, given_offset, given_count, given_speed )
{
  var div = document.getElementById( given_div_id );
  var div_id = given_div_id;
  var margin = 0;
  var offset = given_offset;
  var count = given_count;
  var speed = given_speed;
  var start_time = elapsed_time;
  div.style.marginLeft = '0px';
  var next_time = elapsed_time + speed;
  var runner = function()
  {
    margin = (offset * Math.floor((elapsed_time - start_time) / speed)) % count;
    div.style.marginLeft = (-margin) + 'px';
    next_time += speed;
    process_list.push( { fn: runner, when: next_time, id: div_id } );
  };
  process_list.push( { fn: runner, when: next_time, id: div_id } );
}
function stop_strip( given_div_id )
{
  var newlist = []
  while ( process_list.length > 0 )
  {
    var p = process_list.shift();
    if ( p.id != given_div_id )
    {
      newlist.push( p )
    }
  }
  process_list = newlist;
}
window.onload=load;
window.onunload=GUnload;

function ie6adjust()
{
  var temp_height = document.documentElement.clientHeight;
  var temp_width = document.documentElement.clientWidth;

  rhs_wid = (temp_width - 560);
  if (rhs_wid < 100)
    rhs_wid = 100;
  if (rhs_wid > 750)
    rhs_wid = 750;
  rhs_div.style.width = rhs_wid+ "px";
  top_slider_div.style.width = rhs_wid + "px";
  price_list_div.style.width = rhs_wid + "px";

  if ( temp_height < 400 )
    temp_height = 400;
  main_panel_div.style.height = (temp_height - 60) + "px";
  contact_div.style.top = (temp_height - 55) + "px";;
  rhs_div.style.height = (temp_height - 60) + "px";
  price_list_div.style.height = (temp_height - 305) + "px";
  setTimeout( ie6adjust, 500 );
}
