var $j = jQuery.noConflict();

jQuery.easing.def = "easeOutCubic";

$j(document).ready(function() {
	
	
	
	/* Setup variables
	-----------------------------------------------------*/
	$slideshow = false;
	$count = 0;
	$speed = 0;
	
	
	
	/* Variables
	-----------------------------------------------------*/
	$win_w = 560;
	$list = $j(".overviewImage");
	$spacers = $j(".imageSpacer");
	$total = $list.length;
	
	$spacerWidth = 3;
	$condensed = 560/$total;
	$expanded = 350;
	$shrunk = (560-$expanded)/($total-1);
	
	
	/* Initialize the gallery
	-----------------------------------------------------*/
	resetImages();
	$speed = 800;
	
	
	
	function resetImages() {
		
		for ($i=0; $i<$total; $i++) {
			
			// Set the div position
			$left = String(($condensed * $i)+"px");
			
			// Set the image offset
			$condensedPos = String("-"+$j($list[$i]).attr("condensed")+"px");
			
			// Animate the image
			$j($list[$i]).stop().animate({
				left: $left,
				width: $condensed
				}, $speed, function() {
			});
			
			// Animate the margin
			$j($list[$i]).children("img").stop().animate({
				marginLeft: $condensedPos
				}, $speed, function() {
			});
			
			if ($i<($total-1)) {
				
				$spacerPos = String((($condensed * ($i+1)) - 1) +"px");

				// Animate the spacer
				$j($spacers[$i]).stop().animate({
					left: $spacerPos
					}, $speed, function() {
				});
				
			}
		}
		
		$j("#photoCredit").remove();
	}
	
	function slideImage($pos) {
		
		$left = 0;
		
		for ($i=0; $i<$total; $i++) {
			
			if ($i==$pos) {
				
				
				$artist = $j(".overviewImage:eq("+$pos+")").attr("image_artist");
				$detail = $j(".overviewImage:eq("+$pos+")").attr("image_detail");
				$credit = "<div id='photoCredit'><p>"+$artist+"</p><p class='detail'><span class='blueHash'>//&nbsp;&nbsp;</span>"+$detail+"</p></div>"
				
				$j("#photoCredit").remove();
				
				$j("#mainContent").children(".col_3").prepend($credit);
				
				$j($list[$i]).stop().animate({
					left: $left,
					width: $expanded
					}, $speed, function() {
				});
	
				$expandedPos = String("-"+$j($list[$i]).attr("expanded")+"px");
				
				$j($list[$i]).children("img").stop().animate({
					marginLeft: $expandedPos
					}, $speed, function() {
				});
				
				$left = $left + $expanded;
	
				if ($i<($total-1)) {
					
					$spacerPos = String(($left - 1) +"px");
					
					// Animate the spacer
					$j($spacers[$i]).stop().animate({
						left: $spacerPos
						}, $speed, function() {
					});
					
				}
				
			} else {
				
				$j($list[$i]).stop().animate({
					left: $left,
					width: $shrunk
					}, $speed, function() {
				});
	
				$condensedPos = String("-"+$j($list[$i]).attr("condensed")+"px");
				
				$j($list[$i]).children("img").stop().animate({
					marginLeft: $condensedPos
					}, $speed, function() {
				});
				
				$left = $left + $shrunk;
				
				if ($i<($total-1)) {
					
					$spacerPos = String(($left - 1) +"px");
					
					// Animate the spacer
					$j($spacers[$i]).stop().animate({
						left: $spacerPos
						}, $speed, function() {
					});
					
				}
				
			}
		}
		
	}
	
	
	
	
	/* Mouse Functions
	-----------------------------------------------------*/
	
	$j(".eventBlock").hover(
		function () {
			$index = $j(this).prevAll().length;
			slideImage($index);
		}, 
		function () {
			resetImages();
		}
	);
	
	$j(".overviewImage").hover(
		function () {
			$j(".eventBlock").removeClass("selected");
			$index = Number(Number($j(this).attr("pos")) - 1);
			$j(".eventBlock:eq("+$index+")").addClass("selected");
			slideImage($index);
		}, 
		function () {
			$j(".eventBlock").removeClass("selected");
			resetImages();
		}
	);
	
	$j(".overviewImage").click(function () { 
		window.location = $j(this).attr("link");
    });
	
});
