$(document).ready(function() {
	//$('div.prev').html($(".car_1_content").html());
    $('#mycarousel').jcarousel();
	$('.jcarousel-list li:odd a').addClass("dark");
	
	$('#mycarousel a').bind('click', function () {
		switchCar(this.id);
	});
	$('.phpr').bind('click', function() {
		switchPhoto(this.id);
	});
	
	$('#commentSend').bind('click', function() {
		submitComment();											 
	 });

});

function switchCar(id) {
	var takeFrom = "."+id+"_content";
	loadContent(id);
	$('.jcarousel-list li a').removeClass();
	$('#'+id).addClass("actived");
	$('.jcarousel-list li:odd a').addClass("dark");
	$('.phpr').bind('click', function() {
		switchPhoto(this.id);
	});
}
function switchPhoto(id) {
	$('.prev .img_holder img').attr("src","cars/"+id+".jpg");
}

function loadContent(id) {
	$.get('/fp.ajax.php',{ car: id },
	function(data) {
		$('.prev').html(data);
	});
	
}

function submitComment() {
	var text = $("#commentText").val();
	var car = parseInt($("#carID").val());
	if(text!='') {
		$.post('/comments.ajax.php', { message: text, car_id: car },
		   function(data) {
				if(data=='1') addComment(text);
				else alert('Комментарий не был добавлен');
		   });
	}
	else alert('Введите текст комментария!');
}

function addComment(text) {
	$("#endOfComments").prepend('<div class="commentContainer"><span id="author">Ваш комментарий</span><span id="date">сейчас</span><div id="text">'+text+'</div></div>');
	$("#commentText").val('');
}

function deleteComment(comment) {
	if(parseInt(comment)!=0) {
		$.post('/comments.ajax.php', { comment_id: comment },
			   function(data) {
					if(data=='1') $("#com"+comment).fadeOut();
					else alert('Комментарий не был удален');
			   });
	}
}
