var testing = /(local\.?|beta\.)/i.test(window.location.host);
var navWidth = navOffset = 0;

var getStyleProperty = (function(){
	var prefixes = ['Moz', 'Webkit', 'Khtml', 'O', 'Ms'];
	var _cache = { };
	function getStyleProperty(propName, element) {
		element = element || document.documentElement;
		var style = element.style, prefixed, uPropName;
		if (arguments.length == 1 && typeof _cache[propName] == 'string') {
			return _cache[propName];
		}
		if (typeof style[propName] == 'string') {
			return (_cache[propName] = propName);
		}
		uPropName = propName.charAt(0).toUpperCase() + propName.slice(1);
		for (var i=0, l=prefixes.length; i<l; i++) {
			prefixed = prefixes[i] + uPropName;
			if (typeof style[prefixed] == 'string') {
				return (_cache[propName] = prefixed);
			}
		}
	}
	return getStyleProperty;
})();

function arrowVisibility(element, arrows) {
	var $elem = $(element);
	var current = $elem.scrollLeft();
	var max = $(element+'Scroll').width() - $elem.width();
	if (current == 0) {
		$('#'+arrows+'Left').fadeTo(250, 0);
	} else {
		$('#'+arrows+'Left').fadeTo(250, 1);
	}
	if (current == max) {
		$('#'+arrows+'Right').fadeTo(250, 0);
	} else {
		$('#'+arrows+'Right').fadeTo(250, 1);
	}
}

$(document).ready(function() {
	// Progressive Enhancement
	$('.nav ul li').each(function() {
		navWidth += $(this).width();
		navWidth += parseInt($(this).css('padding-left'), 10) + parseInt($(this).css('padding-right'), 10);
		navWidth += parseInt($(this).css('margin-left'), 10) + parseInt($(this).css('margin-right'), 10);
		navWidth += parseInt($(this).css('border-left-width'), 10) + parseInt($(this).css('border-right-width'), 10);
	});
	$('.nav ul').css('width', navWidth);
	if (typeof getStyleProperty('boxShadow') == 'string') {
		$('.frame').css({'border': 'none', 'padding': 4, 'box-shadow': '0 1px 4px #666', '-moz-box-shadow': '0 1px 4px #666', '-webkit-box-shadow': '0 1px 4px #666'});
	}
	// Shared functions
	if ($('#pages')) {
		$('#pages>div').css({'float': 'left', 'overflow': 'hidden', 'position': 'relative', 'width': 512});
		$('#pages').css({'overflow': 'hidden', 'position': 'relative', 'width': $('#pages>div').length * 512});
		$('#pages>div').css({'height': $('#pages').css('height')});
		$('#pages').before('<div id="pager" style="float: left; overflow: hidden; margin-right: 32px; width: 256px"><ul style="margin: 0; padding: 0"></ul></div>').wrap($('<div id="pageScroller" style="float: left; overflow: hidden; width: 512px"></div>'));
		$('#pageScroller').before('<div id="pageLeft" />').after('<div id="pageRight" />');
		$('#pages div h3').each(function() {
			$('#pager ul').append('<li style="list-style: none; text-align: right"><a href="#'+$(this).parent().attr('id')+'" style="height: 24px; line-height: 24px">'+$(this).html()+'</a></li>');
		});
		if ($('#pageScroller').length > 0) {
			var pageScroller = $('#pageScroller').position();
			$('#pageLeft, #pageRight').css({'background': 'url(/images/pageScroller.png) repeat-y', 'height': $('#pageScroller').height(), 'position': 'absolute', 'top': pageScroller.top, 'width': 16, 'z-index': 100}).hide();
			$('#pageLeft').css({'background-position': 'left top', 'left': pageScroller.left}).hide();
			$('#pageRight').css({'background-position': 'right top', 'left': pageScroller.left + $('#pageScroller').width() - 16}).hide();
		}
		$('#pager li a').click(function() {
			$('#pageLeft, #pageRight').fadeIn(50);
			$('#pager a').css('color', '');
			$(this).css('color', '#1a1a1a');
			$('#pageScroller').animate({ 'scrollLeft': $($(this).attr('href')).position().left }, 800, null, function() {
				$('#pageLeft, #pageRight').fadeOut();
			});
			return false;
		});
		$('#pager li:first a').css('color', '#1a1a1a');
	}
	if ($('body').hasClass('news')) {
		if ($('#gallery').children('a').size() > 8) {
			$('#gallery').css({'height': 100, 'margin-bottom': 8, 'position': 'relative'}).wrapInner($('<div id="image" style="height: 100px; overflow: hidden; width: 800px"><div id="imageScroller" style="overflow: hidden"></div></div>'));
			$('#image').before('<div id="imageLeft" style="background-position: left top; left: -16px" />').after('<div id="imageRight" style="background-position: right top; right: -16px" />');
			$('#imageLeft, #imageRight').css({'background-image': 'url(/images/imageScroller.png)', 'height': 100, 'position': 'absolute', 'top': 0, 'width': 20, 'z-index': 1});
			$('#imageScroller').css({'height': 100, 'width': $('#imageScroller').children('a').size() * 100});
			$('#imageScroller a').css({'float': 'left'});
			$('#imageLeft').click(function() {
				$('#image').animate({ 'scrollLeft': '-=800px' }, 800, null, function() {
					 arrowVisibility('#image', 'image');
				});
			});
			$('#imageRight').click(function() {
				$('#image').animate({ 'scrollLeft': '+=800px' }, 800, null, function() {
					 arrowVisibility('#image', 'image');
				});
			});
			arrowVisibility('#image', 'image');
		}
		$('#gallery a').loupe({ folder: '/images' });
	}
	if ($('body').hasClass('contact')) {
		$('form').submit(function(){
			$('.submit').addClass('submitting');
			$('#submit').attr('disabled', 'true').attr('value', 'Sending...');
			var str = $('form').serialize();
			$.ajax({
				type: 'POST',
				url: 'contact',
				data: str,
				success: function(msg){
					$('#note').ajaxComplete(function(event, request, settings){
						if(msg == 'OK'){
							result = '<div class="success">Your message has been sent.<br>Thank you!</div>';
							$('.field, .submit').hide();
						}else{
							$('.submit').removeClass('submitting');
							$('#submit').removeAttr('disabled').attr('value', 'Submit');
							result = msg;
						}
						$(this).html(result);
					});
				}
			});
			return false;
		});
	}
});