jQuery(document).ready(function(){

	jQuery('#sh_reset p a').live('click', function() {

		query='';
		var killdup = new Array();

		jQuery('#shopcard table select[name=fas]').each(function() {
			price_id=jQuery(this).val();
			quant=jQuery(this).parent().next().children('input').val();
			if (query>'') {
				query+='&';
			}

			if (killdup[price_id]>0) {
				query=query.replace('good['+price_id+']='+killdup[price_id],'');
				quant=Number(killdup[price_id])+Number(quant);
			}
			killdup[price_id]=quant;

			query+='good['+price_id+']='+quant;
		});

		jQuery.getJSON('/user_modules/shop.php?'+query,{
				x: 1,
				action: 'recount'
			}, function(data) {
					if (data.quant==0) {
						jQuery('#cart p').html('<a href="/shop/cart/">Корзина</a> пока что пуста');
					} else {
						jQuery('#cart p').html('В <a href="/shop/cart/">корзине</a> '+data.quant+' товаров на сумму '+data.sum+' р.');
					}
			});


		jQuery.get('/user_modules/shop.php', {
			x: 1,
			action: 'cart'
		}, function(data) {
			jQuery('#in_content').html(data);
		});
		return false;
	});

	jQuery('a.cart_del').live('click', function() {

		if (confirm('Вы действительно хотите удалить товар из корзины?')==false) {
			return false;
		};

		price_id=jQuery(this).parent().parent().children('td').children('select[name=fas]').val();
		jQuery.getJSON('/user_modules/shop.php',{
				x: 1,
				action: 'del',
				price_id: price_id
			}, function(data) {
					if (data.quant==0) {
						jQuery('#cart p').html('<a href="/shop/cart/">Корзина</a> пока что пуста');
					} else {
						jQuery('#cart p').html('В <a href="/shop/cart/">корзине</a> '+data.quant+' товаров на сумму '+data.sum+' р.');
					}
		});
		jQuery(this).parent().parent().remove();

		jQuery.getJSON('/user_modules/shop.php', {
			x: 1,
			action: 'get_sum'
		}, function (data) {
			jQuery('#total_sum').html(data+' руб.');
		});

		return false;
	});

	jQuery('#s_field').bind('click', function() {
		if (jQuery(this).val()==jQuery(this).attr('title')) {
			jQuery(this).val('');
		}
	})
	jQuery('#s_field').bind('blur', function() {
		if (jQuery(this).val()=='') {
			jQuery(this).val(jQuery(this).attr('title'));
		}
	})

	jQuery('p.cat_cart a').click(function() {
		price_id=jQuery(this).attr('title');
		howmany=1;
		ts=this;
		jQuery.getJSON('/user_modules/shop.php',{
				x: 1,
				action: 'add',
				price_id: price_id,
				quant: howmany
			}, function(data) {
					if (data.quant==0) {
						jQuery('#cart p').html('<a href="/shop/cart/">Корзина</a> пока что пуста');
					} else {
						jQuery('#cart p').html('В <a href="/shop/cart/">корзине</a> '+data.quant+' товаров на сумму '+data.sum+' р.');
					}

		if (jQuery('#added').length==0) {
			jQuery(ts).after('<p id="added">Товар добавлен</p>');
			jQuery('#added').hide(0).css({color: 'red'}).show(500, function() {
				jQuery('#added').hide(1000, function() {jQuery('#added').remove()});
			});
		}


				});
		return false;


	});

	jQuery('#add_cart a').click(function() {
		price_id=jQuery('#t_pref select[name=fas]').val();
		howmany=jQuery('#t_pref input[name=quant]').val();
		jQuery.getJSON('/user_modules/shop.php',{
				x: 1,
				action: 'add',
				price_id: price_id,
				quant: howmany
			}, function(data) {
					if (data.quant==0) {
						jQuery('#cart p').html('<a href="/shop/cart/">Корзина</a> пока что пуста');
					} else {
						jQuery('#cart p').html('В <a href="/shop/cart/">корзине</a> '+data.quant+' товаров на сумму '+data.sum+' р.');
					}

		if (jQuery('#added').length==0) {
			jQuery('#t_price').after('<p id="added">Товар добавлен</p>');
			jQuery('#added').hide(0).css({color: 'red'}).show(500, function() {
				jQuery('#added').hide(1000, function() {jQuery('#added').remove()});
			});
		}


				});
		return false;
	});

	jQuery('#tovar_img a').click(function() {

		var tis=this;
		jQuery('body').append('<div id="timg_overlay"></div>');
		jQuery('#timg_overlay').css({
			background: '#019340',
			display: 'none',
			height: '100%',
			left: 0,
			top: 0,
			opacity: '0.7',
			position: 'fixed',
			width: '100%',
			zIndex: '30',
			textAlign: 'center'
		});

		jQuery('body').append('<div id="timg_msg_overlay"></div>');
		jQuery('#timg_msg_overlay').css({
			zIndex: '100',
			textAlign: 'center',
			position: 'fixed',
			left: 0,
			top: 0,
			width: '80%',
			padding: '50px 10% 50px 10%',
			fontSize: '20px',
			display: 'none'
		});


		jQuery('#timg_overlay').fadeIn('normal', function() {
			jQuery('#timg_msg_overlay').html('<img src="'+jQuery(tis).children('img').attr('src')+'"><p><a href="#">Закрыть</a></p>');
			var tmpsh=sewindowHeight();

			jQuery('#timg_msg_overlay').fadeIn('normal', function() {

						if (tmpsh-100>0)
						{
							tmpsh-=100;
						}
						if (jQuery('#timg_msg_overlay').height()>tmpsh) {
							jQuery('#timg_msg_overlay img').css({'height':tmpsh+'px'});
						}


				jQuery('#timg_msg_overlay').click(function() {
					jQuery('#timg_msg_overlay').fadeOut('normal', function() {
						jQuery('#timg_overlay').fadeOut('normal', function() {
							jQuery('#timg_overlay, #timg_msg_overlay').remove();
						})
					})
					return false;
				});
			});
		});



		return false;
	});
});

function sewindowHeight() {
var de = document.documentElement;
return self.innerHeight || ( de && de.clientHeight ) || document.body.clientHeight;
}

function sewindowWidth() {
var de = document.documentElement;
return self.innerWidth || ( de && de.clientWidth ) || document.body.clientWidth;
}
