/**
 * Javascript Object
 *
 * @author 		Paul James Campbell
 * @url 		http://www.pauljamescampbell.co.uk/
 * @copyright 	Copyright (c) 2009 All rights reserved.
 */
var pjc = window.pjc = function( $ ) {
	
	// Constructor
	(function () {
		
		$( document ).ready(
			function () {
				decryptEmailAddresses();
				modForIEUsers();
			}
		);
		
		function modForIEUsers () {
			if( $.browser.msie && $.browser.version < 7 ) {
				pjc.displayError(
					"Hi. You're using IE 6 right? I've decided to veto full support for this browser. Please use another browser for the best experience!" 
				);
			}
			if( $.browser.msie ) {
				$(".disciplines li:not(:last-child) a, #services li:not(:last-child) a").append(" /");
			}
		}
		
		function decryptEmailAddresses() {
			$("a[href*='[at]']").each(
				function() {
					var el = $( this );
					el.attr("href",
						el.attr("href").replace("[at]", "@")
					);
					el.text(
						el.text().replace("[at]", "@")
					);
				}
			);
		}
		
	})();	
	
	return {
		
		addProjectRollover : function () {
			$("#projects .grid_4 img, #projects .grid_4 h3 a").hover(
				function() {
					$( this ).parents(".grid_4").addClass("active");
				},
				function () {
					$( this ).parents(".grid_4").removeClass("active");
				}
			);
		},
		
		displayError : function ( message ) {
			if( !$("#error_message").length ) {
				$( "body" ).append('<p id="error_message"></p>');
			}
			var el = $("#error_message");
			el.hide().html( 
				message
			).css(
				"left", $("#page").offset().left
			).clearQueue().fadeIn().delay(
				5000
			).fadeOut();
		},
	
		addProjectImageChanger : function () {
			
			/*
			ADD ROLLOVER STATE ( maybe with semi-transparent overlay? )
			*/
			
			var main = {
					div : $("#main_image"),
					img : $("#main_image > img"),
					span : $("#main_image > span")
				},
				thumbs 		= $(".sub_image"),
				suffixs = {
					large : "_large",
					small :  "_small"
				};
			
			function imageErrorCallback () {
				pjc.displayError(
					"Oops, something has gone a bit wrong on this project. Maybe try another project."
				);
			}
			
			thumbs.click(
				function() {
					
					var el = $( this ),
						small = {
							div : el,
							img : el.children("img"),
							span : el.children("span"),
							src : main.img.attr("src").replace( suffixs.large, suffixs.small ),
							text : main.span.text(),
							image : $( new Image() )
						}
						
					// Update main data object
					$.extend( main,
						{
							src : small.img.attr("src").replace( suffixs.small, suffixs.large ),
							text : el.text(),
							image : $( new Image() )
						}
					);
					
					// Load main image
					main.image.load( 
						function () {
							var img = $( this );
							img.hide();	
							main.div.removeClass("loading");
							main.img.replaceWith( this );
							main.span.text( main.text );
							$.extend( main, 
								{
									// Force a refresh of the main.img variable
									img : main.div.children("img")
								}
							);
							img.fadeIn("slow");
						}
					).error(
						imageErrorCallback
					).attr(
						"src", main.src
					);
					
					// Load thumb image
					small.image.load( 
						function () {
							var img = $( this );
							img.hide();	
							small.div.removeClass("loading");
							small.img.replaceWith( small.image );
							small.span.text( small.text );
							img.fadeIn();
						}
					).error(
						imageErrorCallback
					).attr(
						"src", small.src
					);
				}
			);
		}
	
	};

} (jQuery);