/*
  Copyright (c) 2005 Snapvine, LLC. All rights reserved.
  THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF Snapvine, LLC
  The copyright notice above does not evidence any actual or intended publication of such source code.
*/

var SvTab = Class.create();
SvTab.prototype = {
  // Constructor finds and binds html elements for this tab
  initialize: function(name, idx) {
    this.tab_area_name = name;
    this.tab_id = idx;
    this.selected   = document.getElementById(name + "_t" + idx + "_s");
    this.unselected = document.getElementById(name + "_t" + idx + "_u");
    this.hover      = document.getElementById(name + "_t" + idx + "_h");
    this.content    = document.getElementById(name + "_t" + idx + "_c");
    this.unselected.style.cursor="pointer";
    this.hover.style.cursor="pointer";
    this.current_tab = false;
  },

  // Select
  select: function() {
    this.selected.style.display   = "inline";
    this.unselected.style.display = "none";
    this.hover.style.display      = "none";
    this.content.style.display    = "block";
    this.current_tab = true;
  },

  // Unselect
  unselect: function() {
    this.selected.style.display   = "none";
    this.unselected.style.display = "inline";
    this.hover.style.display      = "none";
    this.content.style.display    = "none";
    this.current_tab = false;
  },

  // hover-over only applies if the tab is not yet selected
  start_hover: function() {
    if (!this.current_tab) {
      this.unselected.style.display = "none";
      this.hover.style.display      = "inline";
    }
  },
  end_hover: function() {
    if (!this.current_tab) {
      this.unselected.style.display = "inline";
      this.hover.style.display      = "none";
    }
  }
};

var g_tab_state = new Array(); // stores input elements with name "tab_state"
var g_tab_areas = new Array(); // stores div elements with name "tab_area"
var SvTabArea = Class.create();
SvTabArea.prototype = {

  // Constructor keeps track of instances in g_tab_areas
  initialize: function(name) {

    // find all the tabs and create tab objects
    this.name = name;
    this.tabs = new Array();
    a = document.getElementsByName(this.name + "_tab");
    for (i=0;i<a.length;i=i+1) {
      this.tabs[i] = new SvTab(name, i);
    }

    // handle URL params
    this.selection = this.tabs[0];
    this.selection.current_tab=true;
    idx = get_param(name);
    if (idx) {
      this.select(idx);
    } else {
      this.select(0);
    }
  },

  // select a new tab
  select: function(idx) {
    for (i=0;i<this.tabs.length;i=i+1) {
      if (i==idx) {
        this.tabs[i].select();
        this.selection = this.tabs[i];
      } else {
        this.tabs[i].unselect();
      }
    }
  },

  // start display hover over
  start_hover: function(idx) {
    for (i=0;i<this.tabs.length;i=i+1) {
      if (i==idx) {
        this.tabs[i].start_hover();
      } else {
        this.tabs[i].end_hover();
      }
    }
  },

  // end display hover over
  end_hover: function(idx) {
    this.tabs[idx].end_hover();
  },

  // return URL params used to preserve tab state
  param_string: function() {
    return (this.name + "=" + this.selection.tab_id);
  }
};

// Returns a single string that contains the current
// selection of individual tabs on the page. This string
// can be passed as query string parameters.
function page_wide_tab_state() {
  param="";
  for (i=0;i<g_tab_areas.length;i=i+1) {
    if (i>0) { param = param + "&"; }
    param = param + tab_area(i).param_string();
  }
  return param;
}

// Set page_wide_tab_state on all hidden input tags, so that
// when a form is submitted, the page-wide tab state gets
// submitted also.
function set_tab_state_param() {
  s = page_wide_tab_state();
  for (i=0;i<g_tab_state.length;i=i+1) {
    g_tab_state[i].value = s;
  }
  return true
}

// Create and attach a hidden tag that will contain page-wide
// tab state.
function setup_tab_state_param(elt, i){
  tag = document.createElement("input");
  tag.type = "hidden";
  tag.name = "tab_state";
  elt.parentNode.appendChild(tag);
  elt.onclick = set_tab_state_param;
  g_tab_state.push(tag);
  g_tab_state[i] = tag;
}

// Initialize page
$(function(){
  var a = document.getElementsByName("tab_area")
  for (i=0;i<a.length;i=i+1) {
    name = a[i].id;
    g_tab_areas[name] = new SvTabArea(name);
    g_tab_areas.push(g_tab_areas[name]);
  }
  var a = document.getElementsByName("send_tab_state")
  for (i=0;i<a.length;i=i+1) {
    setup_tab_state_param(a[i], i);
  }
});

// This function returns a tab_area object that can handle
function tab_area(name) {
  return g_tab_areas[name];
}

