/*	Load
----------------------------------------------- */
Event.domLoad(function() {
		flickerFix();
		logo();
		PopUp.hookup("external", function() {
			return PopUp.newWindow(this);
		});
		var bodyId = document.body.id;
		switch(bodyId) {
			case "home-page":
				try {
					var so = new SWFObject("assets/flash/home.swf", "home-flash", "507", "338", "8", "#FFF8E2");
					if (!so.write("home-lifestyle")) {
						SlideShow.init();	
					}
				} catch(e) {
					SlideShow.init();
				}
			break;
			case "writers-page":
				Writer.init();
			break;
			case "concept-page":
				SlideShow.init();
			break;
			case "clients-page":
				SlideShow.init();
			break;
			case "about-what-page":
				SlideShow.init();
			break;
			case "about-who-page":
				SlideShow.init();
			break;
			case "media-page":
				SlideShow.init();
			break;
			case "testimonials-page":
				SlideShow.init();
			break;
		}	
		positionBackground();
	}
);		
Event.add(window, "resize", function(){positionBackground();});

/*	General
------------------------------ */
function logo() {
	var link = document.getElementById("site-logo");
	if (link) {
		link.onmouseover = function() {	
			var logo = link.getElementsByTagName("img")[0];
			logo.style.marginTop = "-121px";
		};
		link.onmouseout = function() {	
			var logo = link.getElementsByTagName("img")[0];
			logo.style.marginTop = "0";
		};
	}
}

function flickerFix() {
	try {
		document.execCommand("BackgroundImageCache", false, true);
	} catch(e) {}
}

function positionBackground(){
	var backgroundImageWidth = 765;
	var size = getPageSize();
	var body = document.body;
	if ( navigator.userAgent.indexOf("Safari") != -1){ 
		if (size % 2 == 0) { 
			++size;
		}
		body.style.backgroundPosition = (size/2 - (backgroundImageWidth / 2)) + "px 0px";
	}
}

function getPageSize() {
	var myWidth = 0, myHeight = 0;
	if (typeof( window.innerWidth) == "number" ) {  
		myWidth = window.innerWidth;  
		myHeight = window.innerHeight;
	} 
	return myWidth;
}

/*	Writers
------------------------------ */
var Writer = {
	header : null,
	profile : null,
	ajax : new Ajax(),
	init : function() {
		var writer = document.getElementById("writer-profile");
		if (!writer) { return; }
		this.header = writer.getElementsByTagName("h2")[0];
		this.profile = document.getElementById("profile");

		var writers = document.getElementById("writers");
		var w = writers.getElementsByTagName("a");
		for (var i = 0, c = w.length; i < c; i++) {
			w[i].onclick = function() {
				return Writer.fetch(this);
			};
		}
		var random = document.getElementById("random-profile");
		if (random) {
			var randomLink = random.getElementsByTagName("a")[0];
			randomLink.onclick = function() {
				return Writer.fetch(this);
			};
		}
		var next = document.getElementById("writer-nav-next");
		if (next) {
			next.onclick = function() {
				return Writer.fetch(this, true);
			};
		}
		var prev = document.getElementById("writer-nav-prev");
		if (prev) {
			prev.onclick = function() {
				return Writer.fetch(this, true);
			};
		}
	},	
	fetch : function(e, nav) {
		var cache = AjaxCache.get(encodeURI(e.href));
		if (cache && e.href.indexOf("random") == -1) {
			Writer.header.innerHTML = cache.title;
			Writer.profile.innerHTML = cache.profile;

			var next = document.getElementById("writer-nav-next");
			if (next && cache.navNext) {
				next.href = "the_writers.php?wid=" + cache.navNext;
			}
			var prev = document.getElementById("writer-nav-prev");
			if (prev && cache.navPrev) {
				prev.href = "the_writers.php?wid=" + cache.navPrev;
			}
		} else {
			this.ajax.set(e.href, {'ajax': 'true'});
			this.ajax.complete = function() {
				var json = this.toJson();
				Writer.header.innerHTML = json.title;
				Writer.profile.innerHTML = json.profile;
				AjaxCache.set(encodeURI(this.rawUrl), json);
				Writer.profile.style.background = "none";

				var next = document.getElementById("writer-nav-next");
				if (next && json.navNext) {
					next.href = "the_writers.php?wid=" + json.navNext;
				}
				var prev = document.getElementById("writer-nav-prev");
				if (prev && json.navPrev) {
					prev.href = "the_writers.php?wid=" + json.navPrev;
				}
			};
			this.ajax.busy = function() {
				Writer.profile.style.background = "url(assets/bg/ajax.gif) no-repeat center center";
				Writer.header.innerHTML = "";
				Writer.profile.innerHTML = "";
			};
			this.ajax.get();
		}
		if (!nav) {
			scroll(0,0);
		}
		return false;
	}
};

/*	Slideshow
------------------------------ */
var SlideShow = {
	canvas: null,
	hero: null,
	imagery: new Array(),
	cursor: 0,
	bounds: 0,
	frequency: 2,
	easing: {timer: null},
	init: function() {
		this.canvas = document.getElementById("concept-hero");
		if (slides && this.canvas) {
			this.hero = this.canvas.getElementsByTagName("img")[0];
			for (var i = 0, c = slides.length, image; i < c; i++) {
				this.imagery[i] = document.createElement("img");
					
				this.imagery[i].src = slides[i]; 
				this.imagery[i].alt = this.hero.alt;
			}		
			this.bounds = this.imagery.length;
			this.easing.timer = setInterval("SlideShow.fetch()", this.frequency * 1000);
		}
	},
	fetch: function() {
		if (this.cursor == this.bounds) {
			this.cursor = 0;
		}
		this.canvas.replaceChild(this.imagery[this.cursor], this.canvas.getElementsByTagName("img")[0]);
		++this.cursor;
	}
};