var isSSL = (window.location.toString ().indexOf ("https") == 0);
var winLoc = window.location.toString ();
if (winLoc.indexOf (".webuzz.im/") != -1) {
	window.location.replace ("http://webuzz.im/");
}
var j2sBuildDate = "20100120";
var j2sRelative = "2.0.1-v" + j2sBuildDate;
var zimRelative = "wbzz-v20100228";
var widgetRelative = "wz-v20100205";

var host = window.location.host;
if (host != null && host.indexOf ("www.") == 0) {
	host = host.substring (4);
}
window["j2s.xss.cookie.url"] = "http://c." + host + "/xss-cookie.html";

window["swt.desktop.vscrollbar"] = true;

function multipleSites (path) {
	var length = path.length;
	if ((length > 15 && path.substring (0, 15) == "http://archive.")
			|| (length > 9 && path.substring (0, 9) == "http://a.")) {
		var index = path.lastIndexOf ("/");
		if (index < length - 3) {
			var arr = ['a', 'e', 'i', 'o', 'u', 'y'];
			var c1 = path.charCodeAt (index + 1);
			var c2 = path.charCodeAt (index + 2);
			var idx = (length - index) * 3 + c1 * 5 + c2 * 7; // Hash
			return path.substring (0, 7) + arr[idx % 6] + path.substring (8);
		}
	}
	return path;
}

function generatingScriptFunction (script) {
	return function () {
		eval (script);
		return false;
	};
};

function fixLink (id, args, icon) {
	var alaaDiv = document.getElementById (id);
	if (alaaDiv == null) {
		return;
	}
	var o = window["j2s.lib"];
	//var j2sRelative = (o.alias ? o.alias : o.version);
	var j2sBase = o.base + j2sRelative + "/";
	// var zimRelative = "webuzz-v20090308";
	var zimBase = o.base + zimRelative + "/";
	if (typeof args == "string") {
		alaaDiv.href = args;
		alaaDiv.target = "_blank";
	} else {
		var argsStr = "[";
		if (args != null && args.length > 0) {
			for (var i = 0; i < args.length; i++) {
				argsStr += "\'" + args[i] + "\'";
				if (i != args.length - 1) {
					argsStr += ",";
				}
			}
		}
		argsStr += "]";
		var script = "if(a='im.webuzz.g.MainWindow@" + zimBase + "',r=" + argsStr + ",window['ClazzLoader']!=null)$w$(a,r);else{var d=document,t='onreadystatechange',x=d.createElement('SCRIPT'),f=function(){var s=this.readyState;if(s==null||s=='loaded'||s=='complete'){$w$(a,r);}};x.src='" + j2sBase + "j2slib.z.js';(typeof x[t]=='undefined')?x.onload=f:x[t]=f;d.getElementsByTagName('HEAD')[0].appendChild(x);void(0);}";
		alaaDiv.href = "javascript:" + script;
		if (navigator.userAgent.toLowerCase ().indexOf ("msie") != -1) {
			alaaDiv.href = "#"; //"javascript:void(0);"
			alaaDiv.onclick = generatingScriptFunction (script);
		}
	}
	if (icon != null) {
		var iconURL = multipleSites (zimBase + "im/webuzz/g/images/" + icon);
		if (alaaDiv.className != null && alaaDiv.className.indexOf ("shortcut-item") != -1) {
			alaaDiv.style.backgroundImage = "url('" + iconURL + "')";
		} else {
			for (var j = 0; j < alaaDiv.childNodes.length; j++) {
				var item = alaaDiv.childNodes[j];
				if (item != null && item.className != null
						&& item.className.indexOf ("alaa-icon") != -1) {
					item.style.backgroundImage = "url('" + iconURL + "')";
					break;
				}
			}
		}
	}
}

function addLink (id, title, text, style) {
	var demosDiv = document.getElementById ("demos");
	if (demosDiv == null) {
		return;
	}
	var anchor = document.createElement ("A");
	anchor.id = id;
	anchor.title = title;
	anchor.className = "alaa ignored";
	anchor.href = "#";
	if (style != null) {
		anchor.style.cssText = style;
	}
	document.body.insertBefore (anchor, demosDiv);
	var iconSpan = document.createElement ("SPAN");
	iconSpan.className = "alaa-icon";
	anchor.appendChild (iconSpan);
	anchor.appendChild (document.createTextNode (text));
}
function addDockLink (id, text) {
	var el = document.getElementById (id);
	if (el != null) {
		return;
	}
	var anchor = document.createElement ("A");
	anchor.id = id;
	anchor.className = "alaa";
	anchor.href = "#";
	anchor.style.display = "none";
	document.body.appendChild (anchor);
	var iconSpan = document.createElement ("SPAN");
	iconSpan.className = "alaa-icon";
	anchor.appendChild (iconSpan);
	anchor.appendChild (document.createTextNode (text));
}
function initDesktop () {
if (window["webuzz.im.type"] == null) {
addDockLink ("alaa-facebook", "Facebook Chat");
addDockLink ("alaa-gtalk", "Google Talk");
addDockLink ("alaa-msn", "Windows Live / MSN Messenger");
addDockLink ("alaa-ymsg", "Yahoo! Messenger");
addDockLink ("alaa-aim", "AOL Instant Messenger / AIM");
addDockLink ("alaa-icq", "ICQ");
addDockLink ("alaa-jabber", "Jabber");
addDockLink ("alaa-wemail", "WeMail - Web Mail Client for Gmail, Live Mail/Hotmail, Yahoo! Mail/Rocket Mail, AOL Mail/AIM Mail and others");
}
}

function loadDesktop () {
	initDesktop ();
	loadJ2SApp ();
}
if (document.body != null) {
	initDesktop ();
} else {
	/* for Mozilla */
	if (document.addEventListener) {
		document.addEventListener ("DOMContentLoaded", loadDesktop, false);
	} else if (/WebKit/i.test (navigator.userAgent)) { // sniff
		var _timer = window.setInterval (function () {
			if (/loaded|complete/.test (document.readyState)) {
				window.clearInterval (_timer);
				loadDesktop (); // call the onload handler
			}
		}, 10);
	} else {
// for Internet Explorer (using conditional comments)
/*@cc_on @*/
/*@if (@_win32)
document.write ("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
var script = document.getElementById ("__ie_onload");
script.onreadystatechange = function () {
	if (this.readyState == "complete") {
		loadDesktop (); // call the onload handler
	}
};
/*@end @*/
	}
}


function parseIMType () {
	var hash = window.location.hash;
	var imType = null;//"gtalk";
	if ("#gtalk" == hash) {
		imType = "gtalk";
	} else if ("#msn" == hash || "#msnlive" == hash) {
		imType = "msn";
	} else if ("#ymsg" == hash || "#ymessenger" == hash) {
		imType = "ymsg";
	} else if ("#aim" == hash) {
		imType = "aim";
	} else if ("#icq" == hash) {
		imType = "icq";
	} else if ("#jabber" == hash) {
		imType = "jabber";
	} else if ("#facebook" == hash || "#fb" == hash) {
		imType = "facebook";
	} else {
		var url = window.location.toString ().toLowerCase ();
		url += "#" + document.referrer;
		if (url.indexOf ("talk") != -1) {
			imType = "gtalk";
		} else if (url.indexOf ("msn") != -1 || url.indexOf ("live") != -1 || url.indexOf ("wlm") != -1) {
			imType = "msn";
		} else if (url.indexOf ("ymessenger") != -1 || url.indexOf ("ymsg") != -1 || url.indexOf ("yahoo") != -1) {
			imType = "ymsg";
		} else if (url.indexOf ("aim") != -1 || url.indexOf ("aol") != -1) {
			imType = "aim";
		} else if (url.indexOf ("icq") != -1) {
			imType = "icq";
		} else if (url.indexOf ("jabber") != -1) {
			imType = "jabber";
		} else if (url.indexOf ("facebook") != -1 || url.indexOf ("fb") != -1) {
			imType = "facebook";
		}
	}
	return imType;
}

function cleanJ2SBackground () {
	var clzz = [ "header", "to-logo", "j2s-container", "j2s-annotation", "more-j2s", "more-j2s-continue"/*, "copyright-footer"*/ ];
	var els = document.body.childNodes;
	for (var i = els.length - 1; i >= 0; i--) {
		var el = els[i];
		if (el.className == "copyright-footer") {
			el.style.fontSize = "13pt";
			el.style.textAlign = "left";
			el.style.marginLeft = "24px";
			continue;
		}
		if (el.className == "j2s-annotation") {
			var e = document.createElement ("DIV");
			e.style.height = "100px";
			document.body.insertBefore (e, el);
			e = document.createElement ("DIV");
			e.className = "description";
			e.style.marginLeft = "24px";
			e.style.marginRight = "350px";
			e.style.fontSize = "13pt";
			document.body.insertBefore (e, el);
		}
		for (var j = 0; j < clzz.length; j++) {
			if (el.className == clzz[j]) {
				el.parentNode.removeChild (el);
			}
		}
	}
};

function generateScriptCallback () {
	return function () {
		var s = this.readyState;
		if (s == null || s == "loaded" || s == "complete") {
			if (window["ClazzLoader"] != null) {
				window["j2s.lib"].onload (this);
			}
			this.onreadystatechange = null;
			this.onload = null;
		}
	};
};
function loadJ2SLibZJS (path, cb) {
	var sxr = document.createElement ("SCRIPT");
	sxr.src = path;
	sxr.type = "text/javascript";
	if (cb) {
		var t = "onreadystatechange";
		var xhrCallback = generateScriptCallback ();
		if (typeof sxr[t] == "undefined") {
			sxr.onload = xhrCallback;
		} else {
			sxr[t] = xhrCallback;
		}
	}
	document.getElementsByTagName ("HEAD")[0].appendChild (sxr);
};

function loadJ2SApp (forced) {
	// Some visitors use sneakypass to use webuzz.im services. 
	// We just make some change so it will get a much better user
	// experience. We have NO business relationship with sneakypass.
	var isSneakyPass = window.location.host.indexOf ("sneakypass") != -1;
	if (isSneakyPass && forced != true) {
		custom_handler = function () {
			loadJ2SApp (true);
		};
		return;
	}
	var o = window["j2s.lib"];
	if (o.loaded) {
		return;
	}
	var site = null;
	if (o.sites != null && o.sites.length > 0 && o.index >= o.sites.length) {
		alert ("Failed to load Java2Script core libraries from given "
				+ o.sites.length + " sites : " + o.sites + " !\r\n"
				+ "Please check your network!");
		return;
	} else if (o.sites == null || o.sites.length == 0 || o.index >= o.sites.length) {
		site = "http://archive.java2script.org/";
	} else {
		site = o.sites[o.index];
		o.index++;
	}
	loadJ2SLibZJS(site + (o.alias ? o.alias : o.version) + "/j2slib.z.js", o.onload);
	window.setTimeout (loadJ2SApp, 3000); // Not loaded, try again with another site
};

window["j2s.object.native"] = true;

window["j2s.lib"] = {
	sites : [
			"http://a.webuzz.im/",
			"http://a.java2script.org/",
			"http://archive.java2script.org/",
			"http://a.java2script.net/",
			"http://a.java2script.com/",
			"http://a.kexmail.com/",
			"http://a.izuz.net/",
			"http://a.uziz.net/"
		],
	index : 0,
	base : null,
	alias : j2sRelative, //"2.0.0-v20090310",
	version : "v" + j2sBuildDate, // "v20090310",
	/*forward : true,*/
	mode : "dailybuild",
	loaded : false,
	onload : function (script) {
		var o = window["j2s.lib"];
		if (o.loaded) {
			return;
		}
		o.loaded = true;
		var src = script.src;
		var loadingSite = "http://archive.java2script.org/";
		for (var i = 0; i < o.sites.length; i++) {
			if (src.indexOf (o.sites[i]) == 0) {
				loadingSite = o.sites[i];
				o.base = loadingSite;
				break;
			}
		}
		//var j2sRelative = (o.alias ? o.alias : o.version);
		ClazzLoader.packageClasspath ("java", loadingSite + j2sRelative, true);
		ClazzLoader.packageClasspath ("org.eclipse.swt", loadingSite + j2sRelative, true);
		//ClazzLoader.packageClasspath ("net.sf.j2s.store", loadingSite + j2sRelative);
		ClazzLoader.packageClasspath ("org.eclipse.swt.examples.controlexample", "http://demo.java2script.org/controls/bin/");
		ClazzLoader.setPrimaryFolder (loadingSite + j2sRelative);

		ClazzLoader.loadClass ("org.eclipse.swt.widgets.Display", function () {
			$wt.widgets.Display.getDefault();

if (window["webuzz.im.type"] == null) {
fixLink ("alaa-gtalk", ["--gtalk", "java2script.talk", "zzzzzzzz"], "g-logo.png");
fixLink ("alaa-msn", ["--msn", "java2script.talk@gmail.com", "zzzzzzzz"], "m-logo.png");
fixLink ("alaa-ymsg", ["--ymsg", "java2script.talk", "zzzzzzzz"], "y-logo.png");
fixLink ("alaa-aim", ["--aim", "j2stalk", "zzzzzzzz"], "a-logo.png");
fixLink ("alaa-icq", ["--icq", "582890450", "zzzzzzzz"], "i-logo.png");
fixLink ("alaa-jabber", ["--jabber", "java2script.talk@jabber.org", "zzzzzzzz"], "j-logo.png");
fixLink ("alaa-facebook", ["--facebook", "java2script.talk", "zzzzzzzz"], "b-logo.png");
fixLink ("alaa-wemail", "http://webuzz.im/mail/", "mail-logo.png");
}
var alaaMail = document.getElementById ("alaa-mail");
if (alaaMail != null) {
	alaaMail.target = "_blank";
}

			//ClazzLoader.packageClasspath (["com.swtdesigner", "im.webuzz", "im.webuzz.prefs"], loadingSite + zimRelative);
			ClazzLoader.setPrimaryFolder (loadingSite + zimRelative);
			//ClazzLoader.packageClasspath ("iz", loadingSite + zimRelative, true);
			//ClazzLoader.packageClasspath (["com.swtdesigner"], loadingSite + zimRelative);
			ClazzLoader.packageClasspath ("im.webuzz", loadingSite + zimRelative, true);
			ClazzLoader.packageClasspath (["com.swtdesigner", "im.webuzz.google.chrome", "im.webuzz.balance", "im.webuzz.space", "im.webuzz.news", "im.webuzz.background", "im.webuzz.webrowse", "im.webuzz.kaixinmeme.widget"], loadingSite + widgetRelative);

			ClazzLoader.loadClass ("im.webuzz.g.MainWindow", function () {
                                net.sf.j2s.ajax.SimplePipeRequest.switchToQueryMode (1000);
				//window.setTimeout (function () {
				//	im.webuzz.g.MainWindow.checkSpace ();
				//}, 3000);
				window.defaultWindowLeft = "200"
				window.defaultWindowTop = "28"
				var imType = parseIMType ();
				if (imType == null) {
					imType = window["webuzz.im.type"];
				}
				if (imType != null) {
					im.webuzz.g.MainWindow.main(["--" + imType]);
				} else {
					net.sf.j2s.store.SimpleStore.getDefault ().execute ({
						run : function () {
							var store = net.sf.j2s.store.SimpleStore.getDefault ();
							var p = store.getProperty ("G.pref");
							var delay = 2000;
							if (p != null) {
								window.setTimeout (function () {
									im.webuzz.g.MainWindow.main(["--gtalk"]);
								}, delay);
								delay += 2000;
							}
							p = store.getProperty ("M.pref");
							if (p != null) {
								window.setTimeout (function () {
									im.webuzz.g.MainWindow.main(["--msn"]);
								}, delay);
								delay += 2000;
							}
							if (delay >= 4000) {
								return; // ok 2 messengers at most
							}
							p = store.getProperty ("Y.pref");
							if (p != null) {
								window.setTimeout (function () {
									im.webuzz.g.MainWindow.main(["--ymsg"]);
								}, delay);
								delay += 2000;
							}
							if (delay >= 4000) {
								return; // ok 2 messengers at most
							}
							p = store.getProperty ("B.pref");
							if (p != null) {
								window.setTimeout (function () {
									im.webuzz.g.MainWindow.main(["--facebook"]);
								}, delay);
								delay += 2000;
							}
							if (delay >= 4000) {
								return; // ok 2 messengers at most
							}
							p = store.getProperty ("A.pref");
							if (p != null) {
								window.setTimeout (function () {
									im.webuzz.g.MainWindow.main(["--aim"]);
								}, delay);
								delay += 2000;
							}
						}
					});
				}
				ClazzLoader.loadClass ("im.webuzz.space.SpaceUtility", function () {
					window.setTimeout (function () {
						im.webuzz.space.SpaceUtility.checkSpace ();
					}, 2000);
				});
				if (O$.isIE) {
					//ClazzLoader.packageClasspath ("im.webuzz.google.chrome", loadingSite + "../im.webuzz.google.chrome/bin/");
					window.setTimeout (function () {
						ClazzLoader.loadClass ("im.webuzz.google.chrome.Chrome4IEWidget", function () {
							im.webuzz.google.chrome.Chrome4IEWidget.addChromeRecommendation ();
						});
					}, 750);
				}
			});

			ClazzLoader.loadClass ("im.webuzz.balance.BuzzAccessibility", function () {
				cleanJ2SBackground();
				im.webuzz.balance.BuzzAccessibility.setAccessibleOKCallback ({
					run : function () {
						ClazzLoader.loadClass ("im.webuzz.news.ServicesNewsWidget", function () {
							im.webuzz.news.ServicesNewsWidget.updateNews (im.webuzz.balance.BuzzAccessibility.getServiceServer() + "news/simplerpc");
							window.setTimeout (function () {
								ClazzLoader.loadClass ("im.webuzz.background.BingBackgroundWidget", function () {
									im.webuzz.background.BingBackgroundWidget.updateBackground (im.webuzz.balance.BuzzAccessibility.getServiceServer() + "bingbg/simplerpc");
								});
							}, 1000 + Math.round (Math.random () * 1000));
						});
						window.setTimeout (function () {
							ClazzLoader.loadClass ("im.webuzz.kaixinmeme.widget.WeBrowseWidget", function () {
								im.webuzz.kaixinmeme.widget.WeBrowseWidget.main([]);
							});
						}, 3000 + Math.round (Math.random () * 3000));
						/*
						window.setTimeout (function () {
							ClazzLoader.loadClass ("im.webuzz.webrowse.WeBrowseWidget", function () {
								im.webuzz.webrowse.WeBrowseWidget.main([]);
							});
						}, 3000 + Math.round (Math.random () * 3000));
						*/
					}
				});
				im.webuzz.balance.BuzzAccessibility.test ("u/c");
			});

			if (window["webuzz.im.type"] == null) {
			ClazzLoader.loadClass ("im.webuzz.ViewportSenseUtils", function () {
				if (document.getElementById ("gsidebar") == null) {
					im.webuzz.ViewportSenseUtils.startAdRotating();
				} else {
					im.webuzz.ViewportSenseUtils.switchingAds();
				}
			});
			}
		});
	}
};

{
	var sites = window["j2s.lib"].sites;
	var host = window.location.host.toLowerCase ();
	var segments = host.split (/\./);
	if (segments.length >= 2) {
		var domain = segments[segments.length - 2] + "." + segments[segments.length - 1];
		var keyPart = "." + domain + "/";
		for (var i = 0; i < sites.length; i++) {
			if (sites[i].indexOf (keyPart) == sites[i].length - keyPart.length) {
				if (i != 0) {
					var tmp = sites[i];
					for (var j = i; j >= 1; j--) {
						sites[j] = sites[j - 1];
					}
					sites[0] = tmp;
				}
				break;
			}
		}
	}
}

window.onbeforeunload = function () {
	var count = 0;
	if (window["ClazzLoader"] != null) {
		var g = Clazz.declarePackage ("im.webuzz.g");
		if (g.MainWindow != null) {
			var disp = org.eclipse.swt.widgets.Display.getDefault ();
			var shells = disp.getShells ();
			for (var i = 0; i < shells.length; i++) {
				var win = shells[i];
				if (win != null && !win.isDisposed ()
						&& win.getClass ().getName () == "im.webuzz.g.MainWindow"
						&& !win.logoutFromUI && !win.loginElsewhere) {
					win.setMinimized (false);
					win.setVisible (true);
					count++;
				}
			}
		}
	}
	if (count > 0) {
		return "You need to logout " + (count == 1 ? "given messenger" : "these " + count + " messengers") + " before leaving this page.";
	}
};

if (window["imSensed"] == null || !imSensed) {
	if (document.body != null) {
		loadJ2SApp ();
	}
}

window["webuzz.im.servers"] = [
	"http://z1.webuzz.im/",
	"http://z2.webuzz.im/",
	"http://z1.izuz.net/",
	"http://z2.izuz.net/",
	"http://z1.uziz.net/",
	"http://z2.uziz.net/",
	"http://z1.demo.java2script.org/",
	"http://z2.demo.java2script.org/",
	"http://z1.java2script.org/",
	"http://z2.java2script.org/",
	"http://z1.java2script.net/",
	"http://z2.java2script.net/",
	"http://z1.kexmail.com/",
	"http://z2.kexmail.com/",
	"http://z1.java2script.com/",
	"http://z2.java2script.com/"
];
/*
window["webuzz.im.pages"] = [
	"http://ziger.net/p/engadget-20090421.html",
	"http://ziger.net/p/techmeme-20090421.html",
	"http://ziger.net/p/digg-20090421.html",
];
*/
