var products = new Products();
var promos = new Promos();

function Product(id, quantity) {
  this.id = id;
  this.quantity = quantity;
}

function Products() {
  this.loaded = false;
  this.prodArray = new Array();
  this.length = 0;

  this.add = function(id, quantity) {
    if (this.loaded) {
      if (!quantity) quantity = 1;
      var product = this.find(id);
      if (product)
        product.quantity += quantity;
      else {
        product = new Product(id, quantity);
        this.prodArray[this.length] = product;
        this.length++;
      }
    }
  }

  this.get = function(index) {
    return this.prodArray[index];
  }

  this.find = function(id) {
    for (var i = 0; i < this.length; i++) {
      var product = this.get(i);
      if (product.id == id)
        return product;
    }
    return null;
  }

  this.load = function() {
    var cookie = cookies['products'];
    if (cookie) {
      var items = cookie.split('|');
      for (var j = 0; j < items.length; j++) {
        var item = items[j].split('_');
        if (item) {
          product = new Product(parseInt(item[0]), parseInt(item[1]));
          this.prodArray[this.length] = product;
          this.length++;
        }
      }
    }
    this.loaded = true;
  }

  this.save = function() {
    if (this.loaded) {
      var cookieStr = new String();
      for (var i = 0; i < this.length; i++) {
        var product = this.get(i);
        if (product.quantity) {
          if (cookieStr.length) cookieStr += '|';
          cookieStr += product.id + '_' + product.quantity;
        }
      }
      cookies.set('products', cookieStr);
    }
  }
}

function Promo(id, name, price, url, image) {
  this.id = id;
  this.name = name;
  this.price = price;
  this.url = url;
  this.image = new Image(100, 100);
  this.image.src = image;
}

function Promos() {
  this.promoArray = new Array();
  this.length = 0;
  this.current = 0;
  
  this.add = function(id, name, price, url, image) {
    var promo = new Promo(id, name, price, url, image);
    this.promoArray[this.length] = promo;
    this.length++;
  }

  this.get = function() {
    var product = this.promoArray[this.current];
    this.current++;
    if (this.current == this.length)
      this.current = 0;
    return product;
  }
}

function setPromo() {
  var promo = promos.get();
  if (promo) { // && promo.image.complete) {
    var elem = document.getElementById('promo-name');
    if (elem) setNodeText(elem, promo.name);
    elem = document.getElementById('promo-image');
    if (elem) elem.src = promo.image.src;
    var elem = document.getElementById('promo-link');
    if (elem) elem.href = promo.url;
    elem = document.getElementById('promo-price');
    if (elem) setNodeText(elem, promo.price);
  }
  setTimeout(setPromo, 5000);
}

function init() {
  cookies.load();
  products.load();
  setPromo();

  var re = new RegExp('/actions/([a-z]+)');
  if (re.exec(location.pathname)) {
    var helpLink = document.getElementById('help');
    if (helpLink) helpLink.hash = RegExp.$1;
  }

  var email = document.getElementById('shop-email');
  if (email) email.setAttribute('href', 'mailto:' + getNodeText(email));
}

function addToBasket(id) {
  products.add(id);
  products.save();
  window.location.reload();
  return false;
}

function showImage() {
  var prodPanel = document.getElementById('prod-panel');
  var prodImage = document.getElementById('prod-image-panel');
  if (prodImage) {
    prodImage.style.display = 'block';
    prodPanel.style.display = 'none';
  }
  return false;
}

function hideImage() {
  var prodPanel = document.getElementById('prod-panel');
  var prodImage = document.getElementById('prod-image-panel');
  prodPanel.style.display = 'block';
  prodImage.style.display = 'none';
  return false;
}

function wantInvoiceClik(checked) {
  document.cust_info.firm.disabled = !checked;
  document.cust_info.nip.disabled = !checked;
}

function getTextNode(cell) {
  for (var i = 0; i < cell.childNodes.length; i++) {
    var elem = cell.childNodes[i];
    if (elem.tagName == 'DIV') {
      for (var j = 0; j < elem.childNodes.length; j++) {
        var node = elem.childNodes[j];
        if (node.nodeType == 3)
          return node;
      }
    }
  }
  return null;
}

function formatPrice(value) {
  if (value == 0)
    value = '0.00';
  else {
    value = Math.round(100 * value).toString();
    value = value.substring(0, value.length - 2) + '.' + value.substring(value.length - 2);
  }
  return value + ' z\u0142';
}

function calculate(field) {
  var quantity = parseInt(field.value);
  if (isNaN(quantity)) quantity = 1;
  field.value = quantity;
  var this_cell = field.parentNode;
  var row = this_cell.parentNode;
  var price_cell = row.cells.item(3);
  var price_node = getTextNode(price_cell);
  var price = parseFloat(price_node.nodeValue);
  var value_cell = row.cells.item(4);
  var value_node = getTextNode(value_cell);
  var old_value = parseFloat(value_node.nodeValue);
  var new_value = quantity * price;
  var diff = new_value - old_value;
  value_node.nodeValue = formatPrice(new_value);
  total_row = row.nextSibling;
  while (total_row.className != 'total')
    total_row = total_row.nextSibling;
  var total_cell = total_row.cells.item(total_row.cells.length - 1);
  var total_node = getTextNode(total_cell);
  var total = parseFloat(total_node.nodeValue);
  total_node.nodeValue = formatPrice(total + diff);
  var product = products.get(total_row.rowIndex - row.rowIndex - 1);
  product.quantity = quantity;
  products.save();
}

function changeQuantity(button, value) {
  var this_cell = button.parentNode;
  var row = this_cell.parentNode;
  var quantity_cell = row.cells.item(1);
  var field = quantity_cell.getElementsByTagName('INPUT')[0];
  var quantity = parseInt(field.value);
  if (isNaN(quantity)) quantity = 1;
  quantity += value;
  if (quantity < 0) quantity = 0;
  field.value = quantity;
  calculate(field);
}

function showPanel(panel) {
  if (panel.style.display == 'none') {
    var skinPanel = document.getElementById('skin-panel');
    var bgPanel = document.getElementById('bg-panel');
    var colorPanel = document.getElementById('color-panel');
    skinPanel.style.display = 'none';
    bgPanel.style.display = 'none';
    colorPanel.style.display = 'none';
    panel.style.display = 'block';
  }
}

function showSkinPanel() {
  showPanel(document.getElementById('skin-panel'));
  return false;
}

function showBgPanel() {
  showPanel(document.getElementById('bg-panel'));
  return false;
}

function showColorPanel() {
  showPanel(document.getElementById('color-panel'));
  preloadColorButtons();
  preloadColors();
  return false;
}

function setSkin(name) {
  cookies.set('skin', name, 1);
  return showBgPanel();
}

function setBg(name) {
  cookies.set('background', name, 1);
  return showColorPanel();
}

preloadImages('/images/', 'do_koszyka_on.gif');
