var info_opened = 0;
var links_opened = 0;

$(document).ready(function() {

	$("#orig-image").mouseover(function() {

		$(".photo-controls-panel").fadeIn("slow");

		$(".photo-controls-panel").css('margin-left', ( ($(".photo_info").parent().width() - $(".photo-controls-panel").width() ) / 2) - 3 );
		$(".photo-controls-panel").css('margin-top', ( $(".photo_info").parent().height() - $(".photo-controls-panel").height() ) );
		//$(".photo-controls-panel").css('height', $(".photo_info").parent().height() );

	});

	$("#orig-image").mouseleave(function() {

		$(".photo-controls-panel").fadeOut("fast");

	});

	$(".orig-image-series-first").mouseover(function() {

		$(".photo-controls-panel").fadeIn("slow");

		$(".photo-controls-panel").css('margin-left', ( ($(".orig-image-series-first").width() - $(".photo-controls-panel").width() ) / 2) - 3 );
		$(".photo-controls-panel").css('margin-top', ( $(".orig-image-series-first").height() - $(".photo-controls-panel").height() ) - 3 );
		//$(".photo-controls-panel").css('height', $(".photo_info").parent().height() );

	});

	$(".orig-image-series-first").mouseleave(function() {

		$(".photo-controls-panel").fadeOut("fast");

	});

	if($("#flashMessage").attr('class') == 'message success')
	{
		$(".photo-controls-panel").css("margin-top", parseInt($(".photo-controls-panel").css("margin-top")) + 16 + 'px');
	}

});

function initModerate()
{
	$("#dialog-form").dialog({
		buttons: {
			"Закрити": function(){
				$(this).dialog("close");
			}
			, "Видалити": function(){
				if(confirm('Are you sure you want to delete this photo?'))
				{

					if ($("#BanReason").val() == '')
					{
						alert("Ви не ввели причини видалення фото");
						return false;
					}

					$("#ModcpForm").attr('action', '/modcp/del/'+photo_id)
					$("#ModcpForm").trigger('submit');

					$(this).dialog("close");
				}
			}
			, "Модерувати": function(){
				$("#BanRetUrl").val(here_url);
				$("#ModcpForm").trigger('submit');

				$(this).dialog("close");
			}
		},
		width: 420,
		heigth: 750,
		modal: true,
		resizable: false,
		autoOpen: false
	});

	$(".moderate").click(function(){

		photo_id = parseInt($(this).attr('id').substr(4));

		$.get("/modcp/index/"+photo_id, { here: here_url }, function(data)
		{
			$("#dialog-form").html(data);

			$("#dialog-form").dialog('open');

		});

	});
}

function initCommentControls()
{
	$(".mod-edit").click(function(){

		$("#ModCommentCancel").trigger('click');

		comment_id = parseInt($(this).attr("id").substr(17));

		$("#mod-edit-form-"+comment_id).remove();
		$("#mod-del-form-"+comment_id).remove();

		$("#comment-container-"+comment_id).css('display', 'none');

		$("#comment-container-"+comment_id).after('<div id="mod-edit-form-'+comment_id+'">'
		+'<h3>Редагувати коментар</h3>'
		+'<form method="post" id="ModCommentForm" action="/comments/mod_edit/'+comment_id+'">'
		+'<input type="hidden" name="data[ModComment][page]" value="'+current_page
		+'<textarea rows="4" cols="50" id="ModCommentBody" name="data[ModComment][body]" >'+$("#comment-body-"+comment_id).text()+'</textarea><br />'
		+'причина редагування:<br />'
		+'<textarea id="ModCommentReason" name="data[ModComment][reason]"></textarea><br /><br />'
		+'<input type="submit" id="ModCommentSave" name="submit" value="Зберегти" class="submit" />&nbsp;'
		+'<input type="button" id="ModCommentCancel" name="calcel" value="Відмінити" class="submit" />'
		+'</form></div>');

		$("#ModCommentCancel").bind('click',function(e){

			$("#mod-edit-form-"+comment_id).remove();
			$("#comment-container-"+comment_id).css('display','block');

		});

		$("#ModCommentSave").bind('click',function(e){

			if ($("#ModCommentReason").val() == '')
			{
				alert('Причина редагування не може бути порожньою');
				return false;
			}
			else
				$("#ModCommentForm").submit();

		});


	});


	/*$(".mod-del").click(function(){


		$("#ModCommentCancel").trigger('click');


		comment_id = parseInt($(this).attr("id").substr(16));

		$("#mod-edit-form-"+comment_id).remove();
		$("#mod-del-form-"+comment_id).remove();

		$("#comment-container-"+comment_id).css('display', 'none');

		alert();

		$("#comment-container-"+comment_id).after('<div id="mod-del-form-'+comment_id+'">'
		+'<h3>Видалити коментар</h3>'
		+'<form method="post" id="ModCommentForm" action="/comments/mod_del/'+comment_id+'">'
		+'<input type="hidden" name="data[ModComment][page]" value="'+current_page
		+'<br />причина видалення:<br />'
		+'<textarea id="ModCommentReason" name="data[ModComment][reason]"></textarea><br /><br />'
		+'<input type="submit" id="ModCommentSave" name="submit" value="Видалити" class="submit" />&nbsp;'
		+'<input type="button" id="ModCommentCancel" name="calcel" value="Відмінити" class="submit" />'
		+'</form></div>');

		$("#ModCommentCancel").bind('click',function(e){

			$("#mod-del-form-"+comment_id).remove();
			$("#comment-container-"+comment_id).css('display','block');

		});

		$("#ModCommentSave").bind('click',function(e){

			if ($("#ModCommentReason").val() == '')
			{
				alert('Причина видалення не може бути порожньою');
				return false;
			}
			else
				$("#ModCommentForm").submit();

		});

	});*/
}

function initCommentReply()
{
	$(".replay-to").click(function(){

		cur_comment_id = parseInt($(this).attr("id").substr(14));
		to_user_id = parseInt($("#userid-for-comment-"+cur_comment_id).val());

		to_user_login = $("#login-for-comment-id-"+cur_comment_id).text();

		$("#comment-to").text(" ");
		$("#comment-to").append(" <div class='to-element'><span>кому:</span>" + to_user_login + "</div>");


		$("#CommentToUserId").val(to_user_id);
		$("#CommentParentId").val(cur_comment_id);

		$("#comment-to").append(" <a id=\"clear\">&nbsp;</a>");

		$("#comment-to").show();

		$("#clear").bind("click", function(e){

			$("#comment-to").hide();
			$("#comment-to").text('');
			$("#CommentToUserId").val('');
			$("#CommentParentId").val('');
			$("#CommentToCommentId").val('');
		});

		$("#CommentComment").focus();

	});
}

function enableReply(cur_comment_id)
{
	to_user_id = parseInt($("#userid-for-comment-"+parseInt(cur_comment_id)).val());
	to_user_login = $("#login-for-comment-id-"+parseInt(cur_comment_id)).text();

	if(to_user_login == '')
	{
		$("#comment-to").hide();
		$("#comment-to").text('');
		$("#CommentToUserId").val('');
		$("#CommentParentId").val('');
		$("#CommentToCommentId").val('');

		return false;
	}

	$("#comment-to").text(" ");
	$("#comment-to").append(" <div class='to-element'><span>кому:</span>" + to_user_login + "</div>");


	$("#CommentToUserId").val(to_user_id);
	$("#CommentParentId").val(parseInt(cur_comment_id));

	$("#comment-to").append(" <a id=\"clear\">&nbsp;</a>");

	$("#comment-to").show();

	$("#clear").bind("click", function(e){

		$("#comment-to").hide();
		$("#comment-to").text('');
		$("#CommentToUserId").val('');
		$("#CommentParentId").val('');
		$("#CommentToCommentId").val('');
	});

	$("#CommentComment").focus();
}

function initPhoto()
{
	$("#toAnnotate").remove();
	$(".image-annotate-canvas").remove();
	$(".jcrop-holder").remove();
	$("#image-annotate-edit-form").remove();

	$(".photo_info").hide();
	$(".photo_links").hide();

	$("#orig-image").append(orig_image);
	$("#orig-image").append(annotate_canvas);

	is_annotate = 0;
    is_cropping = 0;
	links_opened = 0;
	info_opened = 0;
}

function initPhotoBtns()
{
	//info btn
	$("#info_btn").click(function() {

		if(info_opened == 0)
		{
			initPhoto();

			$(".photo_info").hide();
			$(".photo_links").hide();

			$("#links_btn").removeClass('active_popup');
			$("#notice_btn").removeClass('active_popup');
			$("#crop_btn").removeClass('active_popup');

			$(".photo_info").show();
			$("#photo_info_close").click(function() {
				initAnnotate();
				$(".photo_info").hide();
				$("#info_btn").removeClass('active_popup');
				info_opened = 0;
			});

			$(".photo_info").css('margin-left', ( $(".photo_info").parent().width() - 474 ) / 2 );
			$(".photo_info").css('margin-top', ( $(".photo_info").parent().height() - $(".photo_info").height() ) / 2 );

			$(".photo_info").draggable();

			$("#info_btn").addClass('active_popup');

			links_opened = 0;
			info_opened = 1;
			is_cropping = 0;
			is_annotate = 0;
		}
		else
		{
			initAnnotate();

			$(".photo_info").hide();
			$("#info_btn").removeClass('active_popup');
			info_opened = 0;
		}

	});

	$("#info_btn").mouseover(function() {

		$("#info_btn span").show();

	});

	$("#info_btn").mouseout(function() {

		$("#info_btn span").hide();

	});

	//links btn
	$("#links_btn").click(function() {

		if(links_opened == 0)
		{
			initPhoto();

			$(".photo_links").hide();
			$(".photo_info").hide();

			$("#info_btn").removeClass('active_popup');
			$("#notice_btn").removeClass('active_popup');

			$(".photo_links").show();
			$("#photo_links_close").click(function() {
				initAnnotate();
				$(".photo_links").hide();
				$("#links_btn").removeClass('active_popup');
				links_opened = 0;
			});

			$(".photo_links").css('margin-left', ( $(".photo_links").parent().width() - 474 ) / 2 );
			$(".photo_links").css('margin-top', ( $(".photo_links").parent().height() - $(".photo_links").height() ) / 2 );

			$(".photo_links").draggable();

			$("#links_btn").addClass('active_popup');

			info_opened = 0;
			links_opened = 1;
			is_cropping = 0;
			is_annotate = 0;
		}
		else
		{
			initAnnotate();

			$(".photo_links").hide();
			$("#links_btn").removeClass('active_popup');
			links_opened = 0;
		}

	});

	$("#links_btn").mouseover(function() {

		$("#links_btn span").show();

	});

	$("#links_btn").mouseout(function() {

		$("#links_btn span").hide();

	});

	//full btn
	$("#full_btn").mouseover(function() {

		$("#full_btn span").show();

	});

	$("#full_btn").mouseout(function() {

		$("#full_btn span").hide();

	});

	//notice btn
	$("#notice_btn").click(function() {

		initAnnotate();

	});

	$("#notice_btn").mouseover(function() {

		$("#notice_btn span").show();

	});

	$("#notice_btn").mouseout(function() {

		$("#notice_btn span").hide();

	});

	//crop btn
	$("#crop_btn").click(function() {

		initCrop();

	});

	$("#crop_btn").mouseover(function() {

		$("#crop_btn span").show();

	});

	$("#crop_btn").mouseout(function() {

		$("#crop_btn span").hide();

	});

	//fav btn
	$("#fav_btn").click(function() {

		switchFavoriteIcon($(this));

	});

	$("#fav_btn").mouseover(function() {

		$("#fav_btn span").show();

	});

	$("#fav_btn").mouseout(function() {

		$("#fav_btn span").hide();

	});

}

function initSeriesBtns()
{
	//info btn
	$("#info_btn").click(function() {

		if(info_opened == 0)
		{
			initPhoto();

			$(".photo_info").hide();

			$("#links_btn").removeClass('active_popup');
			$("#notice_btn").removeClass('active_popup');

			$(".photo_info").show();
			$("#photo_info_close").click(function() {
				initAnnotate();
				$(".photo_info").hide();
				$("#info_btn").removeClass('active_popup');
				info_opened = 0;
			});

			$(".photo_info").css('margin-left', ( $(".orig-image-series-first").width() - 474 ) / 2 );
			$(".photo_info").css('margin-top', ( $(".orig-image-series-first").height() - $(".photo_info").height() ) / 2 );

			$(".photo_info").draggable();

			$("#info_btn").addClass('active_popup');

			links_opened = 0;
			info_opened = 1;
			is_cropping = 0;
			is_annotate = 0;
		}
		else
		{
			$(".photo_info").hide();
			$("#info_btn").removeClass('active_popup');
			info_opened = 0;
		}

	});

	$("#info_btn").mouseover(function() {
		$("#info_btn span").show();
	});

	$("#info_btn").mouseout(function() {
		$("#info_btn span").hide();
	});

	//fav btn
	$("#fav_btn").click(function() {

		switchFavoriteIcon($(this));

	});

	$("#fav_btn").mouseover(function() {

		$("#fav_btn span").show();

	});

	$("#fav_btn").mouseout(function() {

		$("#fav_btn span").hide();

	});

}
