var siteroot = (typeof(fullsiteroot) == "function") && (typeof(sitehost) == "function") ? fullsiteroot().replace(sitehost(), "") : "/";

try {document.execCommand("backgroundimagecache", false, true)} catch (e) {}

var Cookie={
	set:function(c_name,value) {
		var today = new Date;
		today.setDate(today.getDate()+3650);
		document.cookie=c_name + "=" + escape(value) + ";expires=" + today.toGMTString() + ";path=/";
	},
	get:function(c_name) {
		if (document.cookie.length>0) {
			c_start=document.cookie.indexOf(c_name + "=");
			if (c_start!=-1) {
				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));
			}
		}
		return null;
	},
	remove:function(c_name) {this.set(c_name,"",-1);}
}

Event.observe(window, "load" ,function() {
	var Expandable = {
		Batchers: $A(),
		Items : $A(),
		BatchState : false,
		Id : "expandable_" + location.pathname.replace(".ssx", ""),
		duration : 0.3,
		Toggle : function() {
			var container = this.parentNode.parentNode.parentNode;
			var content = this.parentNode.nextSibling;
			if (!container.lock && content) {
				var expanded = arguments.length == 1 && "boolean" == typeof arguments[0] ? !arguments[0] : Element.hasClassName(container, "expanded");
				if (expanded) {

					Expandable.Items.each(function(e, index) {
						var c = e.parentNode.parentNode.parentNode;
						if (c == container && Cookie.get(Expandable.Id) == index) {
							Cookie.remove(Expandable.Id);
						}
					});
					Effect.BlindUp(content, {duration:Expandable.duration});
					container.lock = setTimeout(function() {
						Element.addClassName(this, "collapsed");
						Element.removeClassName(this, "expanded");
						if (document.getElementsByClassName("expanded").length == 0) {
							Expandable.Batchers.each(function(e) {
								Element.cleanWhitespace(e);
								var text = e.firstChild.title.split("/");
								e.firstChild.innerHTML = text[0];
							})
						}
						this.lock = false;
					}.bind(container), Expandable.duration * 1000);
				}
				else {
					Expandable.Items.each(function(e, index) {
						var c = e.parentNode.parentNode.parentNode;
						if (c == container) {
							Cookie.set(Expandable.Id, index);
						}
					});
					Expandable.Items.each(function(e, index) {
						var c = e.parentNode.parentNode.parentNode;
						if (Element.hasClassName(c, "expanded")) {
							Expandable.Toggle.apply(e);
						}
					});
					Element.addClassName(container, "expanded");
					Element.removeClassName(container, "collapsed");
					Effect.BlindDown(content, {duration:Expandable.duration});
					if (document.getElementsByClassName("collapsed").length == 0) {
						Expandable.Batchers.each(function(e) {
							Element.cleanWhitespace(e);
							var text = e.firstChild.title.split("/");
							e.firstChild.innerHTML = text[1];
						})
					}
				};
			};
		},
		Batch : function() {
			Element.cleanWhitespace(this);
			Expandable.BatchState = !Expandable.BatchState;
			Expandable.Items.each(function(e) {
				Expandable.Toggle.apply(e, [Expandable.BatchState]);
			});
			var text = this.firstChild.title.split("/");
			if (text.length == 2) {
				this.firstChild.innerHTML = this.firstChild.innerHTML == text[0] ? text[1] : text[0];
			}
		}
	};
	var Redirect = {
		container:{}
	};
	Behaviour.register({
		"button.expander" : function(button) {
			Expandable.Batchers.push(button);
			Event.observe(button, "click", Expandable.Batch.bind(button));
		},
		"a.expander_focus" : function(link) {
			Event.observe(link, "click", function() {
				var anchors = document.getElementsBySelector("a[name="+link.hash.substring(1)+"]");
				if (anchors && anchors[0]) {
					Expandable.Toggle.apply(anchors[0], [true]);
				}
			}.bind(link))
		},
		"div.moduledesc div.bottom h2 a" : function(title) {
			if (title.href.indexOf("javascript:") > -1) {
				Expandable.Items.push(title);
				Element.cleanWhitespace(title.parentNode.parentNode);
				Event.observe(title, "click", Expandable.Toggle.bind(title));
			}
		},
		"ul.contributors a" : function(title) {
			Element.cleanWhitespace(title.parentNode);
			Event.observe(title, "mouseover", function() {
				var popup = title.nextSibling;
				if (popup) Element.addClassName(popup, "visible");
			}.bind(title));
			Event.observe(title, "mouseout", function() {
				var popup = title.nextSibling;
				if (popup) Element.removeClassName(popup, "visible");
			}.bind(title));
		},
/*		"a[target=_blank]" : function(link) {
			if (link.id != "exiter") {
				link.redirect = link.href;
				link.href = "javascript://popup: " + link.href;
				link.target = "_self";
				Event.observe(link, "click", function() {
					var redirectUrl = Element.hasClassName(this, "internal") ? "" : document.getElementById("outboundRedirect").getAttribute("value") + "?page=";
					Redirect.container = window.open(redirectUrl + this.redirect,"","width=400,height=420");
					Redirect.container.focus();
					return false;
				}.bind(link));
			}
			else {
				Event.observe(link, "click", function() {
					window.close();
					window.open(link.href, "","width=800,height=475,scrollbars,location,status,toolbar,menubar,resizable,scrollbars")
					return false;
				}.bind(link));
			}
		},
*/
		".primarynav a" : function(link){
			if (link.href.indexOf("void(0)") >= 0) Element.addClassName(link,"disablednav");
		},
		".secondarynav a" : function(link){
			if (link.href.indexOf("void(0)") >= 0) Element.addClassName(link,"disablednav");
		}
	});
	Behaviour.apply();
	var current = Cookie.get(Expandable.Id)
	if (Expandable.Items[current]) {
		Expandable.Toggle.apply(Expandable.Items[current]);
	}
	var q = location.search.replace("?","").split("&");
	for (var i = 0, l = q.length; i < l; i++) {
		var pair = q[i].split("=");
		switch (pair[0]) {
			case "open":
				var element = document.getElementById(pair[1]);
				if (element && !Element.hasClassName(element.parentNode.parentNode.parentNode, "expanded")) {
					Cookie.remove(Expandable.id);
					Expandable.Toggle.apply(element);
				}
				break;
		}
	}

	//embed info session flash
	if ($("infoSessionFlash")) {
		var flashvars = {};
		var attributes = {};
		var params = {allowScriptAccess: "always", wmode:"transparent"};
		var flashFile = siteroot + "local/flash/map_f8_545x470.swf";
		var targetDiv = "infoSessionFlash";
		swfobject.embedSWF(flashFile,targetDiv, "545", "470", "9","", flashvars, params, attributes);
	}

});

var toggleBox = function(id, status) {
	Element[status ? "removeClassName" : "addClassName"](document.getElementById(id), "nextdisabled");
}

var disableNext = function() {
	toggleBox("buttonNext", false);
}

var provClick = function(prov) {
	var content = 'info_' + prov;
	var ele = $('sessionInfo');
	var provContent = $(content).innerHTML;
	$('sessionInfoContent').innerHTML = provContent;
	pageTracker._trackEvent('Map of Canada', prov, (provContent.indexOf('infoTable')>0 ? 'Has Sessions' : 'No Sessions'));
	if (prov=="ON" || prov=="QC") {
		ele.className = 'longSessionInfo';
	} else {
		ele.className = '';
	}
	$('sessionInfoCloser').className='sessionInfoCloser';
	ele.scrollTop = 0;
}

var sessionInfoCloser = function(){
	$('sessionInfo').className='hidden';
	$('sessionInfoCloser').className='hidden';
}
