function updateCart(){
	$('#myCart').each(function(){
		$(this).load(websitePath+'cart/view/'+websiteSess+'?'+Math.random(),function(){
			$('#myCart a.popup').popupPage();
		});		
	});
}

function addToCart(data,fn){
	$.post(websitePath+'cart/add/'+websiteSess,data,function(res){
		var a = eval(res);
		if (a.result){
			if (fn&&typeof fn=='function'){
				fn.call(this,res);
			} else updateCart();
		} else {
			if (a.msg){
				showMessage(a.msg);
			}
		}
	});
}

function editCart(data,fn){
	$.post(websitePath+'cart/update/'+websiteSess,data,function(res){
		var a = eval(res);
		if (a.result){
			if (fn&&typeof fn=='function'){
				fn.call(this,a.html);
			}
		} else {
			if (a.msg){
				showMessage(a.msg);
			}
		}
	});
}

function deleteCart(data,fn){
	$.post(websitePath+'cart/delete/'+websiteSess,data,function(res){
		if (fn&&typeof fn=='function'){
			fn.call(this,res);
		}
	});
}

(function($){

	$.fn.colorBox = function(fn){
		return this.each(function(){
			var $this = $(this);
			var _w = 35;

			var $box = null;
			if ($this.hasClass('color-list')){
				$box = $this;
			} else {
				$box = $this.find('div.color-box');			
			}
			if (!$box||!$box.size()) return false;
			
			var $prev = $box.find('a.prev'), $next = $box.find('a.next');
			var $curr = $box.find('span.current-color');
			
			var $ul = $box.find('ul.color-list');
			var w = $box.find('div.color').width();
			if (!w){
				if ($box.hasClass('color-box')) w = 260;
				else {
					var dw = parseInt($box.parents('div:first').css('width'));
					if (dw<900) w = dw - 40; else w = 160;
				}
			}
			var length = _w * $ul.find('li').size(), shift = parseInt(w/(2*_w))*_w;
			var _l = w - w % _w;
			$box.find('div.color').width(_l);
			$ul.width(length);
			
			function _lockButtons(){
				$prev.unbind('click').click(function(){
					return false;
				});
				$next.unbind('click').click(function(){
					return false;
				});
			}
			
			function _drawButtons(){
				if (length>_l){
					$prev.show();
					$next.show();
				} else {
					$prev.hide();
					$next.hide();
				}
				$prev.unbind('click').click(function(){
					_rollBox(-1);
					return false;
				});
				$next.unbind('click').click(function(){
					_rollBox(1);
					return false;
				});
			}
			
			function _rollBox(dir){
				var left = parseInt($ul.css('left')) || 0;
				var _x = parseInt((dir>0) ? left-shift : left+shift);
				if (_x>0) _x = 0;
				if (_x<-(length-w+shift)) _x = left;
				if (_x==left) return false;
				_lockButtons();
				$ul.animate({'left':_x+'px'},500,function(){
					_drawButtons();
				});
			}
			
			function _setCurrent(){
				var $li = $(this).parents('li:first');
				$ul.find('> li').removeClass('s');
				$li.addClass('s');
				$ul.focus();
				if (fn&&typeof fn=='function'){
					fn.call(this);
				}
			}
			
			_drawButtons();
			
			$ul.find('> li a').unbind('click').click(function(){
				_setCurrent.call(this);
				return false;
			});
			if ($curr.size()){
				$ul.find('> li[value="'+$curr.text()+'"] a').click();
			} else {
				$ul.find('> li:first a').click();
			}
		});
	}

	$.fn.planetLoginBox = function(){
		return this.each(function(){
			var $box = $(this);
			
			$box.find('.tabs > li a').unbind('click').click(function(){
				var id = $(this).attr('href').substring(1);
				var a = id == 'first-box' ? 'second-box' : 'first-box';
				$box.find('.tabs > li').removeClass('s');
				$(this).parent().addClass('s');
				$('#'+a).fadeOut('def',function(){
					$('#'+id).fadeIn('slow');	
				});
				return false;	
			});
			
			$box.find('input[type="text"],input[type="password"]').smartField();
			
			$box.find('input[name="login"]').checkField();
		});
	}
	
	$.fn.boxTape = function(opts){
		var settings = $.extend({
			'easing' : 'easeInBack'
		},opts);
		return this.each(function(){
			var $box = $(this), $list = $box.find('ol:first');
			if (!$list.size()) $list = $box.find('ul:first');
			var $curr = $list.find('> li.s');
			var pos = 0, i = 0;
			$list.find('> li').each(function(){
				if ($(this).attr('value')==$curr.attr('value')) pos = i;
				i++;
			});
			var picWidth = $list.find('> li:first').width();
			var len = $list.find('> li').size();
			var step = 1;
			
			$list.each(function(){
				if (this.tagName=='UL'){
					picWidth += 5;
					step = 3;
					return false;
				}
			});
			var ulWidth = picWidth*len;

			var $prev = $box.find('div.a > a.prev:first'), $next = $box.find('div.a > a.next');
			lockedTape = false;
			
			function _checkButtons(){
				$list.width(ulWidth);
				$prev.show();
				$next.show();
				if (pos*step-step<0) $prev.hide();
				if (pos*step+step>=len) $next.hide();
			}
			
			function _rollBox(dir){
				if (lockedTape) return false;
				pos += dir;
				_checkButtons();
				var cnt = -(picWidth*pos*step);
				lockedTape = true;
				if (dir) $list.animate({left:cnt},750,settings.easing,function(){
					lockedTape = false;
				});
			}
			
			$prev.click(function(){
				_rollBox(-1);
				return false;
			});
			$next.click(function(){
				_rollBox(1);
				return false;
			});
			
			_checkButtons();
		});
	}
	
	$.fn.popupPage = function(){
		return this.each(function(){
			if (!$.fn.fancybox) return false;
			var $this = $(this);
			$this.fancybox({frameWidth: '850', frameHeight: '490', autoScale: false, overlayShow: true});
		});
	}
	
	$.fn.productItem  = function(){
		return this.each(function(){
			var $el = $(this);
			var $quant = $el.find('input[name="quantity"]'), $size = $el.find('select[name="size"]'),
				$online = $el.find('input[name="min_quantity"]'),// $offline = $el.find('input[name="min_offline"]'),
				$color = $el.find('ul.color-list'),	$update = $el.find('a.update'), 
				$delete = $el.find('a.delete'), $add = $el.find('input[name="add"]'), 
				$select = $el.find('a.select'), $prod = $el.find('input[name="product_id"]');
			var id = parseInt($el.attr('value'),10), is_reserve = false;
			if (id<=0){
				$el.find('input[name="reserve_id"]').each(function(){
					id = parseInt($(this).val());
					is_reserve = true;
				});
			}

			if (!id||!$quant.size()||!$online.size()) return true;
			
			function _check(fn){
				var accept = true;
				var color = 0;
				if (parseInt($online.val(),10)>parseInt($quant.val(),10)){
					showMessage(getL('min_quantity_is')+' '+$online.val());
					$quant.val($online.val());
					accept &= false;
				}
				if ($size.size()&&$size.val()=='0'){
					showMessage(getL('select_size'));
					accept &= false;
				}
				if ($color.size()){
					color = $color.find('> li.s').attr('value');
					if (!color){
						showMessage(getL('select_color'));
						accept &= false;
					}
				}
				if (accept && fn && typeof fn=='function'){
					$.post(websitePath+'cart/stock/'+websiteSess,_values(),function(data){
						var a = eval(data);
						if (!a.result){
							accept &= false;
						} else if (a.question){
							accept &= askQuestion(a.question);
						} else if (a.diff>=0){
							accept &= true;
						} else {
							accept &= false;
						}
						if (accept && a.value){
							$quant.val(a.value);
						}
						if (a.sessionID){
							websiteSess = a.sessionID;
						}
						fn.call(this,accept);
					});
				} else if (fn && typeof fn=='function'){
					fn.call(this,accept);
				}
				return accept;
			}
			
			function _color(){
				return $color.size() ? $color.find('> li.s').attr('value') : 0;
			}
			
			function _values(){
				return a = {
					id		: id,
					product : $prod.size() ? $prod.val() : id,
					size	: $size.val() || -1,
					color	: _color(),
					quantity: $quant.val(),
					reserve	: is_reserve ? 1 : 0
				};
			}
			
			$add.unbind('click').click(function(){
				_check(function(r){
					if (!r) return false;
					addToCart(_values(),function(data){
						var $li = $el;
						if (parent!=window){
							$li = parent.$('ul.product-list > li[value="'+id+'"]');
							if (parent.$.fn.fancybox) parent.$.fn.fancybox.close();
							parent.updateCart();
						} else {
							updateCart();
						}
						if ($('#myCart').size()){
							$li.effect('transfer',{to:'#myCart', className:'ui-effects-transfer'},'slow');
						} else {
							var url = parent!=window ? parent.location.href : location.href;
							var a = url.split('#',2);
							url = a[0];
							url += url.indexOf('?')>0 ? '&' : '?';
							url += 'sessionID='+websiteSess;
							if (parent!=window){
								parent.redirect(url);
							} else {
								redirect(url);
							}
						}
					});
				});
				return false;
			});
			
			$update.unbind('click').click(function(){
				_check(function(r){
					if (!r) return false;
					var $div = $el.parents('ul:first').parent();
					editCart(_values(),function(data){
						$div.html(data);
						$div.find('> ul table').tableColumns();
						$div.find('> ul > li').productItem();
					});
				});
				return false;
			});
			
			$delete.unbind('click').click(function(){
				var $div = $el.parents('ul:first').parent();
				deleteCart(_values(),function(data){
					$div.html(data);
					$div.find('> ul table').tableColumns();
					$div.find('> ul > li').productItem();
				});
				return false;
			});
			
			var $box = $el.find('.color-box');
			
			$el.colorBox(function(){
				var src = $(this).find('> img').attr('src');
				$select.find('> img').attr('src',src);
				$box.hide();
			});
			
			$select.unbind('click').click(function(){
				if ($box.css('display')=='none') $box.show(); else $box.hide();
				return false;
			});
			
			$quant.unbind('keypress').keypress(function(event){
				if (event.which==13){
					$add.click();
					$update.click();
				}
			});

		});
	}
	
})(jQuery)