//////////////////////
//  OVER CONTENTS
////////////////////////

dojo.require("dojo.dnd.Mover");
dojo.require("dojo.dnd.Moveable");
dojo.require("dojo.dnd.move");

var blackFadeDur = 500;
function showBlackPanel(finalval,high){
	finalval = finalval==null?.8:finalval;
	high = high==null?false:true;
	var bp = dojo.byId('blackpanel'+(high?'high':''));
	bp.style.display = 'block';
	fixBlackPanel();
	dojo.animateProperty({
		node: "blackpanel"+(high?'high':''),
		duration:blackFadeDur,
		properties: { opacity: {start:0,end:finalval } }
	}).play();
}
function hideBlackPanel(onlyhigh){
	var bp2 = dojo.byId('blackpanelhigh');
	//bp.style.display = 'none';
	if(!onlyhigh){
		var bp = dojo.byId('blackpanel');
		dojo.animateProperty({
			node: "blackpanel",	duration:blackFadeDur,
			properties: {opacity: {start:bp.style.opacity,end:0 } }}).play();
		setTimeout(function(){bp.style.display = 'none';},blackFadeDur);
	}
	dojo.animateProperty({
		node: "blackpanelhigh",	duration:blackFadeDur,
		properties: {opacity: {start:bp2.style.opacity,end:0 } }}).play();
	setTimeout(function(){bp2.style.display = 'none';},blackFadeDur);
}
function fixBlackPanel(){
	var bp = dojo.byId('blackpanel');
	var tempstate = bp.style.display;
	bp.style.display = 'none';
	//hideBlackPanel();
	var pss = getPageSize();
	//bp.style.width = pss[0]+"px";
	//bp.style.height = pss[1]+"px";
	bp.style.height = Math.max(dojo.byId("main").offsetHeight,pss[1])+"px";
	bp.style.display = tempstate;
	var bph = dojo.byId('blackpanelhigh');
	tempstate = bph.style.display;
	bph.style.display = 'none';
	//bp.style.width = pss[0]+"px";
	bph.style.height = pss[1]+"px";
	bph.style.display = tempstate;
}
function showOverContents(high){
	high = high==null?false:true;
	var oc = dojo.byId('overcontents'+(high?'high':''));
	oc.style.display = 'block';
	fixOverContents(high);
}
function fixOverContents(high,dontgototop){
	var oc = dojo.byId('overcontents'+(high?'high':''));
	//var op = dojo.byId('overpreview');
	var pss = getPageSize();
	//var psc = getPageScroll();
	//var psc = getPageScroll();
	//oc.style.top = (psc[1] + (pss[3] / 10))+"px";
	var owid = getElementWidth("overcontents"+(high?'high':''));
	var npos = (pss[0]/2)-(owid/2);
	oc.style.left = npos+"px";
	var mainph = dojo.byId('mainph');
	if(!dontgototop) mainph.scrollTop = 0;
	//op.style.width = (pss[0]-60)+"px";
}
function closeOverContents(onlyhigh){
	hideBlackPanel(onlyhigh);
	if(!onlyhigh){
		var container = dojo.byId('overcontents');
		container.style.display='none';
		container.innerHTML = "";
	}
  	var container2 = dojo.byId('overcontentshigh');
	container2.style.display='none';
	container2.innerHTML = "";
  	//var previewer = dojo.byId('overpreview');
	//previewer.style.display='none';
	//dojo.byId('previewtitle').innerHTML = "";
	//dojo.byId('previewcode').innerHTML = "";
	//dojo.byId('previewextra').innerHTML = "";
	var bp = dojo.byId('blackpanel');
	bp.onclick=null;
}



var modalFadeDur = 500;
var lastonclose;
function showModal(text,onclose){
	var modalw = dojo.byId('modalw');
	var modalcont = dojo.byId('modalcont');
	var modaltext = dojo.byId('modaltext');
	modaltext.innerHTML = text;
	modalw.style.display = 'block';
	dojo.animateProperty({
		node: "modalw",
		duration:modalFadeDur,
		properties: { opacity: {start:0,end:1 } }
	}).play();
	var pss = getPageSize();
	modalw.style.height = Math.max(dojo.byId("main").offsetHeight,pss[1])+"px";
	var owid = getElementWidth(modalcont);
	var ohei = getElementHeight(modalcont);
	var nposx = (pss[0]/2)-(owid/2);
	var nposy = (pss[1]/2)-(ohei/2);
	nposy = 300;
	modalcont.style.left = nposx+"px";
	modalcont.style.top = nposy+"px";
	lastonclose = onclose;
}
function closeModal(){
	var modalw = dojo.byId('modalw');
	dojo.animateProperty({
		node: "modalw",
		duration:modalFadeDur,
		properties: { opacity: {start:1,end:0 } }
	}).play();
	setTimeout(function(){modalw.style.display = 'none';},blackFadeDur);
	if(lastonclose!=null) eval(lastonclose);
}


function makeOverpreviewerDragable(prevId){
	var previewer = dojo.byId(prevId==null?'overpreview':prevId);
	if(previewer!=null){
		var parts = dojo.query(".corner",previewer).concat(dojo.query(".qpborder",previewer)).concat(dojo.query(".previewtitle",previewer));
		for(var i=0;i<parts.length;i++){
			new dojo.dnd.Moveable(previewer, {handle: parts[i]});
		}
	}
}









function loadOverContents(url,onload){
	showBlackPanel();
  	var container = dojo.byId('overcontents');
	container.style.display='';
	container.innerHTML = getLoadingHTML();
	fixOverContents();
	dojo.xhrGet({
		url: url,
		onload: onload,
		load: function(responseObject, ioArgs){
				if(responseObject==null){ this.error(); return; }
				container.innerHTML = responseObject;
				fixBlackPanel();
				fixOverContents();
				if(this.onload!=null) this.onload();
				return responseObject;
			},
		error: function(response, ioArgs){
			console.log("ERROR LOADING EDITOR",response, ioArgs);
			container.innerHTML = getLoadingErrorHTML(true);
			//editRecord(lastediturl);
			return response;
		},
		handleAs: "text"
	});
}