/*
===============================================================================

Copyright 2008 Simon MacDonald

===============================================================================

This file is part of LSDEditor.

    LSDEditor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    LSDEditor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with LSDEditor in the file COPYING.txt  If not, 
    see <http://www.gnu.org/licenses/>.

===============================================================================
Script Title:		|	lsedit.js
Author:				|	Simon MacDonald
Date:				|	28/10/2008
Version:			|	1.0
===============================================================================
Description: This script file provides javascript functionality to interact with PHP
serverside scripts that insert content into the tinyMCE Editor and save edited 
content to the server.
===============================================================================

Update History	
===============================================================================
03062009 - changes to getContent() to fix Safari bug

===============================================================================
*/

/* 
===============================================
	global variables
===============================================
*/

var text1,text2, page, pagetitle, pagetext, config; 


function init(params)

{
config = params;
}
/* 
===============================================
	Functions that respond to unsuccessful Ajax calls
===============================================
*/
function reportError(request) {

	var response = request.responseText;
	var d = $('response');
		d.innerHTML = "998: Unknown Error: " + response;
}
/* 
===============================================
	Functions that respond to successful Ajax calls
===============================================
*/
function updatePage(request) {

	var response = request.responseText;
	var d = $(pagetitle);
		d.innerHTML = response;
}

function sendupdate(request) {
	var ed = tinyMCE.get('editbox');
	var rt = request.responseText;
	rt = decodeURIComponent(rt);
	ed.setProgressState(0); // Hide progress
	ed.setContent(rt);
}

function hideText () {
	var d = $("response");
	d.innerHTML = "";
/*	alert("hidden");*/
}
function saved(request) {
	var d = $("response");
	d.innerHTML = "Changes saved...";
	setTimeout(hideText(),2000);
	getContent(pagetitle) ;

}
/* 
===============================================
	Mootools functions
===============================================
*/
/*function getContent(page)  
{


	var demo_path = config['jsPath']+"content/";
	//We can use one Request object many times.
	var req = new Request.HTML
		({ 
		 	url:demo_path+page+'.html', 
			onSuccess: function(html) 
				{
					//Clear the text currently inside the results div.
					$('hotnews').set('text', '');
					//Inject the new DOM elements into the results div.
					$('hotnews').adopt(html);
				},
			//Our request will most likely succeed, but just in case, we'll add an
			//onFailure method which will let the user know what happened.
			onFailure: function() 
			{
				$('response').set('text', 'getContent Function: The request to obtain the page text failed.');
			}
		}).send();
}*/
function getContent(page)  
{

	var params = "pagename="+page; //parameters for the getcontent.php
	var contentPath = config.jsPath +"getcontent.php";
	//We can use one Request object many times.
	var req = new Request.HTML
		({ 
		 	url:contentPath, 
			onSuccess: function(html) 
				{
					//Clear the text currently inside the results div.
					$('hotnews').set('text', '');
					//Inject the new DOM elements into the results div.
					$('hotnews').adopt(html);
					console.log("getContent successful");
				},
			//Our request will most likely succeed, but just in case, we'll add an
			//onFailure method which will let the user know what happened.
			onFailure: function(xhr) 
			{
				err = '<h1>getContent Function: The request to obtain the page text failed:</h1> ' ;
				$('response').adopt(err);
				console.log(err);
			}
		}).send(params);
}
function sendContent(page)  
{
	var ed = tinyMCE.get('editbox');
	/*ed.setProgressState(1);*/
	var	ptxt = ed.getContent();
	ptxt = encodeURIComponent(ptxt);
	pagecontent = "pagename=" + editpage + "&pagetext=" + ptxt;
	sendurl = config.jsPath + 'sendcontent.php?';
		
	//We can use one Request object many times.
	var req = new Request.HTML
		({ 
		 	url:sendurl, 
			method:'post',
			onSuccess: function(html) 
				{
					//Clear the text currently inside the results div.
					var mess = "<h2 style='color:green'>Changes saved ...</h2> ";
					var r = $('response')
					$('response').innerHTML = mess;
					var t=setTimeout("$('response').set('text', '')",2000);	
				},
			//Our request will most likely succeed, but just in case, we'll add an
			//onFailure method which will let the user know what happened.
			onFailure: function(html) 
			{
				$('response').set('text', 'The send request failed');
			}
		}).send(pagecontent);
}
/*
===============================================
	Functions that make Ajax calls
===============================================
*/

function oldsendContent() 
{
	var ed = tinyMCE.get('editbox');
	/*ed.setProgressState(1);*/
	var	ptxt = ed.getContent();
	ptxt = encodeURIComponent(ptxt);
	pageparams = "pagename=" + editpage + "&pagetext=" + ptxt;

	var url = config["jsPath"] + "sendcontent.php";
	var request = new Ajax.Request(
					url, 
					{
							method: 'post',
							parameters: pagecontent,
							onSuccess: saved,
							onFailure: reportError
					}
		);

}
