Shadowbox.init();

$(function(){
	$('#price .name').click(function(){$(this).next("div").toggle("slow");});
	$('#shop-search select').combobox();
	$('#faq select, #fzapis select').combobox();
	$('#shop-items .order span').click(function(){
		promptOrder($(this).attr('aid'));
	});
	$('#shop-item .order span').click(function(){
		promptOrder($(this).attr('aid'));
	});
	if ($.cookie('count')!=null) $('#count').text($.cookie('count'));
});

function promptOrder(id){
	var c = 0;
	var count = prompt('Введите количество товара:', '1');
	if (isInteger(count)) {
		$.cookie('tid_' + id, count, {path: '/'});
		if ($.cookie('count')!=null)
			c = parseInt($.cookie('count')) + parseInt(count);
		else
			c = parseInt(count);
		$.cookie('count', c, {path: '/'});
		$('#count').text(c);
	};
};

function trim(str){return str.replace(/(^\s+)|(\s+$)/g, "");};

function randomNumber (m,n)
{
  m = parseInt(m);
  n = parseInt(n);
  return Math.floor( Math.random() * (n - m + 1) ) + m;
}

function isInteger(field)
{
  var val;

  if (field == ''){
      alert('Вы не ввели число.');
      return false;
  };
  val = parseInt(field);
  if (isNaN(val)){
      alert('Вы ввели не числовое значение.');
      return false;
  };
  if (val != field){
      alert('Введите целое число.');
      return false;
  };
  if (val <= 0){
      alert('Введите положительное число');
      return false;
  };
  return true;
}

function getOrder(){
	var c = parseInt($.cookie('count'));
	if(c > 0){
		$('#content').load('/order/');
	}else{
		alert("Извините, но корзина пуста.");
	};
}

function setOrder(){
   $('#content').load('/order/do-send'+getFormValues("shop-order"), {'hi':'hi'}, function(){clearCookie();});
}

function parseCookie() {
   // Разделение cookie.
   var cookieList = document.cookie.split("; ");
   // Массив для каждого cookie в cookieList.
   var s = '';
   for (var i = 0; i < cookieList.length; i++) {
      // Разделение пар имя-значение.
      var name = cookieList[i].split("=");
      // Декодирование и добавление в cookie-массив.
      var tmp = name[0].split('_');
      if (tmp[0]=='tid') s = s + tmp[1] + '*' + name[1] + '|';
   }
   $('#content').load('/order/val-'+s);
}

function getFormValues(fId) {
    var res = new String();
    var s = new String();
    $("#"+fId+" input,#"+fId+" textarea,,#"+fId+" select").each(function(){
            if((this.type!='button')&&(this.type!='file')){
                s = trim(this.value);
                if(s.length>0) res = res + '.' + this.name + '-' + encodeURIComponent(s);
            };
        }
    );
    $("#" + fId + " radio").filter(":checked").each(function(){res = res + ',"' + this.name + '":"' + this.value + '"';});
    return res;
};

function clearCookie() {
   // Разделение cookie.
   var cookieList = document.cookie.split("; ");
   // Массив для каждого cookie в cookieList.
   var s = '';
   for (var i = 0; i < cookieList.length; i++) {
      // Разделение пар имя-значение.
      var name = cookieList[i].split("=");
      // Декодирование и добавление в cookie-массив.
      var tmp = name[0].split('_');
      if (tmp[0]=='tid') $.cookie(name[0], null, {path: '/'});
   }
   $.cookie("count", "0", {path: '/'});
   $('#count').text("0");
}



