function str_replace(search, replace, subject) {
	return subject.split(search).join(replace);
}

(function($){
	function fresize(){
		if ($('#footer').width() != $('#main').width()){
			$('#footer').width($('#main').width() < 1160 ? 1160 : $('#main').width());
  	}
	}

  $(document).ready(function() {
		fresize();
    $(window).resize(fresize);
    
    $('#carousel').show().jcarousel();

    var fnc = $("[href$='.jpg']").fancybox();
    
		$('#carousel li img').click(function(){
			$('#big_pic').attr('src', str_replace('")', '', str_replace('url("', '', str_replace("_th.", "_big.", $(this).css("background-image")))));
			$('#big_pic').attr('alt', $(this).parent().attr('title'));
			return false;
		});
		
		$('#big_pic').click(function(){
			$("a[title='" + $(this).attr('alt') + "']").click();
			return false;
		});
		
		$('.to_cart').click(function(){

			id = $(this).children('span').text();
			count_inp = $('#to_cart_count').attr('value');
			count = ((count_inp === undefined)||(count_inp === '')) ? 1 : count_inp;

			if (count > 0){
				$('#basket').load('/ajax/addbasket.php', {id: id, count: count}, function(){
					alert('Товар добавлен в корзину в количестве ' + count + 'шт.\n' + 'Всего товаров в корзине: ' + $('#basket').text() + ' шт.');
				});
			}
			return false;
		});
		
		$('.delete_from_cart').click(function(){
			id = $(this).children('span').text();
			$('#basket').load('/ajax/deletebasket.php', {id: id}, function(){
				location.reload(true);
			});
			return false;
		});
		$('#feedback').submit(function(){
			var data = {
				'user'  : $("#feedback input[name = 'name']").val(),
				'phone' : $("#feedback input[name = 'phone']").val(),
				'email' : $("#feedback input[name = 'email']").val(),
				'text'  : $("#feedback textarea[name = 'text']").val()
			}
			if((data.phone == '')&&(data.email == '')){
				alert('Укажите телефон или email чтобы мы могли с Вами связаться');
			}else if(data.text == ''){
			  alert('Вы не ввели сообщение');
			}else{
				$.post('/ajax/esend.php', data, function(resp){
				  if(resp == 1){
						$("#feedback input[name = 'name']").val('');
						$("#feedback input[name = 'phone']").val('');
						$("#feedback input[name = 'email']").val(''),
						$("#feedback textarea[name = 'text']").val('');
            alert('Сообщение отправлено.\nМенеджер свяжется с Вами в ближайшее время.');
					}
				},"text");
			}
			return false;
		});
		
	});
})(jQuery);

