$(document).ready(function (){
	$('a.gallery').lightBox();
	$("select").selectbox();
	$(".specifications tr:even").css("background-color", "#e6f1fa");
})

function countUp(id)
	{
		var input = $("input.count_" + id);
		var value = input.val();
		if( value < 99 ) input.val(parseInt(value)+1);
	}
	
function countDown(id)
	{
		var input = $("input.count_" + id);
		var value = input.val();
		if( value > 1 ) input.val(parseInt(value)-1);
	}
	
function addToCart(id, path, buy)
{
	var url = path;
	//id = id.split("-")[1];
	var action = (buy == false)? "ADD2BASKET" : "BUY";
	
	var elementId = document.getElementsByName(id)[0].value.split(":")[0];
	//alert(!elementId); return false;
	var id = id.split("-")[1];
	var className = ".count_" + id;
	var quantity = $(className).val();
	//alert(quantity);
	
	
	if( isNaN(elementId) || !elementId )
	{
		showMessage("Вы не выбрали размер");
		return false;
	}
	else
	{
		 var id = elementId;
		 $.get(
			  //url + '?action=' + action + '&id=' + id + '&quantity=' + quantity,
			  document.location.protocol + "//" + document.location.host + '/js/basket_ajax.php' + '?id=' + id + '&quantity=' + quantity,
			  function( msg ){
				//alert( msg );
				showMessage( msg );
				if( buy == false ) document.location.href = document.location;
				else document.location.href = document.location.protocol + "//" + document.location.host + '/personal/cart/';
			  }
			);
	}
	
}

function changePrice(id, value)
{
	var value = value.split(":")[1];
	var id = id.split("-")[1];
	if( !isNaN(value) ) $(".price-" + id).html(number_format(value,{decimals: 0,thousands_sep:" "}));
}


function number_format(_number, _cfg){
  function obj_merge(obj_first, obj_second){
    var obj_return = {};
    for (key in obj_first){
      if (typeof obj_second[key] !== 'undefined') obj_return[key] = obj_second[key];
      else obj_return[key] = obj_first[key];
      }
    return obj_return;
  }
  function thousands_sep(_num, _sep){
    if (_num.length <= 3) return _num;
    var _count = _num.length;
    var _num_parser = '';
    var _count_digits = 0;
    for (var _p = (_count - 1); _p >= 0; _p--){
      var _num_digit = _num.substr(_p, 1);
      if (_count_digits % 3 == 0 && _count_digits != 0 && !isNaN(parseFloat(_num_digit))) _num_parser = _sep + _num_parser;
      _num_parser = _num_digit + _num_parser;
      _count_digits++;
      }
    return _num_parser;
  }
  if (typeof _number !== 'number'){
    _number = parseFloat(_number);
    if (isNaN(_number)) return false;
  }
  var _cfg_default = {before: '', after: '', decimals: 2, dec_point: '.', thousands_sep: ','};
  if (_cfg && typeof _cfg === 'object'){
    _cfg = obj_merge(_cfg_default, _cfg);
  }
  else _cfg = _cfg_default;
  _number = _number.toFixed(_cfg.decimals);
  if(_number.indexOf('.') != -1){
    var _number_arr = _number.split('.');
    var _number = thousands_sep(_number_arr[0], _cfg.thousands_sep) + _cfg.dec_point + _number_arr[1];
  }
  else var _number = thousands_sep(_number, _cfg.thousands_sep);
  return _cfg.before + _number + _cfg.after;
}

function ret2digit_post(elem){
	elem.value = elem.value.replace(/\D+/, "").substr(0,2);
}

function showMessage( msg )
{
	$("<div class='message'>" + msg  + "</div>").appendTo("body");
	$(".message").fadeIn('slow', function(){
		setTimeout(function(){$(".message").fadeOut('slow').hide();}, 1000);
		
	});
}
