// Nuggetz Javascript
// Ollie Phillips

function editor(nugget,nuggetzsession){

	editorContent = document.getElementById(nugget).innerHTML;
	
	// Editor form
	var editorForm= '';	
	editorForm += '<div id="nuggetzlogo"><img src="nuggetz/images/nuggetz-logo.gif" alt="Nuggetz Logo" /></div><div id="nuggetzmenu"><a href="http://www.nuggetz.co.uk/help.htm" target="_blank">Help</a> | <a href="http://www.nuggetz.co.uk/" target="_blank">About Nuggetz</a> | <a href="http://www.nuggetz.co.uk/premium.htm">Buy Full Version</a></div>';
	editorForm += '<div style="clear:both;"></div><div id="mceeditor"></div><br/><div id="panelcontrol"><input type="image" value="Save changes" src="nuggetz/images/save.gif" onclick="ajaxSave(\''+ nugget +'\',\''+ nuggetzsession +'\');" />&nbsp;&nbsp;';
	editorForm += '<input type="image" value="Cancel" id="closePanel" src="nuggetz/images/close.gif" onclick="return closeEditor();" /></div><div id="panelstatus">Loaded: '+ nugget +' nugget..</div>';

	// Mask background	
    $('<div id="editorbackground"/>').appendTo('body').show();
	
	// Hide on click outside
	$('#editorbackground').click(function() {
		$('#editordialog').fadeOut(500,function(){
			tinyMCE.execCommand("mceRemoveControl", true, "mceeditor");
			$('#editordialog').remove();
		});
		$('#editorbackground').fadeOut(500,function(){
			$('#editorbackground').remove();
		});
	});
	
	// Create Editor dialog box
    $('<div id="editordialog" />').prependTo('body');
	$('#editordialog').append(editorForm).fadeIn(2000);
	document.getElementById('mceeditor').innerHTML = editorContent;
	tinyMCE.execCommand("mceAddControl", true, "mceeditor");
	
}

function login(fail){

	// Login form
	var loginForm = '';
	loginForm += '<div style="text-align:center;"><img src="nuggetz/images/nuggetz-logo.gif" alt="Nuggetz Logo" /></div>';	
		
	if(fail){
		loginForm += '<div style="text-align:center"><span class="loginerror">Login details incorrect</span></div><br/>';
	}else{
		loginForm += '<br/>';
	}
	
	loginForm += '<div class="nuggetzloginmessage"><form name="nuggetzlogin" action="nuggetz/admin/login.php" method="post">';
	loginForm += 'Username : <input type="text" name="nuggetzuser" id="nuggetzuser" style="width:140px;"/><br/><br/>';
	loginForm += 'Password : <input type="password" name="nuggetzpassword" id="nuggetzpassword" style="width:140px;"/><br /><br/>';
	loginForm += '<div id="loginPanelcontrol"><input type="image" name="submit" id="nuggetzsubmit" value="submit" src="nuggetz/images/login.gif" /></div></form><br/></div>';
	
	// Mask background	
    $('<div id="loginbackground"/>').appendTo('body').show();
	
	// Hide on click outside
	$('#loginbackground').click(function() {
		$('#logindialog').fadeOut(500,function(){
			$('#logindialog').remove();
		});
		$('#loginbackground').fadeOut(500,function(){
			$('#loginbackground').remove();
		});
	});
	
	// Create Login dialog box
    $('<div id="logindialog" />').prependTo('body');
	$('#logindialog').append(loginForm).fadeIn(2000);
	
}

function logout(){

	// Logout form
	var logoutForm = '';	
	logoutForm += '<div style="text-align:center;"><img src="nuggetz/images/nuggetz-logo.gif" alt="Nuggetz Logo" /></div><br />';
	logoutForm += '<div class="nuggetzlogoutmessage">Please confirm you wish to log out of Nuggetz</div><br /><br/>';
	logoutForm += '<div id="logoutPanelcontrol"><input type="image" id="closeLogoutPanel" src="nuggetz/images/logout.gif" onclick="return logoutConfirm();" /></div><br/>';

	// Mask background	
    $('<div id="loginbackground"/>').appendTo('body').show();
	
	// Hide on click outside
	$('#loginbackground').click(function() {
		$('#logindialog').fadeOut(500,function(){
			$('#logindialog').remove();
		});
		$('#loginbackground').fadeOut(500,function(){
			$('#loginbackground').remove();
		});
	});

	// Create Logout dialog box
    $('<div id="logindialog" />').prependTo('body');
	$('#logindialog').append(logoutForm).fadeIn(2000);

}

function logoutConfirm(){
	location.href='nuggetz/admin/logout.php';
}

function closeEditor(){
	// Hide on click close/cancel
	$('#editordialog').fadeOut(500,function(){
		tinyMCE.execCommand("mceRemoveControl", true, "mceeditor");
		$('#editordialog').remove();
	});
	$('#editorbackground').fadeOut(500,function(){
		$('#editorbackground').remove();
	});

}

// Ajax save form
var time_variable;
var thenugget; 
function getXMLObject()  //XML OBJECT
{
   var xmlHttp = false;
   try {
     xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
   }
   catch (e) {
     try {
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
     }
     catch (e2) {
       xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
     }
   }
   if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
     xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
   }
   return xmlHttp;  // Mandatory Statement returning the ajax object created
}
 
var xmlhttp = new getXMLObject();	
 
function ajaxSave(nugget,nuggetzsession) {
  var getdate = new Date();
  thenugget = nugget;
  thesession = nuggetzsession;		  
  if(xmlhttp) { 
	var tinyMCEContent = tinyMCE.get('mceeditor').getContent();  
    var pageValue = escape(tinyMCEContent); 
    xmlhttp.open("POST","nuggetz/admin/ajaxsave.php?nugget="+nugget+"&sid="+thesession+"&amp;dummy=" + escape(getdate.getTime()),true); 	
    xmlhttp.onreadystatechange  = handleServerResponse;
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send("pagevalue="+pageValue); //Posting 
	
  }
}
 
function handleServerResponse() {

   if (xmlhttp.readyState == 4) {
     if(xmlhttp.status == 200) {
	
	var a_p = "";
	var d = new Date();

	var curr_hour = d.getHours();
	
	if (curr_hour == 0){
   		curr_hour = 12;
   	}
		
	var curr_min = d.getMinutes();
	curr_min = curr_min + "";

	if (curr_min.length == 1){
   		curr_min = "0" + curr_min;
   	}

	var curr_sec = d.getSeconds();
	curr_sec= curr_sec + "";

	if (curr_sec.length == 1){
   		curr_sec = "0" + curr_sec;
   	}
		
    document.getElementById(thenugget).innerHTML=xmlhttp.responseText; //Update the HTML Form element 
	document.getElementById('panelstatus').innerHTML = 'Saved: '+ thenugget +' at ' + curr_hour + ":" + curr_min + ":" + curr_sec + '..';
     }
     else {
        alert("Error during Save attempt. Please try again");
     }
   }
}