var agent     = navigator.userAgent.toLowerCase();
var is_ie     = ((agent.indexOf("msie") != -1) && (agent.indexOf("opera") == -1));
var is_opera  = (agent.indexOf("opera") != -1);
var is_mac    = (agent.indexOf("mac") != -1);
var is_mac_ie = (is_ie && is_mac);
var is_win_ie = (is_ie && !is_mac);
var is_safari = (agent.indexOf('safari') != -1 && is_mac);
var is_gecko  = (navigator.product == "Gecko");

var mpwin = 0;
var mpcount = 0;
function NRPMediaPlayer(cls, id, http_host)
{
  if (mpwin) mpwin.close();
  var  args = cls + "', '" + id;
  if(is_safari)
  {
    mpcount++;
    mpPopup(cls, id, http_host);
  } else {
    mpPopup(cls, id, http_host);
  }
}

function mpPopup(cls, id, http_host)
{
  var  width=548;
  var  height=304;

  var re = new RegExp('.*ubl\.com.*');
  var  url = http_host;

  if(url.match(re))
  {
    width=400;
    height=415;
  }

  mpwin = window.open(http_host + '/mediaplayer/class/' + cls + '/id/' + id + '/index.php', 'mpwin'+mpcount,
                      'toolbar=no,location=no,directories=no,status=no,menubar=no,titlebar=0,' +
                      'scrollbars=no,resizable=no,width='+width+',height='+height);
}


function get_cart()
{
  var cart = new Array();
  var a = new Array();
  var s = WM_readCookie('cart');

  if (s.length > 0)
  {
    a = s.split(';');

    for (var i = 0; i < a.length; i++)
    {
      var item = a[i].split(':');

      cart[item[0]] = item[1];
    }
  }

  return cart;
}

function set_cart(cart)
{
  var pid, s = '';

  for (pid in cart) if (cart[pid] > 0)
  {
    s += s.length > 0 ? ';' : '';

    s += pid + ':' + cart[pid];
  }

  var f = document.forms['checkout_form'];

  if (f) f.elements['items'].value = s;

  WM_setCookieEasy('cart', s);

  return s;
}

function set_product_quantity(pid, qty)
{
  var cart = get_cart();

  if ((1 * qty) + 1 >= 1)
  {
    cart[pid] = qty;

    if (set_cart(cart))
    {
      view_cart();
    }
    else shop_more();
  }

  return (cart[pid] ? 1 * cart[pid] : 0);
}

function get_product_quantity(pid)
{
  var cart = get_cart();

  return (cart[pid] ? 1 * cart[pid] : 0);
}

function add_product(pid)
{
  var qty = get_product_quantity(pid);

  set_product_quantity(pid, ++qty);
}

function remove_product(pid)
{
  set_product_quantity(pid, 0);
}

function view_cart()
{
  var s = location.search;

  var arg = 'show_cart=true';

  if (s.length < 2) s = '?' + arg;

  if (s.indexOf(arg) < 0) s += '&' + arg;

  location.search = s;
}

function shop_more()
{
  var s = location.search;

  var arg = 'show_cart=true';

  s = s.replace('?' + arg, '');

  s = s.replace('&' + arg, '');

  location.search = s;
}

function checkout()
{
  var f = document.forms['checkout_form'];

  if (f) f.submit();
}
