var WS_SWF, CL_CLIENTS

CL_CLIENTS = null;

function ucFirst(string) {
	if (('undefined' != typeof string) && (string.length > 0)) {
		return string.charAt(0).toUpperCase() + string.slice(1);
	}
	
	return "";
}

document.observe("dom:loaded", function() {
	//swfobject activation
	WS_SWF = new ws_swfobject({
		'ws-clock': {
			'params': {
				'movie': '/release/includes/flash/good-old-clock.swf',
				'wmode': 'transparent'
			}
		}
	})
	
	//related items (with tabs)
	$$("div.related-items").each(function(div) {
		new tab_control(div);
	});
	
	if (gallery_ctrl) {
		//large gallery items
		$$(".gallery").each(function(element) {
			new gallery_ctrl(element);
		});
		
		$$(".gallery-list").each(function(element) {
			new gallery_ctrl(element);
		});
	}

	if (small_gallery_ctrl) {	
		//small gallery items
		$$(".gallery-small").each(function(element) {
			var settings = null;
			
			if (element.id == "staff-gallery-small") {
				settings = {
					'width': 260,
					'height': 72,
					'append_class': 'staff-zoomer',
					'timeout': 5
				};
				
			}
			
			new small_gallery_ctrl(element, settings);
		});
	}
	
	//handle toolbar buttons
	$$("ul.toolbar").each(function(toolbar) {
		toolbar.getElementsBySelector("li").each(function(button) {
			switch (button.readAttribute("rel")) {
				case "back":
				case "gallery-back":
					button.observe("click", function(event) {
						event.stop();
						window.history.back();
					});
					break;
			}
		});
	});
	
	//news archive fold-ups
	$$("h3.news-archive-year").each(function(h3, index) {
		if (index > 0) {
			h3.observe("click", function(event) {
				event.stop();
				h3 = event.findElement("h3");

				if ($(h3.id + "-months")) {
					$(h3.id + "-months").toggle();
				}
			});

			$(h3.id + "-months").toggle();
			h3.addClassName("archive-year-toggle");
		}

		// /news/year/2010/month/june/
		if (location.pathname.indexOf("/year/" + h3.innerHTML + "/") >= 0) {
			var details = location.pathname.split("/");
			if (details.length > 3) {
				$("body").down("h1").innerHTML = "What we were up to in " + ucFirst(details[5]) + " " + details[3];
			} else {
				$("body").down("h1").innerHTML = "What we were up to in " + details[3];
			}
		}

	});
}, this);

document.observe("lightview:loaded", function() {
	$$("div.article a").each(function(anchor) {
		if (anchor.readAttribute("href").match(/(jpeg|gif|png)$/)) {
			anchor.observe('click', function(event) {
				var anchor = event.findElement("a");
				event.stop();
				Lightview.show({ href: anchor.readAttribute("href"), rel: 'image' });
			});
		}
	});
});

Event.observe(window, "load", function() {
	var ws_gmap = $("ws-gmap");
		
	if (ws_gmap != null) {
		new gmap(ws_gmap);
	}
});

var tab_control = Class.create({
	initialize: function(div) {
		var controls, tab_wrapper;
		
		controls = div.getElementsBySelector("*[id^='tab-control']");
		
		if (controls.length <= 1) {
			return false;
		}
		
		this.container = div;
		this.controls = controls;
		this.tabs = {};
		this.current_tab = null;
		
		this.interactive = false;
		this.pulses = 0;
		
		this.options = {
			'active_control_class': 'tab-control-active',
			'pulse_interval': 5000,
			'max_pulses': 3
		}
		
		//create control container
		this.control_container = new Element("div", {
				'class': 'tab-controls'
		});
		
		this.container.insert({
			top: this.control_container
		});
		
		//gather up all tab controls and associated tabs
		this.controls.each(function(control, index) {
			var tab_id, tab, control_id, control_link;
			
			//place control into container
			this.control_container.insert(control);
			
			//extract associated tab id
			tab_id = control.id.match(/tab-control-(.*)/i)[1];
			tab = $(tab_id);
			control.getStorage().set("tab_id", tab_id);
			
			if (tab != null) {
				this.tabs[tab_id] = {
					'tab_id': tab_id,
					'tab': tab
				};
			}
			
			//add click event to control, or valid link within control
			control_link = control.down("a[rel = '" + tab_id + "']");
			
			control.insert(new Element("span").insert(
				new Element("a", {
					'href': control_link.readAttribute("href"),
					'title': "See all " + (control_link.textContent ? control_link.textContent : control_link.innerText)
				}).update("See all")
			));
			
			//set event on control element (or internal link)
			if (control_link != null) {
				this.tabs[tab_id]['control_link'] = control_link;
				
				control_link.addClassName("tab-control");
				control_link.observe("click", function(event) {
					this.show(control.getStorage().get("tab_id"));
					event.stop();
				}.bind(this));
			} else {
				control.observe("click", function() {
					this.show(control.getStorage().get("tab_id"));
				}.bind(this));
			}
		}, this);
		
		//place tabs into parent containers for animation purposes
		var index = 0, tab_wrapper;
		
		for (var tab_id in this.tabs) {
			tab = this.tabs[tab_id];
			tab_wrapper = new Element("div", {
				'class': 'tab-wrapper'
			});
			
			this.control_container.insert({
				bottom: tab_wrapper.insert(tab['tab'])
			});
			
			tab_wrapper.writeAttribute("id", tab['tab'].readAttribute("id"));
			tab['tab'].writeAttribute("id", null);
			
			tab['tab'] = tab_wrapper;
			
			if (index++ != 0) {
				tab_wrapper.hide();
			} else {
				this.current_tab = tab;
				
				if (this.current_tab['control_link'] != null) {
					this.current_tab['control_link'].addClassName(this.options.active_control_class);
				}
			}
		};
	},
	
	show: function(tab_id) {
		//show a particular tab - get new tab
		var tab = this.tabs[tab_id];
		
		if (tab == this.current_tab) {
			return false;
		}
		
		this.interactive = true;
		
		//turn off current control link
		if (this.current_tab['control_link'] != null) {
			this.current_tab['control_link'].removeClassName(this.options.active_control_class);
		}
		
		if (tab != null) {
			Effect.SlideUp(this.current_tab['tab'], {
				duration: 0.7,
				afterFinish: function() {
					window.setTimeout(function() {
						Effect.SlideDown(tab['tab'], {
							duration: 0.3
						});
					}, 200)
				}.bind(this)
			});
			
			if (tab['control_link'] != null) {
				tab['control_link'].addClassName(this.options.active_control_class);
			}
			
			this.current_tab = tab;
		}
	}
});

var ws_swfobject = Class.create({
	initialize: function(settings) {
		this.settings = settings || {};
		
		$$(".swfobject").each(function(el) {
			var id = el.identify();
			
			if (this.settings[id] === undefined) {
				//no settings for this flash instance
				this.settings[id] = {
					'params': null,
					'callback': null,
					'checkplayer': null
				}
			} else {
				//settings exist - set defaults
				this.settings[id] = ob_set_defaults(this.settings[id], {
					'params': null,
					'callback': null,
					'checkplayer': null
				});
			}
			
			this.create(
				el,
				this.settings[id].params,
				this.settings[id].callback,
				this.settings[id].checkplayer
			);
		}, this);
	},
	
	create: function(el, params, callback, checkplayer) {
		var el_params = el.title.strip().split(","), param, 
			classes = el.className.strip().split(" "), check;
		
		params = params || {};
		
		//add params found in element
		for (var a = 0; a < el_params.length; a++) {
			param = el_params[a].split("=");
			params[param[0]] = param[1];
		}
		
		if (!params.movie) return false;
		if (!el.id) el.identify();
		
		var swf_class = classes.without("swfobject").join(" ") || null;
		
		if(params.stagewidth == "fluid") {
			var width = "100%";
		} else {
			var width = (!params.width) ? el.getWidth() : params.width.toString().replace(/[^\d]/g, "");
		}
		var height = (!params.height) ? el.getHeight() : params.height.toString().replace(/[^\d]/g, "");
		
		if (checkplayer) {
			check = new flensed.checkplayer("6", function() {});
			
			if (check.checkPassed) {
				check.DoSWF(
					params.movie,
					el.id,
					width,
					height,
					null,
					params,
					{
						'class': swf_class
					},
					callback
				);
			} else {
				if (typeof(callback) == "function") {
					callback({
						'status': flensed.checkplayer.SWF_FAILED
					});
				}
			}
		} else {
			swfobject.embedSWF(
				params.movie,
				el.id,
				width,
				height,
				"6.0.0",
				"",
				null,
				params,
				{
					'class': swf_class
				},
				callback
			);
		}
		
		return true;
	}
});

function ob_set_defaults(ob, defaults) {
	var key;
	
	for (key in defaults) {
		if (ob[key] === undefined) {
			ob[key] = defaults[key];
		}
	}
	
	return ob;
}
