function cartUnitDDInit(){
if ($.browser.msie && $.browser.version < 7) { return false; }
$('li', '#cartpreid').draggable({ opacity: 0.75, helper:'clone', 'revert':true, appendTo: 'body' });
$('body').droppable({
	accept: 'li',
	drop: function(event, ui) { 
		if(confirm('Удалить товар из корзины заказа?')){
			var liid = $(ui.draggable).attr('id');
			var id = liid.replace(/cartpre/, '');
			$.post('/cgi-bin/oda2/user/cart.pl',{'action':'DELETE','ID':id},function(data){
				if (data=='true'){
					
					$(ui.draggable).fadeOut("slow", function(){
						$(ui.draggable).remove();
//alert($('#cartpreid').length);
//alert($('#cartpreid li').length);
						if(!$('li', '#cartpreid').length){
							$('.rcart, .rcartsub').slideUp("slow",function(){$(this).remove();});
						}else{
							$('#cartcostid').load('/ajax/cart-cost.html');
						}
					});
				}else{alert(data)};
			});			
		}
	}
});
//$('#cartpreid').carPreRe();
}

(function($) {
	$.fn.carPreRe = function(settings) {
		settings = jQuery.extend({
			dtime:100,
			dxarrow:35,
			paddingla:10,
			paddingra:10,
			leftarrowid:$('#carrl'),
			rightarrowid:$('#carrr'),
			arrowld:'/i/carrld.png',
			arrowla:'/i/carrla.png',
			arrowrd:'/i/carrrd.png',
			arrowra:'/i/carrra.png'
			
		},settings);
		
		var cunum = $('li',this).length;
		var jQueryMatchedObj = this;

		function _initialize() {
			_arrowInit();
			_mousewheelInit();
		
		}

		this.stop = function(){
			alert('stop');
		}

		function _mousewheelInit(){
			$(jQueryMatchedObj).mousewheel(function(objEvent, intDelta){
			    if (intDelta > 0){
					_arrowTopWheel(intDelta);
				} else if (intDelta < 0){
					_arrowBottomWheel(intDelta);
				}
				objEvent.stopPropagation();
	 		    objEvent.preventDefault();
				//_arrowInit();
			});
		}

		function _arrowInit(){
			var li = $('ul',jQueryMatchedObj).position().left;
			var ri = $('li:eq('+parseInt(cunum-1)+')',jQueryMatchedObj).offset().left+$('li:eq('+parseInt(cunum-1)+')',jQueryMatchedObj).width();
//alert($(settings.rightarrowid).length);
			var ra = $(settings.rightarrowid).parent().position().left;

			if (li < 0){
				$(settings.leftarrowid).attr({'src':settings.arrowla}).css({'cursor':'pointer'}).bind('mousedown', _arrowTopD).bind('mouseup', _arrowTopU);
			}else{
				$(settings.leftarrowid).attr({'src':settings.arrowld}).css({'cursor':'default'}).unbind('mousedown, mouseup');
			}


			if (ri > ra){
				$(settings.rightarrowid).attr({'src':settings.arrowra}).css({'cursor':'pointer'}).bind('mousedown', _arrowBottomD).bind('mouseup', _arrowBottomU);
			}else{
				$(settings.rightarrowid).attr({'src':settings.arrowrd}).css({'cursor':'default'}).unbind('mousedown, mouseup');
			}
		}

		function _arrowBottomWheel(delta){
			var ri = $('li:eq('+parseInt(cunum-1)+')',jQueryMatchedObj).offset().left+$('li:eq('+parseInt(cunum-1)+')',jQueryMatchedObj).width();
			var ra = $(settings.rightarrowid).parent().position().left;
			if (ri > ra){
				var lx = $('ul',jQueryMatchedObj).position().left;
				lx += settings.dxarrow*delta;
				$('ul',jQueryMatchedObj).css({'left':parseInt(lx)});
			}
		}
		function _arrowTopWheel(delta){
			var lx = $('ul',jQueryMatchedObj).position().left;
			if(lx<0){
				lx += settings.dxarrow*delta;
				$('ul',jQueryMatchedObj).css({'left':parseInt(lx)});
			}
		}
		function _arrowBottomD(){
			var ri = $('li:eq('+parseInt(cunum-1)+')',jQueryMatchedObj).offset().left+$('li:eq('+parseInt(cunum-1)+')',jQueryMatchedObj).width();
			var ra = $(settings.rightarrowid).parent().position().left;
			if (ri > ra){
				var lx = $('ul',jQueryMatchedObj).position().left;
				lx -= settings.dxarrow;
				$('ul',jQueryMatchedObj).css({'left':parseInt(lx)});
				window.arrbottomto = setTimeout(_arrowBottomD, settings.dtime);
			}else{
				_arrowBottomU();
			}
		}

		function _arrowBottomU(){
			_arrowInit();
			clearTimeout(window.arrbottomto);
		}

		function _arrowTopD(){
			var lx = $('ul',jQueryMatchedObj).position().left;
			if(lx<0){
				lx += settings.dxarrow;
				$('ul',jQueryMatchedObj).css({'left':parseInt(lx)});
				window.arrbottomto = setTimeout(_arrowTopD, settings.dtime);
			}else{
				_arrowTopU();
			}
		}

		function _arrowTopU(){
			_arrowInit();
			clearTimeout(window.arrbottomto);
		}


		return _initialize();
	};
})(jQuery);

$(document).ready(function(){


if($('#cartpreid').length){	
	//$('#cartpreid').carPreRe();
	//cartUnitDDInit();
}

});

//oda2e-shop
function eventShow(inner){$('#'+inner).addClass('bgevent');}
function eventHide(inner){$('#'+inner).removeClass('bgevent');}

function showCart(inner){
$('.'+inner).addClass('bgevent');
$("#"+inner).load('/cgi-bin/oda2/user/cart.pl',{'action':'showCart'}, function(){$('.'+inner).removeClass('bgevent');});
}

function showFCart(inner){
eventShow(inner);
$("#"+inner).load('/cgi-bin/oda2/user/cart.pl',{'action':'showCartFull', 'INT':'cart-default'}, function(){eventHide(inner)});
}

function addToCart(idlist, id, num, inner){
if(typeof parseInt(num*1)=='NaN'){ num = 1; };
$('.'+inner).addClass('bgevent');
$.post('/cgi-bin/oda2/user/cart.pl',{
	   'action':'addToCart',
	   'oda2idlist':idlist,
	   'oda2id':id,
	   'oda2num':num}, function(data){ 

$('.'+inner).removeClass('bgevent'); 
$('#cartpreid').load('/ajax/cart-pre.html');
});
}

//eventHide('innerECart');
function reCalcCart(){
var arts = '';
$('.artCartNum').map(
 function (){
	 var id = $(this).attr('id').split('_');
	 var num = ($(this).val())*1;
	 if(!num)num=1;
	 arts += id[1]+':'+id[2]+':'+num+'|';
 }
);
$.post('/cgi-bin/oda2/user/cart.pl',{'action':'RECALC','ARR':arts},function(data){
if (data=='true'){
 //document.location=document.location;
 //showCart('innerECart')
 showFCart('mycart');
}else{alert(data)};
});
}


function deleteArtCart(id){
if (!confirm('Удалить?')){return false;}
$.post('/cgi-bin/oda2/user/cart.pl',{'action':'DELETE','ID':id},function(data){
if (data=='true'){
 //document.location=document.location;
 //showCart('innerECart')
 showFCart('mycart');
}else{alert(data)};
});
}

function addShip(idlist, idship){
$.post('/cgi-bin/oda2/user/cart.pl',{'action':'SHIPSET','IDLIST':idlist,'IDSHIP':idship},function(data){
if (data=='true'){
 showCart('innerECart')
}else{alert(data)};
});
}

function setPay(idlist, idpay){
$.post('/cgi-bin/oda2/user/cart.pl',{'action':'PAYSET','IDLIST':idlist,'IDPAY':idpay},function(data){
if (data=='true'){
 showCart('innerECart')
}else{alert(data)};
});
}





jQuery.fn.extend({
	mousewheel: function(up, down, preventDefault) {
		return this.hover(
			function() {
				jQuery.event.mousewheel.giveFocus(this, up, down, preventDefault);
			},
			function() {
				jQuery.event.mousewheel.removeFocus(this);
			}
		);
	},
	mousewheeldown: function(fn, preventDefault) {
		return this.mousewheel(function(){}, fn, preventDefault);
	},
	mousewheelup: function(fn, preventDefault) {
		return this.mousewheel(fn, function(){}, preventDefault);
	},
	unmousewheel: function() {
		return this.each(function() {
			//jQuery(this).unmouseover().unmouseout();
			jQuery.event.mousewheel.removeFocus(this);
		});
	},
	unmousewheeldown: jQuery.fn.unmousewheel,
	unmousewheelup: jQuery.fn.unmousewheel
});


jQuery.event.mousewheel = {
	giveFocus: function(el, up, down, preventDefault) {
		if (el._handleMousewheel) jQuery(el).unmousewheel();
		
		if (preventDefault == window.undefined && down && down.constructor != Function) {
			preventDefault = down;
			down = null;
		}
		
		el._handleMousewheel = function(event) {
			if (!event) event = window.event;
			if (preventDefault)
				if (event.preventDefault) event.preventDefault();
				else event.returnValue = false;
			var delta = 0;
			if (event.wheelDelta) {
				delta = event.wheelDelta/120;
				if (window.opera) delta = -delta;
			} else if (event.detail) {
				delta = -event.detail/3;
			}
			if (up && (delta > 0 || !down))
				up.apply(el, [event, delta]);
			else if (down && delta < 0)
				down.apply(el, [event, delta]);
		};
		
		if (window.addEventListener)
			window.addEventListener('DOMMouseScroll', el._handleMousewheel, false);
		window.onmousewheel = document.onmousewheel = el._handleMousewheel;
	},
	
	removeFocus: function(el) {
		if (!el._handleMousewheel) return;
		
		if (window.removeEventListener)
			window.removeEventListener('DOMMouseScroll', el._handleMousewheel, false);
		window.onmousewheel = document.onmousewheel = null;
		el._handleMousewheel = null;
	}
};


function randNum(n){
	var r = '';
	for(var i=0;i<n;i++){
		r = r+''+Math.random();
	}
	return r;
}

