var notifies = new Array();
$(function (){
	load_messages(); 
	
	$( "#dialog:ui-dialog" ).dialog( "destroy" );
	
	
	$( ".add" ).button({
		icons: { primary: "ui-icon-plus" },text: true
	});
	$( ".add_category" ).button({
		icons: { primary: "ui-icon-plus" },text: true
	});
	
});

function load_messages(){
	$.getJSON('/base/messages', function(data){
		if(data != null)
		{
			$( "#dialog:ui-dialog" ).dialog( "destroy" );
			
			if(data.type == 'check')
				var type = 'ui-icon-circle-check';
			else if(data.type == 'info')
				var type = 'ui-icon-info';
			else
				var type = 'ui-icon-alert';
				
			message(type, data.text);
		}
	});
}

function message(mtype, text){
	if(mtype == 'check')
		var type = 'ui-icon-circle-check';
	else if(mtype == 'info')
		var type = 'ui-icon-info';
	else
		var type = 'ui-icon-alert';
				
	var $dialog = $('<div></div>').html('<div id="message-window" title="Wiadomość"><p><span class="ui-icon '+type+'" style="float:left; margin:0 7px 50px 0;"></span>'+text+'</p></div>')
		.dialog({
			width:500,
			modal: true,
			buttons: {
				Ok: function() {
					$( this ).dialog( "close" );
				}
			}
		});
}

function updateTips( t ) {
	var tips = $( ".validateTips" );
	tips
		.text( t )
		.addClass( "ui-state-highlight" );
	setTimeout(function() {
		tips.removeClass( "ui-state-highlight", 1500 );
	}, 500 );
}

function checkLength( o, n, min, max ) {
	if ( o.val().length > max || o.val().length < min ) {
		o.addClass( "ui-state-error" );
		updateTips( "Długość pola " + n + " musi być pomiędzy " +
			min + " i " + max + "." );
		return false;
	} else {
		return true;
	}
}

function checkRegexp( o, regexp, n ) {
	if ( !( regexp.test( o.val() ) ) ) {
		o.addClass( "ui-state-error" );
		updateTips( n );
		return false;
	} else {
		return true;
	}
}

function add_category(module_name){
	var name = $( "#name" ),
		allFields = $( [] ).add( name );

	$( "#new-category-form" ).dialog({
		autoOpen: true,
		width: 500,
		modal: true,
		buttons: {
			"Zapisz": function() {
				var bValid = true;
				allFields.removeClass( "ui-state-error" );
				bValid = bValid && checkLength( name, "name", 3, 50 );
				
				if (bValid) {
					$.post("/admin/category/save", { name : name.val(), module : module_name }, function(data){
						data = $.parseJSON(data);
						$("#category_id").append('<option value="'+data.category.id+'">'+data.category.name+'</option>');
					});
					$(this).dialog( "close" );
				}
			},
			"Anuluj": function() {
				$( this ).dialog( "close" );
			}
		},
		close: function() {
			allFields.val( "" ).removeClass( "ui-state-error" );
		}
	});
}


function edit_window(type, id, field){
	$( "#edit-window" ).dialog({
		autoOpen: true,
		height: "auto",
		width: 600,
		modal: true,
		buttons: {
			"Zapisz": function() {
				notify_add('saving','loading','Zapisywanie...');
				var content = $('#edit_window_content').html();
				content = content.replace(/<br>/gi, '<br />');
				if(type == 'news'){
					if(field == 'text'){
						$.post("/admin/news/save", {quick : true, id : id, text : content}, function(data) {
							data = $.parseJSON(data);
							if(data.status == 'error')
								message('error',data.message);
							if(field == 'text_ext')
								$(".news_"+id+"_ext").html(content);
							else
								$(".news_"+id).html(content);
							notify_hide('saving');
						});
					}else{
						$.post("/admin/news/save", {quick : true, id : id, text_ext : content}, function(data) {
							data = $.parseJSON(data);
							if(data.status == 'error')
								message('error',data.message);
							if(field == 'text_ext')
								$(".news_"+id+"_ext").html(content);
							else
								$(".news_"+id).html(content);
							notify_hide('saving');
						});
					}
				}
				else
					alert('nieznany typ');
				$(this).dialog( "close" );
			},
			"Anuluj": function() {
				$( this ).dialog( "close" );
			}
		},
		close: function() {}
	});
	$("#edit-window").show();
}

function notify_add(name, type, text){
	var icon = '';
	if(type == 'loading')
		icon = '';
	
	notifies[name] = icon+''+text;
	var content = '';
	for(var i in notifies){
		content += notifies[i]+'<br />';
	}
	$("#notify .notify_content").html(content);
	if($("#notify:visible").length == 0)
		$("#notify").slideDown('slow');
}

function notify_hide(name){
	delete notifies[name];
	var content = '';
	if(notifies.length == 0)
		setTimeout(function(){$("#notify").slideUp('slow');},1000);
	else
	{
		for(var i in notifies){
			content += notifies[i]+'<br />';
		}
		$("#notify .notify_content").html(content);
	}
}

function save_comment(comment_id, module, item_id, username){
	$.post("/comment/save", {id : comment_id, module: module, item_id : item_id, text : $("#text").val(), username : username}, function(data) {
		data = $.parseJSON(data);
		message('check',data.message);
		var tpl = '<div class="comment_'+data.comment.id+'">';
		tpl += '<div class="header" style="border-bottom:1px solid black;">';
		tpl += 'Autor: '+data.comment.username; 
		tpl += 'Dodano: '+data.comment.create_date; 
		tpl += '</div>';
		tpl += '<div class="comment">'+data.comment.text+'</div>';
		tpl += '</div>';
		$(".comments").html(tpl+''+$(".comments").html());
	});
}

function comment_remove(id){
	$.get("/comment/delete/"+id, function(data) {
		$(".comment_"+id).fadeOut();
	});
}
