﻿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(10,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(){
				if(Toolkit.hasTransformFeature())
					panel.className += " disappear";
				else
					panel.style.display = "none";
				Cookie.setCookie(
						"_contribute_wizard_close",
						"1",
						new Date(new Date().getTime() + 2*31*24*60*60*1000)
				);
			}
			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;
			}
			
			var a = document.createElement("a");
			a.id = "contributionIcon";
			a.style.position = "absolute";
			a.style.zIndex = "10";
			a.style.right = "80px";
			a.style.top = "4px";
			a.style.width = "20px";
			a.style.height = "20px";
			a.href = "javascript:void(0)";
			a.title = "This site is under contribution,Feel free to revamp it!";
			a.style.background = "url(http://www.itemon.com/chromeplus/res/layout/contribute.png) 0 0 no-repeat";
			c.parentNode.insertBefore(a,c);
			a.onclick = function(){
				if(Toolkit.hasTransformFeature())
					panel.className = panel.className.replace("disappear","");
				else
					panel.style.display = "block";
			}
			if(!Cookie.getCookie("_contribute_wizard_close")){
			}else{
				panel.style.display = "none";
				if(Toolkit.hasTransformFeature()){
					panel.className += " disappear";
					panel.style.display = "block";
				}
			}
			
		}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();
});
