/////////////// MACROMEDIA FUNCTIONS ///////////////

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


// JavaScript Document

var WISL = {
	hadAnchorChange: false,
	init: function() {
		Event.observe(window, "load", this.handleWindowLoad.bind(this));
		window.dhtmlHistory.create({
			toJSON: function(o) {
				return Object.toJSON(o);
			},
			fromJSON: function(s) {
				return s.evalJSON();
			}
		});
	},
	gotoMainPage: function(p_url, b_close) {
		var b_opener = true;
		try {
			var od = opener.document;
		} catch (e) {
			b_opener = false;
		}
		if (b_opener) {
			opener.document.location.href = p_url;
			b_close ? window.close() : opener.focus();
		} else {
			var win = window.open(p_url, "linkOpener");
			b_close ? window.close() : win.focus();
		}
	},
	handleWindowLoad: function() {
		//alert("handleWindowLoad");
		setTimeout(this.startup.bind(this), 100);
	},
	startup: function() {
		//alert("startup");
		this.startupNavLinks();
		dhtmlHistory.initialize();
		dhtmlHistory.addListener( this.handleAnchorChange.bind(this) );
		if (dhtmlHistory.isFirstLoad()) this.handleAnchorChange(dhtmlHistory.getCurrentLocation());
	},
	debugLink: function() {
		alert("This link is not functional yet - needs content.");
	},
	
	// navigation setup
	mainNavLinks: null,
	p2navLinks: null,
	p3hovers: null,
	p4popup: null,
	p5topics: null,
	startupNavLinks: function() {
		this.mainNavLinks = new NavGroup("mainNavLinks");
		this.mainNavLinks.title = $("imgPageTitle");
		this.mainNavLinks.images = ["images/sl_title_what_is_sl.gif","images/sl_title_what_makes_it_work.gif","images/sl_title_how_do_use.gif","images/sl_title_sl_cycle.gif","images/sl_title_lets_get_going.gif"];
		MM_preloadImages.apply(window, this.mainNavLinks.images);
		for (var i=1; i<=5; i++) {
			var img = $("mainNavLink"+i);
			if (img) this.mainNavLinks.addItem( new MainPageLink(img, i) );
		}
		this.p2navLinks = new NavGroup("p2navLinks",["mainNavLinks"]);
		this.p2navLinks.imagePrefixes = ["images/s2_btn_1_","images/s2_btn_2_","images/s2_btn_3_","images/s2_btn_4_","images/s2_btn_5_","images/s2_btn_6_","images/s2_btn_7_","images/s2_btn_8_"];
		for (var i=1; i<=8; i++) {
			var img = $("p2navLink"+i);
			if (img) this.p2navLinks.addItem( new Page3NavLink(img, i) );
		}
		this.p3hovers = [
			new HoverImage("p2navHover1", "images/s3_buttons_elementary_"),
			new HoverImage("p2navHover2", "images/s3_buttons_middle_"),
			new HoverImage("p2navHover3", "images/s3_buttons_high_")
		];
		this.p4popup = new PopupCycle("p4preview", "p4close", "p4over","p4intro");
		this.p5topics = new NavGroup("p5topics",["mainNavLinks"]);
		this.p5topics.cssPrefixes = ["dd_title_implement_", "dd_title_meet_", "dd_title_publications_"];
		for (var i=1; i<=3; i++) this.p5topics.addItem( new ToggleTopic(i) );
	},
	
	// RSH navigation functions
	handleAnchorChange: function(newLocation) {
		if (dhtmlHistory.isSafari) newLocation = WISL.getManualLoc(); // override the built-in RSH functionality, as it's ##*(#@($&!! in Safari (off-by-1 errors in history stack, at best)
		newLocation = unescape(newLocation); // just in case
		//alert("newLocation=" + newLocation);
		this.hadAnchorChange = true;
		// clean up non-navigational elements
		this.p4popup.hide();
		// process each nav group
		var o = (newLocation) ? newLocation.toQueryParams("|") : {};
		for (var a in this) {
			if (this[a] instanceof NavGroup) {
				var idx = (o[a]) ? parseInt(o[a]) : 1;
				this[a].select(idx);
			}
		}
	},
	getManualLoc: function() {
		var h = document.location.hash;
		return (h.indexOf("#") == 0) ? h.substr(1, h.length) : h;
	}
}

// Application Startup
WISL.init();



// CLASS NavGroup
var NavGroup = Class.create();
NavGroup.prototype = {
	name: null,
	parentGroups: null,
	current: null,
	items: null,
	initialize: function(name, parentGroups) {
		this.name = name;
		this.parentGroups = $A(parentGroups);
		this.items = [];
	},
	clicked: function(obj) {
		// navigate using anchors
		if (this.current == obj) return;
		var a = "";
		this.parentGroups.each(function(value, idx){
			a += (a.length==0) ? "" : "|";
			a += value + "=" + WISL[value].current.index;
		});
		a += (a.length==0) ? "" : "|";
		a += this.name + "=" + obj.index;
		//document.location.href = "#" + a;
		dhtmlHistory.add(a);
		WISL.handleAnchorChange(a);
	},
	select: function(obj) {
		// actually update the screen visuals
		if (typeof(obj) == "number") obj = this.items[obj-1];
		if (this.current == obj) return;
		this.unselect();
		this.current = obj;
		this.current.select();
	},
	unselect: function() {
		if (this.current) this.current.unselect();
	},
	addItem: function(navitem) {
		this.items.push( navitem );
		navitem.owner = this;
		if (typeof(navitem["preloadImages"]) == "function") navitem.preloadImages();
	}
};


// ABSTRACT CLASS NavGroup
var NavLink = Class.create();
NavLink.prototype = {
	owner: null,
	element: null,
	index: 0,
	initialize: function(elm, idx) {
		this.element = elm;
		this.index = idx;
		Event.observe(this.element, "click", this.handleClick.bind(this));
		Event.observe(this.element, "mouseover", this.handleMouseOver.bind(this));
		Event.observe(this.element, "mouseout", this.handleMouseOut.bind(this));
	},
	
	// extend these functions to provide enhancements
	handleClick: function() {
		this.owner.clicked(this);
	},
	handleMouseOver: function() {
		if (this.owner.current != this) this.showOn();
	},
	handleMouseOut: function() {
		if (this.owner.current != this) this.showOff();
	},
	
	// abstract functions - must override (no need to $super though)
	select: function() {},
	unselect: function() {},
	showOn: function() {},
	showOff: function() {},
	
	// utility functions, override if desired
	toString: function() {
		return "Object[NavLink:index=" + this.index + "]";
	}
};


// CLASS MainPageLink extends NavLink
var MainPageLink = Class.create(NavLink, {
	initialize: function($super, elm, idx) {
		$super(elm, idx);
		this.page = $("page"+idx);
	},
	select: function() {
		this.showOn();
		if (this.page) this.page.style.display = "block";
		// update the page title
		this.owner.title.src = this.owner.images[this.index-1];
	},
	unselect: function() {
		this.showOff();
		if (this.page) this.page.style.display = "none";
	},
	showOn: function() {
		this.element.src = "images/nav_square_" + this.index + "_on.gif";
	},
	showOff: function() {
		this.element.src = "images/nav_square_" + this.index + "_off.gif";
	},
	preloadImages: function() {
		this.preloads = [];
		this.preloads[0] = new Image(); this.preloads[0].src = "images/nav_square_" + this.index + "_on.gif";
		this.preloads[1] = new Image(); this.preloads[1].src = "images/nav_square_" + this.index + "_off.gif";
	}
});


// CLASS Page3NavLink extends NavLink
var Page3NavLink = Class.create(NavLink, {
	initialize: function($super, elm, idx, prefix) {
		$super(elm, idx);
		this.prefix = prefix;
		this.page = $("p2navPage"+idx);
	},
	select: function() {
		this.showOn();
		if (this.page) this.page.style.display = "block";
	},
	unselect: function() {
		this.showOff();
		if (this.page) this.page.style.display = "none";
	},
	showOn: function() {
		this.element.src = this.owner.imagePrefixes[this.index-1] + "on.gif";
	},
	showOff: function() {
		this.element.src = this.owner.imagePrefixes[this.index-1] + "off.gif";
	},
	preloadImages: function() {
		this.preloads = [];
		this.preloads[0] = new Image(); this.preloads[0].src = this.owner.imagePrefixes[this.index-1] + "on.gif";
		this.preloads[1] = new Image(); this.preloads[1].src = this.owner.imagePrefixes[this.index-1] + "off.gif";
	}
});


// CLASS HoverImage
var HoverImage = Class.create();
HoverImage.prototype = {
	image: null,
	prefix: null,
	initialize: function(id, prefix) {
		this.image = $(id);
		this.prefix = prefix;
		Event.observe(this.image, "mouseover", this.handleMouseOver.bind(this));
		Event.observe(this.image, "mouseout", this.handleMouseOut.bind(this));
		this.preload();
	},
	preload: function() {
		MM_preloadImages(this.prefix + "on.gif", this.prefix + "off.gif");
	},
	handleMouseOver: function() {
		this.image.src = this.prefix + "on.gif";
	},
	handleMouseOut: function() {
		this.image.src = this.prefix + "off.gif";
	}
};


// CLASS PopupCycle
var PopupCycle = Class.create();
PopupCycle.prototype = {
	div: null,
	open: null,
	initialize: function(c_open, c_close, c_div, c_intro) {
		this.div = $(c_div);
		this.open = $(c_open);
		this.intro = $(c_intro);
		Event.observe(c_open, "click", this.handleOpenClick.bind(this));
		Event.observe(c_open, "mouseover", this.handleOpenOver.bind(this));
		Event.observe(c_open, "mouseout", this.handleOpenOut.bind(this));
		Event.observe(c_close, "click", this.handleCloseClick.bind(this));
		this.preload();
	},
	preload: function() {
		MM_preloadImages('images/s4a_graph.gif','images/s4a_graph_over.gif','images/s4_box_top.gif','images/s4_box_mdl.gif','images/s4_box_bot.gif');
		MM_preloadImages('objects/cycle/servicelearningc_r2_c16_f2.jpg','objects/cycle/servicelearningc_r3_c10_f2.jpg','objects/cycle/servicelearningc_r7_c18_f2.jpg','objects/cycle/servicelearningc_r8_c3_f2.jpg','objects/cycle/servicelearningc_r12_c7_f2.jpg','objects/cycle/servicelearningc_r13_c20_f2.jpg','objects/cycle/servicelearningc_r14_c2_f2.jpg','objects/cycle/servicelearningc_r14_c8_f2.jpg','objects/cycle/servicelearningc_r14_c14_f2.jpg','objects/cycle/servicelearningc_r16_c19_f2.jpg','objects/cycle/servicelearningc_r17_c6_f2.jpg','objects/cycle/servicelearningc_r19_c11_f2.jpg','objects/cycle/servicelearningc_r19_c15_f2.jpg','objects/cycle/slstudentcommownership.gif','objects/cycle/slidentifying.gif','objects/cycle/slestablishing.gif','objects/cycle/slreflection.gif','objects/cycle/slreflectionduring.gif','objects/cycle/slpostservice.gif','objects/cycle/slplanning.gif','objects/cycle/slmeaningful.gif','objects/cycle/slanalysis.gif','objects/cycle/slevaluation.gif','objects/cycle/slcelebration.gif','objects/cycle/slnewapplications.gif','objects/cycle/slobservation.gif');
	},
	handleOpenClick: function() {
		this.show();
	},
	handleOpenOver: function() {
		this.open.className = "screen4_column1_ovr";
	},
	handleOpenOut: function() {
		this.open.className = "screen4_column1";
	},
	handleCloseClick: function() {
		this.hide();
	},
	show: function() {
		this.intro.style.display = "none";
		this.div.style.display = "block";
	},
	hide: function() {
		this.intro.style.display = "block";
		this.div.style.display = "none";
	}
};


// CLASS ToggleTopic
var ToggleTopic = Class.create();
ToggleTopic.prototype = {
	owner: null,
	index: 0,
	showing: false,
	over: false,
	prefix: null,
	title: null,
	content: null,
	initialize: function(idx) {
		this.index = idx;
		this.title = $("p5topic" + idx);
		this.content = $("p5content" + idx);
		Event.observe(this.title, "click", this.handleClick.bind(this));
		Event.observe(this.title, "mouseover", this.handleMouseOver.bind(this));
		Event.observe(this.title, "mouseout", this.handleMouseOut.bind(this));
		this.preload();
	},
	preload: function() {
		MM_preloadImages('images/arrow.gif','images/dd_title_implement_open.gif','images/dd_title_meet_open.gif','images/dd_title_publications_open.gif','images/dd_title_implement_closed.gif','images/dd_title_meet_closed.gif','images/dd_title_publications_closed.gif','images/dd_title_implement_closed_over.gif','images/dd_title_meet_closed_over.gif','images/dd_title_publications_closed_over.gif');
	},
	handleClick: function() {
		this.owner.clicked(this);
	},
	handleMouseOver: function() {
		this.over = true;
		if (this.owner.current != this) this.showOn();
	},
	handleMouseOut: function() {
		this.over = false;
		if (this.owner.current != this) this.showOff();
	},
	select: function() {
		this.setVisible(true);
	},
	unselect: function() {
		this.setVisible(false);
	},
	showOn: function() {
		this.title.className = this.owner.cssPrefixes[this.index-1] + "closed_over";
	},
	showOff: function() {
		this.title.className = this.owner.cssPrefixes[this.index-1] + "closed";
	},
	setVisible: function(b_show) {
		this.content.style.display = (b_show) ? "block" : "none";
		this.title.className = this.owner.cssPrefixes[this.index-1] + ((b_show) ? "open" : (this.over) ? "closed_over" : "closed");
		this.showing = b_show;
	}
};
