window.onload = function() {
	$(".toggle").tooltip({showURL: false,extraClass: "tooltip_font"});

	/*$('#video_slide_center').jCarouselLite({
		scroll: 1,
		visible: 3,
		rows: 1,
		btnNext: ".slide_right",
		btnPrev: ".slide_left",
		speed: 800,
		circular: true
	});

	if ($('.scroll_photos').length){
		var start = calculate_start();
		$('.scroll_photos').jCarouselLite({
			start:start,
			scroll: 1,
			visible: 4,
			rows: 1,
			btnNext: ".photo_right",
			btnPrev: ".photo_left",
			speed: 800,
			circular: true
		});
	}*/
	if ($('.scroll_photos').length){
		var st = calculate_start();
		$('#carousel').jcarousel({scroll:1,wrap:'circular',start:st + 1});
	}

	if ($('#carousel_video').length){
		var st = calculate_start();
		$('#carousel_video').jcarousel({scroll:1,wrap:'circular',start:st + 1});
	}

	$(".rating_stars").live("click",function(){
		var id = $(this).parent().children("#id").val();
		var node = $(this).parent().children("#node").val();
		var val = parseInt($(this).text());
		var obj = $(this);
		$("#rate_desc_text").empty().append('Подождите...');

		$.ajax({
			type: "POST",
			url: "/misc/rating",
			dataType: "json",
			data: "id="+id+"&node="+node+"&val="+val,
			success: function(msg){
				if (msg.error == ''){
					obj.parent().parent().parent('div').empty().append(msg.html);
					$("#rate_desc_text").empty().append(msg.msg);
					if ($("#rate_votes_value").length){
						$("#rate_votes_value").empty().append(msg.rate);
					}
					if ($("#rate_votes_total").length){
						$("#rate_votes_total").empty().append(msg.votes);
					}
				}else{
					$("#rate_desc_text").empty().append(msg.error);
				}
			}
		});
	});

	$("#poll_submit").click(function(){
		var valid = false;
		$(".input_poll").each(function(){
			if ($(this).css('background-image').match(/check_active/)){
				valid = true;
			}
		});

		if (valid){
			var vote = $("#pollid").val();

			$("#vote_button_container").hide();
			$("#vote_loader_container").show();

			var id = 0;
			$(".input_poll").each(function(){
				if ($(this).css('background-image').match(/check_active/)){
					var str = $(this).attr("id");
					id = str.replace(/value_/,'');
				}
			});

			$.ajax({
				type: "POST",
				url: "/misc/post_poll/",
				data: "vote="+vote+"&q="+id,
				dataType:'json',
				success: function(msg){
					$("#vote_button_container").hide();
					$("#vote_loader_container").hide();
					$("#show_poll_results").remove();

					if (msg.error == ''){
						$("#poll_container").empty().append(msg.html);
					}else{
						$("#vote_errors").empty().append(msg.error);
					}
				}
			});
		}else{
			alert('Вы не выбрали вариант ответа.');
		}
	});

	$("#show_poll_results").click(function(){
		$(this).text('Подождите...');

		$.ajax({
			type: "POST",
			url: "/misc/get_poll/",
			data: "vote=current",
			dataType:'json',
			success: function(msg){
				$("#vote_button_container").hide();
				$("#vote_loader_container").hide();
				$("#show_poll_results").remove();

				if (msg.error == ''){
					$("#poll_container").empty().append(msg.html);
				}else{
					$("#vote_errors").empty().append(msg.error);
				}
			}
		});
	});

	$(".gallery_photo").click(function(e){
		e.preventDefault();
		var id = $(this).parent().children("#pid").val();

		$(".photos_ajax").show();

		$.ajax({
			type: "POST",
			url: "/photos/get_photodata/",
			data: "id="+id,
			dataType:'json',
			success: function(msg){
				$("#current_foto").slideUp(300,function(){
					$("#current_foto").empty().append(msg.img);
					$("#current_foto").slideDown(300);
				});

				$("#photo_views").empty().append(msg.views);
				$(".photo_rate_container").empty().append(msg.rate);
				$("#comments_container").empty().append(msg.comments);
				$("#current_photo").val(msg.id);

				$(".photos_ajax").hide();
			}
		});
	});

	$(".ajax_video").click(function(e){
		e.preventDefault();
		var id = $(this).parent().children("#pid").val();
		$(".photos_ajax").show();

		$.ajax({
			type: "POST",
			url: "/video/get_videodata/",
			data: "id="+id,
			dataType:'json',
			success: function(msg){
				$("#video_block").empty().append(msg.html);
				$("#video_views").empty().append(msg.views);
				$("#comments_container").empty().append(msg.comments);
				$("#rate_container").empty().append(msg.rate);
				$("#video_id").val(msg.id);
				$(".photos_ajax").hide();
				$("#h1_video").empty().append(msg.name);
			}
		});
	});
}

function calculate_start(){
	var x = 0;
	var valid = 0;
	$(".gallery_photo").each(function(){
		var ismy = $(this).parent().children("#itsme").val();
		if (ismy == 'Y'){
			valid = x;
		}
		x++;
	});
	return valid;
}
