var Cookie = {
	'set' : function(name,value,duration) {
		var expire = "",d;
		if(typeof duration == "object") {
			d = new Date();
			switch($H(duration).keys().first()) {
				case "months" : d.setTime(d.getTime()+(duration.months*4*7*24*60*60*1000)); break;
				case "weeks" : d.setTime(d.getTime()+(duration.weeks*7*24*60*60*1000)); break;
				case "days" : d.setTime(d.getTime()+(duration.days*24*60*60*1000)); break;
				case "hours" : d.setTime(d.getTime()+(duration.hours*60*60*1000)); break;
				case "minutes" : d.setTime(d.getTime()+(duration.minutes*60*1000));	break;
				case "seconds" : d.setTime(d.getTime()+(duration.seconds*1000)); break;
			}
			expire = "; expires="+d.toGMTString();
		} else {
			expire = "; expires="+duration;
		}
		return (document.cookie = escape(name) + "=" + escape(value || '') + expire + "; path=/");
	},
	'get' : function(name) {
		var c = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
		return (c ? unescape(c[2]) : null);
	},
	'erase': function(name) {
		var c = Cookie.get(name) || true;
		Cookie.set(name, '', -1);
		return c;
	},
	'accept': function() {
		if (typeof navigator.cookieEnabled == 'boolean')
			return navigator.cookieEnabled;
		Cookie.set('_test', '1');
		return (Cookie.erase('_test') === '1');
	}
}
var neteffect = {
	timestamp : new Date().getTime(),
	gui : {}
};

// +------------------------------------------------------------+
// | OBSERVABLE													|
// +------------------------------------------------------------+
neteffect.Observable = Class.create({
	observe : function(evt,observer)
	{
		if(!this.__observers__)
			this.__observers__ = {};
		if(!this.__observers__[evt])
			this.__observers__[evt] = [];
		this.__observers__[evt].push(observer);
		return this;
	},
	stopObserving : function(evt,observer)
	{
		if(this.__observers__ && this.__observers__[evt])
		{
			var i = this.__observers__[evt].indexOf(observer);
			this.__observers__[evt].splice(i,1);
		};
		return this;
	},
	fire : function(evt)
	{
		if(!this.__observers__ || !this.__observers__[evt.type])
			return this;
		for(var i = 0;i<this.__observers__[evt.type].length;i++)
			this.__observers__[evt.type][i].apply(this,[evt]);
		return this;
	}
});

// +------------------------------------------------------------+
// | This shouldn't be needed but... sigh						|
// +------------------------------------------------------------+
neteffect.onDomReady = (function(){
	
	var app = new neteffect.Observable();
	var eventType = "domready";
	var fired = false;
	
	function doOnLoad()
	{
		if (!fired) {
			app.fire({type: eventType});
			fired=true;
		};
		return;
	};
	
	if(window.onload && window.onload!=doOnLoad)
	{
		var old = window.onload;
		app.observe(eventType,old);
	};
	
	window.onload = doOnLoad;
	
	function subscribe(listener)
	{
		app.observe(eventType,listener);
	};
	
	return subscribe;
})();

neteffect.Cache = new function()
{
	var storage = {};
	var uid = 1;
	
	return {
		set : function (data,id)
		{
			var id = id || uid++;
			var data = data || {};
			storage[id] = data;
			return id;
		},
		get : function (id)
		{
			return storage[id] || null;
		},
		remove : function (id)
		{
			delete storage[id];
		},
		getElementCache : function(element)
		{
			if(!element._cacheId)
				element._cacheId = this.set({});
			return this.get(element._cacheId);
		},
		setElementCache : function(element,data)
		{
			var id = element._cacheId || uid++;
			var data = data || {};				
			this.set(data,id);
		}
	};
}();

// +------------------------------------------------------------+
// | VW SimpleSlideShow											|
// | - CUSTOM, Not reusable!									|
// +------------------------------------------------------------+
neteffect.SimpleSlideShow = Class.create({
	initialize : function(list)
	{
		// if list has already been turned into a slideshow
		// update it instead.
		var cache = neteffect.Cache.getElementCache(list);
		if(cache.slideshow)
			return cache.slideshow.update();
		cache.slideshow = this;
		
		this.list = $(list);
		this.totalHeight = 0;
		this.buttonPosition = 0;
		this.children = [];

		this.update();
		
		list.setStyle({
			height : this.totalHeight+"px"
		});
		
		function setEnabled(){
			list.addClassName("js-enabled");
			list.stopObserving("click",setEnabled);
		}
		
		list.observe("click", setEnabled )
		
		

	},
	togglePanel : function(panel,button)
	{
		this.currentButton.removeClassName("active");
		this.currentPanel.addClassName("closed");
		button.addClassName("active");
		panel.removeClassName("closed");
		this.currentPanel = panel;
		this.currentButton = button;
		button.blur();
	},
	update : function()
	{
		this.list.childElements().each(function(element,index){
			
			this.totalHeight = Math.max(this.totalHeight,element.getHeight());

			element.setStyle({
				position : "absolute",
				top: "0px",
				left: "0px"
			});
			
			var panel = element.down("div.panel");
			var button = element.down("div.button");
			var bp = this.buttonPosition;
			
			if(!panel || !button)
				return;
			
			button.setStyle({
				position: "absolute",
				top: bp+"px",
				right: "-486px",
				zIndex : 1
			});
			
			this.buttonPosition += button.getHeight()+2;
			
			if(index == 0)
			{
				this.currentPanel = panel;
				this.currentButton = button;
				button.addClassName("active");
			}
			else
			{
				panel.addClassName("closed");
			};

			button.observe("click",this.togglePanel.bind(this,panel,button))
			
			button.observe("mouseover",function(e){
				if (!this.hasClassName("active")) {
					this.addClassName("hover");
				};
			});
			
			button.observe("mouseout",function(e){
				this.removeClassName("hover");
			});
			
			this.children.push(element)
			
		}.bind(this));
	}
});

// +------------------------------------------------------------+
// | ACCORDION													|
// +------------------------------------------------------------+
neteffect.Accordion = Class.create({
	initialize : function(container) {
		
		this.panels = [];
		this.current = null;
		
		$$("div.panel").each(function(element,index){
			
			var data = {
				panel : element,
				bar : element.down(".bar"),
				content : element.down(".content")
			};
			
			data.bar.setStyle({cursor:"pointer"});
			
			if (index) 
				data.content.hide();
			else {
				this.current = data;
				data.bar.addClassName("active");
			};
			
			data.content.down("p").innerHTML = data.content.down("p").innerHTML.truncate(255);
			
			this.panels.push(data);
			
			data.bar.observe("click",this.toggle.bindAsEventListener(this,data));
			
		}.bind(this));
		
	},
	toggle : function(e,param) {
		if(this.current && param.content === this.current.content)
			return;
		if (this.current) {
			this.current.bar.removeClassName("active");
			this.current.content.hide();
		};
		
		param.content.show();
		param.bar.addClassName("active");
		param.bar.focus();
		
		this.current = param;
		
	}
});

//+-------------------------------------------------------------+
//| Window Popup Object											|
//+-------------------------------------------------------------+
neteffect.PopUp = function(params)
{
	// ensure a new instance.
	if(this == neteffect)
		return new neteffect.PopUp(params);
	
	// set (default) arguments.
	var url = params.url || "#";
	var tar = params.target || null;
	var props = "";
		
	// default options
	var options = {
		"menubar" : "0",
		"location" : "0",
		"resizable" : "0",
		"scrollbars" : "0",
		"status" : "0",
		"toolbar" : "0",
		"directories" : "0",
		"width" : "200",
		"height" : "200"
	};
	
	if(params.width === "full")
		params.width = screen.width;
	
	if(params.height === "full")
		params.height = screen.height;
	
	// build parameter string.
	for(var key in options)
		props += key + "="+(params[key]||options[key])+",";
	
	var posX = params.x || (screen.width / 2) - ((params.width||options.width)/2);
	var posY = params.y || (screen.height / 2) - ((params.height||options.height)/2);
	
	// remove trailing comma.
	props = props.replace(/\,$/,"");
	
	var api = {
		open : function()
		{
			var o = window.open(url,tar,props);
			o.moveTo(posX,posY);
			return this;
		}
	};
	
	// open window.
	api.open();
	
	return api; 

};

$(document).observe("dom:loaded",function(){
	
	if ( flashVersionOk ) {
		return;
	}
	
	var list = $$(".alternate-main-nav > li");

	list.invoke("observe", "click", function(){
		list.invoke("removeClassName","hover");
		$(this).addClassName("hover");
	}).invoke("observe", "hover", function(){
		list.invoke("removeClassName","hover");
		$(this).addClassName("hover");
	});
	
	
});

$(document).observe("dom:loaded",function(){
	
	$$(".youtube-player").each(function( el ){
		
		// <div class="youtube-player" data-youtubeid="XXXX"></div>
		var source = el.getAttribute("data-youtubeid");
		//var so = new SWFObject()
		//el.innerHTML = so.getHTML();
		
		
	});
	
});

$(document).observe("dom:loaded",function(){
	var iframe;
	$$("#exterior .thumbnails img").invoke("observe","click",function(e){
		if (!iframe){
			iframe = $("pixel-iframe");
		}
		if (!iframe){
			return;
		}
		iframe.src = iframe.src.replace(/color=[^&]*$/,"color="+e.target.getAttribute("alt").split(":")[0].trim());
	});
	
});   

