// JavaScript Document
var Site = {PageList:[]};
var ajxURL = {url:"/logic.aspx"};
var ajx = new Ajax.Request(ajxURL);

function noAuth(){
	if(QueryString.site){
		$("login").style.display = "";
		$("login").style.backgroundColor = "#F00";
		setTimeout("$('login').style.backgroundColor = '#609BD6';",500);
		$("login").style.borderColor = "#800";
		setTimeout("$('login').style.borderColor = '#00376D';",500);
		$("u").value = "";
		$("p").value = "";
		$("u").focus();
	}else{
		Msg.alert("Site not loaded","No site ID could be found.");
	}
};
function doLogin(){
	ajx.send({f:"loadSite",u:$v("u"),p:$v("p"),sid:QueryString.site},ajxLoader)
};
function logout(){
	document.cookie = 'AuthHash=; expires=Sat, 1 Jan 2000 00:00:00 UTC; path=/';
	document.location = document.location;
}
function changePassword(){
	Focus.load("340x230", "Change Password", "<iframe width=\"100%\" height=\"100%\" id=\"FocusFrame\" frameborder=\"0\" src=\"password.htm\"></iframe>",
	[
		{value:"Save & Close",onclick:function(){parent.$("FocusFrame").contentWindow.SAVE()}},
		{value:"Cancel",onclick:function(){parent.Focus.clear()}, selected:true}
	]);
	toggleToolBar("close");
}
function loadPage(){
	$("u").onfocus = function(){
		this.style.backgroundColor = "#FFF";
	};
	$("u").onblur = function(){
		this.style.backgroundColor = "#CCC";
	};
	$("p").onfocus = function(){
		this.style.backgroundColor = "#FFF";
	};
	$("p").onblur = function(){
		this.style.backgroundColor = "#CCC";
	};
	if(!(Detect.browser == "Firefox" && Detect.version >= 2 ) && !(Detect.browser == "Explorer" && Detect.version >= 7)){
		alert("You are using an unsupported web browser. Please use either Internet Explorer 7 or Firefox 2.\n\nYou will now be redirected...");
		if(Detect.browser == "Explorer") document.location.href = "http://www.microsoft.com/windows/downloads/ie/getitnow.mspx";
		else document.location.href = "http://www.mozilla.com/en-US/firefox/";
		return false;
	};
	Editor.obj = $("editor");
	Editor.bar = $("editorBar");
	Editor.title = $("editorBarTitle");
	Editor.clear();
	ContextMenu.obj = $("ContextMenu");
	Tooltip.obj = $("tooltip");
	Tooltip.hide();
	Tooltip.add($("Publishing"),"Go to &quot;Site Publishing&quot;");
	Msg.obj = $("msg");
	Msg.title = $("msgtitle");
	Msg.content = $("msgbody");
	Msg.buttons = $("msgbuttons");
	Focus.obj = $("focusBox");
	Focus.border = $("focusBorder");
	Focus.title = $("focusTitle");
	Focus.body = $("focusBody");
	Focus.buttons = $("focusButtons");
	
	// Snip Save Button
	$("snipSave").onclick = function(){
		$("editPane").contentWindow.SAVE();
	};
	
	$("snipClose").onclick = function(){
		Editor.clear();
	}
	
	$("handle").onclick = toggleToolBar;
	
	if(QueryString.site) $("site").src = "/intro/" + QueryString.site + ".htm";
	
	ajx.send({f:"loadSite"},ajxLoader);
};
function toggleToolBar(setTo){
	if (setTo != ("open" && "close")) setTo = ($("toolbar").style.left == "0px") ? "close" : "open";
	switch(setTo){
		case "open":
			$("handle").src = "images/int-barRight-min.png";
			$("toolbar").style.left = "0px";
		break
		case "close":
			$("handle").src = "images/int-barRight-max.png";
			$("toolbar").style.left = "-270px";
		break
	}
};
function loadMe(obj){
	$("site").src = "site/" + Site.SiteId + "/" + obj.id + "?nocache=" + (new Date()).getTime();
	toggleToolBar("close");
}
function addSnip(areaAndPageId){
	Msg.alert("Add a Snippet", "Would you like to create a new Snippet, or add an existing one?",[
		{value: "New",onclick:function(){
			Msg.hide();
			Editor.Key = areaAndPageId;
			Editor.Type = "newsnip";
			Editor.load();
		}},
		{value: "Existing",onclick:function(){
			Msg.hide();
			Editor.Key = areaAndPageId;
			Editor.Type = "existingsnip";
			Editor.load();
		}},
		{value: "Cancel",onclick:function(){Msg.hide()}}
	]);
}
function editSnip(SnipId){
	ajx.send({f:"getSnip",d:SnipId},ajxEditSnip);
}
function removeSnipFromPage(PageId, SnipId){
	Msg.alert("Remove Snippet?","Are you sure you want to remove this snippet?",[
		{value:"Yes",onclick: function(){
			ajx.send({f:"removeSnipFromPage",d:PageId + "!|!" + Site.ActiveEditArea + "!|!" + SnipId},ajxChangePageSnips);
		}},
		{value:"Cancel", selected:true, onclick: function(){
			Msg.hide();
		}}
	]);
}

var Editor = {
	setSize: function(x){
		var vals = x.split("x");
		var newW = parseInt(vals[0]);
		var newH = parseInt(vals[1]);
		if((newW+60) > windowWidth()) newW = windowWidth()-60;
		if((newH+70) > windowHeight()) newH = windowHeight()-70;
		this.bar.style.width = newW + "px";
		this.bar.style.marginLeft = "-" + Math.round(newW/2) + "px";
		this.bar.style.marginTop = "-" + (Math.round(newH/2)+15) + "px";
		
		this.obj.style.width = newW + "px";
		this.obj.style.height = newH + "px";
		this.obj.style.marginLeft = "-" + Math.round(newW/2) + "px";
		this.obj.style.marginTop = "-" + (Math.round(newH/2)-5) + "px";
	},
	Key:0,
	Name:"",
	Type:0,
	Data:{},
	show: function(){
		this.obj.style.display = "";
		this.bar.style.display = "";
	},
	hide: function(){
		this.obj.style.display = "none";
		this.bar.style.display = "none";
	},
	clear: function(){
		this.bar.style.display = "none";
		this.obj.style.display = "none";
		this.Key = 0;
		this.Name = "";
		this.Type = 0;
		this.Data = {};
	},
	setTitle: function(x){
		this.title.innerHTML = x;
	},
	load: function(){
		if(this.Key != 0){
			this.title.innerHTML = "Loading editor...";
			this.obj.innerHTML = "<iframe width=\"100%\" height=\"100%\" id=\"editPane\" frameborder=\"0\" src=\"/edit/" + this.Type + "/\"></iframe>";
			this.obj.style.display = "";
			this.bar.style.display = "";
			$("snipSave").style.display = "";
		}else{
			Msg.alert("Not Authorized","You do not have permission to edit this.");
		}
	},
	setSaveTip: function(x){
		Tooltip.add($("snipSave"), x);
	},
	hideSaveButton: function(){
		$("snipSave").style.display = "none";
	},
	setCloseTip: function(x){
		Tooltip.add($("snipClose"), x);
	}
}
var Tooltip = {
	hide: function(){
		this.obj.style.display = "none";
		this.obj.innerHTML = "";
	},
	show: function(e,txt,myXY){
		this.obj.innerHTML = txt;
		
		var xy = [].concat(myXY(e));
		xy.each(function(x){return (x + 15)});
		
		this.obj.style.left = "0px";
		this.obj.style.top = "0px";
		
		this.obj.style.visibility = "hidden";
		this.obj.style.display = "";
		//alert(xy[0] + " :: " + this.obj.offsetWidth + " :: " + windowWidth());
		var x = (xy[0] + this.obj.offsetWidth > windowWidth()) ? xy[0] - this.obj.offsetWidth - 20 : xy[0];
		
		this.obj.style.left = x + "px";
		this.obj.style.top = xy[1] + "px";
		
		this.obj.style.visibility = "visible";
	},
	move: function(e,myXY){
		var xy = [].concat(myXY(e));
		xy = xy.each(function(x){return (x + 15)});
		if(xy[0] + this.obj.offsetWidth > windowWidth()) xy[0] = (xy[0] - this.obj.offsetWidth - 20);
		this.obj.style.left = xy[0] + "px";
		this.obj.style.top = xy[1] + "px";
	},
	add: function(el,txt,myXY){
		if(myXY == null) myXY = mouseXY;
		el.onmouseover = function(e){
			Tooltip.show(e,txt,myXY);
		}
		el.onmousemove = function(e){
			Tooltip.move(e,myXY);
		}
		el.onmouseout = function(){
			Tooltip.hide();
		}
	}
};
var ContextMenu = {
	hide: function(){
		this.obj.style.display = "none";
		this.obj.innerHTML = "";
	},
	show: function(e,mWidth,menu,myXY){
		var menuIds = [];
		this.obj.innerHTML = "";
		this.obj.style.width = mWidth;
		[].concat(menu).each(function(e){
			if(e=="-"){
				ContextMenu.obj.innerHTML += "<hr size=\"1\" noshade=\"noshade\" width=\"" + (mWidth - 10) + "\" align=\"center\" />";
			}else if(isString(e)){
				ContextMenu.obj.innerHTML += "<div>" + e + "</div>";
			}else{
				// menu = [{id, html, etc}]
				var itemId = e.id + "_" + (new Date()).getTime();
				ContextMenu.obj.innerHTML += "<div button id='" + itemId + "'>" + e.html + "</div>";
				e.html = null;
				menuIds.push({id:itemId,opt:e});
			}
		});
		
		// apply options
		menuIds.each(function(e){
			Object.inherit($(e.id),e.opt);
		});
		
		var xy = [].concat(myXY(e));
		
		this.obj.style.left = "0px";
		this.obj.style.top = "0px";
		
		this.obj.style.visibility = "hidden";
		this.obj.style.display = "";
		//alert(xy[0] + " :: " + this.obj.offsetWidth + " :: " + windowWidth());
		var x = (xy[0] + this.obj.offsetWidth > windowWidth()) ? xy[0] - this.obj.offsetWidth : xy[0];
		
		var totalVertHeight = xy[1] + this.obj.offsetHeight;
		var y = (totalVertHeight > windowHeight()) ? xy[1] - (totalVertHeight - windowHeight()) : xy[1];
		
		this.obj.style.left = x + "px";
		this.obj.style.top = y + "px";
		
		this.obj.style.visibility = "visible";
	}
};
var mouseXY = function(){
	if (document.all){
		return function(e){
			if (!e) var e = window.event;
			return [
				e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft,
				e.clientY + document.body.scrollTop	+ document.documentElement.scrollTop
			]
		}
	}else if (document.getElementById){
		return function(e){
			if (!e) var e = window.event;
			return [e.pageX, e.pageY]
		}
	}else{
		return function(e){ return [0,0] };
	}
}();
var Focus = {
	open: false,
	load: function(size, title, body, buttons){
		this.open = true;
		if(buttons==null) buttons = [{value:"Close",onclick:function(){Focus.clear()}, selected:true}];
		var w = parseInt(size.split("x")[0]);
		var h = parseInt(size.split("x")[1]);
		if((w+50) > windowWidth()) w = windowWidth()-100;
		if((h+50) > windowHeight()) h = windowHeight()-100;
		
		this.obj.style.width = w + "px";
		this.obj.style.height = h + "px";
		this.obj.style.marginLeft = "-" + (Math.round(w/2)+5) + "px";
		this.obj.style.marginTop = "-" + (Math.round(h/2)+5) + "px";
		this.body.style.width = (w-20) + "px";
		this.body.style.height = (h-80) + "px";
		
		$("dropclothwhite").style.width = this.body.style.width;
		$("dropclothwhite").style.height = this.body.style.height;
		
		w += 50;
		h += 50;
		this.border.style.width = (w+10) + "px";
		this.border.style.height = (h+10) + "px";
		this.border.style.marginLeft = "-" + (Math.round(w/2)+5) + "px";
		this.border.style.marginTop = "-" + (Math.round(h/2)+5) + "px";
		this.border.style.display = "";
		
		this.title.innerHTML = title;
		this.body.innerHTML = body;
		this.buttons.innerHTML = "";
		var buttonOptions = [];
		buttons.each(function(e){
			var bId = e.value + (new Date()).getTime();
			buttonAction = (e.selected == true) ? "submit" : "button";
			Focus.buttons.innerHTML += "<input type=\"" + buttonAction + "\" id=\"" + bId + "\" class=\"alertButton\">";
			buttonOptions.push({id:bId, opt:e});
		});
		buttonOptions.each(function(e){
			Object.inherit($(e.id),e.opt);
		});
		this.obj.style.display = "";
		$("dropcloth").style.display = "";
		this.onClear = function(){};
	},
	clear: function(){
		this.open = false;
		this.title.innerHTML = "";
		this.body.innerHTML = "";
		this.buttons.innerHTML = "";
		this.obj.style.display = "none";
		this.border.style.display = "none";
		if(!Msg.open) $("dropcloth").style.display = "none";
		this.onClear();
	}
}
function windowWidth(){
	if(document.body.offsetWidth) return document.body.offsetWidth;
	if(window.innerWidth) return window.innerWidth;
}
function windowHeight(){
	if(document.body.offsetHeight) return document.body.offsetHeight;
	if(window.innerHeight) return window.innerHeight;
}
var Msg = {
	open: false,
	show: function(){
		this.open = true;
		$("dropcloth").style.display = "";
		this.obj.style.display = "";
	},
	hide: function(){
		this.open = false;
		if(!Focus.open) $("dropcloth").style.display = "none";
		this.obj.style.display = "none";
	},
	alert: function(title,body,buttons){
		if(buttons==null) buttons = [{value:"Ok",onclick:function(){Msg.hide()}, selected:true}];
		this.title.innerHTML = title;
		this.content.innerHTML = body;
		this.buttons.innerHTML = "";
		var buttonOptions = [];
		var buttonFocus = "";
		buttons.each(function(e){
			var bId = e.value + (new Date()).getTime();
			Msg.buttons.innerHTML += "<input type=\"button\" id=\"" + bId + "\" class=\"alertButton\">";
			buttonOptions.push({id:bId, opt:e});
		});
		buttonOptions.each(function(e){
			if(e.opt.selected) buttonFocus = e.id;
			Object.inherit($(e.id),e.opt);
		});
		this.show();
		if(buttonFocus != "") $(buttonFocus).focus();
	}
};
var Progress = {
	show:function(){
		Progress.update("",0);
		$("progressInfo").style.display = "";
		$("progressCloth").style.display = "";
	},
	update:function(title,data){
		$("progressTitle").innerHTML = title;
		$("progressData").innerHTML = data + "%";
		$("progressData").style.width = data + "%";
		if(data == 100)
			Progress.hide();
	},
	hide:function(){
		$("progressInfo").style.display = "none";
		$("progressCloth").style.display = "none";
	}
};
var updatePagesData = [];
var updatePagesTotal = 0;
function updatePages(a){
	if(isArray(a)){
		updatePagesData = a.compact();
		updatePagesTotal = updatePagesData.length;
		ajx.send("f=savePage&d=" + updatePagesData.pop() + "!|!0",ajxUpdatePage)
	}else{
		alert("invalid array, was not able to update pages");
	}
}
function LaunchPublishing(){
	Focus.load("300x400", "Site Publishing", "<iframe width=\"100%\" height=\"100%\" frameborder=\"0\" src=\"publishing.htm\"></iframe>");
};
function refreshPublishing(listArr){
	if(isCollection(listArr)){
		Site.PagesToPublish = (listArr.length != 0).toString();
		$("Publishing").style.display = (Site.PagesToPublish.toLowerCase() == "true" && Site.CanPublish.toLowerCase() == "true") ? "" : "none";
	}else{
		if(Site.CanPublish.toLowerCase() == "true") ajx.send("f=getPagesToPublish", ajxPageListHandler);
	}
};
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
function launch(o){
	toggleToolBar("close");
	Editor.Key = 1;
	Editor.Type = o.id.split(" ").join("");
	Editor.load();
}
window.$ = $;
document.onclick = function(){
	ContextMenu.hide();
}
