$(document).ready(function(){	
	
	$(".gallery:first a[rel^='prettyPhoto']").prettyPhoto({animationSpeed:'normal',theme:'light_square',slideshow:4000});
	$(".gallery:gt(0) a[rel^='prettyPhoto']").prettyPhoto({animationSpeed:'normal',slideshow:17000});

	
	function Tizer(fnc, IdName, LiWidth, see){
		var w = $('#' + IdName).width(),
			Ox = parseInt($('#' + IdName).css('marginLeft')),
			margin = (fnc == 'left') ? -LiWidth : LiWidth;
		if ((fnc == 'left' && -Ox < w-see*LiWidth) || (fnc == 'right' && Ox < 0)) {
			$('#' + IdName).animate({marginLeft: (Ox + margin) + 'px'}, 500);
		}
	}

	$('#tizer-left, #tizer-right').click(function(){
		Tizer(this.id.replace('tizer-', ''), 'tizer', 200, 4);
		return false;
	});

	$('#opinion-left, #opinion-right').click(function(){
		Tizer(this.id.replace('opinion-', ''), 'opinion', 400, 1);
		return false;
	});


	if ($('.text .text-left').height() > $('.text .text-right').height()) {
		$('.text .text-right').height($('.text .text-left').height());
	} else {
		$('.text .text-left').height($('.text .text-right').height());
	}
	$('.text').height($('.text .text-right').height());
	$('#tizer').width($('#tizer li').size()*200);
	$('.center .text-right div.view ul').width($('.center .text-right div.view ul li').size()*400);

	$('.text-left .manufacturers a, #b_manufacturers_audio, #b_manufacturers_theatre').click(function(){
	id = this.id.replace('manufacturers_', '').replace('b_', '');
	$('.text-left .manufacturers a span').css('color', '#3F342E');
	if (id == 'audio' || id == 'theatre') {
		$('#manufacturers_' + id + ' span').css('color', '#9A3933');
		$('ul.manufacturers-list li a').css('color', '#CEBBA2'); 
		$('ul.manufacturers-list li.' + id + ' a').css('color', '#9A3933');
		$('.center .general .img').css('background', 'url(/tmp/img/banner/' + id + '.jpg) no-repeat scroll 0 0 transparent');
	} else {
		$('ul.manufacturers-list li a').css('color', '#9A3933');
	}
		$('#tizer_type').val(id);
		$('#tizer_position').val(0);
	return false;
	});

	$('.product .param .vote ul li').hover(function(){
		var vote = this.id.replace('rating_', '').split('_');
		$('.product .param .vote ul').css('background-position', '-' + (10-vote[1])*13 + 'px 2px');
	});
	$('.product .param .vote ul li').click(function(){
		var vote = this.id.replace('rating_', '').split('_');
				
		$.getJSON('/tmp/js/vote.php?product_id=' + vote[0] + '&rating=' + vote[1], function(data) {
			if (data.code == 0) {
				alert('Произошла ошибка!');	
			} else {
				$('.product .param .vote').html('<div class="messgage">Спасибо! Ваш голос учтен!</div>');
				$('.product .param .vote').delay(3000).animate({left: '+=50', height: 'toggle'});	
			}
		});	
		return false;	
	});

	function basket(quantity,total) {
		$('#basket').html('товаров ' + quantity + ' на ' + total + ' руб.');
	
	}

	$('.buy').click(function(){
		var buy = this.id.replace('buy_', '').split('_');
		
		if (buy[3] == 0) {
			param = '&param=0';
		} else {
			param = '&param[model]=' + $('#model').val() + '&param[level]=' + $('#level').val();
		}
		if (buy[2] == 0) {
			buy[2] = $('#quantity').val();
		}
		
		$.getJSON('/tmp/js/basket.php?action=' + buy[0] + '&product_id=' + buy[1] + '&quantity=' + buy[2] + param, function(data) {
			if (data.code == 0) {
				alert('Произошла ошибка!');	
			} else {
				basket(data.quantity, data.total);
				alert('Товар добавлен!');
			}
		});
		return false;
	});	


	$('table.basket-list tr td.td-edit a.edit').click(function(){
			id = this.id.replace('edit_', '');
			$.getJSON('/tmp/js/basket.php?action=edit&key=' + id + '&quantity=' + $('#quantity_' + id).val(), function(data) {
				if (data.code == 0) {
					alert('Произошла ошибка!');	
				} else {
					basket(data.quantity, data.total);
					$('div.total').text('Всего товаров ' + data.quantity + ' на сумму: ' + data.total + ' руб.'); 
					$('#item_price_' + id).html(data.item + ' руб.');
				}
			});		
		return false;
	});	


	$('table.basket-list tr td.td-del a.del').click(function(){
		if (confirm('Удалить товар?')) {
			id = this.id.replace('del_', '');
			$.getJSON('/tmp/js/basket.php?action=del&key=' + id, function(data) {
				if (data.code == 0) {
					alert('Произошла ошибка!');	
				} else {
						
					if (data.quantity > 0) {
						basket(data.quantity, data.total);
						$('div.total').text('Всего товаров ' + data.quantity + ' на сумму: ' + data.total + ' руб.');
					} else {
						$('#basket').text('В корзине пусто');
						$('div.total').text('');
						$('div.MainText').html('<p>&nbsp;</p><p>В Вашей корзине нет товаров</p>');
						$('.div-registration').css('display', 'none');
						$('#registration').css('display', 'none');
					}
					$('#tr_' + id).css('display', 'none');
				}
			});		
		}
		return false;
	});	

	$('a.registration-on').click(function(){
		$('#registration').fadeIn('slow');
		if ($('.text .text-left').height() > $('.text .text-right').height()) {
			$('.text .text-right').height($('.text .text-left').height());
			$('.text').height($('.text .text-left').height());
		} else {
			$('.text .text-left').height($('.text .text-right').height());
			$('.text').height($('.text .text-right').height());
		}
		return false;
	});

	$('.td-reg-radio input[type="radio"]').click(function() {  
		 if (this.id.replace('delivery_', '') == 1) {
		 	$('#delivery_address').fadeIn('slow');
		 } else {
		 	$('#delivery_address').fadeOut('slow');
		}
	});
});
