$(function(){

	jQuery.fn.encHTML = function() { 
	  return this.each(function(){ 
		var me   = jQuery(this); 
		var html = me.html(); 
		me.html(html.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;')); 
	  }); 
	}; 
	 
	jQuery.fn.decHTML = function() { 
	  return this.each(function(){ 
		var me   = jQuery(this); 
		var html = me.html(); 
		me.html(html.replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>')); 
	  }); 
	}; 
	 
	jQuery.fn.isEncHTML = function(str) { 
	  if(str.search(/&amp;/g) != -1 || str.search(/&lt;/g) != -1 || str.search(/&gt;/g) != -1) 
		return true; 
	  else 
		return false; 
	}; 
	 
	jQuery.fn.decHTMLifEnc = function(){ 
	  return this.each(function(){ 
		var me   = jQuery(this); 
		var html = me.html(); 
		if(jQuery.fn.isEncHTML(html)) 
		  me.html(html.replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>')); 
	  }); 
	};
	
	// Lightbox
	
	$('#content_text a.get_picture').livequery(function(){ 
			
		$('#content_text a.get_picture').lightBox({
			imageLoading: '/js/images/loading.gif',
			imageBtnClose: '/js/images/close.gif',
			imageBtnPrev: '/js/images/prev.gif',
			imageBtnNext: '/js/images/next.gif',
			containerResizeSpeed: 350
		});
			
	});

	// Overlay
	
    $("#start_overlay[rel]").overlay({

		close: 'a.preview_overlay,a.close_overlay,a.save_overlay',

        onBeforeLoad: function() {

			this.getBackgroundImage().expose({color: '#000'});
        },

        onClose: function() {
            $.expose.close();
        }

    });

	$(".preview_overlay").click(function() {
	
		tinyMCE.triggerSave(false,false);
		var text = $("#text").val();
		$("#content_text").html(text);

	});


	// Control favorites

	$(".save_overlay").click(function() {
		tinyMCE.triggerSave(false,false);
		var url_preview = "/ajax_save_text.php";
		
		window.setTimeout(function(){
			$('#admin_info').fadeOut(1000);
		}, 4000);
		
		window.setTimeout(function(){
			$('#admin_info').css("display","none");
		}, 5000);
		
		$.php(url_preview, $('form#admin_edit').formToArray(true));
		return false;

		$.ajax({

			url: url_preview,

			type: "POST",
		    data: $('form#admin_edit').formToArray(true),
		    dataType : "json",
		   
			beforeSend: function(){
				return php.beforeSend();
			},
			success: function(data, textStatus){   
				return php.success(data, textStatus);
			},
			error: function (xmlEr, typeEr, except) {
				return php.error(xmlEr, typeEr, except);                  
			},
			complete: function (XMLHttpRequest, textStatus) {              
				return php.complete(XMLHttpRequest, textStatus);
			}
		});
	});
	
});