/*
 * Ian McNamara - The Creative Company
 * 2010.01.10
 *
 */
 
 
$('document').ready( 
	function() {

		// Initialize the jQuery.scrollTo / localScroll
		$('#page_content').localScroll();
		
		// Initialize shadowbox
		Shadowbox.init();

		// Stories
		// Switching substory behavior
		$("#stories .menu a").click( function(event) {
			// React based on class name.
			$('#stories .substory1').css("display","none");
			$('#stories .substory2').css("display","none");
			$('#stories .substory3').css("display","none");
			switch($(this).attr("class")) {
				case "s1": $('#stories .substory1').css("display","block"); break;
				case "s2": $('#stories .substory2').css("display","block"); break;
				case "s3": $('#stories .substory3').css("display","block"); break;
				default: break;
			}
		} );
		
		
		/*
		// Substory thumb behavior
		$('#stories .substory img.thumb').click( function(event) {
			// Swap out the image, getting the image name from the thumb name.
			var fullThumbName = $(this).attr("src");
			var pos = fullThumbName.lastIndexOf("/") + 1;
			var thumbName = fullThumbName.substr(pos);
			var imageName = thumbName.replace("thumb","image").replace(".gif",".jpg");
			var mainImage = $(this).parents('.substory').eq(0).find('.title img.image');
			$(mainImage).attr("src","img/" + imageName);
			// Remove any other .current class tags, set the current thumb to current.
			var subThumbs = $(this).parent().find('img.thumb');
			for (var i = 0; i < subThumbs.length; i++) {
				//$(subThumbs).eq(i).attr("class", $(subThumbs).eq(i).attr("class").replace("current","") );
				$(subThumbs).eq(i).attr("class","thumb");
			}
			$(this).attr("class","thumb current");
		} );
		*/
		
		
		// Substory thumb behavior
		$('#stories .substory img.thumb').click( function(event) {
			// Swap out the image, getting the image name from the thumb name.
			var fullThumbName = $(this).attr("src");
			var pos = fullThumbName.lastIndexOf("/") + 1;
			var thumbName = fullThumbName.substr(pos);
			var imageName = thumbName.replace("thumb","image").replace(".gif",".jpg");
			
			/*
			// Get the wrapper, build the new image block, plug it in.
			var wrapperDiv = $(this).parents('.substory').eq(0).find('.title .main_frame .wrapper');
			// <img class="image" src="img/stories_sub1_image_1.jpg" width="334" height="222" />
			var imageDiv = "<img class=\"image\" src=\"img/"+imageName+"\" width=\"334\" height=\"222\" />";
			$(wrapperDiv).html(imageDiv);
			*/
			
			// Get the wrapper
			var wrapperDiv = $(this).parents('.substory').eq(0).find('.title .main_frame .wrapper');
			// <img class="image" src="img/stories_sub1_image_1.jpg" width="334" height="222" />
			// Build the new image and get the previous image.
			var ni = "<img class=\"image\" src=\"img/"+imageName+"\" width=\"334\" height=\"222\" />";
			var pi = $(wrapperDiv).html();
			var pi = pi.substr(pi.lastIndexOf("<img"));
			// Plug it in.
			$(wrapperDiv).html(pi+" "+ni);

			// Remove any other .current class tags, set the current thumb to current.
			var subThumbs = $(this).parent().find('img.thumb');
			for (var i = 0; i < subThumbs.length; i++) {
				//$(subThumbs).eq(i).attr("class", $(subThumbs).eq(i).attr("class").replace("current","") );
				$(subThumbs).eq(i).attr("class","thumb");
			}
			$(this).attr("class","thumb current");
		} );
		
		
	} 
);
