function bookmark()
{
	var title = "Президент 2010 - Выборы президента в Украине";
	var url = "http://president2010.org/";
	if (window.sidebar) {
		window.sidebar.addPanel(title, url, "");
	} else if(window.opera && window.print) {
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	}else if(document.all) {
		window.external.AddFavorite(url, title);
	}else {
		alert( lang['add_bookmark_alert'] );
	}
	return false;
}

function jqmShow( id )
{
	var maskHeight = $(document).height();  
	var maskWidth = $(window).width();  
	$('#overlay').css({'width':maskWidth,'height':maskHeight});  
	$('#overlay').fadeTo(0,0.2);
	$('#overlay').fadeIn(0);
	var height = $(window).scrollTop() + $(window).height()/2 - $(id).height()/2;  
	var width = $(window).scrollLeft() + $(window).width()/2 - $(id).width()/2;  
	$(id).css('top', height );
	$(id).css('left', width );
	$(id).fadeIn(100);
}

function messageShow( message )
{
	$('#message_window div').text(message);
	jqmShow('#message_window');
}

$(function() {
	$("#main_stats ul li div strong").css('opacity','0.6');
	
	if ($("#element_comment_add_form").length>0) {
		$.get("/elem/comment_add_form", function(data) {
			$("#element_comment_add_form").replaceWith(data);
		});
	}
	$("#add2fav").click( function(){
		bookmark();
		return false;
	});
	
	$(document).bind( 'keydown', 'ctrl+left', function() {
		
		if ( $('#prev').length > 0 )
		{
			$('#prev').click();
			location.href = $('#prev').attr('href');			
		}
	});

	$(document).bind( 'keydown', 'ctrl+right', function() {
		if ( $('#next').length > 0 )
		{
			$('#next').click();
			location.href = $('#next').attr('href');
		}
	});	
	
	$('#overlay').click(function(){  
		$(this).hide();
		$('.modal_window').hide();
	});
	
	$('.modal_window_close').live("click", function() {  
		$(this).parents('.modal_window').hide();
		$('.modal_window').hide();
		$('#overlay').hide();
		return false;
	});
	
	$('#link_print').click(function(){
		window.print();
		return false;
	});

	$('#link_send_friend').click(function(){
		jqmShow('#send_friend_window');
		return false;
	});
		
	$('#send_friend_submit').click(function(){
		$("#send_friend_loader").css("display", "none");
		var errors_div = $("#send_friend_window").find(".errors");
		$(errors_div).text('');
		var email = $('#input_email').val();
		var text = $('#input_text').val();
		var errors = false;
		if ( email == '' )
		{
			$(errors_div).append(lang['send_friend_email_empty'] + '<br />');
			errors = true;
		}
		if ( text == '' )
		{
			$(errors_div).append(lang['send_friend_text_empty'] + '<br />');
			errors = true;
		}
		if ( !errors )
		{
			$("#send_friend_loader").css("display", "block");
			$.post("/news/send_friend",
				{
					'data[email]': email,
					'data[text]': text
				},
				function( msg )
				{
					$("#send_friend_loader").css("display", "none");
					if ( msg.result == 'success' )
					{
						$('.modal_window').hide();
						$('#overlay').hide();						
					}
					else if ( msg.result == 'error' )
					{
						$.each( msg.errors, function( index, item )
						{
							$(errors_div).append(item + '<br />');
						});
					}
					else
					{
						alert( 'Unknown error. Contact administrator' );
					}
				}, 'json'
			);
		}
		return false;
	});
	
	$('.comment_delete').live("click", function() {
		if ( !confirm( 'Удалить комментарий?' ))
		{
			return false;
		}
		var comment_object = $(this).parents('.comment_object:last');
		$('.comment_object .comment_body').remove();
		result = $(comment_object).attr('id').match('^.*_([0-9]+)$');
		if ( result )
		{
			var id = result[1];
			$.post("/comment/delete", { 'data[id]' : id }, function( msg ) {
				if ( msg.status == 'success' )
				{
					$.each( msg.ids, function( index, item )
					{
						$("#comment_"+item).remove();
					});
				}
			}, 'json');
		}
		return false;
	});
	
	$('.comment_hide').live("click", function() {
		var comment_object = $(this).parents('.comment_object:last');
		$('.comment_object .comment_body').remove();
		result = $(comment_object).attr('id').match('^.*_([0-9]+)$');
		if ( result )
		{
			var id = result[1];
			$.post("/comment/hide", { 'data[id]' : id }, function( msg ) {
				if ( msg.status == 'success' )
				{
					$.each( msg.ids, function( index, item )
					{
						$("#comment_"+item).prepend('<p class="comment_hidden"><strong>Скрыт</strong></p>');
						$("#comment_"+item+" .comment_hide").text('Отобразить');
						$("#comment_"+item+" .comment_hide").addClass("comment_show");
						$("#comment_"+item+" .comment_hide").removeClass("comment_hide");
					});
				}
			}, 'json');
		}
		return false;
	});
	$('.comment_show').live("click", function() {
		var comment_object = $(this).parents('.comment_object:last');
		$('.comment_object .comment_body').remove();
		result = $(comment_object).attr('id').match('^.*_([0-9]+)$');
		if ( result )
		{
			var id = result[1];
			$.post("/comment/show", { 'data[id]' : id }, function( msg ) {
				if ( msg.status == 'success' )
				{
					$.each( msg.ids, function( index, item )
					{
						$("#comment_"+item+" .comment_hidden").remove();
						$("#comment_"+item+" .comment_show").text('Скрыть');
						$("#comment_"+item+" .comment_show").addClass("comment_hide");
						$("#comment_"+item+" .comment_show").removeClass("comment_show");
					});
				}
				else if ( msg.status == 'error' )
				{
					alert( msg.error );
				}
			}, 'json');
		}
		return false;
	});
	
	$('.link_add_comment').live("click", function() {
		var comment_object = $(this).parents('.comment_object:last');
		var comment_form = $('#form_add_comment').html();
		$('.comment_object .comment_body').remove();
		$(comment_object).append('<div class="comment_body">' + comment_form + '</div>');
		$('#form_add_comment').find("[name]").each( function(){
			$(comment_object).find("[name=" + $(this).attr("name") + "]").val($(this).val());
		});
		results = $(comment_object).attr('id').match('^(.*)_([0-9]+)$');
		if ( results )
		{
			$(comment_object).find("[name='object']").val(results[1]);
			$(comment_object).find("[name='object_id']").val(results[2]);
		}
		return false;
	});
	
	$(".comment_submit").live("click", function() {
		var loader = $(this).parents(".comment_body").children("img");
		var visible = loader.is(':visible');
		if ( visible )
		{
			return;
		}		
		var comment_body = $(this).parents(".comment_body");
		var id = $(this).parents(".comment_body").find("[name='object_id']").val();
		var post_data = {};
		$(this).parents(".comment_body").children("[name]").each( function(){
			post_data["data[" + $(this).attr("name") + "]"] = $(this).val();
		});
		post_data['data[object_url]'] = page_url;
		loader.show();
		$.post("/comment/add", post_data, function( msg ) {
			loader.hide();
			$(comment_body).find(".errors").text('');
			if ( msg.status == 'success' )
			{
				$("#form_add_comment").find("[name=name]").val( $(comment_body).find("[name=name]").val() );
				$("#form_add_comment").find("[name=email]").val( $(comment_body).find("[name=email]").val() );
				$("#form_add_comment").find("[name=website]").val( $(comment_body).find("[name=website]").val() );
				$('.comment_object .comment_body').remove();
				if ( msg.level == 1 )
				{
					$("#comments").show();
					$("#comments").append( msg.html );
				}
				else
				{
					$("#comment_last_" + id).append( msg.html );
				}
				location.href = '#comment_' + msg.id;
			}
			else if ( msg.status == 'error' )
			{
				$.each( msg.errors, function( index, item )
				{
					$(comment_body).find(".errors").append('<p>' + item + '</p>');
				});
			}
			else
			{
				alert( 'Ошибка добавления комментария' );
			}
			}, 'json'
		);
		
		return false;
	});	
});