dojo.require("dojo.parser");
dojo.require("dojo.data.ItemFileReadStore");
dojo.require("dijit.layout.BorderContainer");
dojo.require("dijit.layout.AccordionContainer");
dojo.require("dijit.layout.TabContainer");
dojo.require("dijit.layout.ContentPane");
dojo.require("dijit.Tree");
dojo.require("dojo.fx.easing");
dojo.require("dojox.fx._core");
dojo.require("dijit.Dialog");

if(currentVersion === undefined){
	//	fallback.
	var currentVersion = "1.7";
}

var helpDialog;

//	redefine the base URL.
page = page || "";
	var _href = window.location.href.replace(window.location.protocol + '//' + window.location.hostname + '/','')
		.replace('jsdoc/', '')	//	to handle legacy api.dojotoolkit.org URL formation
		.replace(window.location.hash, '');
	baseUrl = window.location.protocol + "//" + window.location.hostname + "/" 
		+ _href.replace(currentVersion + "/", "").replace(page, "").replace(page.split("/").join("."), "").replace(".html","");
	//console.log("The new base URL is ", baseUrl);
	delete _href;

var classTree, classStore;
function smoothScroll(args){
	//	NB: this is basically dojox.fx.smoothScroll, but for some reason smoothScroll uses target.x/y instead
	//	of left/top.  dojo.coords is returning a different y than the top for some reason.  Maybe position will
	//	be better post 1.3.
	if(!args.target){ 
		args.target = dojo.coords(args.node, true);
	}
	var _anim = function(val){
		args.win.scrollLeft = val[0];
		args.win.scrollTop = val[1];
	};

	var anim = new dojo._Animation(dojo.mixin({
		beforeBegin: function(){
			if(this.curve){ delete this.curve; }
			var current = { x: args.win.scrollLeft, y: args.win.scrollTop };
			anim.curve = new dojox.fx._Line([ current.x, current.y ], [ args.target.l, args.target.t - 12 ]);
		},
		onAnimate: _anim
	}, args));
	return anim;
}

paneOnLoad = function(data){
	var context = this.domNode;
	dojo.query("a.jsdoc-link", this.domNode).forEach(function(link){
		//	UGLY FIX: linkage is coming back as relative links, so for DTK we need to make them "full".
		var h = dojo.attr(link, "href");
		dojo.attr(link, "href", baseUrl + h);

		//	set up the internal link handlers so that they open in new tabs instead of replacing the page.
		link.onclick = function(e){
			dojo.stopEvent(e);
			/*
			var tmp = this.href.replace('http://','')
				.replace('www.','')
				.replace('staging.', '')
				.replace('dojotoolkit.org/api/','').split('/');
			*/
			var l = window.location,
				tmp = this.href.replace(l.protocol + "//" + l.host + "/", "")
					.replace("api/", "").split("/");
			var version = tmp[0];
			var page = tmp.slice(1).join(".");
			addTabPane(page, version);
			return false;
		};
	});

	dojo.query("a.inline-link", this.domNode).forEach(function(link){
		link.onclick = function(e){
			dojo.stopEvent(e);
			var target = dojo.query('a[name="' + this.href.substr(this.href.indexOf('#')+1) + '"]', context);
			if(target.length){
				var anim = smoothScroll({
					node: target[0],
					win: context,
					duration: 600
				}).play();
			}
			return false;
		};
	});

	//	build the toolbar.
	var link = null, perm = dojo.query("div.jsdoc-permalink", context), l = window.location;
	if(perm.length){
		link = baseUrl + perm[0].innerHTML;
		link = link.replace('/jsdoc/','');
		/*
		if(link.indexOf('/api/') != -1) {
			link = link.replace('/api/','');
		}
		*/
	}
	
	var tbc = (link ? '<span class="jsdoc-permalink"><a class="jsdoc-link" href="' + link + '">Permalink</a></span>' : '')
		+ '<label>View options: </label>'
		+ '<span class="trans-icon jsdoc-private"><img src="' + baseUrl + 'css/icons/24x24/private.png" align="middle" border="0" alt="Toggle private members" title="Toggle private members" /></span>'
		+ '<span class="trans-icon jsdoc-inherited"><img src="' + baseUrl + 'css/icons/24x24/inherited.png" align="middle" border="0" alt="Toggle inherited members" title="Toggle inherited members" /></span>';
	var toolbar = dojo.create("div", {
		className: "jsdoc-toolbar",
		innerHTML: tbc		
	}, this.domNode, "first");

	//	if SyntaxHighlighter is present, run it in the content
	if(SyntaxHighlighter){
		SyntaxHighlighter.highlight();
	}

	var privateOn = false, inheritedOn = true;
	//	hide the private members.
	dojo.query("div.private, li.private", this.domNode).style("display", "none");

	//	make the summary sections collapsable.
	dojo.query("h2.jsdoc-summary-heading", this.domNode).forEach(function(item){
		dojo.connect(item, "onclick", function(e){
			var d = e.target.nextSibling;
			while(d.nodeType != 1 && d.nextSibling){ d = d.nextSibling; }
			if(d){
				var dsp = dojo.style(d, "display");
				dojo.style(d, "display", (dsp=="none"?"":"none"));
				dojo.query("span.jsdoc-summary-toggle", e.target).forEach(function(item){
					dojo[(dsp=="none"?"removeClass":"addClass")](item, "closed");
				});
			}
		});

		dojo.query("span.jsdoc-summary-toggle", item).addClass("closed");

		//	probably should replace this with next or something.
		var d = item.nextSibling;
		while(d.nodeType != 1 && d.nextSibling){ d = d.nextSibling; }
		if(d){
			dojo.style(d, "display", "none");
		}
	});

	//	set up the buttons in the toolbar.
	dojo.query("div.jsdoc-toolbar span.trans-icon", this.domNode).forEach(function(node){
		if(dojo.hasClass(node, "jsdoc-private")){
			dojo.addClass(node, "off");
			dojo.connect(node, "onclick", dojo.hitch(this, function(e){
				privateOn = !privateOn;
				dojo[(privateOn ? "removeClass" : "addClass")](node, "off");
				dojo.query("div.private, li.private", this.domNode).forEach(function(n){
					var state = (privateOn ? "" : "none");
					dojo.style(n, "display", state);
				});
			}));
		} else {
			dojo.connect(node, "onclick", dojo.hitch(this, function(e){
				inheritedOn = !inheritedOn;
				dojo[(inheritedOn ? "removeClass" : "addClass")](node, "off");
				dojo.query("div.inherited, li.inherited", this.domNode).forEach(function(n){
					var state = (inheritedOn ? "" : "none");
					if(!(!privateOn && dojo.hasClass(n, "private"))){
						dojo.style(n, "display", state);
					}
				});
			}));
		}
	});

	//	set the title
	var w = dijit.byId("content").selectedChildWidget;
	
	//	finally set the content of the printBlock.
	dojo.byId("printBlock").innerHTML = w.domNode.innerHTML;

	(function(pane){
		var helpLink = dojo.create("a", {
			"class": "feedback",
			href: "https://docs.google.com/spreadsheet/viewform?hl=en_US&formkey=dFlDcHEyaHMwbEd4MFBObkNrX0E1MFE6MQ&entry_0=" + encodeURIComponent(link),
			target: "_blank",
			innerHTML: "Error in the documentation? Can’t find what you are looking for? Let us know!"
		}, pane.domNode);

		dojo.connect(helpLink, "onclick", function(event){
			if(!event.button && !event.metaKey && !event.ctrlKey && !event.shiftKey && !event.altKey){
				dojo.stopEvent(event);
				helpDialog.set("content", dojo.create("iframe", {
					src: this.href,
					frameborder: "0",
					style: "width: 47em; height: 500px; border: 0 none"
				}));
				helpDialog.show();
			}
		});
	}(this));

	//	check to see if there's any "fake hashes" in the returned markup and do something about it.
	//	This is here in case someone came from an old URL that drilled down to the field name of an object.
	var hashCheck = dojo.query("div.jsdoc-hash-reference", this.domNode);
	if(hashCheck.length){
		//	TODO: try to change the canonical link too?  Is that even worth it?
		var hash = hashCheck[0].innerHTML, 
			title = this.attr("title");
		title = title.replace("." + hash, "");
		this.attr("title", title);
		document.title = title + " - " + (siteName || "The Dojo Toolkit");
		var target = dojo.query('a[name$="' + hashCheck[0].innerHTML + '"]', this.domNode);
		if(target.length){
			var anim = smoothScroll({
				node: target[0],
				win: this.domNode,
				duration: 600
			}).play();
		}
	} else {
		document.title = w.title + " - " + (siteName || "The Dojo Toolkit");
	}
};

addTabPane = function(page, version){
	var p = dijit.byId("content");
	var url = baseUrl + "lib/item.php?p=" + page.split(".").join("/") + "&v=" + (version || currentVersion);
	var title = page.split("/").join(".");
	
	//	get the children and make sure we haven't opened this yet.
	var c = p.getChildren();
	for(var i=0; i<c.length; i++){
		if(c[i].title == title){
			p.selectChild(c[i]);
			return;
		}
	}
	var pane = new dijit.layout.ContentPane({ 
		href: url, 
		title: title, 
		closable: true,
		parseOnLoad: false,
		onLoad: dojo.hitch(pane, paneOnLoad)
	});
	p.addChild(pane);
	p.selectChild(pane);
	return pane;
};

buildTree = function(){
	//	handle changing the tree versions.
	if(classTree){
		classTree.destroyRecursive();
	}

	//	load the class tree data.
	var sendURL = (baseUrl + 'lib/class-tree.php?v=' + currentVersion).replace('/jsdoc/','/');
	classStore = new dojo.data.ItemFileReadStore({
		url: sendURL
	});

	classTree = new dijit.Tree({
		store: classStore,
		query: { type: 'root' },
		getIconClass: function(item, opened){
			if(!item){ return "icon16 objectIcon16"; }
			if(item == this.model.root) {
				return "icon16 namespaceIcon16";
			} else {
				if(classStore.getValue(item, "type") == "root"){
					if(classStore.getValue(item, "name") == "djConfig"){
						return "icon16 objectIcon16";
					}
					return "icon16 namespaceIcon16";
				} else {
					return "icon16 " + classStore.getValue(item, "type") + "Icon16";
				}
			}
		},
		onClick: function(item){
			addTabPane(classStore.getValue(item, 'fullname'), currentVersion);
		}
	});
	dijit.byId("classTreePane").domNode.appendChild(classTree.domNode);
};

versionChange = function(e){
	var cv = currentVersion, v = this.options[this.selectedIndex].value;
	if(v.length){
		// switch to the current version and reload the tree.
		currentVersion = v;
		//	TODO: reload the trees.
	} else {
		//	revert the selection.
		for(var i=0, l=this.options.length; i<l; i++){
			if(this.options[i].value == currentVersion){
				this.selectedIndex = i;
				v = this.options[this.selectedIndex].value;
				break;
			}
		}
	}

	//	if we reverted, bug out.
	if(cv == v){ return; }
	currentVersion = v;
	buildTree();
};

dojo.addOnLoad(function(){
	var w = dijit.byId("content");
	if(w){
		dojo.subscribe(w.id + "-selectChild", w, function(arr){
			document.title = this.selectedChildWidget.title + " - " + (siteName || "The Dojo Toolkit");
			dojo.byId("printBlock").innerHTML = this.selectedChildWidget.domNode.innerHTML;
		});
	}

	// global:
	helpDialog = new dijit.Dialog({ title: "Feedback" }).placeAt(document.body);
	helpDialog.startup();

	var s = dojo.byId("versionSelector");
	s.onchange = dojo.hitch(s, versionChange);

	buildTree();

	if(page && currentVersion) {
		var p = addTabPane(page, currentVersion);

		//	handle any URL hash marks.
		if(p && window.location.hash.length){
			var h = dojo.connect(p, "onLoad", function(){
				dojo.disconnect(h);
				var target = dojo.query('a[name$="' + window.location.hash.substr(window.location.hash.indexOf('#')+1) + '"]', p.domNode);
				if(target.length){
					var anim = smoothScroll({
						node: target[0],
						win: p.domNode,
						duration: 600
					}).play();
				}
			});
		}
	}
});

