fat = {};

fat.modules = {};

fat.modules.homepage = {
	switchPhoto : function() {
	    var $active = $('#HomePageImage img.active');

	    if ( $active.length == 0 ) $active = $('#HomePageImage img:last');
		
		//alert($active.html());
	
	    var $next = $active.next().length ? $active.next()
	        : $('#HomePageImage img:first');
	
		fat.modules.homepage.switchCaption($next.attr('pos'));
		
	    $active.addClass('last-active');
	
	    $next.hide()
	        .addClass('active')
	        .fadeIn(1000, function() {
	            $active.removeClass('active last-active');
	        });
	},
	switchCaption : function(pos) {
		
		var nameSpan = $('#HomePageImage .artist-name');
		var mediumSpan = $('#HomePageImage .artist-medium');
		var linkTag = $('#HomePageImage .caption');
		
		var nImage = $('#HomePageImage img[pos='+pos+']');
		//alert(nImage.attr('rel'));
		nameSpan.text(nImage.attr('rel'));
		mediumSpan.text(nImage.attr('med'));
		linkTag.attr('href', nImage.attr('href'));
	}
}; //end homepage

fat.modules.artists = {
	switchPhoto : function(id) {
	    var $active = $('#LargePhoto div.active');
	    var $next = $("#LargePhoto div[rel='"+id+"']");
	    if($active.attr('rel') != id) { 
			$active.addClass('last-active');
		    $next.hide()
		        .addClass('active')
		        .fadeIn(500, function() {
		            $active.removeClass('active last-active');
		        });
	    }
	}
};

$(function() {
	//$.preloadCssImages();
	
	$(document).scroll(function () {
		var locHeaders = $('.location-header');
	    var sticky = locHeaders.first();
	
	   
        if (sticky.offset().top < $(window).scrollTop()) {
            //Above screen
            //alert("GO!"); // Exit loop
            $('.location-header-fixed').show();
			
        } else {
        	$('.location-header-fixed').hide();
        }
	});
	
	$(document).keydown(function(e) {
		//alert(e.keyCode);
		
		if (e.keyCode) {
        	key = e.keyCode;
  		} else { // mozilla
	        key = e.which;
      	}
		switch(key) {
			case 37: 
			 $('img.prev').click();
			  break;
			case 38: 
			  $("#ProjectTitle, #DesignTitle").click();
			  break;
			case 39: 
			  $('img.next').click();
			  break;
			case 40: 
			  $("#ProjectTitle, #DesignTitle").click();
			  break;
			case 13: 
			  //$.keynav.activate();
			  break;
		}
	});
	
	var homeCopyWrap = $("#HomeContent");
	if(homeCopyWrap.length) {
	    setInterval( "fat.modules.homepage.switchPhoto();", 7000 );
	}
	
	// ARTIST LIST PAGE
	
	var hiConf = {
		over: openThumb,
		timeout: 200,
		interval: 50,
		out: closeThumb
	}
	
	function openThumb() {
		$(this).find('.a-list-thumb').stop(true, true).slideDown(400);
	}
	
	function closeThumb() {
		$(this).find('.a-list-thumb').stop(true, true).slideUp(400);
	}
	
	$('.a-li').hoverIntent(hiConf);
	
	/*$('.a-li').hover(function() {
		$(this).find('.a-list-thumb').stop(true, true).slideDown(400);
	}, function() {
		$(this).find('.a-list-thumb').stop(true, true).slideUp(400);
	});*/
	
	
	// ARTIST PAGE
	$("#Thumbs img.a-thumb").click(function() {
		fat.modules.artists.switchPhoto($(this).attr('rel'));
	});
	

	$('.dl-more').click(function() {
		$(this).parent().find('.dl-info-wrap').slideToggle(250);
		return false;
	});
	
});
