/*----------------------------------------------------------------------------------------------*  
*S. Mohammed ALsharaf  
*Date: 02/05/2007  
*Email:Satrun77@hotmail.com  
*-----------------------------------------------------------------------------------------------*/  
// onload start the function Start to create DOM element  
if(FastInit) {  
	//FastInit.addOnLoad(StartIt);  
} else {  
	//Event.observe(window, 'load', StartIt);  
} 

var Path = "edit_site/";
//var Site = location.href;
var Site = "http://www.namukulu-motel.nu/";

function StartIt()  
{    

}   
//-----------------------------------------------------------------------------------------------------*  
// the following are the functions used in the script above............................................  
//-----------------------------------------------------------------------------------------------------*  
function ViewLoginForm()
{
	var html = "<form action='' method='post' id='mini_login_form'>";
	html+= "<div class='mini_login_title'>Log in<span><img onclick='CloseLogin()' src='/"+Path+"images/close.png' width='16' height='16' alt='Close' title='Close' /></span></div>"
	html+= "<div class='mini_input'>"
	html+= "<label>Username</label>"
	html+= "<input type='text' name='mini_Username' id='mini_Username' size='20' />"
	html+= "</div>"
	html+= "<div class='mini_input'>"
	html+= "<label>Password</label>"
	html+= "<input type='password' name='mini_Password' id='mini_Password' size='20' />"
	html+= "</div>"
	html+= "<div class='mini_btn'>"
	html+= "<input type='button' value='~ Log in ~' name='mini_Loginbtn' id='mini_Loginbtn' onclick='GoLogin()' />"
	html+= "</div>"
	html+= "</form><div id='mini_login_bg'></div><div id='mini_login_loading' style='display:none'><img src='/"+Path+"images/ani.gif' /><br />Wait Please...</div>";

	$('mini_login').innerHTML = html;

	var arrayPageSize = getPageSize();
	$('mini_login').style.width = arrayPageSize[0]-2 + 'px';
	$('mini_login').style.height = arrayPageSize[1]-2 + 'px';
	$('mini_login_bg').style.width = arrayPageSize[0]-2  + 'px';
	$('mini_login_bg').style.height = arrayPageSize[1]-2  + 'px';
	
	new Effect.Appear('mini_login');  
}
function CloseLogin()
{
	new Effect.Fade('mini_login'); 
	//$('mini_login').style.width =  '0px';
	//$('mini_login').style.height ='0px';
	//$('mini_login_bg').style.width ='0px';
	//$('mini_login_bg').style.height = '0px';
	//$('mini_login').innerHTML = "";
}
function GoLogin()
{
	showLoad();
	new Ajax.Request("/"+Path+'ajax/ajaxRequests.php',  
	{  
		method:'post',  
		parameters: $('mini_login_form').serialize(true) ,   
		onSuccess: function(transport)  
		{  
			var response = transport.responseText || "You could not login. Please try again later.";  
			hideLoad();
			if(response != "Yes")
			{
				
				alert(response); 
			}
			else
			{
				window.location='?Mode=Edit-Page-Content';	
			}
			
		},  
		onFailure: function(){ hideLoad(); aler("You could not login. Please try again later."); }  
	});  
}
function showLoad()
{
	new Effect.Appear('mini_login_loading'); 
}
function hideLoad()
{
	$('mini_login_loading').style.display = 'none';
	//new Effect.Fade('mini_login_loading'); 
}
function UpdatePageContent()
{
	var content = tinyMCE.getContent();
	
	content = content.replace("%0A", "");
	
	var pageval = $('page').value;
	var titleval = $('pageTitle').value;
	var oldtxt = $('editpagbtn').value;
	SavingAnimation('On',oldtxt);
	new Ajax.Request("/"+Path+'ajax/ajaxRequests.php',  
	{  
		method:'post',  
		parameters: {mini_PageTxxt:content , update :'update_page_content' , page : pageval , pageTitle : titleval} ,   
		onSuccess: function(transport)  
		{  
			var response = transport.responseText || "Page content could not be saved.";  
			SavingAnimation('Off',oldtxt);
			alert(response); 
		},  

		onFailure: function(){ SavingAnimation('Off',oldtxt); aler("Page content could not be saved."); }  
	});

}
function SavingAnimation(status,oldtxt)
{
	if(status == "On")
	{
		$('editpagbtn').disabled = true;
		$('editpagbtn').value = "Saving ...";
	}
	else
	{
		$('editpagbtn').disabled = false;
		$('editpagbtn').value = oldtxt;
	}
}

function LogOut()
{
	new Ajax.Request("/"+Path+'ajax/ajaxRequests.php',  
	{  
		method:'post',  
		parameters: {logout:'True'} ,   
		onSuccess: function(transport)  
		{  
			var response = transport.responseText || "You could not log out.";  
			if(response == 'Yes')
			{
				window.location=Site;
			}
		},  

		onFailure: function(){ aler("You could not log out."); }  
	});
}
// X and Y of an element by by Peter-Paul Koch & Alex Tingle  
function findPosX(obj)  
  {  
    var curleft = 0;  
    if(obj.offsetParent)  
        while(1)  
        {  
          curleft += obj.offsetLeft;  
          if(!obj.offsetParent)  
            break;  
          obj = obj.offsetParent;  
        }  
    else if(obj.x)  
        curleft += obj.x;  
    return curleft;  
  }  
  
  function findPosY(obj)  
  {  
    var curtop = 0;  
    if(obj.offsetParent)  
        while(1)  
        {  
          curtop += obj.offsetTop;  
          if(!obj.offsetParent)  
            break;  
          obj = obj.offsetParent;  
        }  
    else if(obj.y)  
        curtop += obj.y;  
    return curtop;  
  }  

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.com
//
function getPageScroll(){

	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}
