
window.domLoaded = false;
window.onDOM = function() {
	window.domLoaded = true;
	SuperDial.init();
};


window.onload = function() {
	if(!window.domLoaded)
		SuperDial.init();
}

SuperDialClass = function() {
	this.theme = "default";
	this.no_preview_image = "http://superdial.org/themes/"+this.theme+"/images/no-preview.png";
	this.no_theme_preview_image = "http://superdial.org/images/null.gif";
	this.defaultStatusMessage = "Welcome to <strong>SuperDial</strong>";
};


SuperDialClass.prototype.init = function() {
	
	this.boxwrap = document.getElementById("boxwrap");
	this.dialerbox = document.getElementById("dialerbox");
	this.loginbox = document.getElementById("loginbox");
	this.registerbox = document.getElementById("registerbox");
	this.accountbox = document.getElementById("accountbox");
	
	this.currentDrawn = this.dialerbox;
	
	document.getElementById("statusmessage").innerHTML = this.defaultStatusMessage;
	
	if(this.dialerbox.offsetWidth < (window.innerWidth?window.innerWidth:document.body.offsetWidth) || this.dialerbox.offsetHeight < (window.innerHeight?window.innerHeight:document.body.offsetHeight)) {
		document.body.style.overflow = "hidden";
		if(document.documentElement && document.documentElement.style)
			document.documentElement.style.overflow = "hidden";
	}
	
	if(!document.getElementById("mouseFollower")) {
		SuperDial.mouseFollower = document.createElement("div");
		SuperDial.mouseFollower.style.position = "absolute";
		SuperDial.mouseFollower.style.zIndex = "1000";
		if(document.all && parseFloat(navigator.appVersion) < 7)
			SuperDial.mouseFollower.style.filter = "alpha(opacity=50)";
		else
			SuperDial.mouseFollower.style.opacity = "0.5";
		SuperDial.mouseFollower.style.left = "-999em";
		SuperDial.mouseFollower.style.top = "-999em";
		SuperDial.mouseFollower.id = "mouseFollower";
		SuperDial.mouseFollower.className = "mouseFollower";
		document.body.appendChild(SuperDial.mouseFollower);
	}
	else
		SuperDial.mouseFollower = document.getElementById("mouseFollower");
	
	if(!document.getElementById("loadingCover")) {
		SuperDial.loadingCover = document.createElement("div");
		SuperDial.loadingCover.setAttribute("id", "loadingCover");
		document.body.appendChild(SuperDial.loadingCover);
	}
	else
		SuperDial.loadingCover = document.getElementById("loadingCover");
	SuperDial.loadingCover.className = "loadingCover";
	SuperDial.loadingCover.style.display = "none";
	SuperDial.loadingCover.innerHTML = "<span class=\"inner\">Please Wait...</span>";

	// Search Bar:
	document.getElementById("searchbutton_top").onclick = function(e) {
		if(!e)
			e=window.event;
		this.blur();
		this.parentNode.Submit();
		return nullEvent(e);
	};
	//document.getElementById("searchbutton_top").onmousedown = nullEvent;
	//document.getElementById("searchbutton_top").onmouseup = nullEvent;

	document.getElementById("search_keywords").onclick = function(e) {
		if(!e)
			e=window.event;
		this.focus();
		return true;
	};
	document.getElementById("search_keywords").onmousedown = function(){return true;};
	document.getElementById("search_keywords").onmousemove = function(){return true;};
	document.getElementById("search_keywords").onmouseup = function(){return true;};
	document.getElementById("search_keywords").oncontextmenu = function(){return true;};
	document.getElementById("search_keywords").onselectstart = function(){return true;};
	document.getElementById("search_keywords").focus();
	
	window.clearInterval(window.focustimeout);
	
	document.getElementById("loginbutton_top").onclick = function(e) {
		if(!e)e=window.event;
		if(SuperDial.drawingInProgress && SuperDial.drawingInProgress == true)
			return false;
		if(this.getText().toLowerCase()=="login") {
			document.getElementById("registerbutton_top").setText("register");
			document.getElementById("loginbutton_top").setText(" . . . ");
			document.getElementById('login_username').value = "";
			document.getElementById('login_password').value = "";
			document.getElementById('login_username').onkeydown = function(e) { if(!e)e=window.event; if((e.keyCode&&e.keyCode==13)||(e.which&&e.which==13)) document.getElementById('superdial_login').submit(); };
			document.getElementById('login_password').onkeydown = function(e) { if(!e)e=window.event; if((e.keyCode&&e.keyCode==13)||(e.which&&e.which==13)) document.getElementById('superdial_login').submit(); };
			SuperDial.onDrawComplete = function() {
				document.getElementById('login_username').focus();
				document.getElementById('loginbutton_top').setText('dialer');
				document.getElementById('superdial_login').setAttribute("action","javascript: void(0);");
				document.getElementById('superdial_login').submit = function() {
					SuperDial.setCursor("busy");
					var u = document.getElementById('login_username').value;
					var p = document.getElementById('login_password').value;
					var xmlhttpid = "login_"+Math.floor(Math.random()*999999);
					SuperDial.SendData("http://superdial.org/", "action=login&login_username="+u+"&login_password="+p+"&sid="+xmlhttpid, xmlhttpid, "SuperDial.LoginCallback(SuperDial.xmlhttp_postdata['"+xmlhttpid+"'].responseText);");
					SuperDial.defaultLoginStatusText = document.getElementById('login_status').innerHTML;
					document.getElementById('login_status').innerHTML = "attempting login...";
					return false;
				};
			};
			SuperDial.Draw(SuperDial.loginbox);
		}
		else if(this.getText().toLowerCase()=="dialer") {
			document.getElementById("loginbutton_top").setText(" . . . ");
			SuperDial.onDrawComplete = function() { document.getElementById('loginbutton_top').setText('login'); };
			SuperDial.Draw(SuperDial.dialerbox);
		}
		else if(this.getText().toLowerCase()=="logout") {
			document.getElementById("registerbutton_top").setText("settings");
			document.getElementById("loginbutton_top").setText(" . . . ");
			var xmlhttpid = "logout_"+Math.floor(Math.random()*999999);
			SuperDial.SendData("http://superdial.org/", "action=logout&sid="+xmlhttpid, xmlhttpid, "SuperDial.LogoutCallback(SuperDial.xmlhttp_postdata['"+xmlhttpid+"'].responseText);");
		}
		this.blur();
		if(e)
			return nullEvent(e);
	};
	document.getElementById("loginbutton_top").onmousedown = nullEvent;
	document.getElementById("loginbutton_top").onmouseup = nullEvent;
	
	
	document.getElementById("registerbutton_top").onclick = function(e) {
		if(SuperDial.drawingInProgress && SuperDial.drawingInProgress == true)
			return false;
		if(this.getText().toLowerCase()=="register") {
			document.getElementById("loginbutton_top").setText("login");
			document.getElementById("registerbutton_top").setText(" . . . . ");
			SuperDial.registerButtonAction = function(ihtml) {
				SuperDial.registerbox.innerHTML = ihtml;
				document.getElementById('register_username').value = "";
				document.getElementById('register_password').value = "";
				document.getElementById('register_password2').value = "";
				document.getElementById('captcha_usertext').value = "";
				document.getElementById('register_submit').onclick = function(e) { if(!e)e=window.event; SuperDial._register(); return window.nullEvent(e); };
				document.getElementById('register_username').onkeydown = function(e) { if(!e)e=window.event; if((e.which&&e.which==13)||(e.keyCode&&e.keyCode==13)){ SuperDial._register(); return window.nullEvent(e); } };
				document.getElementById('register_password').onkeydown = function(e) { if(!e)e=window.event; if((e.which&&e.which==13)||(e.keyCode&&e.keyCode==13)){ SuperDial._register(); return window.nullEvent(e); } };
				document.getElementById('register_password2').onkeydown = function(e) { if(!e)e=window.event; if((e.which&&e.which==13)||(e.keyCode&&e.keyCode==13)){ SuperDial._register(); return window.nullEvent(e); } };
				document.getElementById('captcha_usertext').onkeydown = function(e) { if(!e)e=window.event; if((e.which&&e.which==13)||(e.keyCode&&e.keyCode==13)){ SuperDial._register(); return window.nullEvent(e); } };
				SuperDial.onDrawComplete = function() {
					SuperDial._register = function() {
						SuperDial.setCursor("busy");
						var u = document.getElementById('register_username').value;
						var p = document.getElementById('register_password').value;
						var p2 = document.getElementById('register_password2').value;
						var captchausertext = document.getElementById('captcha_usertext').value;
						var captchatext = document.getElementById('captcha_text').value;
						var xmlhttpid = "register_"+Math.floor(Math.random()*999999);
						SuperDial.SendData("http://superdial.org/", "action=register&register_username="+u+"&register_password="+p+"&register_password2="+p2+"&captcha_usertext="+captchausertext+"&captcha_text="+captchatext+"&sid="+xmlhttpid, xmlhttpid, "SuperDial.RegisterCallback(SuperDial.xmlhttp_postdata['"+xmlhttpid+"'].responseText);");
						SuperDial.defaultRegisterStatusText = document.getElementById('register_status').innerHTML;
						document.getElementById('register_status').innerHTML = "attempting to register...";
						return false;
					};
					document.getElementById('register_username').focus();
					document.getElementById('registerbutton_top').setText('dialer');
					//document.getElementById('superdial_register').action = "javascript:%20;";
					document.getElementById('superdial_register').setAttribute("action","javascript:%20;");
					document.getElementById('superdial_register').setAttribute("onsubmit","return false;");
					//try{ document.getElementById('superdial_register').submit = function() { this.DoSubmit(); return false; }; } catch(err){}
					//try{ document.getElementById('superdial_register').Submit = function() { this.DoSubmit(); return false; }; } catch(err){}
					//document.getElementById('superdial_register').submit = function() {
				};
				SuperDial.Draw(SuperDial.registerbox);
			};
			var xmlhttpid = "register_getdials_"+Math.floor(Math.random()*999999);
			SuperDial.GetData("http://superdial.org/?ext=registerboxcontents", xmlhttpid, "SuperDial.registerButtonAction(SuperDial.xmlhttp_data['"+xmlhttpid+"'].responseText);");
		}
		else if(this.getText().toLowerCase()=="dialer") {
			document.getElementById("registerbutton_top").setText(" . . . . ");
			SuperDial.onDrawComplete = function() { document.getElementById('registerbutton_top').setText(SuperDial.auth==true?"settings":"register"); };
			SuperDial.Draw(SuperDial.dialerbox);
		}
		else if(this.getText().toLowerCase()=="settings") {
			document.getElementById("registerbutton_top").setText(" . . . . ");
			SuperDial.settingsButtonAction = function(data) {
				var response = {result:false,message:'Communication Error'}
				if(data && data.length>1)
					try{ response = eval('('+data+')'); }catch(e){ response={result:false,message:'Parse Error: '+e.message}; }
				
				if(!response || !response.result || response.result===false) {
					alert("Could not open account settings. "+response.message);
					document.getElementById('registerbutton_top').setText('settings');
					return false;
				}
				
				var currentTheme = document.getElementById("superdial_style").getAttribute("href").replace(/^\/themes\//mgi,'').replace(/\/style\.css$/mgi,'');
				
				var b = SuperDial.accountbox;
				
				b.innerHTML = "";
				
				var themebox = document.createElement("div");
				themebox.className = "settings_area";
				b.appendChild(themebox);
				
				var themetitle = document.createElement("h3");
				themetitle.className = "settings_title";
				themetitle.innerHTML = "Choose a Theme";
				b.appendChild(themetitle);
				
				var themelist = document.createElement("ul");
				themelist.className = "themes_list";
				themebox.appendChild(themelist);
				
				SuperDial.themePicker_themes = [];
				
				var img,label,wrap;
				for(var x=0; x<response.themes.length; x++) {
					response.themes[x];
					wrap = document.createElement("li");
					wrap._themeName = response.themes[x].name;
					wrap.className = "themes_item" + (response.themes[x].name==currentTheme?" current":"");
					wrap.onclick = function() {
						SuperDial.ChangeTheme(this._themeName);
						for(var x=0; x<SuperDial.themePicker_themes.length; x++) {
							SuperDial.themePicker_themes[x].wrap.className = SuperDial.themePicker_themes[x].wrap.className.replace(/\scurrent/mgi,"");
						}
						this.className = this.className.replace(/\scurrent/mgi,"")+" current";
						var ctxt=this;
						function doBlur(){ctxt.blur();}
						window.setTimeout(doBlur,1);
					};
					wrap.onmouseover = wrap.onfocus = function() {
						this.className = this.className.replace(/\sover/mgi,"")+" over";
					};
					wrap.onmouseout = wrap.onblur = function() {
						this.className = this.className.replace(/\sover/mgi,"");
					};
					wrap.onkeydown = function(e) {
						var key = e.keyCode?e.keyCode:e.which;
						if(key==13)
							this.onclick();
					};
					// make list items selectable
					wrap.setAttribute("tabindex", 500+x);
					themelist.appendChild(wrap);
					
					img = document.createElement("img");
					img.setAttribute("src",response.themes[x].preview?response.themes[x].preview:SuperDial.no_theme_preview_image);
					img.className = "themes_item_image";
					wrap.appendChild(img);
					
					label = document.createElement("span");
					label.innerHTML = response.themes[x].title?response.themes[x].title:response.themes[x].name;
					label.className = "themes_item_label";
					wrap.appendChild(label);
					
					SuperDial.themePicker_themes.push({
						name	: response.themes[x].name,
						preview	: response.themes[x].preview,
						wrap	: wrap,
						img		: img,
						label	: label
					});
				}
				
				SuperDial.onDrawComplete = function() { document.getElementById('registerbutton_top').setText('dialer'); };
				SuperDial.Draw(SuperDial.accountbox);
			};
			var xmlhttpid = "register_getdials_"+Math.floor(Math.random()*999999);
			SuperDial.GetData("http://superdial.org/?ext=settingsdata", xmlhttpid, "SuperDial.settingsButtonAction(SuperDial.xmlhttp_data['"+xmlhttpid+"'].responseText);");
		}
		this.blur();
		if(e)
			return nullEvent(e);
	};
	document.getElementById("registerbutton_top").onmousedown = nullEvent;
	document.getElementById("registerbutton_top").onmouseup = nullEvent;
	
	
	document.onmouseup = function(e) {
		if(window.currentcontextmenu && window.currentcontextmenu.open==true)
			window.currentcontextmenu.Close();
	};
	
	document.onselectstart = nullEvent;
	document.ondragstart = nullEvent;
	document.onmousemove = function(e) {
		if(!e) e=window.event;
		
		if(window.currentcontextmenu && window.currentcontextmenu.opening==true) {
			window.currentcontextmenu.opening = false;
			window.currentcontextmenu.open = true;
		}
		
		if(SuperDial.mouseFollower && SuperDial.mouseFollower.style) {
			if( ((e.pageX?e.pageX:(e.clientX+document.body.scrollLeft)) + 12 + SuperDial.mouseFollower.offsetWidth) <= (window.innerWidth?window.innerWidth:document.body.offsetWidth) )
				SuperDial.mouseFollower.style.left = ((e.pageX?e.pageX:(e.clientX+document.body.scrollLeft)) + 12)+"px";
			if( ((e.pageY?e.pageY:(e.clientY+document.body.scrollTop)) + 12 + SuperDial.mouseFollower.offsetHeight) <= (window.innerHeight?window.innerHeight:document.body.offsetHeight) )
				SuperDial.mouseFollower.style.top = ((e.pageY?e.pageY:(e.clientY+document.body.scrollTop)) + 12)+"px";
			/*
			SuperDial.mouseFollower.style.display = "block";
			if(SuperDial.mouseFollowerHideTimer)
				clearTimeout(SuperDial.mouseFollowerHideTimer);
			SuperDial.mouseFollowerHideTimer = setTimeout("SuperDial.mouseFollower.style.display='none'; SuperDial.mouseFollowerHideTimer=null;", 2000);
			*/
		}
		if(e.preventDefault)
			e.preventDefault();
		if(e.returnValue)
			e.returnValue=false;
		return false;
	};
	document.oncontextmenu = nullEvent;
	window.onselectstart = nullEvent;
	window.ondragstart = nullEvent;
	//window.onmousemove = nullEvent;
	window.oncontextmenu = nullEvent;
	window.onmousedown = nullEvent;
	SuperDial.boxwrap.onmousemove = function(e) {
		if(!e) e=window.event;
		window.onmousedown = function(e) { return true; };
	};
	SuperDial.boxwrap.onmouseout = function(e) {
		window.onmousedown = nullEvent;
	};
	
	var dials = new Array();
	var buttons = new Array();
	var all = document.getElementsByTagName("*");
	for(var y in all) {
		if(all[y] && all[y].className && all[y].className.indexOf("dialurl{") > -1)
			dials.push(all[y]);
		if(all[y] && all[y].className && all[y].className.indexOf("button") > -1)
			buttons.push(all[y]);
	}
	
	for(var c=0; c<buttons.length; c++) {
		var i = buttons[c].getElementsByTagName("span");
		for(var x in i)
			if(i[x] && i[x].className && i[x].className.toLowerCase().indexOf("innertext")>-1)
				buttons[c].iText = i[x];
		//buttons[c].iText = buttons[c].childNodes[0];
		buttons[c].getText = function() {
			var text = this.iText.innerHTML;
			while(text.indexOf("&nbsp;")>-1)
				text = text.replace("&nbsp;"," ");
			return text;
		};
		buttons[c].setText = function(text) {
			while(text.indexOf(" ")>-1)
				text = text.replace(" ","&nbsp;");
			this.iText.innerHTML = text;
		};
	}
	
	
	var ad_keywords = " ";
	
	
	if(dials.length > 0)
		SuperDial.auth = true;
	else
		SuperDial.auth = false;
	
	for(var c=0; c<dials.length; c++) {
		var start = dials[c].className.indexOf("dialurl{")+8;
		dials[c].dialurl = dials[c].className.substring( start , dials[c].className.indexOf("}",start-1) );
		
		var t = new Image();
		t.d = dials[c];
		t.onload = function() { this.d.src = this.src; };
		t.onerror = function() { this.d.src = SuperDial.no_preview_image; };
		var start = dials[c].className.indexOf("dialimg{")+8;
		t.src = dials[c].className.substring( start , dials[c].className.indexOf("}",start-1) );
		
		var start = dials[c].className.indexOf("dialname{")+9;
		dials[c].dialname = dials[c].className.substring( start , dials[c].className.indexOf("}",start-1) );
		ad_keywords += dials[c].dialname+" ";
		
		dials[c].onclick = function(e) {
			this.Dial();
			if(!e) e=window.event;
			if(e.preventDefault)
				e.preventDefault();
			if(e.returnValue)
				e.returnValue=false;
			return false;
		};
		
		dials[c].onmousedown = function(e) {
			if(!e) e=window.event;
			
			this.mdx = e.pageX?e.pageX:(e.clientX+document.body.scrollLeft);
			this.mdy = e.pageY?e.pageY:(e.clientY+document.body.scrollTop);
			
			if((e.which&&e.which==2)||(e.button&&e.button==2))
				this.Menu();

			if(e.preventDefault)
				e.preventDefault();
			if(e.returnValue)
				e.returnValue=false;
			return false;
		};
		dials[c].onmouseup = nullEvent;
		dials[c].onselectstart = nullEvent;
		dials[c].oncontextmenu = nullEvent;
		
		dials[c].onmouseover = function(e) { SuperDial.setStatus(this.dialname); this.className += " over"; };
		dials[c].onmouseout = function(e) { SuperDial.setStatus(null); var o=this.className; if(o.indexOf(" over")>-1) this.className = o.substring(0,o.indexOf(" over")) + o.substring(o.indexOf(" over")+5,o.length); };
		
		dials[c].Dial = function(params) {
			this.className = this.className.replace(" over","");
			if(params=="undefined" || params==null)
				params = "";
			if(params.indexOf("newwin") > -1)
				window.open(this.dialurl);
			else if(params.indexOf("fullscreen") > -1) {
				var win = window.open(this.dialurl, null, "statusbar=no,fullscreen=yes,status=no,navigation=no,title=no,toolbars=no,toolbar=no;",false);
				win.moveTo(1,1);
				if(screen.availWidth)
					win.resizeTo(screen.availWidth, screen.availHeight);
				else
					win.resizeTo(screen.clientWidth, screen.clientHeight);
				win.moveTo(0,0);
			}
			else
				window.location.href = this.dialurl;
		};
		
		dials[c].rndid = 0;
		
		dials[c].Update = function(src) {
			SuperDial.setCursor("busy");
			var id = parseInt(this.id.substring( "dialer_".length , this.id.length ));
			var pos = parseInt(this.alt.substring( "dialer_".length , this.alt.length ));
			var dialbox = 1;
			var xmlhttpid = this.id+"_dialupdate_"+Math.floor(Math.random()*999999);
			SuperDial.SendData("http://superdial.org/", "action=dialupdate&dialid="+id+"&dialname="+(this.dialname!="undefined"?encodeURIComponent(this.dialname):"unnamed")+"&url="+encodeURIComponent(this.dialurl)+"&imageurl="+encodeURIComponent(src?src:this.src)+"&pos="+pos+"&dialbox="+dialbox+"&sid="+xmlhttpid, xmlhttpid, "document.getElementById('"+this.id+"').UpdateCallback(SuperDial.xmlhttp_postdata['"+xmlhttpid+"'].responseText);");
			this.src = "http://superdial.org/?ext=image&dialid="+id+"&rndid=" + (this.rndid++);
		};
		
		dials[c].UpdateCallback = function(callback) {
			if(SuperDial.statusRevert_timer)
				clearTimeout(SuperDial.statusRevert_timer);
			var response = SuperDial.evalJSON(callback);
			SuperDial.setStatus(response['message']);
			SuperDial.statusRevert_timer = setTimeout("SuperDial.setStatus(); SuperDial.statusRevert_timer=null;", 3000);
			SuperDial.setCursor("default");
			if(response['result'] == true && response['errorcount']==0)
				return true;
			else
				return false;
		};
		
		dials[c].Menu = function() {
			if(window.currentcontextmenu && (window.currentcontextmenu.open==true || window.currentcontextmenu.opening==true))
				window.currentcontextmenu.Close();
			
			this.className = this.className.replace(" over","");
			this.className += " active";
			var m = document.createElement("div");
			m.dialer = this;
			m.className = "dialer_contextmenu";
			m.style.position = "absolute";
			m.style.left = this.mdx+"px";
			m.style.top = this.mdy+"px";
			m.Close = function() {
				window.currentcontextmenu = null;
				var o=this.dialer.className;
				if(o.indexOf(" active")>-1)
					this.dialer.className = o.substring(0,o.indexOf(" active")) + o.substring(o.indexOf(" active")+5,o.length);
				this.open = false;
				this.opening = false;
				try {
					document.body.removeChild(this);
				} catch(e) {}
			};
			m.open = false;
			m.opening = true;
			
			var items = {
				"Open":function(){ this.m.dialer.Dial(); },
				"Pop Open":function(){ this.m.dialer.Dial("newwin"); },
				"Open Fullscreen":function(){ this.m.dialer.Dial("fullscreen"); },
				"Change Link":function(){
					var newurl = prompt("Enter new URL",this.m.dialer.dialurl);
					if(!newurl || newurl==null || newurl=="undefined" || newurl.length < 3)
						return false;
					if(newurl.indexOf("://") < 0 || newurl.indexOf("://") > 6)
						newurl = "http://" + newurl;
					var end = newurl.indexOf("/",newurl.indexOf("://")+4)+1;
					var root = newurl.substring( 0 , (end>5?end:newurl.length) );
					if(root.substring(root.length-1,root.length)!="/")
						root += "/";
					this.m.dialer.dialurl = newurl;
					SuperDial.setCursor("busy");
					/**/
					if(this.m.dialer.src==SuperDial.no_preview_image) {
						var t = new Image();
						t.d = this.m.dialer;
						t.onload = function(){ this.d.src=this.src; this.d.Update(); };
						t.onerror = function() { this.d.src = SuperDial.no_preview_image; this.d.Update(); };
						t.src = root + "favicon.ico";
					}
					else
						this.m.dialer.Update();
					/**/
					// document.getElementById("dialer_contextmenuitem_use_thumbnail").onmouseup();
					return true;
				},
				"Change Image"	: function(){
					var oldsrc = this.m.dialer.src==SuperDial.no_preview_image ? "default" : this.m.dialer.src;
					var src = prompt("Image URL",oldsrc);
					if(!src || src==null || src=="undefined" || src.length<3)
						return false;
					SuperDial.setCursor("busy");
					if(src == "default")
						src = SuperDial.no_preview_image;
					if(src.indexOf("://") < 0 || src.indexOf("://") > 6)
						src = "http://" + src;
					var t = new Image();
					t.oldsrc = this.m.dialer.src;
					t.d = this.m.dialer;
					t.onload = function(){ SuperDial.setCursor("default"); this.d.Update(this.src);};
					t.onerror = function() { this.d.Update(this.oldsrc); };
					t.src = src;
				},
				"Use Thumbnail"	: function() {
					SuperDial.setCursor("busy");
					
					var url = this.m.dialer.dialurl;
					
					if(!window._thumbWaiters)
						window._thumbWaiters = [];
					
					var img = new Image();
					window._thumbWaiters.push(img);
					img._i = window._thumbWaiters.length-1;
					img._d = this.m.dialer;
					img.rndid = 0;
					img.TryReload = function() {
						this.src = this.src.replace(/\&rndid=[0-9]*/mgi,"") + "&rndid=" + (this.rndid++);
					};
					img.ThumbQueued = function() {
						window.setTimeout("window._thumbWaiters["+this._i+"].TryReload();", 1000);
					};
					img.ThumbAvailable = function() {
						SuperDial.setCursor("default");
						this._d.Update(this.src);
					};
					img.onload = function() {
						if(this.width<10 || this.height<10)
							this.ThumbQueued();
						else
							this.ThumbAvailable();
					};
					img.src = "http://images.pageglimpse.com/v1/thumbnails?size=large&devkey=395de821967de2ff224a727dd70ccc42&nothumb=http://amoebaos.com/images/icons/null.gif&root=no&url=" + url;
				},
				"Change Name"	: function(){
					var oldname = this.m.dialer.dialname;
					var name = prompt("Enter name",oldname);
					if(!name || name==null || name=="undefined" || name.length < 2)
						return false;
					this.m.dialer.dialname = name;
					this.m.dialer.Update();
				},
				"Check Rank":function(){
					var u = this.m.dialer.dialurl;
					s=document.createElement('script');
					s.src='http://ranxd.com/b/2/?domain='+escape(u.substring(u.indexOf('://')+3,u.length).replace(/\/$/mgi,''));
					document.getElementsByTagName('head')[0].appendChild(s);
				}
			};
			
			for(var x in items) {
				var id = x.toLowerCase().replace(/\s/mgi,"_");
				var i = document.createElement("div");
				i.m = m;
				i.id = "dialer_contextmenuitem_"+id;
				i.className = "dialer_contextmenuitem dialer_contextmenuitem_"+id;
				i.innerHTML = "<div class=\"inner\">" + (x+"").replace(/</m,'&lt;').replace(/>/m,'&gt;') + "</div>";
				i._func = items[x];
				i.onmouseup = function(e){
					if(!e)e=window.event;
					this._func(e);
					this.m.Close();
				};
				i.onmouseover = function(e) { this.className += " over"; };
				i.onmouseout = function(e) {
					if(this.className.indexOf(" over") > -1) {
						var before = this.className.substring( 0 , this.className.indexOf(" over") );
						var after = this.className.substring( this.className.indexOf(" over")+5 , this.className.length );
						this.className = before + after;
					}
				};
				m.appendChild(i);
			}
			
			setTimeout("window.currentcontextmenu.opening=false; window.currentcontextmenu.open=true;", 400);
			this.menu = m;
			window.currentcontextmenu = m;
			document.body.appendChild(m);
		};
	}
	
	
	/*
	var ad_space = document.getElementById("ad_space");
	if(!ad_space) {
		ad_space = document.createElement("div");
		ad_space.id = "ad_space";
		ad_space.setAttribute("id","ad_space");
		document.body.appendChild(ad_space);
	}
	ad_space.style.cssText = "display:block; position:relative; margin:auto; margin-top:20px; text-align:center; height:90px; width:728px;";
	shoppingads_ad_client = "e3411b672cc3b88b5e2b";
	shoppingads_ad_campaign = "b0164ce35efc4e28f70339b23ae7d147";
	shoppingads_ad_width = "728";
	shoppingads_ad_height = "90";
	shoppingads_ad_kw = ad_keywords.substring(0,ad_keywords.length-1);
	if(shoppingads_ad_kw.length<2)
		shoppingads_ad_kw = "most_popular";
	shoppingads_color_border =  "AAAAAA";
	shoppingads_color_bg =  "AAAAAA";
	shoppingads_color_heading =  "444444";
	shoppingads_color_text =  "777777";
	shoppingads_color_link =  "BBBBBB";
	shoppingads_attitude = "false";
	shoppingads_options =  "n";
	
	var this_server = 'ads.shoppingads.com';
	function scrubSXX(str) {
		//str = str.replace(/eval\(/g, "");
		//str = str.replace(/javascript:/g, "");
		//str = str.replace(/[^A-Za-z0-9#\-;\,_]/g, "");
		return str;
	}
	
	var keywords = "";
	var ad_format =  shoppingads_ad_width+'x'+shoppingads_ad_height;
	//shoppingads_ad_kw = shoppingads_ad_kw.replace(new RegExp("\\s+", "g"),"_");
	click_parent = "http://superdial.org/";
	
	var newcode = "<iframe name='shoppingads_ad_frame' height='" + scrubSXX(shoppingads_ad_height) 
	+ "' width='" + scrubSXX(shoppingads_ad_width)
	+ "' frameborder='0' marginwidth='0' marginheight='0' vspace='0' hspace='0' allowtransparency='true' scrolling='no' src=\"http://"+this_server+"/pagead/ads?ad_client="+ scrubSXX(shoppingads_ad_client)
	+ "&campaign="+scrubSXX(shoppingads_ad_campaign)
	+ "&format="+scrubSXX(ad_format)
	+ "&b="+scrubSXX(shoppingads_color_border)
	+ "&bg="+scrubSXX(shoppingads_color_bg)
	+ "&h="+scrubSXX(shoppingads_color_heading)
	+ "&d="+scrubSXX(shoppingads_color_text)
	+ "&l="+scrubSXX(shoppingads_color_link)
	+ "&ad_kw="+scrubSXX(shoppingads_ad_kw)
	+ "&kw="+scrubSXX(keywords)
	+ "&attitude="+scrubSXX(shoppingads_attitude)
	+ "&top="+escape(click_parent)
	+ "&opt="+scrubSXX(shoppingads_options)    
	+ "\"></iframe>";
	
	ad_space.innerHTML = newcode;
	*/
};



SuperDialClass.prototype.setStatus = function(text) {
	if(!text || text==null || text=="undefined" || text.length < 2)
		text = this.defaultStatusMessage;
	document.getElementById("statusmessage").innerHTML = text;
};


SuperDialClass.prototype.setCursor = function(cursor) {
	if(!cursor || cursor==null || cursor=="undefined")
		SuperDial.mouseFollower.className = "mouseFollower";
	else
		SuperDial.mouseFollower.className = "mouseFollower "+cursor;
	
	if( (cursor+"").toLowerCase()=="busy" )
		SuperDial.loadingCover.style.display = "block";
	else
		SuperDial.loadingCover.style.display = "none";
};



SuperDialClass.prototype.Draw = function(what) {
	if(this.drawingInProgress && this.drawingInProgress==true)
		return false;
	this.drawingInProgress = true;
	
	if(!what.id || what.id==null || what.id=="undefined")
		what.id = "SuperDial_autoid_"+Math.floor(Math.random()*9999999999);
	
	var t = 0;
	var max = this.boxwrap.offsetWidth-4;
	for(t=0; t<=30; t++)
		setTimeout("SuperDial.boxwrap.style.width='"+(max-(Math.sin(Math.PI*(t/30-1/2))+1)*max/2)+"px'; SuperDial.boxwrap.style.letterSpacing='"+Math.round((-(Math.sin(Math.PI*(t/30-1/2))+1)*3/8)*100)/100+"em';", t*15);
	setTimeout("document.getElementById('"+this.currentDrawn.id+"').style.display='none'; document.getElementById('"+what.id+"').style.display='block'; SuperDial.boxwrap.style.letterSpacing='0';", (t+1)*15);
	var p = 0;
	max = this.boxwrap.offsetWidth-4;
	for(p=0; p<30; p++)
		setTimeout("SuperDial.boxwrap.style.width='"+((Math.sin(Math.PI*(p/30-1/2))+1)*max/2)+"px'; SuperDial.boxwrap.style.letterSpacing='"+Math.round(((-3/4)+(Math.sin(Math.PI*(p/30-1/2))+1)*3/8)*100)/100+"em';", (p+t)*15);
	setTimeout("SuperDial.boxwrap.style.letterSpacing='0'; SuperDial.drawingInProgress=false; if(SuperDial.onDrawComplete) SuperDial.onDrawComplete();", (p+t)*15);
	this.currentDrawn = what;
};








SuperDialClass.prototype.LoginCallback = function(callback, a, optionalTheme) {
	if(this.loginStatusRevert_timer) {
		clearTimeout(this.loginStatusRevert_timer);
		this.loginStatusRevert_timer = null;
	}
	
	if(a && a=="to_dialer") {
		SuperDial.dialerbox.innerHTML = callback;
		document.getElementById("loginbutton_top").setText(" . . . ");
		SuperDial.onDrawComplete = function() {
			SuperDial.auth = true;
			SuperDial.init();
			document.getElementById('loginbutton_top').setText('logout');
			document.getElementById('registerbutton_top').setText('settings');
		};
		SuperDial.Draw(SuperDial.dialerbox);
		SuperDial.setCursor("default");
		return true;
	}

	var response = this.evalJSON(callback);
	if(response.result == true && response.errorcount==0) {
		if (response.theme && response.theme.length>0) {
			SuperDial.ChangeTheme(response.theme);
		}
		var xmlhttpid = "login_getdials_"+Math.floor(Math.random()*999999);
		SuperDial.GetData("http://superdial.org/?ext=dialpadcontents", xmlhttpid, "SuperDial.LoginCallback(SuperDial.xmlhttp_data['"+xmlhttpid+"'].responseText, 'to_dialer')");
	}
	else {
		SuperDial.setCursor("default");
		document.getElementById("login_username").style.borderColor = "#880000";
		document.getElementById("login_username").style.backgroundColor = "#FFEEAA";
		document.getElementById("login_password").style.borderColor = "#880000";
		document.getElementById("login_password").style.backgroundColor = "#FFCC77";
		document.getElementById("login_password").value = "";
		this.loginStatusRevert_timer = setTimeout("document.getElementById('login_status').innerHTML = SuperDial.defaultLoginStatusText; SuperDial.loginStatusRevert_timer = null;", 2000);
		document.getElementById("login_status").innerHTML = "login failed.";
	}
};



SuperDialClass.prototype.LogoutCallback = function(callback, a) {
	if(this.logoutStatusRevert_timer) {
		clearTimeout(this.logoutStatusRevert_timer);
		this.logoutStatusRevert_timer = null;
	}
	
	if(a && a=="to_dialer") {
		SuperDial.dialerbox.innerHTML = callback;
		document.getElementById("loginbutton_top").setText(" . . . ");
		SuperDial.onDrawComplete = function() {
			SuperDial.auth = true;
			SuperDial.init();
			document.getElementById('loginbutton_top').setText('login');
			document.getElementById('registerbutton_top').setText('register');
		};
		SuperDial.Draw(SuperDial.dialerbox);
		SuperDial.setCursor("default");
		return true;
	}

	var response = this.evalJSON(callback);
	if(response['result'] == true && response['errorcount']==0) {
		var xmlhttpid = "logout_getdials_"+Math.floor(Math.random()*999999);
		SuperDial.GetData("http://superdial.org/?ext=dialpadcontents", xmlhttpid, "SuperDial.LogoutCallback(SuperDial.xmlhttp_data['"+xmlhttpid+"'].responseText, 'to_dialer')");
	}
	else {
		SuperDial.setStatus("Logout failed. Please clear your cookies.");
	}
};




SuperDialClass.prototype.RegisterCallback = function(callback,a) {
	if(this.registerStatusRevert_timer) {
		clearTimeout(this.registerStatusRevert_timer);
		this.registerStatusRevert_timer = null;
	}
	
	if(a && a == "to_dialer") {
		SuperDial.dialerbox.innerHTML = callback;
		SuperDial.onDrawComplete = function() {
			SuperDial.auth = true;
			SuperDial.init();
			document.getElementById('loginbutton_top').setText('logout');
			document.getElementById('registerbutton_top').setText('settings');
			//window.location.reload();
		};
		SuperDial.Draw(SuperDial.dialerbox);
		SuperDial.setCursor("default");
		return true;
	}
	
	var response = this.evalJSON(callback);
	if(response['result'] == true && response['errorcount']==0) {
		/*
		SuperDial.setCookie("username",response['username'],"3");
		SuperDial.setCookie("userid",response['userid'],"3");
		SuperDial._registerFrame = document.createElement("iframe");
		SuperDial._registerFrame.style.cssText = "position:absolute; left:-1px; top:-1px; width:1px; height:1px; display:block;";
		document.body.appendChild(SuperDial._registerFrame);
		SuperDial._registerFrame.src = "http://superdial.org/?sid="+Math.random();
		SuperDial._registerFrame.setAttribute("src","http://superdial.org/?sid="+Math.random());
		*/
		//SuperDial._register2 = function() {
			var xmlhttpid = "register_getdials_"+Math.floor(Math.random()*999999);
			SuperDial.GetData("http://superdial.org/?ext=dialpadcontents", xmlhttpid, "SuperDial.RegisterCallback(SuperDial.xmlhttp_data['"+xmlhttpid+"'].responseText, 'to_dialer')");
		//};
		//window.setTimeout("SuperDial._register2();", 1000);
		document.getElementById("registerbutton_top").setText(" . . . ");
	}
	else {
		SuperDial.RegistrationFailed = function(newform) {
			SuperDial.setCursor("default");
			document.getElementById("registerbox").innerHTML = newform;
			document.getElementById('superdial_register').submit = function() {
				SuperDial.setCursor("busy");
				var u = document.getElementById('register_username').value;
				var p = document.getElementById('register_password').value;
				var p2 = document.getElementById('register_password2').value;
				var captchausertext = document.getElementById('captcha_usertext').value;
				var captchatext = document.getElementById('captcha_text').value;
				var xmlhttpid = "register_"+Math.floor(Math.random()*999999);
				SuperDial.SendData("http://superdial.org/", "action=register&register_username="+u+"&register_password="+p+"&register_password2="+p2+"&captcha_usertext="+captchausertext+"&captcha_text="+captchatext+"&sid="+xmlhttpid, xmlhttpid, "SuperDial.RegisterCallback(SuperDial.xmlhttp_postdata['"+xmlhttpid+"'].responseText);");
				SuperDial.defaultRegisterStatusText = document.getElementById('register_status').innerHTML;
				document.getElementById('register_status').innerHTML = "attempting to register...";
				return false;
			};
			document.getElementById('register_username').onkeydown = function(e) { if((e.keyCode&&e.keyCode==13)||(e.which&&e.which==13)) document.getElementById('superdial_register').submit(); };
			document.getElementById('register_password').onkeydown = function(e) { if((e.keyCode&&e.keyCode==13)||(e.which&&e.which==13)) document.getElementById('superdial_register').submit(); };
			document.getElementById('register_password2').onkeydown = function(e) { if((e.keyCode&&e.keyCode==13)||(e.which&&e.which==13)) document.getElementById('superdial_register').submit(); };
			document.getElementById('captcha_usertext').onkeydown = function(e) { if((e.keyCode&&e.keyCode==13)||(e.which&&e.which==13)) document.getElementById('superdial_register').submit(); };
			document.getElementById("register_username").style.borderColor = "#800";
			document.getElementById("register_username").style.backgroundColor = "#FEA";
			document.getElementById("register_password").style.borderColor = "#800";
			document.getElementById("register_password").style.backgroundColor = "#FC7";
			document.getElementById("register_password").value = "";
			document.getElementById("register_password2").style.borderColor = "#800";
			document.getElementById("register_password2").style.backgroundColor = "#FC7";
			document.getElementById("register_password2").value = "";
			document.getElementById('captcha_usertext').value = "";
			document.getElementById("register_username").focus();
			this.registerStatusRevert_timer = setTimeout("document.getElementById('login_status').innerHTML = SuperDial.defaultRegisterStatusText; SuperDial.registerStatusRevert_timer = null;", 2000);
			document.getElementById("register_status").innerHTML = "registration failed.<br />"+response['message'];
			SuperDial.setCursor("default");
		}
		var xmlhttpid = "register_failure_"+Math.floor(Math.random()*999999);
		SuperDial.GetData("http://superdial.org/?ext=registerboxcontents", xmlhttpid, "SuperDial.RegistrationFailed(SuperDial.xmlhttp_data['"+xmlhttpid+"'].responseText)");
	}
};






SuperDialClass.prototype.getCookie = function(c_name) {
	c_start=document.cookie.indexOf(c_name + "=")
	if(document.cookie.length==0 || c_start==-1)
		return null;
	else {
		c_start = c_start + c_name.length+1;
		c_end = document.cookie.indexOf(";", c_start);
		if(c_end==-1)
			c_end = document.cookie.length;
		return unescape(document.cookie.substring(c_start,c_end))
	} 
};

SuperDialClass.prototype.setCookie = function(c_name,value,expiredays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString()+";path=/;domain=.superdial.org");
	return true;
};








// General XMLHttp functions:

// SuperDial.SendData( "URL" , "param1=value1&param2=value2" , xmlhttpID, "[CODE TO EXEC ON COMPLETE]" );

SuperDialClass.prototype.xmlhttp_postdata = new Array();
SuperDialClass.prototype.SendData = function(url, parameters, output, returnf) {
	if(!output)
		output = Math.random();
	this.xmlhttp_postdata[output] = this.GetXmlHttpObject()
	if (this.xmlhttp_postdata[output]==null) {
		alert ("Your web browser does not support AJAX.\nAJAX is supported by most desktop web browsers made after 1999.\n\nWe recommend FireFox, for safety, speed and reliability.\nGo to http://www.mozilla.com/en-US/ to download the latest version free.");
		window.open("http://www.mozilla.com/en-US/");
		return false;
	}
	
	//xmlhttp_postdata[output].returnFunction = returnf;
	this.xmlhttp_postdata[output].onreadystatechange = function() { if(SuperDial.xmlhttp_postdata[output].readyState==4) { if(SuperDial.xmlhttp_postdata[output].status==200) eval(returnf); else throw("SuperDial > SendData() failed for request \""+output+"\"."); } };
	this.xmlhttp_postdata[output].open('POST', url, true);
	this.xmlhttp_postdata[output].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	this.xmlhttp_postdata[output].setRequestHeader("Content-length", parameters.length);
	this.xmlhttp_postdata[output].setRequestHeader("Connection", "close");
	this.xmlhttp_postdata[output].send(parameters);
	
	return output;
}


SuperDialClass.prototype.xmlhttp_data = new Array();
SuperDialClass.prototype.GetData = function(url,output,returnf) {
	this.xmlhttp_data[output] = this.GetXmlHttpObject();
	if (this.xmlhttp_data[output]==null) {
		alert ("Your web browser does not support AJAX.\nAJAX is supported by all common web browsers, and is required in order to use SuperDial.");
		window.open("http://www.mozilla.com/en-US/");
		return false;
	}
	//xmlhttp_functions[output] = returnf;
	this.xmlhttp_data[output].onreadystatechange = function() { if(SuperDial.xmlhttp_data[output].readyState==4 || SuperDial.xmlhttp_data[output].readyState=="complete") { if(SuperDial.xmlhttp_data[output].status==200) eval(returnf); else throw("SuperDial > GetData() failed for request \""+output+"\"."); } };
	this.xmlhttp_data[output].open("GET",url,true);
	this.xmlhttp_data[output].send(null);
} 

SuperDialClass.prototype.GetXmlHttpObject = function() {
	var xmlHttp=null;
	try { xmlHttp=new XMLHttpRequest(); }
	catch (e) {
		try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
	}
	return xmlHttp;
}


SuperDialClass.prototype.evalJSON = function(JSONtext) {
	return eval("("+JSONtext+")");
};




SuperDialClass.prototype.PreviewTheme = function(theme) {
	if(!this.preview_oldthemeurl)
		this.preview_oldthemeurl = document.getElementById("superdial_style").getAttribute("href");
	document.getElementById("superdial_style").setAttribute("href","/themes/"+theme+"/style.css");
	return true;
};
SuperDialClass.prototype.UnpreviewTheme = function() {
	document.getElementById("superdial_style").setAttribute("href",this.preview_oldthemeurl);
	this.preview_oldthemeurl = null;
	return true;
};

SuperDialClass.prototype.ChangeTheme = function(theme) {
	this.setCursor("busy");
	var xmlhttpid = this.id+"_settheme_"+Math.floor(Math.random()*999999);
	this.SendData("/", "action=settheme&theme="+encodeURIComponent(theme)+"&sid="+xmlhttpid, xmlhttpid, "SuperDial.onThemeSaveComplete(SuperDial.xmlhttp_postdata['"+xmlhttpid+"'].responseText,decodeURIComponent('"+encodeURIComponent(theme)+"'));");
};

SuperDialClass.prototype.onThemeSaveComplete = function(data,theme) {
	var repsonse = {result:false,message:'Cannot set theme: Communication Error.'};
	if(data && data.length>1)
		try{ response = this.evalJSON(data); }catch(e){ response={result:false,message:'Cannot set theme: Parse Error.'}; }
	
	this.setCursor("default");
	
	if(response && response.result==true) {
		document.getElementById("superdial_style").setAttribute("href","/themes/"+theme+"/style.css");
	}
	else
		alert(response.message);
};




function nullEvent(e,allowBubble) {
	if(!e) e = window.event;
	if(e!=null) {
		if(e.stopPropagation!=null && allowBubble!=true)
			try{ e.stopPropagation(); }catch(err){};
		if(e.preventDefault != null)
			try{ e.preventDefault(); }catch(err){};
		if(allowBubble!=true)
			try { e.cancelBubble(); } catch(err){};
		try { e.returnValue = false; } catch(err){};
	}
	return false;
} 




  // Clean Up:
SuperDial = new SuperDialClass();
try{ delete SuperDialClass; }catch(err){}
SuperDialClass = null;

window.User = {};

