/*
 * (c) KAYAC Inc. <http://www.kayac.com/>
 * MIT Licensed <http://www.opensource.org/licenses/mit-license.php>
 */

if ($.browser.msie) document.execCommand('BackgroundImageCache', false, true);


(function($) {
	var userAgent = navigator.userAgent.toLowerCase();
	var b = $.browser;
	$.extend(b, {
		msie6 : b.msie && b.version < 7,
		iphone: b.safari && /iphone/.test(userAgent),
		chrome: b.safari && /chrome/.test(userAgent)
	});
})(jQuery);

new function() {
	var options = {
		path: '/img'
	};

	$.replaceImageSetup = function(o) {
		options = o;
	};

	$.fn.replaceImage = function(ext) {
		return this.each(function() {
			if (!this.id) return;
			var self = $(this);

			var img = $('<img/>');
			var src = options.path+'/'+this.id+'.'+ext;
			img.attr({
				src: src,
				alt: self.text()
			});
			var a = $('a', self);
			var element = a.length ? a.eq(0).html(img) : img;
			self.html(element);
		});
	};
};

$.fn.external = function() {
	return this.each(function(){ this.target = '_blank'; });
};

$.fn.roll = function roll(o) {
	o = o || '_o';
	return this.each(function() {
		var src = this.src;
		if (!src) {
			var target = $('img, input[type="button"]', this);
			if (target.length) roll.call(target, o);
			return;
		}

		var out  = src;
		var over = src.replace(/(\.[^.]+$)/, o+'$1');
		$('<img src="'+over+'"/>');
		$(this).hover(
			function() { this.src = over; },
			function() { this.src = out; }
		);
	});
};

$.fn.fl = function(o) {
	if (!o) {
		var id = this.attr('id');
		return $.browser.msie ? window[id] : document[id];
	}

	o = $.extend({
		version: '9.0.47',
		params : {
			wmode: 'transparent'
		}
	}, o);
	if (swfobject.hasFlashPlayerVersion(o.version)) {
		o.attrs = $.extend(o.attrs || {}, {
			data  : o.src,
			width : o.width,
			height: o.height
		});
		if (o.flashvars) o.params.flashvars = $.param(o.flashvars);

		var element = o.id ?
			$('<div id="'+o.id+'"/>').appendTo(this) :
			this;
		swfobject.createSWF(
			o.attrs,
			o.params,
			element.attr('id')
		);
	}
	return this;
};

$.fn.sameHeight = function() {
	var h = 0;
	return this
		.each(function() {
			h = Math.max($(this).height(), h);
		})
		.height(h);
};


stash = {};

if (typeof ArtWall == 'undefined') ArtWall = {};


ArtWall.setup = function() {
	var ext = /j(png|jpg|gif)/;
	$('.jpng, .jjpg, .jgif').each(function() {
		var e = ext.exec(this.className)[1];
		$(this).replaceImage(e);
	});

	$('a[rel=external]').external();
	$('.btn').roll();
	$('.blockCont').sameHeight();

	$('.photoList').each(function() {
		$('a', this).lightBox({
			imageLoading : '/img/lightbox/lightbox-ico-loading.gif',
			imageBtnClose: '/img/lightbox/lightbox-btn-close.gif',
			imageBtnPrev : '/img/lightbox/lightbox-btn-prev.gif',
			imageBtnNext : '/img/lightbox/lightbox-btn-next.gif'
		});
	});

	ArtWall.Navigation.setup();

	var target = location.hash || '#about';
	var a = $('#navPager a[href="'+target+'"]');
	if (target && a.length)
		ArtWall.Navigation.menu({ target: a[0] });
};


ArtWall.Navigation = (function() {
	var container;
	var html = $('html, body');
	var clicked;

	return {
		setup: function() {
			container = $('#container');

			$('#navPager').click(this.menu);
			$('#pager li')
				.click(this.pager)
				.each(function() {
					var prev_button = $(this).hasClass('prev');
					$(this).hover(
						function() {
							if (prev_button && !stash.prev.length || !prev_button && !stash.next.length) return false;

							var params = prev_button ? { left: 0 } : { right: 0 };
							$(this)
								.addClass('hover')
								.find('img').animate(params, 150);
						},
						function() {
							var params = prev_button ? { left: -21 } : { right: -21 };
							$(this)
								.removeClass('hover')
								.find('img').animate(params, 150);
						}
					);
				});
		},

		menu: function(e) {
			var a = $(e.target);
			if (a.hasClass('current')) return false;
			clicked = null;

			ArtWall.Navigation.scroll('#column-'+a.attr('href').slice(1));

			a.addClass('current');
			var before = stash.current;
			if (before) before.removeClass('current');
			stash.current = a;
			var li = a.parent();
			stash.prev = li.prev().find('a');
			stash.next = li.next().find('a');

			return false;
		},

		pager: function(e) {
			var prev = stash.prev;
			var next = stash.next;
			var li = $(e.currentTarget);
			var prev_button = li.hasClass('prev');

			var event;
			if (prev_button) {
				if (prev.length) event = { target: prev[0] };
			}
			else {
				if (next.length) event = { target: next[0] };
			}
			if (event) {
				ArtWall.Navigation.menu(event);
				clicked = li;
				li.mouseleave();
			}
		},

		scroll: function(target) {
			var self = this;
			var t = $(target);
			var left = t.position().left;

			var slide = (container.scrollLeft() != left);
			if ($().scrollTop() == 0)
				slide ? scroll_x() : scroll_complete();
			else
				scroll_y();

			return left;

			function scroll_y() {
				var complete = slide ? scroll_x : scroll_complete;
				html.animate({
					scrollTop: 0
				}, {
					duration: 300,
					easing  : 'easeOutSine',
					complete: complete
				});
			}

			function scroll_x() {
				container.animate({
					scrollLeft: left
				}, {
					duration: 600,
					easing  : 'easeOutSine',
					complete: scroll_complete
				});
			}

			function scroll_complete() {
				if (clicked) {
					var prev = clicked.hasClass('prev');
					if (prev && stash.prev.length || !prev && stash.next.length)
						clicked.mouseenter();
				}
			}
		}
	};
})();
