/*****************************   anDeRan   ************************************/

var is_ie  = !!document.uniqueID;
var is_ie6 = is_ie && navigator.appVersion.match(/MSIE [56]/) && !navigator.appVersion.match(/MSIE [789]/);

if (is_ie6) {
  try {
    document.execCommand("BackgroundImageCache", false, true);
  } catch(err) {}
}

if (is_ie) {
  var XMLHttpRequest = function () {
    return (new ActiveXObject("Msxml2.XMLHTTP")) || (new ActiveXObject("Microsoft.XMLHTTP")) || false;
  };
}

var request = function () {
  try {
    var link = new XMLHttpRequest();
  } catch (e) {
    alert('Your browser doesn\'t support XMLHttpRequest. Please enable XMLHTTPRequest if disabled or update if your browser is old.');
  }

  var loading = document.getElementById('ajax-loading');
  document.onmousemove = function (e) {
    e = e || event;
    loading.style.display = 'block';
    loading.style.top = (e.clientY + (document.documentElement.scrollTop || window.scrollY || document.body.scrollTop) - 16) + 'px';
    loading.style.left = (e.clientX + (document.documentElement.scrollLeft || window.scrollX || document.body.scrollLeft) - 16) + 'px';
  };

  this.get = function (path, analyze, params, no_loading) {
    if (no_loading) {
      loading.style.display = 'none';
    }
    link.onreadystatechange = function () {
      if (link.readyState == 4 && link.status == 200) {
        loading.style.display = 'none';
        document.onmousemove = null;
        if (analyze) {
          analyze(link, params);
        }       
      }
    };
    link.open('GET', path, true);

    if (is_ie6) {
      link.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT');
    }
    link.send('');
  };
};

var changeTotal = function () {
  var articles = document.getElementById('articles');
  var inputs = articles.getElementsByTagName('INPUT');
  var i = inputs.length;
  var price, sum = 0;
  while (i-- > 0) {
    price = parseFloat(inputs[i].parentNode.parentNode.cells[2].innerHTML.replace(/^\$/, ''));
    sum += price * inputs[i].value;
  }

  var total = document.getElementById('total');
  total.innerHTML = '$' + sum.toFixed(2);
};

var timers = {};

var preloadImgs = function () {
  var i = arguments.length;
  while (i-- > 0) {
    (new Image()).src = '/images/' + arguments[i];
  }
};

var initTabs = function () {
  var tabs = document.getElementById('tabs');
  if (tabs) {
    var elems = tabs.getElementsByTagName('SPAN');
    var blocks = tabs.parentNode.getElementsByTagName('DIV');
    var i = elems.length;
    while (i-- > 0) {
      elems[i].onclick = function () {
        if (this.className.indexOf('tab-on') != -1) {
          return false;
        }
        var j = elems.length;
        while (j-- > 0) {
          if (elems[j] == this) {
            elems[j].className += ' tab-on';
          } else {
            elems[j].className = elems[j].className.replace('tab-on', '');
          }
        }
        var num = this.id.split('-');
        var _id = 'content-' + num[1];
        var j = blocks.length;
        while (j-- > 0) {
          if (blocks[j].className.indexOf('block-cnt') != -1) {
            if (blocks[j].id == _id) {
              blocks[j].className = blocks[j].className.replace(/ hidden/g, '');
            } else {
              blocks[j].className += ' hidden';
            }
          }
        }
      };
    }
  }
};

var showNav = function () {
  clearTimeout(this.timeout);
  var ul = this.getElementsByTagName('UL')[0];
  if (!this.fullHeight) {
    this.fullHeight = ul.offsetHeight - (is_ie6 ? 0 : 21);
    ul.style.display = 'block';
//    if (is_ie6) {
//      ul.style.width = '194px';
//    }
  }
  this.className = 'nav-overed';

  ul.style.overflow = 'hidden';
  ul.style.visibility = 'visible';
  var height = ul.style.height ? parseInt(ul.style.height, 10) : 1;
  ul.style.height = height + 'px';
  var maxheight = this.fullHeight;

  var ul_id = ul.parentNode.id;
  clearInterval(window.timers[ul_id]);
  window.timers[ul_id] = setInterval((function () {
    var height = ul.style.height ? parseInt(ul.style.height, 10) : 1;

    if (height < maxheight) {
      ul.style.height = Math.min(height + 10, maxheight) + "px";
    } else {
      clearInterval(window.timers[ul_id]);
    }
  }), 15);
};

var hideNav = function () {
  var ul = this.getElementsByTagName('UL')[0];
  var ul_id = ul.parentNode.id;
  this.timeout = setTimeout(function() {
    clearInterval(window.timers[ul_id]);
    window.timers[ul_id] = setInterval((function () {
      var height = ul.style.height ? parseInt(ul.style.height, 10) : 1;
      if (height > 1) {
        height -= 10;
        ul.style.height = Math.max(1, height) + "px";
      } else {
        ul.style.visibility = "hidden";
        ul.style.height  = "";
        ul.parentNode.className = '';
        clearInterval(window.timers[ul_id]);
      }
    }), 15);
  }, 50);
};

var initNav = function () {
  var nav = document.getElementById('nav');
  if (nav) {
    var lists = nav.getElementsByTagName('UL');
    var a, span, span2, i = lists.length;
    while (i-- > 0) {
      lists[i].parentNode.onmouseover = showNav;
      lists[i].parentNode.onmouseout = hideNav;
      lists[i].parentNode.id = 'id' + i;
      a = lists[i].parentNode.getElementsByTagName('A')[0];
      span = document.createElement('SPAN');
      span.className = 'border-1';
      lists[i].parentNode.insertBefore(span, a);
      span2 = document.createElement('SPAN');
      span2.className = 'border-2';
      span.appendChild(span2);
      span2.appendChild(a);
    }
  }
};

var initLoading = function () {
  var loading = document.createElement('DIV');
  loading.id = 'ajax-loading';
  loading.style.display = 'none';
  document.body.appendChild(loading);
};

window.onload = function () {
  initLoading();
  preloadImgs('common/nav_corn_left.png', 'common/nav_corn_right.png');
  initTabs();
  initNav();
};

