var xmlHttp;
var type = getURLParam("type");
var page_id = getURLParam("page_id");

var tempX = 0;
var tempY = 0;

// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false;

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE);

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;

/*
$().ready(function() {
  $('#popup').jqDrag();
});
*/

window.onload=function()
{
	/*Nifty("div#pictureContainer","tl br medium");*/
}

function getMouseXY(e) {
	
	var IE = document.all?true:false
	
	if (IE) // grab the x-y pos.s if browser is IE
	{ 
		tempX = event.clientX + document.body.scrollLeft
		tempY = event.clientY + document.body.scrollTop
  	} 
	else // grab the x-y pos.s if browser is NS
	{  
		tempX = e.pageX
		tempY = e.pageY
  	}  
  	// catch possible negative values in NS4
	if (tempX < 0){tempX = 0}
	if (tempY < 0){tempY = 0} 
	
}
function loadOp(type,param)
{ 
	document.getElementById("loading").style.display = '';
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
	  alert ("Your browser does not support AJAX!");
	  return;
  	} 
	
	var url = 'components/insertPhoto/?type=page&siteId=' + param;	
	
	xmlHttp.open("post",url,true);
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.send('var=1');
	
}
function stateChanged() 
{ 
	if (xmlHttp.readyState==4)
	{ 
		document.getElementById("popupbody").innerHTML= xmlHttp.responseText;
		document.getElementById("loading").style.display = 'none';
	}
}
function miniloadOp(type,param1,param2)
{ 
	xmlHttp=GetXmlHttpObject();
	
	document.getElementById("minipopup").style.top=tempY + "px";
	document.getElementById("minipopup").style.left=tempX + "px";
	
	showhide('minipopup');
		
	if (xmlHttp==null)
	{
	  alert ("Your browser does not support AJAX!");
	  return;
  	} 
	if(type == 4) //create site
	{
		var url = 'components/createSite';		
	}
	else if(type == 5) //create page
	{
		var url = 'components/createPage?siteId=' + param1 ;	
	}
	else if(type == 6) //create folder
	{
		var url = 'components/createFolder?siteId=' + param1 + '&pageId=' + param2;	
	}
	
	xmlHttp.open("post",url,true);
	xmlHttp.onreadystatechange=ministateChanged;
	xmlHttp.send('var=1');
	
}
function ministateChanged() 
{ 
	if (xmlHttp.readyState==4)
	{ 
		document.getElementById("minipopupbody").innerHTML= xmlHttp.responseText;
		document.getElementById('minipopupclose').style.display = '';
	}
	else
	{
		document.getElementById("minipopupbody").innerHTML= 'loading...';
	}
}
function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
		// Firefox, Opera 8.0+, Safari
	  	xmlHttp=new XMLHttpRequest();
	}
	catch (e)
  	{
  		// Internet Explorer
  		try
    	{
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	}
  		catch (e)
    	{
    		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
  	}
	return xmlHttp;
}

function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (
aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return unescape(strReturn);
} 
function show(type) 
{
	if(type == 1)
	{
		var dv = document.getElementById('LayerOption');
		dv.style.display = '';
	}
	if(type == 2)
	{
		var dv = document.getElementById('LayerOption');
		dv.style.visibility = 'visible';
	}
} 

function hide(type) 
{
	if(type == 1)
	{
		document.getElementById('LayerOption').style.visibility = 'hidden';
	}
} 

function formSubmit(type)
{
	
	var name = document.getElementById("name").value;
	
	if(type == 1) //new page
	{
		//go to location
		window.location="engine.php?n=" + n + "&e=" + e + "&p=" + p + "&c=" + c + "&i=" + i + "&t=removeItem";
	}
	
}

function slideshowhide(div)
{
	var status = document.getElementById(div).style.display;
	
	if(status == 'none')
	{
		$('#'+ div).slideDown('medium');
		grayinout('true');
	}
	else
	{
		$('#'+ div).slideUp('medium');
		grayinout('false');
	}
}
function showhide(div)
{
	var status = document.getElementById(div).style.display;
	
	if(div == 'createFolder')
	{
		if(status == 'none')
		{
			document.getElementById(div).style.display = '';
			document.getElementById('bfolder').style.backgroundColor = '#FFFF99';
		}
		else
		{
			document.getElementById(div).style.display = 'none';
			document.getElementById('bfolder').style.backgroundColor = 'white';
		}
	}
	else if(div =='popup')
	{
		if(status == 'none')
		{
			x = screen.width;
			y = screen.height;
			document.getElementById(div).style.width = x/2 + "px";
			document.getElementById(div).style.height = y/2 + "px";
			document.getElementById(div + 'head').style.width = x/2 + "px";
			document.getElementById(div + 'head').style.height = 0.6/10 * y/2 + "px";
			document.getElementById(div + 'body').style.width = 8.8/10 * x/2 + "px";
			document.getElementById(div + 'body').style.height = 7.5/10 * y/2 + "px";
			document.getElementById(div).style.display = '';
		}
		else
		{
			document.getElementById(div).style.display = 'none';
		}	
	}
	else if(div =='minipopup')
	{
		if(status == 'none')
		{
			document.getElementById(div).style.display = '';
		}
		else
		{
			document.getElementById(div).style.display = 'none';
			document.getElementById('minipopupclose').style.display = 'none';
		}	
	}
}

function grayinout(type) 
{
   if(type == null)
   {
	  
	  $('#greyout').fadeIn('medium');
	  
   }
   else
   {
		$('#greyout').fadeOut('medium');
		$('#pictureContainer').fadeOut('medium');
		
   }
}
function showPicture(purl)
{
	x = screen.width;
	y = screen.height;

	document.getElementById('pictureContainer').style.top = y/12 + "px";
	
	$('#greyout').fadeIn('medium');
	$('#pictureContainer').fadeIn('medium');
	
	document.getElementById('pictureContainer').innerHTML = '<img onclick="" src="../phpThumb/phpThumb.php?src=../zcms/components/upload/' + purl +'&h=' + y/2 + '&f=png">';
}
function appendText(div,newValue) 
{
  	var oldValue = document.getElementById(div).innerHTML;
	
	var appendValue = '<img src="components/upload/' + newValue + '"width="100px"; height="100px" />';
	
	document.getElementById(div).innerHTML = oldValue + appendValue;
	
	document.getElementById('popupbody').innerHTML = '';
	showhide('popup');
}

