$(document).ready(function(){

/*----------------------------ADMIN PANEL----------------------*/

$('#tabs').tabs();
/*---fckeditor--*/
 // default settings
 $.fck.config = {
		//toolbar: 'Basic',
  path: './fckeditor/',
  height: 750
 };
 // initialize
$('textarea#artedittextarea').fck();
$('textarea#newsedittextarea').fck();
$('#artinputdiv').hide();
$('#newsinputdiv').hide();
$('#ajax-loader').hide();

/*-----------------------NEWSY------------------*/
editednow=0;
$("#showaddnews").click(function(){
editednow=0;
$.fck.setHTML('newsedittextarea', ' ')
$('#titleinput').val('');
$('#newsinputdiv').show();
});
$("span.news").click(function(){
id=$(this).attr('id');
editednow=id.replace('news', '');
$.get("index.php?e=admin&a=ajax&w=getnewstitle", {newsid: id }, function(data){
$('#titleinput').val(data);
});
 $.get("index.php?e=admin&a=ajax&w=getnews", {newsid: id }, function(data){
$.fck.setHTML('newsedittextarea', data) });
$('#newsinputdiv').show();
});
$('#submitnews').click(function(){
$.fck.update();
$.post("index.php?e=admin&a=ajax&w=editnews", {id: editednow, content: $('textarea#newsedittextarea').text(), title: $('#titleinput').val()},function(data){
if (editednow==0){
$("#managenews").append("<li><span  class='news' id='news"+data+"'>"+$('#titleinput').val()+"</span><span class='delnews' id='news"+data+"'><img src='page_cross.gif' />Usun</span></li>");
}
$('#newsinputdiv').hide();
		});
/*  $('form#articleeditform').ajaxSubmit({
   complete: function(r){
    alert(r.responseText);
   }
  });*/
 });
$('span.delnews').click(function(){alert('aa');
	id=$(this).attr('id');
	$.post("index.php?e=admin&a=ajax&w=delnews", {newsid: id} );
$(this).parent().hide();
$('#newsinputdiv').hide();
	})
/*-----------------------PODSTRONY--------------*/
editednow=0;
$(".article").click(function(){
id=$(this).attr('id');
editednow=id.replace('article', '');
 $.get("index.php?e=admin&a=ajax&w=getarticle", {articleid: id }, function(data){
$.fck.setHTML('artedittextarea', data) });
$('#artinputdiv').show();
});
$('#submitarticle').click(function(){
$.fck.update();
$.post("index.php?e=admin&a=ajax&w=editarticle", {id: editednow, content: $('textarea#artedittextarea').text()},function(data){
$('#artinputdiv').hide();
		});


/*  $('form#articleeditform').ajaxSubmit({
   complete: function(r){
    alert(r.responseText);
   }
  });*/


 });

/*------------------------VIDEO-----------------*/
$("#managevideos").sortable({
			update: function(e, ui){
				serial=$(this).sortable('serialize');
				$.ajax({
					url: "index.php?e=admin&a=ajax&w=videos",
					type: "POST",
					data: serial
					})
				}
		});
$("span#showaddvideo").click(function(){
$(this).next().toggle('fast');
});
$('span#addvideo').click(function(){
	href=$(this).prev().prev().find('input[name*="href"]').val();
	descr=$(this).prev().prev().find('input[name*="descr"]').val();
	title=$(this).prev().prev().find('input[name*="title"]').val();
	$.post("index.php?e=admin&a=ajax&w=addvideo", {href: href, descr: descr, title: title},function(data){
		$("#managevideos").append("<li id='video_"+data+"'><a href='index.php?e=video&id="+data+"'>"+title+"</a> - "+descr+" <span class='delvideo' id='video"+data+"'><img src='page_cross.gif' />Usun</span></li>");
		});
	$(this).parent().hide();
	$(this).prev().prev().find('input[name*="href"]').val('');
	$(this).prev().prev().find('input[name*="descr"]').val('');
	$(this).prev().prev().find('input[name*="title"]').val('');
	})
$('span.delvideo').click(function(){
	id=$(this).attr('id');
	$.post("index.php?e=admin&a=ajax&w=delvideo", {videoid: id} );
$(this).parent().hide();
	})

/*-----------------------LINKI------------------*/
$("img[alt*='editlink']").click(function(){
$(this).parent().next().toggle('fast');
});

/*-OK-*/
$('span.savelink').click(function(){
	linkid=$(this).parent().attr('id');
	href=$(this).prev().find('input[name*="href"]').val();
	descr=$(this).prev().find('input[name*="descr"]').val();
	alert(href+" "+descr+" "+linkid);
	encoded = encodeURIComponent(href)
	$.post("index.php?e=admin&a=ajax&w=editlink&linkid="+linkid, {href: encoded, descr: descr});
	$(this).parent().hide();
	})

$('span#dellink').click(function(){
linkid=$(this).parent().attr('id');
		/*	$.ajax({
				url: "index.php?e=admin&a=ajax&w=links&groupid="+groupid,
				type: "POST",
				data: serial
				})*/

})
$("img[alt*='addlink']").click(function(){
$(this).parent().next().toggle('fast');
});
$('span#saveaddedlink').click(function(){

})
$('span#addgroup').click(function(){
linkid=$(this).parent().attr('id');
href=$(this).prev().find('input[name*="href"]').val();
descr=$(this).prev().find('input[name*="descr"]').val();
		/*	$.ajax({
				url: "index.php?e=admin&a=ajax&w=links&groupid="+groupid,
				type: "POST",
				data: serial
				})*/

})
$('span#saveaddedgroup').click(function(){
linkid=$(this).parent().attr('id');
name=$(this).prev().find('input[name*="descr"]').val();
		/*	$.ajax({
				url: "index.php?e=admin&a=ajax&w=links&groupid="+groupid,
				type: "POST",
				data: serial
				})*/

})
$("img[alt*='delgroup']").click(function(){
groupid=$(this).parent().parent().attr('id');
		/*	$.ajax({
				url: "index.php?e=admin&a=ajax&w=links&groupid="+groupid,
				type: "POST",
				data: serial
				})*/

})
$("img[alt*='editgroup']").click(function(){
text=$(this).prev().prev('span').text();
$(this).prev().prev('span').replaceWith("<input type='text' value='"+text+"' />");
$(this).prev('span').show();
$(this).hide();
		/*	$.ajax({
				url: "index.php?e=admin&a=ajax&w=links&groupid="+groupid,
				type: "POST",
				data: serial
				})*/

})
$('span.savegroup').click(function(){
groupid=$(this).parent().parent().attr('id');
name=$(this).prev().val();
$(this).prev('input').replaceWith("<span class='portlet-title'>"+name+"</span>");
$(this).next('img').show();
$(this).hide();
		/*	$.ajax({
				url: "index.php?e=admin&a=ajax&w=links&groupid="+groupid,
				type: "POST",
				data: serial
				})*/

})

$('ul.managelinks').sortable({
		connectWith: ['ul'],
		update: function(e, ui){
			serial=$(this).sortable('serialize');
			groupid=$(this).attr("id");
			$.ajax({
				url: "index.php?e=admin&a=ajax&w=links&groupid="+groupid,
				type: "POST",
				data: serial
				})
	
				}
	});
	$(".column").sortable({
			connectWith: '.column',
			cancel: 'li',
			cancel: 'input',
			update: function(e, ui){
				serial=$(this).sortable('serialize');
				columnid=$(this).attr("id");
				$.ajax({
					url: "index.php?e=admin&a=ajax&w=linkgroups&columnid="+columnid,
					type: "POST",
					data: serial
					})
				}
		});

		$(".portlet").addClass("ui-widget ui-widget-content ui-helper-clearfix ui-corner-all")
			.find(".portlet-header")
				.addClass("ui-widget-header ui-corner-all")
				.prepend('<span class="ui-icon ui-icon-plusthick"></span>')
				.end()
			.find(".portlet-content");

		$(".portlet-header .ui-icon").click(function() {
			$(this).toggleClass("ui-icon-minusthick");
			$(this).parents(".portlet:first").find(".portlet-content").toggle();
		});

		$(".column").disableSelection();


});
