//$(document).ready(function() {
//	$(".fbox").fancybox();
//});
//
//function toggleEditor(id) {
//	if (!tinyMCE.getInstanceById(id))
//		tinyMCE.execCommand('mceAddControl', false, id);
//	else
//		tinyMCE.execCommand('mceRemoveControl', false, id);
//}

var jQTapi = null;

function xtest () {
    var api = $("ul.tabs").tabs(0); 
    api.next();
}

function adminInit () {
	textInit ();
}

function textInit () {
	if ( $('#mainDivText').html() != '' ) console.log ('yra teksto');
	else $('#mainDivText').load ( '/admin/textArea' , {} , tinyMCEinit);
}


function textLoad ( id ) {
	textInit();
	$('#tinyText').load('/admin/loadText/'+ id );
	 var api = $("ul.tabs").tabs(0); 
	 api.next();
}

function textShow () {
	textInit ();
	$('.panes > div').fadeOut('fast');
	$('#mainDivText').fadeIn();
}


function insertImage ( src ) {
	$('#tinyText').tinymce().execCommand('mceInsertContent',false, '<img src="' +src + '" alt="image from div" />');
}

function tinyMCEinit () {
	$("#tinyText").tinymce({
	    script_url : "/lib/js/tinyMce/tiny_mce.js",
	    theme : "advanced",
	    plugins : "table,pagebreak",
		theme_advanced_buttons1 : "save,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,|,outdent,indent,blockquote,|,styleselect,formatselect,fontselect,fontsizeselect",
		theme_advanced_buttons2 : "hr,removeformat,tablecontrols,|,undo,redo,|,sub,sup,|,link,unlink,anchor,image,cleanup,code,|,forecolor,backcolor",
		theme_advanced_buttons3 : "",
	    theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
	    pagebreak_separator : "<!-- my page break -->",
		cleanup_on_startup : true,
		entity_encoding : "raw",
		convert_urls : false,
		content_css : "/edit.css",
		setup : function(ed) {
	      // Register example button
	      ed.addButton('image', {
	         title : 'Image manager',
	         onclick : function() {
	    	  fileBoxShow();
	           //ed.windowManager.alert('Hello world!! Selection: ' + ed.selection.getContent({format : 'text'}));
	         }
	      });
	      ed.addButton('save', {
	    	  title: 'Save page',
	    	  onclick: function() {
	     	  x = ed.getContent();
 
	          $.ajax({
		           type: "POST",
		           url:  $('#postLink').val(),
		           data: {"id" : $("#textID").val(), "title" : $("#wsTitle").val(), "text" : $("#tinyText").html() },
		           success: function(msg){
		           	alert  ( msg );
		           }
		        });
	     	  
	      }
	      });	
	   }

	});	
	
}


/*	     	  
 x = ed.selection.getContent();
  console.log(x);
	  */


function fUpload ( id, path ) {
	
	var button = $("#uploadButton-" + id), interval;
	new AjaxUpload(button,{
	 	action: "/files/fupload",
	 	data: {	path : path, id : id },
	//	name: 'myfile',
		onComplete: function(file, response){
	 		$("#files-" + id ).append ( response ) ;						
		}
	});
return;
	
	div = "uploadButton-" + id;
	new AjaxUpload( div ,  {
		 	action: "/files/fupload",
		 	data: {	path : path, id : id },
		 	onComplete: function( file , response ) {
				$("#files-" + id ).append ( response ) ;
				}
	} );
}

function adminUpload ( div, path ) {
	new AjaxUpload(div ,  {
		 	action: "/files/upload",
		 	data: {	path : path },
		 	onComplete: function( file , response ) {
				$("#fileManager").append ("<b>" + file + "</b> uploaded to: <b>" + path + "</b><br/><b>" + response + "</b><br/>") ;
				fileTreeShow ( path );
				}
	} );
}



function fileBoxShow () {
	$("#overlayBox").load("/files/ajtree",{}, function() {
		$("#overlayBox").overlay({ 
		    // custom top position 
		    top: 20, 
		    // some expose tweaks suitable for facebox-looking dialogs 
		    expose: { 
		        // you might also consider a "transparent" color for the mask 
		        color: '#ffe', 
		        // load mask a little faster 
		        loadSpeed: 500, 
		        // highly transparent 
		        opacity: 0.5 
		    }, 
		    // disable this for modal dialog-type of overlays 
		    closeOnClick: true, 
		    // we want to use the programming API 
		    api: true 
		// load it immediately after the construction 
		}).load();
	});
}
 
 

function rowDown (r) {
  var i=r.parentNode.parentNode.rowIndex;
  var table = r.parentNode.parentNode.parentNode.parentNode;
  
  if (i < table.rows.length - 1) {
 	table.insertRow(i);
  	table.rows[i].innerHTML = table.rows[i+2].innerHTML;
  	table.deleteRow(i+2); 
  }
}

function rowUp (r) {
  var i=r.parentNode.parentNode.rowIndex;
  var table = r.parentNode.parentNode.parentNode.parentNode;
  if (i > 0) {
 	table.insertRow(i-1);
  	table.rows[i-1].innerHTML = table.rows[i+1].innerHTML;
  	table.deleteRow(i+1); 
  }
} 
		
function ajaxPost ( dv, url, ev ) {
	   fdata = $("#"+ dv + '-form').serialize();
	         $.ajax({
	           type: "POST",
	           url: url,
	           data: fdata,
	           success: function(msg){
	           	 if ( ! ev ) $("#" + dv).html(msg);
	           	 else {
	           		code = $.base64Decode(ev);
	           		eval ( code );
	           	 }
	           }
	        });
	}

function  addComment ( id ) {
 //for ( i = 0; i < 4; i++)
		$('#commentAll div:first').slideUp('slow', function () { $('#commentAll div:first').remove(); } );
 	};
 	
 	
/*
 * 	File manager  
 */

function fileTreeShow (  ) {
	$("#fileTree").fileTree( { 
		root: "/", 
		olderEvent: "click",
		multiFolder: false,
		script: "/files/genTree/" },
		function (path, type) {
			 $.post ( "/files/fileMan/", { path: path, type: type }, function (msg) {
			$("#fileManager").html(msg) } )
			}
		)
 	}

