(function(){
	function MK(ls){
		this.els = [];
		for(var i = 0;i < ls.length;i++){
			var l = ls[i];
			if(typeof l == 'string'){
				l = $i(l);
			}
			this.els.push(l);
		}
		return this;
	}

	MK.prototype = {
		push:function(a){
			for(var j = 0;j < a.length;j++){
				this.els.push(a[j]);
			}
			return this;
		},
		
		all:function(f){
			for (var i = 0,xn = this.els.length;i < xn;++i){
				f.call(this, this.els[i]);
			}
			return this;
		},

		on:function(t, f){
			var lsn = function(el){
				if(window.addEventListener){
					el.addEventListener(t, f, false);
				} else if (window.attachEvent){
					el.attachEvent('on' + t, function(){
							f.call(el, window.event);
						}
					);
				}
			};
			this.all(function(el){
				lsn(el);
			});
			return this;
		},

		style:function(p, v){
			this.all(function(el){
				var u = v;
				if(p == 'opacity'){
					el.style['filter'] = 'alpha(opacity='+ u +')';		
					u = u/100;
				}
				el.style[p] = u;
			});
			return this;
		},

		css:function(o){
			var that = this;
			this.all(function(el){
				for(var p in o){
					that.style(p, o[p]);
				}
			});
			return this;
		},

		anim:function(f, b, e, t, c){
			var that = this;
			for(var p in b){			
				if(p != 'opacity' || p != 'zIndex'){
					e[p] = (e[p] - b[p]);
				}
			} 
			var nd = new Date().getTime() + t;
			var _a = window.setInterval(function(){
				var k = (t - (nd - new Date().getTime()));
				that.all(function(el){
					if(t <= k){
						window.clearInterval(_a);
						k = t;
					}
					for(var p in b){
						if(p == 'scrollLeft' || p == 'scrollTop'){
							el[p] = f(k, b[p], e[p], t);
						} else {
							var prt = (p == 'zIndex' || p == 'opacity') ? '' : 'px';
							that.style(p, f(k, b[p], e[p], t) + prt);
						}
					}
					if(t == k && c){
						c();
					}
				});
			}, 15);
			return this;
		}
	};
	window.$ = function(){
		return new MK(arguments);
	};

	$i = function(o){
		if(typeof o == 'string'){
			return document.getElementById(o);
		}
		return o;
	};

	$e = function(i, e){
		var d = e ? $i(i) : document;
		return d.getElementsByTagName(e);
	};

	$c = function(c,e){
		var d = e ? $i(e) : document;
		if(!document.getElementsByClassName){
		var r = [];
			c = new RegExp('\\b'+c+'\\b');
			d = d.getElementsByTagName('*');
			for(var i = 0;i < d.length;i++){
				if(c.test(d[i].className)){
					r.push(d[i]);
				}
			}
			return r;
		}
		return d.getElementsByClassName(c);
	};

	xhr = function(u, p, s, f){
		var X = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
		if(p == ''){
			X.open('GET', u, true);
		} else {
			X.open('POST', u, true);
			X.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		}
		X.onreadystatechange = function(){
			if(X.readyState == 4){
				var r = X.responseText;
				if(X.status == 200){
					if(s){s(r, X);}
				} else {
					if(f){f(r, X);}
				}
			}
		};
		X.send(p);
	};

	load = function(s,c){
		var f = document.createElement('script');
		f.type = 'text/javascript';
		f.onloadDone = false;
		if(c) {
            f.onload = function(){
	     		f.onloadDone = true;            
            	c();
            };
	     	f.onreadystatechange = function() {
	     		if (('loaded' === f.readyState || 'complete' === f.readyState) && !f.onloadDone) {
		     		f.onloadDone = true;
	     			c();
	     		}
	     	};
        }
		f.src = s;
		$e(document, 'head')[0].appendChild(f);
	};

	offSet = function(o){
		var l = 0;
		var t = 0;
		if(o.offsetParent){
			do{l += o.offsetLeft;t += o.offsetTop;} while(o = o.offsetParent);
			return [l, t];
		}
	};
	
	preventDefault = function(e){
		if (!e) e = window.event;
		e.cancelBubble = true;
		e.returnValue = false;
		if(e.preventDefault){
			e.preventDefault();
		}
	};
		
	if(!Array.indexOf){
		Array.prototype.indexOf = function(o,i){
			for(var j = this.length, i = i < 0 ? i + j < 0 ? 0 : i + j : i || 0; i < j && this[i] !== o; i++);
			return j <= i ? -1 : i
		}
	}

	ease = {
		cubic : function(t, b, c, d){
			if((t /= d / 2)<1){
				return (c / 2 * t * t * t + b) * 1;
			}
			return (c / 2 * ((t -= 2) * t * t + 2) + b) * 1;
		},
		
		bounce : function(t, b, c, d){
			if((t /= d)<(1 / 2.75)){
				return c * (7.5625 * t * t) + b;
			} else if(t <(2 / 2.75)){
				return c*(7.5625*(t-=(1.5/2.75))*t+.75)+b;
			} else if(t < (2.5 / 2.75)){
				return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .9375) + b;
			}else{
				return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .984375) + b;
			}
		}
	}
	MKAvail = true;
})();

if(/*@cc_on!@*/0){
	if(typeof document.body.style.maxHeight == 'undefined'){
	
	}
}

$(window).on('load', function(){
	var navEl = $e(document, 'nav')[0];
	var allNav = $().push($e(navEl, 'a'));
	var activeEl = 'loading';
	
	$($e(document, 'footer')[0]).push($e('about', 'p')).css({opacity: 80});
	
	setTimeout(function(){
		$('loading').anim(ease.cubic, {opacity: 100}, {opacity: 0}, 500, function(){
			$('loading').css({display: 'none'});
			$('lgo').css({display: 'block'}).anim(ease.cubic, {left: -200, top: -100}, {left: 300, top: 250}, 600, function(){
				$('lgo').anim(ease.cubic, {left: 300, top: 250}, {left: 200, top: 200}, 800);			
				$(navEl).css({display: 'block', opacity: 0}).anim(ease.cubic, {opacity: 0, left: 300, top: 250}, {opacity: 100, left: 480, top: 209}, 800, function(){

					var bg = document.createElement('div');
					bg.id = 'bgimage';
					$(bg).css({width: '100%', position: 'absolute', backgroundColor: '#000', top: '0px', left: '0px', zIndex: 1, opacity: 0});
					
					var bgimg = new Image();
					bgimg.onload = function(){
						bg.appendChild(bgimg);
						$(bgimg).css({width: '100%'});
						var bdy = $e(document, 'body')[0];
						bdy.appendChild(bg);
						$(bg).anim(ease.cubic, {opacity: 0}, {opacity: 100}, 800, function(){
						
							load('http://www.google-analytics.com/ga.js', function(){
								try {
									var pageTracker = _gat._getTracker('UA-8530211-3');
									pageTracker._trackPageview();
								} catch(err) {}
							});
							
						});
					};
					bgimg.src = 'img/b/'+ (Math.round(Math.random() * 3) + 1) +'.jpg';
				});
			});
		});
	}, 1000);
	
	var BGvisible = true;
	var showBG = true;
	allNav.on('click', function(){
		paintSel(this);
				
		var tar = this.getAttribute('href').split('#')[1];
		if (activeEl === tar){return;}
		switch(tar){
			case 'photo':	
				$('bgimage').anim(ease.cubic, {opacity: 100}, {opacity: 0}, 600, function(){
					$('bgimage').css({display: 'none'});
				});
				$(activeEl).anim(ease.cubic, {opacity: 100}, {opacity: 0}, 300, function(){
					$(activeEl).css({display: 'none'});
					$('lgo').anim(ease.cubic, {left: offSet($i('lgo'))[0], top: offSet($i('lgo'))[1]}, {left: -200, top: -300}, 500);			
					$(navEl).anim(ease.cubic, {left: offSet(navEl)[0], top: offSet(navEl)[1]}, {left: 50, top: 50}, 500, function(){
						activeEl = tar;										
						$('photo').css({display: 'block', opacity: 0}).anim(ease.cubic, {opacity: 0}, {opacity: 100}, 500, function(){
							$('photo').css({width: $i('photo').offsetWidth + 'px'});						
						});
					});
				});
				BGvisible = false;
				showBG = false;
				break;
				
			case 'about':
				$(activeEl).anim(ease.cubic, {opacity: 100}, {opacity: 0}, 300, function(){
					$(activeEl).css({display: 'none'});							
					$('lgo').anim(ease.cubic, {left: offSet($i('lgo'))[0], top: offSet($i('lgo'))[1]}, {left: 100, top: 100}, 500);			
					$(navEl).anim(ease.cubic, {left: offSet(navEl)[0], top: offSet(navEl)[1]}, {left: 360, top: 109}, 500, function(){
						activeEl = tar;					
						$('about').css({display: 'block', opacity: 0}).anim(ease.cubic, {opacity: 0}, {opacity: 100}, 500);
					});
				});
				showBG = true;						
				break;
				
			case 'contact':
				$(activeEl).anim(ease.cubic, {opacity: 100}, {opacity: 0}, 300, function(){
					$(activeEl).css({display: 'none'});
					$('lgo').anim(ease.cubic, {left: offSet($i('lgo'))[0], top: offSet($i('lgo'))[1]}, {left: 350, top: 200}, 500);			
					$(navEl).anim(ease.cubic, {left: offSet(navEl)[0], top: offSet(navEl)[1]}, {left: 580, top: 248}, 500, function(){
						activeEl = tar;					
						$('contact').css({display: 'block', opacity: 0}).anim(ease.cubic, {opacity: 0}, {opacity: 100}, 500);
					});
				});		
				showBG = true;						
				break;
				
			case 'blog':
				alert('Or something else ...');
				break;

		}
		if(showBG && !BGvisible){
			$('bgimage').css({display: 'block'}).anim(ease.cubic, {opacity: 0}, {opacity: 100}, 600);
			BGvisible = true;
		}
	});
	
	var paintSel = function(cur) {
		allNav.all(function(el){
			el.className = '';
		})
		cur.className = 'sel'
	};
	
	var fullSize = false;
	var tmbs = $().push($e('photo', 'img'));
	tmbs.on('mouseover', function(){
		if(!fullSize){
			$(this).css({opacity: 70});
		}
	}).on('mouseout', function(){
		if(!fullSize){
			$(this).css({opacity: 100});
		}
	}).on('click', function(){
		if(!fullSize){	
			fullSize = true;
			var that = this;
			tmbs.all(function(el){
				$(el.parentNode).css({backgroundColor: '#000'});
				if(el != that){
					$(el).anim(ease.cubic, {opacity: 100}, {opacity: 30}, 500);
				}
			});
			$(this).css({opacity: 100});
			
			var newSrc = this.src.split('/');
			newSrc = newSrc[newSrc.length - 1];
			
			if(!$i('lightbox')){
				var lgtb = document.createElement('div');
				lgtb.id = 'lightbox';
				$e(document, 'body')[0].appendChild(lgtb);

				var closeBut = document.createElement('img');
				closeBut.id = 'closeB';
				closeBut.setAttribute('alt', 'Close');
				closeBut.src = '/img/closeTMB.png';
				$e(document, 'body')[0].appendChild(closeBut);
				
				$(closeBut).on('click', function(){
					fullSize = false;
					$(closeBut).css({display: 'none'});
					$('lightbox').anim(ease.cubic, {opacity: 100}, {opacity: 0}, 500, function(){
						if($i('lightbox').firstChild){
							$i('lightbox').removeChild($i('lightbox').firstChild);
						}
						$('lightbox').css({display: 'none'});
						$(navEl).anim(ease.cubic, {left: offSet($i(navEl))[0]}, {left: 50}, 500);
						$('photo').anim(ease.cubic, {left: offSet($i('photo'))[0]}, {left: 270}, 500);
					});
				});									
			} else {
				var lgtb = $i('lightbox');
			}
			
			var lbx = $('lightbox');
			lbx.css({opacity: 0});

			setTimeout(function(){
				$(navEl).anim(ease.cubic, {left: offSet($i(navEl))[0]}, {left: -5000}, 500);
				$('photo').anim(ease.cubic, {left: offSet($i('photo'))[0]}, {left: -5000}, 500, function(){
					
					tmbs.all(function(el){
						$(el.parentNode).css({backgroundColor: '#fff'});
						el.fullSize = false;
						$(el).css({opacity: 100});
					});
				
					lbx.css({display: 'block'}).anim(ease.cubic, {opacity: 0}, {opacity: 100}, 500);
					
					var newImg = new Image();
					newImg.onload = function(){
						var nImg = $(newImg);
						nImg.css({opacity: 0});
						lgtb.appendChild(newImg);
						lbx.css({width: newImg.offsetWidth +'px'});
						$(newImg).anim(ease.cubic, {opacity: 0}, {opacity: 100}, 500, function(){
							$('closeB').css({top: (offSet(newImg)[1] + 10) + 'px', left: (offSet(newImg)[0] + newImg.offsetWidth) - 16 + 'px', display: 'block'});
						});
					};
					newImg.src = 'img/l/'+ newSrc;				
				});
			}, 1000);
		}
	}).all(function(el){
		//var preload = new Image();
		//preload.src = el.src.replace('/t/', '/l/');
	});

	var scrolling = false;
	$(document).on('mousemove', function(e){
		if (activeEl != 'photo' || fullSize) { return; }
		var pOs = offSet($i('photo'));
		var pT = pOs[1];
		var pL = pOs[0];
		var pB = pT + $i('photo').offsetHeight;			
		if (!e) { var e = window.event; }
		if (e.pageX || e.pageY){
			pX = e.pageX;
			pY = e.pageY;
		} else if (e.clientX || e.clientY){
			pX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			pY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
		if(pX > pL){
			if((pY > pT) && (pY < (pT + 150))){
				if(scrolling){ return; }
				scrolling = true;
				$('photo').anim(ease.cubic, {scrollTop: $i('photo').scrollTop}, {scrollTop: 0}, 600, function(){
					scrolling = false;
				});
			}
			if((pY > (pB - 150)) && pY < pB){
				if(scrolling){ return; }
				scrolling = true;
				$('photo').anim(ease.cubic, {scrollTop: $i('photo').scrollTop}, {scrollTop: $i('photo').offsetHeight}, 800, function(){
					scrolling = false;
				});
			}		
		}	
	});
});