var pop_showing = 0;
var buy_pop_timeout;

function require_effects() {
  if (typeof Effect == 'undefined')
  {
    warning = "You must include effects.js";
    alert(warning);
  }
}

function ie6() {
	return (
		!!(window.attachEvent && !window.opera) &&
		parseInt(
			navigator.userAgent.substring (navigator.userAgent.indexOf("MSIE")+5)
		)==6
	);
}

function show_cart_pop(obj, id) {
  pop_showing = id;
	var position = Position.cumulativeOffset( $(obj) );
  var cart_pop = $('cart_pop');
  var left = position[0] - 82;
  if ( ie6() ) { left = left + 7; }
  cart_pop.style.left = left + 'px';

  cart_pop.style.top = ( position[1] + 28) + 'px';
  $('cart_pop_inner').update("<div style='padding-top: 32px;'><img src='/images/spinner-666666.gif' /></div>");  
  $('cart_pop').show();
}

function thumb_replace(main_img, new_src, thumbs_count, selected_thumb_index) {
  main_img.src = new_src;
  
  for(i = 0; i < thumbs_count; i++) {
    var thumbName = "thumb" + i;
    $(thumbName).removeClassName('selected');
  }
  
  $("thumb" + selected_thumb_index).addClassName('selected');
}

function hide_pop_after_timeout(id) { 
  require_effects();
  
  if (id == pop_showing) {
    Effect.Fade('cart_pop', {duration: 0.2});
    buy_pop_timeout = null;
  }
}

function quantities_for_select(id, max) {
  var html = "<select id='" + id + "' name='inventory[quantity]'>";
  for(i = 1; i <= max; i++) {
    html += "<option value='" + i + "'>" + i + "</option>\r\n"
  }
  html += "</select>"
  
  return html;
}

function cp_small_mouseover(link_id) {
  $(link_id).addClassName('hover'); 
  
  if ($('buy_now' + link_id) != undefined) { 
    $('buy_now' + link_id).addClassName('hover'); 
  }
}

function cp_small_mouseout(link_id) {
  $(link_id).removeClassName('hover'); 
  if ($('buy_now' + link_id) != undefined) { 
    $('buy_now' + link_id).removeClassName('hover'); 
  }
}

/* fade flashes automatically */
function fade_alert() {
  $A(document.getElementsByClassName('alert')).each(function(o) {
    o.opacity = 100.0
    Effect.Fade(o, {duration: 2.0})
  });
}

var Cookie = {
  set: function(name, value, daysToExpire) {
    var expire = '';
    if (daysToExpire != undefined) {
      var d = new Date();
      d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
      expire = '; expires=' + d.toGMTString();
      path = '; path=/';
    }
    return (document.cookie = escape(name) + '=' + escape(value || '') + expire + path);
  },
  get: function(name) {
    var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
    return (cookie ? unescape(cookie[2]) : null);
  },
  erase: function(name) {
    var cookie = Cookie.get(name) || true;
    Cookie.set(name, '', -1);
    return cookie;
  },
  accept: function() {
    if (typeof navigator.cookieEnabled  == 'boolean') {
      return navigator.cookieEnabled;
    }
    Cookie.set('_test', '1');
    return (Cookie.erase('_test') = '1');
  }
};