// SuperBGImage setup
$(function(){
	$.fn.aob_background({controls_id: 'control'});
	$.fn.aob_barter();
});

// General setup
$(document).ready(function() {

	// Content intialization

	var $ThumbsVisible = false;
	var $AboutVisible  = false;

	$('#loader').hide();
	$('#content_holder').hide();
	$('#navigation').slideToggle(0);	

	// Event handlers 
	
	// Navigation handlers
	$('#about').click(function(){  return updateNavigation(this) });
	$('#art').click(function(){    return updateNavigation(this) });
	$('#barter').click(function(){ return updateNavigation(this) });
	
	$("#leftclose a").click(function() {
		$('#navigation').slideToggle(250, function() {
			$('#leftclose a').toggleClass('down');
			$('#content_holder').slideUp(500);
		});
	});

	// control thumbs click
	$('#control a.thumbs').click(function() { toggleThumbs() });

	//	thumbs
	$("#thumbs a img").click(function() { hideThumbs() });

	$("#thumbs a img").fadeTo("fast", 0.3);
	$("#thumbs a img").hover(function(){
			$(this).fadeTo("fast", 1.0);
		},function(){
			$(this).fadeTo("fast", 0.3); 
	});

	// contentclose click
	$("#contentclose a").click(function() {
		$('#content_holder').slideUp(500, function () {
			clearCurrentNav();
			if ($ThumbsVisible) hideThumbs();
			if ($AboutVisible)  hideAbout();
			$.fn.aob_barter('hide');	
			$('#art').addClass('current');
			$.fn.aob_background('start');
		});	
	});

	// stop basic mousing of images
	$('#mask').click(function(e){
		e.stopPropagation();
		return false;
	});

	// control helper functions
	function updateNavigation(eventElem) {
		eventElem = $(eventElem);

		var id = eventElem.attr('id');

		clearCurrentNav();

		$('#content_holder').slideUp(500, function () {
			switch (id) {
				case 'art':
					if ($ThumbsVisible) hideThumbs();
					if ($AboutVisible)  hideAbout();
					$.fn.aob_barter('hide');	
					eventElem.addClass("current");
					break;
				case 'about':
					if ($ThumbsVisible) hideThumbs();
					$.fn.aob_barter('hide');	
					toggleAbout();
					break;
				case 'barter':
					if ($ThumbsVisible) hideThumbs();
					if ($AboutVisible)  hideAbout();
					$.fn.aob_barter('toggle',function(){$('#art').addClass('current')});	
					break;
			}
		});

		return false;
	}

	function clearCurrentNav() {
		$('ul.nav li a').removeClass('current');
	}

	function hideThumbs() {
		$('#content_holder').slideUp(500);
		$.fn.aob_background('start');
		$ThumbsVisible = false;
	}

	function showThumbs() {
		$('#content_holder').slideUp(500, function () {
			$('#content').hide();
			$('#thumbs').show();
			$('#content_holder').slideDown(500);
		});
		$.fn.aob_background('stop');
		$ThumbsVisible = true;
	}

	function toggleThumbs() {
		$ThumbsVisible ? hideThumbs() : showThumbs();
	}

	function hideAbout() {
		$('#content_holder').slideUp(500);
		$.fn.aob_background('start');
		$.fn.aob_background('show_controls');
		$AboutVisible = false;
	}

	function showAbout() {
		$('#content_holder').slideUp(500, function () {
			$('#thumbs').hide();
			$('#content').show();
			$('#content_holder').slideDown(500);
		});
		$.fn.aob_background('stop');
		$.fn.aob_background('hide_controls');
		$('#about').addClass('current');
		$AboutVisible = true;
	}

	function toggleAbout() {
		$AboutVisible ? hideAbout() : showAbout();
	}
	
	// map any external links to open new windows
	$("a[href^='http:']:not([href*='" + window.location.host + "'][target='_blank'])").live('click', function(){
		$(this).attr('target','_blank');
	});

	// setup our contact form
	$('#contact_form').submit(function(){
		$('#contact_errors').hide();
		jQuery.post('/message', $('#contact_form').serialize(), function(data, textStatus, jqXHR){
			if ( data['status'] == 'success' ) {
				$('#contact_us').html(data['message']);				
			} else {
				$('#contact_errors').html(data['message']);
				$('#contact_errors').show();
			}
		},'json');
		return false;
	});
});

