jQuery( function( $ ) {
	
   	/* * * * *  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^     
 	* * * *		( Init )
 	  * * * * * * * * * * * * * * * * * * * * * * * */

	var sSize = { 
		normal : 1,
		small : 2
	};
	
	var screenWidth = sSize.normal; 
	var screenHeight = sSize.normal;
	
	var n = 0;
	var t = 1;
	
	// Check the screen size, apply scaled CSS as needed.
	//resize();
	$(window).bind('resize', function() { resize();	});
	
	var simple_browser = isTerribleBrowser();			// Determine if we're on an outdated or unsupported browser... scale back if so.

	// $(".gallery_photo").thumbPopup();

	if (false) $("body").js_shell();
	
	// Enable scrolling thumbnail streams for the film strip.
	//makeScrollable($('#footer_container'), $('#footer_content'), 15);
	makeFooterReel($('#footer_wrapper'), $('#footer_content'), $("#nothing"));
	makeThumbReel($('#item_thumbnail_container'), $('#item_thumbnail_reel'), $('.style_option'),
				  $('.thumbnail_cell .active'), $('#item_image'), $('#enlarge_image'));
	$(window).trigger('resize');
	//var c = $('#footer_container').width();
	

	/* * * * * 	^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
	* * * *		( Site Functionality and Nav )
	* * *
	* *
	 */

	// This is disabled for now.
	if (0 && !simple_browser) {
		$('.snav1').click( function() {
			loadNewPage("/aboutjax/");
			toggle_nav(true);
			return false;
		});
		$('.snav4').click( function() {
			loadNewPage("/linksjax/");
			toggle_nav(true);
			return false;
		});
	}
	
	function toggle_nav(keepClosed) {
		if (is_nav_extended()) {
			$('#scissors').fadeOut('fast');
			$('#flurish1').animate({
				left:'-230px'
			}, 750);
			$('.nav_extended').removeClass('active');
		} else if (!keepClosed) {

			$('#flurish1').animate({
				left:'0px'
			}, 750);
			$('.nav_extended').addClass('active');
			$('#scissors').fadeIn('fast');
		}
	}

	function is_nav_extended() {
		return $('#flurish1').hasClass('active');
	}


	/* * * * * 	^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
	* * * *		( Item Detail Image Handlers )
	* * *
	* *
	 */

	function makeThumbReel($wrapper, $container, $style_options, $active_img, $item_image, $enlarge) {
		var $avail_images	= $container.children("div.thumbnail_cell:not(.hidden)");		// All of the currently visible images.
		var cell_width 		= $avail_images.outerWidth();
		
		cell_width 			= (cell_width == 0 ? 78 : cell_width);
		
		var position		= 0;															// The index of the displayed image
		
		sizeContainer();
		
		$wrapper.siblings('a.arrow').click(function(e) {
			var len		= $avail_images.length-4;
			position	+= ( $(this).hasClass('nav_right') ? 1 : -1 );				// Determine which arrow was clicked, adjust direction
			
			if (position > len) {
				position = 0;																// Reached the end, loop back to the beginning.		
			} else if (position < 0) {
				position = len;											// We went below the first, loop to the end-
			}
			
			$wrapper.animate({
					scrollLeft: (position <= len ? (position)*cell_width : 0)
			}, 'fast');
			
			return false;
		});
		
		$container.find("a").click(function(e) {
			// Set this cell to active.
			$(this).parent().siblings().removeClass('active');
			$(this).parent().addClass('active');

			// This updates the primary image...
			img_src = $(this).attr('rel');

			// Create a temp loading img...
			$item_image.parent().prepend("<img id='loading_image' style='display: none;'>");
			
			$('#loading_image').load( function() {
				var id = $item_image.attr('id');
				$item_image.remove();													// Remove the current image
				
				$(this).attr("id", id);													// Replace it with the loaded image
				$(this).css({'display':'block'});
				$item_image = $(this);
				$enlarge.attr('href', $(this).attr('src').replace('displaysmall', 'display'))
			}).attr('src', img_src);
			
			return false;
		});
		
		$style_options.click( function() {
			if($(this).hasClass('active'))  return false;
			
			$(this).siblings($style_options).removeClass('active');
			$(this).addClass('active');
			
			// A style option, such as color or print, has been chosen. Update the thumbnails, etc.
			attribute = '.'+$(this).attr('id');
	
			// Apply classes to thumbnail cells, filtering items by attribute.
			activateAttribute(attribute, $container, true, true);
			$wrapper.scrollLeft(0);

			sizeContainer();
			
			// Click the first thumbnail of the new set finish the process.
			$('div.'+attribute+':first > a').click();
			return false;
		});
		
		function activateAttribute($attribute, $parent, hideOthers, fadeInOut) {
			if (hideOthers) {
				$parent.children().addClass('hidden');
			}
			$parent.children($attribute).removeClass('hidden');
		}
		
		function sizeContainer() {
			$avail_images	= $container.children("div.thumbnail_cell:not(.hidden)");		// All of the currently visible images.
			
			$container.css({width: (cell_width*$avail_images.length)+'px' });
			
			if ($avail_images.length < 4) {
				$wrapper.siblings('div.arrow').addClass('hiddenOp');
			}
			else if ($wrapper.siblings('div.arrow').hasClass('hiddenOp')) { $wrapper.siblings('div.arrow').removeClass('hiddenOp', 'fast'); }
		}
	}
	
	
	/* * * * *  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
	* * * *		( Footer Film Strip Business )
	* * *
	* *
	 */
	
	function makeFooterReel($wrapper, $container, $active_img) {
		var $avail_images	= $container.children();										// All of the currently visible images.
		var cell_width 		= $avail_images.outerWidth();
		var num_shown		= Math.floor($wrapper.outerWidth()/cell_width);					// Number of items visible at the current container width
		cell_width 			= (cell_width == 0 ? 115 : cell_width);
		var position		= 0;															// The index of the displayed image
		var prev_pos		= getCookie('thumbNavPos');
		if (prev_pos) {
			position = parseInt(prev_pos);													// Acquire position from the selected image, if possible.
			scrollToPostion();
			deleteCookie('thumbNavPos');
		}
		
		sizeContainer();
		
		function scrollToPostion() {
			var len		= $avail_images.length-num_shown;
			if (position > len) {
				position = len;																// Reached the end, loop back to the beginning.		
			} else if (position < 0) {
				position = 0;																// We went below the first, loop to the end-
			}
			
			$wrapper.scrollLeft(position*cell_width);
		}
		
		$wrapper.siblings('div.arrow').find('a').click(function(e) {
			var len		= $avail_images.length-num_shown;
			position	+= ( $(this).parent().hasClass('nav_right') ? 1 : -1 );				// Determine which arrow was clicked, adjust direction

			if (position > len) {
				position = 0;																// Reached the end, loop back to the beginning.		
			} else if (position < 0) {
				position = len;																// We went below the first, loop to the end-
			}
			
			$wrapper.animate({
					scrollLeft: (position <= len ? (position)*cell_width : 0)+'px'
			}, 'fast');
			
			return false;
		});
		
		$container.find("a").click(function(e) {
			setCookie('thumbNavPos', position);
		});
		
		$(window).bind('resize', function(e) {
			num_shown		= Math.floor(($(window).width()-$wrapper.siblings('div.arrow').outerWidth()*2)/cell_width);
			$wrapper.css({width: num_shown*cell_width+'px'});
			
			$wrapper.parent().css({ width: $wrapper.siblings('div.arrow').outerWidth()*2+$wrapper.outerWidth()+'px'});
		});
		
		function sizeContainer() {
			$container.css({width: (cell_width*$avail_images.length)+'px' });
		}
	}

	
	function makeScrollable($wrapper, $container, contPadding) {
		//Get menu width
		var prevPos = getCookie('thumbNavPos');                 // Cookie for the previous offset positon
		var edgeScrollersWidth = 50;
		var divWidth = $wrapper.width();
		var enterPos = 0;

		//Remove scrollbars
		$wrapper.css({
			overflow: 'hidden'
		});

		//Find last image container
		var lastLi = $container.find('a:last-child');
		$wrapper.scrollLeft(0);
		//When user move mouse over menu

		// if we have a position cookie, use it

		if (prevPos) {
			$wrapper.scrollLeft((prevPos/1)*-1);
		}
		$wrapper.unbind('mouseenter').bind('mouseenter', function(e) {
			enterPos = e.pageX;
			// $('#log').append('<div>' + $wrapper.scrollLeft() + '</div>');

		});
		$wrapper.unbind('mousemove').bind('mousemove', function(e) {

			//As images are loaded ul width increases,
			//so we recalculate it each time
			var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + contPadding;
			//var factor = float(divWidth)/ float((divWidth/2 - e.pageX));
			// the invisible space
			// so, the distance from an edge vs the unscene pixels that direction should weight the
			var left = ((e.pageX - enterPos)*1.5 + $wrapper.scrollLeft() - $wrapper.offset().left);// * (ulWidth-divWidth) / divWidth;
			enterPos = e.pageX;
			$wrapper.scrollLeft(left);
		});
	}

	/* * * * *  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
	* * * *		( Cookies! )
	* * *
	* *
	 */

	function setCookie(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		} else
			var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	}

	function getCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ')
				c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0)
				return c.substring(nameEQ.length,c.length);
		}
		return null;
	}

	function deleteCookie(name) {
		setCookie(name,"",-1);
	}

	/* * * * *  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
	* * * *		( Conditional Adjustments and Transitions )
	* * *
	* *
	 */

	function resize() {
		speed = 'fast';
		// Conditional handling of screen size.
		if ($(window).width() < 1300 && screenWidth == sSize.normal) {
			// Make narrow
			$(".resizable").addClass("screen_narrow_width");
			$(".resizable_animate").addClass("screen_narrow_width", speed);
			screenWidth = sSize.small;
			
			//$("#logo_scissor_girl").fadeIn('fast');
		} else if ($(window).width() >= 1300 && screenWidth == sSize.small) {
			$(".resizable_animate.screen_narrow_width").removeClass("screen_narrow_width", speed);
			$(".screen_narrow_width").removeClass("screen_narrow_width");
			screenWidth = sSize.normal;
			
			//$("#logo_scissor_girl").fadeOut('fast');
		}
		if ($(window).height() < 885 && screenHeight == sSize.normal) {
			$(".resizable").addClass("screen_short_height");
			$(".resizable_animate").addClass("screen_short_height", speed);
			screenHeight = sSize.small;
		} else if ($(window).height() >= 885 && screenHeight == sSize.small) {
			$(".resizable_animate.screen_short_height").removeClass("screen_short_height", speed);
			$(".screen_short_height").removeClass("screen_short_height");
			screenHeight = sSize.normal;
		}
	}

	function isTerribleBrowser() {
		if ($.browser.msie && $.browser.version.substr(0,1)<7)
			return true;

		var validBrowsers = ['Apple', 'Firefox', 'Chrome', 'MSIE'];
		for (var i=0;i<validBrowsers.length;i++) {
			if (navigator.userAgent.indexOf(validBrowsers[i]) != -1) {
				return false;
			}
		}
		return true;
	}

	function loadNewPage(url) {
		//$("#center_column").append($("<div />").addClass().load(url))
		$("#main_box_container").fadeOut('fast', function() {
			$("#main_box_container").load(url, function () {
				$("#main_box_container").fadeIn('fast');
			});
		}
		);
	}
	
});
