ArtColony = function() {};

// Very Inspired by Alternative Style: Working With Alternate Style Sheets
// Copyright Â© 1998-2006 A List Apart Magazine and Paul Sowden

ArtColony.styler = Class.create();
ArtColony.styler.prototype = {
	initialize: function(el, props) {
		this.props = {
			stylesheets: '',
			txt: 'Adjust layout: '
		}
		Object.extend(this.props, props || {});

		this.li = document.createElement('li');

		el = $(el);
		el.appendChild(this.li);

		this.stylesheets = this.props.stylesheets.split(',');
		
		addEvent(window, 'load', function() { this.onload(); }.bind(this));
		
		c = this.readCookie("styler");
		title = (c) ? c : this.getPreferred();
		this.setActive(title);
	},
	
	onload: function(e) {
		c = this.readCookie("styler");
		title = (c) ? c : this.getPreferred();
		this.setActive(title);
	},

	create: function(c) {
		this.li.innerHTML = ''; // initialize interactive elements
		this.li.appendChild(document.createTextNode(this.props.txt));
		
		for (i = 0; i < this.stylesheets.length; i++) {
			title = this.stylesheets[i];

			if (c == title) {
				strong = document.createElement('strong');
				strong.appendChild(document.createTextNode(title));
				this.li.appendChild(strong);
			}
			else {
				a = document.createElement('a');
				a.onclick = (function(z) { return function() { this.setActive(z); }})(title).bind(this);
				a.appendChild(document.createTextNode(title));	
				this.li.appendChild(a);
			}
			
			if (i != (this.stylesheets.length - 1)) this.li.appendChild(document.createTextNode(' | '));
		}
	},
	
	setActive: function(title) {
		lnks = document.getElementsByTagName("link");
		for (i = 0; i < lnks.length; i++) {
	  		lnk = lnks[i];
	  		if (lnk.getAttribute("rel").indexOf("style") != -1 && lnk.getAttribute("title")) {
	  			(lnk.getAttribute("title") == title) ? lnk.disabled = false : lnk.disabled = true;
	  		}
		}

		this.createCookie("styler", title, 365);
	
		this.create(title);
	},
	
	getActive: function() {
	  lnks = document.getElementsByTagName("link");
	  for (i = 0; i < lnks.length; i++) {
	  		lnk = lnks[i];
	  		if (lnk.getAttribute("rel").indexOf("style") != -1 && lnk.getAttribute("title") && !lnk.disabled) {
	  			return lnk.getAttribute("title");
	  		}
	  }
	},
	
	getPreferred: function() {
	  lnks = document.getElementsByTagName("link");
	  for (i = 0; i < lnks.length; i++) {
	  		lnk = lnks[i];
	  		if (lnk.getAttribute("rel").indexOf("style") != -1 && lnk.getAttribute("rel").indexOf("alt") == -1 && lnk.getAttribute("title")) {
	  			return lnk.getAttribute("title");
	  		}
	  }
	},

	createCookie: function(name, value, days) {
	  if (days) {
	    d = new Date();
	    d.setTime(d.getTime() + (days * 24 * 60 * 60 * 1000));
	    expires = "; expires=" + d.toGMTString();
	  }
	  else expires = "";
	  document.cookie = name + "=" + value + expires + "; path=/";
	},
	
	readCookie: function(name) {
	  nameEQ = name + "=";
	  ca = document.cookie.split(';');
	  for(i = 0; i < ca.length; i++) {
	    c = ca[i];
	    while (c.charAt(0) == ' ') c = c.substring(1, c.length);
	    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	  }
	  return null;
	}	
}

ArtColony.wipe = Class.create();
ArtColony.wipe.prototype = {
	initialize: function(cls, props) {
		this.props = {
			color: '#808080'
		}
		Object.extend(this.props, props || {});

		this.wiped = new Array();

		inputs = document.getElementsByClassName(cls);

		for (var i = 0; i < inputs.length; i++) {
			id = inputs[i].id;
			inputs[i].style.color = this.props.color;
			inputs[i].onfocus = (function (z) { return function() { this.wipe(z); }})(id).bind(this);
			inputs[i].onblur = (function (z) { return function() { this.unwipe(z); }})(id).bind(this);
		}
	},
	
	wipe: function(id) {
		el = $(id);			

		if (this.wiped[id] == null) this.wiped[id] = el.value + "";

		if (el.value == this.wiped[id]) {	
			el.value = "";
			el.style.color = "";
		}
	},
	
	unwipe: function(id) {
		el = $(id);			

		if ((el.value == null) || (el.value.length == 0)) {
			el.style.color = this.props.color;
			el.value = this.wiped[id];
		}
	}
}

ArtColony.litebox = Class.create();
ArtColony.litebox.prototype = {
	initialize: function(el, props) {
		this.props = {
			heading: 'Slideshow',
			path: '',
			txt: 'View This Gallery'
		}
		Object.extend(this.props, props || {});
		
		this.links = new Array();
		this.srcs = new Array();
		this.titles = new Array();
		
		h2 = document.createElement('h2');
		h2.appendChild(document.createTextNode(this.props.heading));		
		
		a = document.createElement('a');
		a.onclick = function () { this.start(); return false; }.bind(this);
		a.appendChild(document.createTextNode(this.props.txt));
		
		p = document.createElement('p');
		p.className = 'slideshow';
		p.appendChild(a)
		
		div = document.createElement('div');
		div.appendChild(h2);
		div.appendChild(p);
		
		el = $(el);
		el.insertBefore(div, el.firstChild);
				
		myLightbox = new Lightbox();
	},
	
	start: function() {
		imageArray = new Array();
		
		for (i = 0; i < this.srcs.length; i++) {
			imageArray.push(new Array(this.props.path + this.srcs[i], this.titles[i], this.links[i]));			
		}
		
		myLightbox.start(imageArray);
	}
}

ArtColony.toggler = Class.create();
ArtColony.toggler.prototype = {
	initialize: function(toggles) {
		for (i = 0; i < toggles.length; i++) {
			pn = toggles[i].parentNode;
			
			children = pn.getElementsByTagName('*');
			
			for (j = 0; j < children.length; j++) {
				el = children[j];
				
				if (Element.hasClassName(el, 'hidden')) {
					el.effect = new fx.Opacity(el, { duration: 1000 });
					el.effect.setOpacity(0);
					
					toggles[i].onmouseover = (function(z) { return function() { z.effect.setOpacity(1); }})(el);
					
					pn.onmouseout = (function(z) { return function(e) { 
						if (!e) e = window.event;
												
						rt = (e.relatedTarget) ? e.relatedTarget : e.toElement;
						
						while (rt != document.body) {
							if (rt == this) return;
							rt = rt.parentNode;
						}
							
						z.effect.custom(1,0);					
					}})(el);
				}
			}
		}
	}
}

// Assimilated from Mad4Milk http://www.mad4milk.net

var ScrollLinks = {
	currentHash: false,
	start: function(){
		this.scroll = new fx.Scroll({duration: 500, transition: fx.sineOut, onComplete: function(){ this.end(); }.bind(this)});
		this.allinks = $c(document.getElementsByTagName('a'));
		this.alldivs = $c(document.getElementsByTagName('div'));
		this.allinks.each(function(lnk) {
			if ((lnk.href && lnk.href.indexOf('#') != -1) && ( (lnk.pathname == location.pathname) 
				|| ('/'+lnk.pathname == location.pathname) ) && (lnk.search == location.search)) {
				div = $(lnk.hash.slice(1));
				lnk.myColor = new fx.Color(div, { duration: 2000 });		
				lnk.onclick = function(){
					this.myColor.customColor("C1F3FF", "FFFFFF");
					ScrollLinks.scroll.clearTimer();
					this.initialHref = this.href;
					this.initialHash = this.hash;
					this.href = "javascript:void(0)";
					setTimeout(function(){ this.href = this.initialHref; }.bind(this), 200);
					ScrollLinks.go(this);
				}
			}
		});
	},

	go: function(link){
		this.currentHash = link.initialHash.slice(1);
		if (this.currentHash) {
			this.alldivs.each(function(lnk){
				if (lnk.id == ScrollLinks.currentHash){				
					if (window.opera) lnk = [lnk].find('parentNode');
					ScrollLinks.scroll.scrollTo(lnk);
					return;
				}
			});
		}
	},

	end: function(){
		if (!/Konqueror|Safari|KHTML/.test(navigator.userAgent)) window.location.hash = "#"+this.currentHash;
		this.currentHash = false;
		if (window.myPos) myPos.update();
	}
}

// Assimilated from S.Andrew http://www.scottandrew.com/

function addEvent(element, name, observer, useCapture) {
	if (element.addEventListener) {
		return element.addEventListener(name, observer, useCapture);
	}
	else if (element.attachEvent) {
		return element.attachEvent('on' + name, observer);
	}
	else {
		return element['on' + name] = observer;
	}
}