var skipChanges = false;
var skipAutoSpin = false;
var websiteURL="http://www.mgmanager.gr/";
(function($){
  var calcFloat = {
    get: function(num){
      var num = num.toString();
      if(num.indexOf('.')==-1) return[0, eval(num)];
      var nn = num.split('.');
      var po = nn[1].length;
      var st = nn.join('');
      var sign = '';
      if(st.charAt(0)=='-'){
        st = st.substr(1);
        sign = '-';
      }
      for(var i=0; i<st.length; ++i) if(st.charAt(0)=='0') st=st.substr(1, st.length);
      st = sign + st;
      return [po, eval(st)];
    },
    getInt: function(num, figure){
      var d = Math.pow(10, figure);
      var n = this.get(num);
      var v1 = eval('num * d');
      var v2 = eval('n[1] * d');
      if(this.get(v1)[1]==v2) return v1;
      return(n[0]==0 ? v1 : eval(v2 + '/Math.pow(10, n[0])'));
    },
    sum: function(v1, v2){
      var n1 = this.get(v1);
      var n2 = this.get(v2);
      var figure = (n1[0] > n2[0] ? n1[0] : n2[0]);
      v1 = this.getInt(v1, figure);
      v2 = this.getInt(v2, figure);
      return eval('v1 + v2')/Math.pow(10, figure);
    }
  };
  $.extend({
    spin: {
      imageBasePath: websiteURL+'shop/imgages/',
      spinBtnImage: 'spin-button.png',
      spinUpImage: 'spin-up.png',
      spinDownImage: 'spin-down.png',
      interval: 1,
      max: null,
      min: null,
      timeInterval: 500,
      timeBlink: 200,
      btnClass: null,
      btnCss: {cursor: 'pointer', padding: 0, margin: 0, verticalAlign: 'middle'},
      txtCss: {marginRight: 0, paddingRight: 0},
      lock: false,
      decimal: null,
      beforeChange: null,
      changed: null,
      buttonUp: null,
      buttonDown: null
    }
  });
  $.fn.extend({
    spin: function(o){
      return this.each(function(){
				o = o || {};
				var opt = {};
				$.each($.spin, function(k,v){
					opt[k] = (typeof o[k]!='undefined' ? o[k] : v);
				});
        
        var txt = $(this);
        
        var spinBtnImage = opt.imageBasePath+opt.spinBtnImage;
        var btnSpin = new Image();
        btnSpin.src = spinBtnImage;
        var spinUpImage = opt.imageBasePath+opt.spinUpImage;
        var btnSpinUp = new Image();
        btnSpinUp.src = spinUpImage;
        var spinDownImage = opt.imageBasePath+opt.spinDownImage;
        var btnSpinDown = new Image();
        btnSpinDown.src = spinDownImage;
        
        var btn = $(document.createElement('img'));
        btn.attr('src', spinBtnImage);
        if(opt.btnClass) btn.addClass(opt.btnClass);
        if(opt.btnCss) btn.css(opt.btnCss);
        if(opt.txtCss) txt.css(opt.txtCss);
        txt.after(btn);
				if(opt.lock){
					txt.focus(function(){txt.blur();});
        }
        
        function spin(vector){
          var val = txt.val();
          var org_val = val;
          if(opt.decimal) val=val.replace(opt.decimal, '.');
          if(!isNaN(val)){
            val = calcFloat.sum(val, vector * opt.interval);
            if(opt.min!==null && val<opt.min) val=opt.min;
            if(opt.max!==null && val>opt.max) val=opt.max;
            if(val != txt.val()){
              if(opt.decimal) val=val.toString().replace('.', opt.decimal);
              var ret = ($.isFunction(opt.beforeChange) ? opt.beforeChange.apply(txt, [val, org_val]) : true);
              if(ret!==false){
              	if(!skipChanges)
                {txt.val(val);skipChanges=false;}
                if($.isFunction(opt.changed)) opt.changed.apply(txt, [val]);
                txt.change();
                src = (vector > 0 ? spinUpImage : spinDownImage);
                btn.attr('src', src);
                if(opt.timeBlink<opt.timeInterval)
                  setTimeout(function(){btn.attr('src', spinBtnImage);}, opt.timeBlink);
              }
            }
          }
          if(vector > 0){
            if($.isFunction(opt.buttonUp)) opt.buttonUp.apply(txt, [val]);
          }else{
            if($.isFunction(opt.buttonDown)) opt.buttonDown.apply(txt, [val]);
          }
        }
        
        btn.mousedown(function(e){
          var pos = e.pageY - btn.offset().top;
          var vector = (btn.height()/2 > pos ? 1 : -1);
          (function(){
          	
            spin(vector);
			
			if(!skipAutoSpin)
            {var tk = setTimeout(arguments.callee, opt.timeInterval);}
            
            $(document).one('mouseup', function(){
              	if(!skipAutoSpin)
            	{clearTimeout(tk); btn.attr('src', spinBtnImage);}
            	else
            	{skipAutoSpin=false;}
            });
			skipAutoSpin=false;
          })();
          return false;
        });
      });
    }
  });
})(jQuery);

$(document).ready(function() {
function save_quantity(destination,args,placer)
{
$.post(destination,args, function(data) {
var d = data.split('|');
$(placer[0]).html(d[0]);
$(placer[1]).html(d[1]);
$(placer[2]).html(d[2]);
$(placer[3]).html(d[3]);
$(placer[4]).html(d[4]);
});//POST

}

$('.quantity').spin({
			buttonUp: function(n,o){
				var data =  $(this).attr('alt').split('|');
				var itemid = data[0];
				var price = data[1];
				var placer = new Array();
				placer[0] = '.total-price';
				placer[1] = '#total-'+itemid;
				placer[2] = '.total-products';
				placer[3] = '.total-price-no-vat';
				placer[4] = '.vat-price';
				save_quantity(websiteURL+'/shop/x_update_quantities.php',{'quantity':n,'mode':'add','price':price,'itemid':itemid},placer);
			},
			buttonDown: function(n,o,v){
				var data =  $(this).attr('alt').split('|');
				var itemid = data[0];
				var price = data[1];
				var placer = new Array();
				placer[0] = '.total-price';
				placer[1] = '#total-'+itemid;
				placer[2] = '.total-products';
				placer[3] = '.total-price-no-vat';
				placer[4] = '.vat-price';
				save_quantity(websiteURL+'/shop/x_update_quantities.php',{'quantity':n,'mode':'remove','price':price,'itemid':itemid},placer);
				
				if (n ==0)
				{
						$(this).parent().parent().remove();
				}
			},
			imageBasePath: websiteURL+'shop/images/',
			min: 0,
			max: 99
		});//END SPIN
$(".empty_cart").click(function(){
location.href = 'http://'+websiteURL+'/shop/x_remove_all.php';					
});//END EMPTY CART

$(".remove-item").click(function(){
  if (confirm('Να διαγραφεί από το καλάθι;')) {
				var itemid = $(this).attr('rel');
				var placer = new Array();
				placer[0] = '.total-price';
				placer[1] = '#total-'+itemid;
				placer[2] = '.total-products';
				placer[3] = '.total-price-no-vat';
				placer[4] = '.vat-price';
				save_quantity(websiteURL+'/shop/x_remove_item.php',{'mode':'remove_item','itemid':itemid},placer);
				$(this).parent().parent().remove();
  }
});//END EMPTY CART
									
});
