﻿chromeplus.reserve("org.chromeplus.net.Cookie");
chromeplus.reserve("org.chromeplus.net.ClassAttributeList");
chromeplus.reserve("org.chromeplus.net.Timer");

Toolkit.lookupIsUnderContribution = function(){
	var defaultHL = document.documentElement.getAttribute("lang").toLowerCase(), approved = false;
	if(!chromeplus.Lang.isUndefined(document.querySelector)){
		approved = (document.querySelector("[class~='under-contribution'][hl='"+defaultHL+"']") != null);
	}else{
		for(var i=0,list=document.getElementById("worldwide").getElementsByTagName("a"),len=list.length;i<len;i++){
			if(list[i].className.contains("under-contribution") && list[i].getAttribute("hl") == defaultHL){
				approved = true;
				break;
			}
		}
	}
	return approved;
}

Toolkit.anonymousInvoke(function(){
	var isUnderContribution = Toolkit.lookupIsUnderContribution();
	if(!isUnderContribution && Cookie.getCookie("_contribute_wizard"))
		return;
	
	var c = document.getElementById("worldwide").parentNode;
	var panel = document.createElement("dd");
	c.appendChild(panel);
	panel.className = "contribute-our-site use-transition disappear";
	if(!isUnderContribution)
		panel.innerHTML = "Join us to build our worldwide sites collaboratively!<a target=\"_blank\" class=\"underline more\" href=\"doc/join-us-building-sites.html\">Read tutorial</a><a class=\"dialog-close\" href=\"javascript:void(0)\" title=\"Close\">X</a>";
	else
		panel.innerHTML = "This site is under contribution,Feel free to revamp it!<br /><a style='padding-left:0' target=\"_blank\" class=\"underline more\" href=\"doc/join-us-building-sites.html\">Read Tutorial</a><a class=\"underline more\" href=\"javascript:void(0)\">Revamp Now</a><a class=\"underline more\" href=\"javascript:void(0)\">Preview Current</a><a class=\"dialog-close\" href=\"javascript:void(0)\" title=\"Close\">X</a>";//
	var thread = new Timer(1000,function(){
		panel.className = "contribute-our-site use-transition";
		if(!Toolkit.hasTransformFeature())
			panel.className = "contribute-our-site";
		var links = panel.getElementsByTagName("a");
		
		if(isUnderContribution){
			links[0].onclick = links[3].onclick = function(){
				panel.parentNode.removeChild(panel);
			}
			links[1].onclick = function(){
				location.assign("doc/building-sites-manifest.php?pl="+document.documentElement.getAttribute("lang"));
			}
			links[2].onclick = function(){
				var search = location.search;
				if(search.contains("preview")){
					location.reload(true);
				}else{
					search = search.contains("?") ? search+"&preview=true" : "?preview=true";
					location.assign(location.protocol+"//"+location.host+location.pathname+search+location.hash);
				}
				return false;
			}
		}else{
			links[0].onclick = links[1].onclick = function(){
				panel.parentNode.removeChild(panel);
				Cookie.setCookie(
					"_contribute_wizard",
					"1",
					new Date(new Date().getTime() + 2*31*24*60*60*1000)
				);
			}
		}
		
	});
	thread.start();
});
