var Origin = Origin || {};var Origin = Origin || {};
Origin.Helper = Origin.Helper || {};var Origin = Origin || {};
Origin.Helper = Origin.Helper || {};
Origin.Helper.Debug = (function () {

	function _log(message) {
		if ($.browser.mozilla) {
			console.log(message);
		} else {
			// do nothing as we want to suppress errors in IE
			//_alert(message);
			return false;
		}
	}
	
	function _groupStart(name) {
		if ($.browser.mozilla) {
			console.group(name);
		} else {
			return false;
		}
	}
	
	function _groupEnd(name) {
		if ($.browser.mozilla) {
			console.groupEnd(name);
		} else {
			return false;
		}
	}
	
	function _alert(message) {
		alert(message);
	}
	
	return {
		toConsole:_log,
		toAlert:_alert,
		groupStart:_groupStart,
		groupEnd:_groupEnd
	};
	
})();var Origin = Origin || {};
Origin.Helper = Origin.Helper || {};
Origin.Helper.Url = {
	_parseUrl: function (uri, i) {
		var re = new RegExp(/^((http[s]?|ftp):\/)?\/?([^:\/\s]+)(:([^\/]*))?((\/\w+)*\/)([\w\-\.]+[^#?\s]+)(\?([^#]*))?(#(.*))?$/gi),
			obj = re.exec(uri);
			
		if (obj === null) {
			return '';
		} else {
			return obj[i];
		}
	},
	_parseQuerystring: function (uri) {
		var qs = {};
		uri.replace(
			new RegExp("([^?=&]+)(=([^&]*))?", "g"),
			function ($0, $1, $2, $3) {
				qs[$1] = $3;
			}
		);
		return qs;
	},
	getSchema: function (uri) {
		return Origin.Helper.Url._parseUrl(uri, 2);
	},
	getDomain: function (uri) {
		return Origin.Helper.Url._parseUrl(uri, 3);
	},
	getPort: function (uri) {
		return Origin.Helper.Url._parseUrl(uri, 5);
	},
	getPath: function (uri) {
		return Origin.Helper.Url._parseUrl(uri, 6);
	},
	getFile: function (uri) {
		return Origin.Helper.Url._parseUrl(uri, 8);
	},
	getQuerystring: function (uri) {
		return Origin.Helper.Url._parseUrl(uri, 9);
	},
	getHash: function (uri) {
		return Origin.Helper.Url._parseUrl(uri, 12);
	},
	getQuerystringAsObject: function (uri) {
		return Origin.Helper.Url._parseQuerystring(Origin.Helper.Url.getQuerystring(uri));
	},
	getQuerystringOnly: function (uri) {
		var qs = {};
		uri.replace(
			new RegExp("([^?=&]+)(=([^&]*))?", "g"),
			function ($0, $1, $2, $3) {
				qs[$1] = $3;
			}
		);
		return qs;
	},
	replaceValue: function (uri, key, value) {
		return uri.replace(new RegExp("([#?&]" + key + ")=[^?&]+"), "$1=" + value);
	},
	getValue: function (uri, key) {
		var results = new RegExp('[\\?&]' + key + '=([^&#]*)').exec(uri);
		if (!results) {
			return 0; 
		}
		return results[1] || 0;
	}
};var Origin = Origin || {};
Origin.View = Origin.View || {};var Origin = Origin || {};
Origin.View = Origin.View || {};
Origin.View.Showcase = Origin.View.Showcase || {};

Origin.View.Showcase.Project = function(index, src, alt) {
	return '<div class="s s' + index + '"><img src="' + src + '" alt="' + alt + '" /></div>';
};var Origin = Origin || {};
Origin.Controller = Origin.Controller || {};var Origin = Origin || {};
Origin.Controller = Origin.Controller || {};

Origin.Controller.Fonts = (function() {
	
	var $rootEl;
	
	$(function() {
		//Origin.Controller.Fonts.init();
	});
	
	function _init() {
		Cufon.replace('.cufon', {fontFamily: 'Origin'});
	}
	
	return {
		init:_init
	};
})();var Origin = Origin || {};
Origin.Controller = Origin.Controller || {};

Origin.Controller.Showcase = (function() {
	
	var $showcaseEl;
	
	$(function() {
		if ($('#work #sc').length) {
			Origin.Controller.Showcase.init();
		}
	});
	
	function _init() {
		$showcaseEl = $('#sc');
		
		var showcaseInstance = new Origin.Controller.Showcase.Default($showcaseEl);
	}
	
	return {
		init:_init
	};
})();var Origin = Origin || {};
Origin.Controller = Origin.Controller || {};
Origin.Controller.Showcase = Origin.Controller.Showcase || {};

Origin.Controller.Showcase.Default = function(obj, opts) {
	this.rootNode = obj;
	this.o = opts || {
		speed : {
			showcaseEnter : 400,
			showcaseLeave : 400,
			projectEnter : 400,
			projectLeave : 600,
			spotEnter : 400,
			spotLeave : 400
		}
	};
	this.projectInstances = [];
	
	this.setupProjects();
};

Origin.Controller.Showcase.Default.prototype = {
	
	isShowcaseActive : false,
	
	setupProjects : function() {
		var me = this;
		this.rootNode.find('> div.p').each(function(index){
			var projectInstance = new Origin.Controller.Showcase.Project($(this), {'index':index});
			me.projectInstances.push(projectInstance);
		});
		this.setupEvents();
	},
	
	setupEvents : function() {
		var me = this;
	
		this.rootNode
			.bind('mouseenter', function(event) {
				me.isShowcaseActive = true;
				me.showcase_MOUSEENTER();
			})
			.bind('mouseleave', function(event) {
				me.isShowcaseActive = false;
				me.showcase_MOUSELEAVE();
			})
			.delegate('.p', 'mouseenter', function(){
				me.project_MOUSEENTER($(this).data('index'));
			})
			.delegate('.p', 'mouseleave', function(){
				me.project_MOUSELEAVE($(this).data('index'));
			})
			.delegate('.p img, .p p:not(.b) a', 'click', function(){
				me.project_CLICK($(this).parents('.p').data('target'));
			})
			.delegate('.b', 'click', function(event) {
				event.preventDefault();
				me.info_CLICK($(this).parents('.p').data('index'));
			})
			.delegate('.b', 'mouseenter', function(event) {
				me.info_MOUSEENTER($(this).parents('.p').data('index'));
			})
			.delegate('.b', 'mouseleave', function(event) {
				me.info_MOUSELEAVE($(this).parents('.p').data('index'));
			});
		
	},
	
	project_MOUSEENTER : function(index) {
		var $this = this.projectInstances[index];
		
		$this
			.colorLayer_fadeIn(this.o.speed.projectEnter)
			.textLayer_fadeIn(this.o.speed.projectEnter)
			.infoLink_fadeIn(this.o.speed.projectEnter);
		
	},
	
	project_MOUSELEAVE : function(index) {
		if (this.isShowcaseActive) {
			
			this.projectInstances[index]
				.colorLayer_fadeOut(this.o.speed.projectLeave)
				.textLayer_fadeOut(this.o.speed.projectLeave)
				.spotLayer_fadeOut(this.o.speed.projectLeave)
				.infoLink_fadeOut(this.o.speed.projectLeave);
			
		} else {
			
			this.projectInstances[index]
				.infoLink_fadeOut(this.o.speed.projectLeave);
			
		}
	},
	
	showcase_MOUSEENTER : function() {
		var me = this;
		$.each(this.projectInstances, function(index) {
			
			me.projectInstances[index]
				.colorLayer_fadeOut(me.o.speed.showcaseEnter)
				.textLayer_fadeOut(me.o.speed.showcaseEnter)
				.spotLayer_fadeOut(me.o.speed.showcaseEnter);
			
		});
		
	},
	
	showcase_MOUSELEAVE : function() {
		var me = this;
		$.each(this.projectInstances, function(index) {
			
			me.projectInstances[index]
				.colorLayer_fadeIn(me.o.speed.projectLeave)
				.textLayer_fadeIn(me.o.speed.projectLeave);
			
		});
	},
	
	info_MOUSEENTER : function(index) {
		
		this.projectInstances[index]
			.infoLinkColor_fadeIn(400);
		
	},
	
	info_MOUSELEAVE :function(index) {
		
		this.projectInstances[index]
			.infoLinkColor_fadeOut(400);
		
	},
	
	info_CLICK : function(index) {
		var $this = this.projectInstances[index];
		if ($this.nodes.image.spotLayer.isVisible) {
			
			$this
				.colorLayer_fadeIn(this.o.speed.projectEnter)
				.spotLayer_fadeOut(this.o.speed.spotLeave);
			
		} else {
			
			$this
				.colorLayer_fadeOut(this.o.speed.projectLeave)
				.spotLayer_fadeIn(this.o.speed.spotEnter);
			
		}
	},
	
	project_CLICK : function(href) {
		window.location = href;
	}
};

var Origin = Origin || {};
Origin.Controller = Origin.Controller || {};
Origin.Controller.Showcase = Origin.Controller.Showcase || {};

Origin.Controller.Showcase.Project = function(obj, opts) {
	
	this.rootNode = obj;
	this.opts = opts || {};
	this.isIE = $('html.oldie').length;
	
	this.debugging = false;
	
	this.setup();
};

Origin.Controller.Showcase.Project.prototype = {
	
	setup : function() {
	
		this.storeBaseReferences();
	
		// create dom elements
		this.appendHtml();
	
		// store new layer references
		this.storeLayerReferences();
	
		// remove original image node
		this.removeOriginalImage();
	
		// store original text colors
		this.storeColors();
	
	},
	
	storeBaseReferences : function() {
	
		var $this = this.rootNode;
	
		// create object to store node references (saves looking up again)
		this.nodes = {
			image : {
				rootNode : $this.find('.i'),
				greyLayer : {
					isVisible : true
				},
				spotLayer : {
					isVisible : false
				},
				colorLayer : {
					isVisible : true
				}
			},
			caption : {
				rootNode : $this.find('.c'),
				title : {
					rootNode : $this.find('.c h3'),
					link : $this.find('.c h3 a')
				},
				blurb : {
					rootNode : $this.find('.c p:not(.b)'),
					link : $this.find('.c p:not(.b) a')
				},
				info : {
					rootNode : $this.find('.c .b')
				}
			}
		};
	
		// tag the rootNode node with an index number (handy for later)
		this.rootNode.data('index', this.opts.index);
	
		// tag the root node with the href target
		this.rootNode.data('target', this.nodes.caption.title.link.attr('href'));
	
	},
	
	storeLayerReferences : function() {
		this.nodes.image.greyLayer.rootNode = this.nodes.image.rootNode.find('.s0');
		this.nodes.image.spotLayer.rootNode = this.nodes.image.rootNode.find('.s1');
		this.nodes.image.colorLayer.rootNode = this.nodes.image.rootNode.find('.s2');
	},
	
	appendHtml : function(obj) {
		var html = '',
			$imgNode = this.nodes.image.rootNode.find('img'),
			src = $imgNode.attr('src'),
			alt = $imgNode.attr('alt');
	
		for (var i = 0, j = 3; i < j; i++) {
			html += Origin.View.Showcase.Project(i, src, alt);
		};
	
		this.nodes.image.rootNode.append(html);
	},
	
	removeOriginalImage : function() {
		this.nodes.image.rootNode.find('img:eq(0)').remove();
	},
	
	storeColors : function() {
		var $title = this.nodes.caption.title,
			$blurb = this.nodes.caption.blurb;
		
		$title.rootNode.data( 'origCol', this.rgb2hex($title.rootNode.css('color')));
		$title.link.data( 'origCol', this.rgb2hex($title.link.css('color')));

		$blurb.rootNode.data( 'origCol', this.rgb2hex($blurb.rootNode.css('color')));
		$blurb.link.data( 'origCol', this.rgb2hex($blurb.link.css('color')));
	},
	
	rgb2hex : function(rgb) {
		
		if (rgb === undefined) {
			return '';
		}
		
		isRGB = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
		
		function hex(x) {
			return ("0" + parseInt(x).toString(16)).slice(-2);
		}
		
		// IE returns the actual hex while all other browsers return a rgb
		if (isRGB === null) {
			return rgb;
		} else {
			return "#" + hex(isRGB[1]) + hex(isRGB[2]) + hex(isRGB[3]);
		}
	},
	
	infoLink_fadeOut : function(speed) {
		var me = this;
		this.nodes.caption.info.rootNode.stop(true, true).animate({
			'opacity': 0
		}, speed);
		return this;
	},
	
	infoLink_fadeIn : function(speed) {
		var me = this;
		this.nodes.caption.info.rootNode.stop(true, true).animate({
			'opacity': 1
		}, speed);
		return this;
	},
	
	infoLinkColor_fadeOut : function(speed) {
		var me = this;
		this.nodes.caption.info.rootNode.find('a').stop(true, true).animate({
			'opacity': 0
		}, speed);
		return this;
	},
	
	infoLinkColor_fadeIn : function(speed) {
		var me = this;
		this.nodes.caption.info.rootNode.find('a').stop(true, true).animate({
			'opacity': 1
		}, speed);
		return this;
	},
	
	colorLayer_fadeOut : function(speed) {
		this.nodes.image.colorLayer.rootNode.find('img').stop(true, true).animate({
			'opacity': 0
		}, speed);
		return this;
	},
	
	colorLayer_fadeIn : function(speed) {
		this.nodes.image.colorLayer.rootNode.find('img').stop(true, true).animate({
			'opacity': 1
		}, speed);
		return this;
	},
	
	spotLayer_fadeOut : function(speed) {
		this.nodes.image.spotLayer.rootNode.find('img').stop(true, true).animate({
			'opacity': 0
		}, speed);
		this.nodes.image.spotLayer.isVisible = false;
		return this;
	},
	
	spotLayer_fadeIn : function(speed) {
		this.nodes.image.spotLayer.rootNode.find('img').stop(true, true).animate({
			'opacity': 1
		}, speed);
		this.nodes.image.spotLayer.isVisible = true;
		return this;
	},
	
	textLayer_fadeOut : function(speed) {
		this.nodes.caption.title.link.stop(true, true).animate({
			'color': '#333333'
		}, speed);
		this.nodes.caption.blurb.rootNode.stop(true, true).animate({
			'color': '#333333'
		}, speed);
		return this;
	},
	
	textLayer_fadeIn : function(speed) {
		var origCol = this.nodes.caption.title.link.data('origCol');
		this.nodes.caption.title.link.stop(true, true).animate({
			'color': origCol
		}, speed);
		var origCol = this.nodes.caption.blurb.rootNode.data('origCol');
		this.nodes.caption.blurb.rootNode.stop(true, true).animate({
			'color': origCol
		}, speed);
		return this;
	}
};

var Origin = Origin || {};
Origin.Controller = Origin.Controller || {};
Origin.Controller.Showcase = Origin.Controller.Showcase || {};

Origin.Controller.Showcase.Client = function(obj, opts) {
	this.rootNode = obj;
	this.o = opts || {
		speed : {
			showcaseEnter : 400,
			showcaseLeave : 400,
			projectEnter : 400,
			projectLeave : 600,
			spotEnter : 400,
			spotLeave : 400
		}
	};
	this.projectInstances = [];
	
	this.setupProjects();
};

Origin.Controller.Showcase.Client.prototype = {
	
	isShowcaseActive : false,

	setupProjects : function() {
		var me = this;
		this.rootNode.find('> div.p').each(function(index){
			var projectInstance = new Origin.Controller.Showcase.Project($(this), {'index':index});
			me.projectInstances.push(projectInstance);
		});
		this.setupEvents();
	},

	setupEvents : function() {
		var me = this;
	
		this.rootNode
			.bind('mouseenter', function(event) {
				me.isShowcaseActive = true;
				//me.showcase_MOUSEENTER();
			})
			.bind('mouseleave', function(event) {
				me.isShowcaseActive = false;
				me.showcase_MOUSELEAVE();
			})
			.delegate('.p', 'mouseenter', function(){
				me.project_MOUSEENTER($(this).data('index'));
			})
			.delegate('.p', 'mouseleave', function(){
				me.project_MOUSELEAVE($(this).data('index'));
			})
			.delegate('.p img, .p p:not(.b) a', 'click', function(){
				me.project_CLICK($(this).parents('.p').data('target'));
			});
	
	},

	project_MOUSEENTER : function(index) {
		var $this = this.projectInstances[index];
			$this
				.colorLayer_fadeIn(this.o.speed.projectEnter)
				.textLayer_fadeIn(this.o.speed.projectEnter);
	},

	project_MOUSELEAVE : function(index) {
		var $this = this.projectInstances[index];
		if (this.isShowcaseActive) {
			
			$this
				.colorLayer_fadeOut(this.o.speed.projectLeave)
				.textLayer_fadeOut(this.o.speed.projectLeave)
				.spotLayer_fadeOut(this.o.speed.projectLeave);
		}
	},

	showcase_MOUSELEAVE : function() {
		var me = this;
		$.each(this.projectInstances, function(index) {
			
			me.projectInstances[index]
				.colorLayer_fadeOut(me.o.speed.projectLeave)
				.textLayer_fadeOut(me.o.speed.projectLeave);
			
		});
	},

	project_CLICK : function(href) {
		window.location = href;
	}

};var Origin = Origin || {};
Origin.Controller = Origin.Controller || {};

Origin.Controller.Client = (function() {
	
	var $rootNode,
		$designNode,
		$showcaseEl,
		
		isSliderVisible = false,
		debugging = false;
	
	$(function(){
		if ($('body#client').length) {
			Origin.Controller.Client.init();
		}
	});
	
	function _init() {
		_debug('Origin.Controller.Client.init');
		
		_initSlider();
		
		
		$rootNode = $('body');
		$designNode = $('#designs');
		$showcaseEl = $('#sc .content');
		
		var showcaseInstance = new Origin.Controller.Showcase.Client($showcaseEl);
		
		$rootNode.delegate('#designs', 'mouseleave', function (){
			$designNode.find('span.arrow a').removeClass('hover');
		});
	}
	
	function _initSlider() {
		$('#slider').anythingSlider({
			'easing' : 'easeInOutQuad',
			'buildStartStop' : false,
			'enableStartStop' : false
		});
	}
	
	function _debug(msg) {
		if (debugging) {
			Origin.Helper.Debug.toConsole(msg);
		}
	}
	
	return {
		init: _init
	};
})();var Origin = Origin || {};
Origin.Controller = Origin.Controller || {};

Origin.Controller.Hero = (function() {
	
	var $rootNode,
	
		$sliderHandle,
		isHandleFocussed = false,
		timer;
	
	$(function(){
		if ($('#hero').length) {
			Origin.Controller.Hero.init();
		}
	});
	
	function _init() {
		$rootNode = $('body');
		$sliderHandle = $('#hero-slider').find('.ui-slider-handle');
		
		//vars
		var conveyor = $('#hero-content'),
			
			/*
			panelA = parseInt($("#contentPanel div.panel.main").css('width').split('px')[0], 10),
			panelB = parseInt($("#contentPanel div.panel.projects").css('width').split('px')[0], 10),
			conveyorWidth = panelA + panelB;
			*/
			
			conveyorWidth = 8460; // this is a temporary hack until Chrome fixes its css width bug

		conveyor.css('width','8460px'); //set length of conveyor

		var sliderOpts = {
			max: conveyorWidth - 960,
			slide: function(e, ui) {
				conveyor.css("left", "-" + ui.value + "px");
			}
		};

		//create slider
		$('#hero-slider').slider(sliderOpts);
		
		$rootNode
			.delegate('#hero-slider', 'mouseenter', function(){
				isHandleFocussed = true;
			})
			.delegate('#hero-slider', 'mouseleave', function(){
				isHandleFocussed = false;
			});
	}
	
	return {
		init: _init
	};
})();var Origin = Origin || {};
Origin.Controller = Origin.Controller || {};

Origin.Controller.Links = (function(){
	
	var $rootNode;
	
	$(function(){
		Origin.Controller.Links.init();
	});
	
	function _init() {
		$rootNode = $('body');
		
		$rootNode.delegate('a.ext', 'click', function(event) {
			event.preventDefault();
			_openNewWindow($(this).attr('href'));
		});
	}
	
	function _openNewWindow(href) {
		window.open(href);
	}
	
	return {
		init: _init
	};
})();
