jQuery(document).ready(function() {
		
		$("#portfolio-carousel").removeClass("nonjs");
		
		var startValue = 0;
		
		var theuserAgent = "";
		
		// check if URL contains paaremters to load a potfoilio 
		deepLink = $.address.path();
		if (deepLink.match("/portfolio/"))	{
			// load the approprate portfolio content
			loadMainContent($("#portfolio-carousel a[href='"+deepLink+"']"));
			// set carousel start position
			startValue = $("#portfolio-carousel a[href='"+deepLink+"']").parent("li").attr("id");
			$("#portfolio-carousel a[href='"+deepLink+"']").addClass("pactive");
			startValue = startValue.replace("thumb-","");
		}
		
		// need to set to the nearest block rather than individual
		if (startValue < 6)	{
			startValue = 0;
		} else if (startValue < 12)	{
			startValue = 7;
		} else if (startValue < 17)	{
			startValue = 14;
		}
		
		jQuery('#portfolio-carousel').jcarousel({
			start: startValue,
			scroll: 6,
			easing: "swing",
			itemFirstInCallback: {
			  visible: 18,
			  onBeforeAnimation:  mycarousel_itemFirstInCallback,
			  itemLoadCallback: mycarousel_itemFirstInCallback
			}
		});
		
		// portfolio thumnbail click event
		$("ul#portfolio-carousel a").click(function(event){ 
			event.preventDefault();

			$("ul#portfolio-carousel a.pactive").removeClass("pactive");
			$(this).addClass("pactive");
			
			// check if its the home page if so rediredt to the portfolio page			
			if(location.pathname == "/" || "/index.php")	{
				location.href = "/portfolio.php#"+$(this).attr('href');
			}
			
			// add tracking click to google
			pageTracker._trackEvent('PortfolioThumbClick', $(this).attr('href'));

			loadMainContent($(this));
			return false;
		});	
		
		$("ul.portfolio-image-nav:visible li a").click(function(event){ 
			event.preventDefault();
			portfolioContentNavigation($(this));
			return false;
		});
		
		$('ul#portfolio-carousel a').click(function(event) {
            $.address.title($.address.title().split(' | ')[0] + ' | ' + $(this).attr("rel").replace("address:/",""));
			$.address.value($(this).attr('href'));
		});
		
		function loadMainContent(obj)	{
			var containerClass = $("div#content").attr("class");
			var theContent = $(obj).attr("class");
			
			//$("div.portfolio-content:visible").fadeOut("fast");
			$("div.portfolio-content").replaceWith("<div id='content-loading'></div>");
			
			if ($("#IE6").length > 0)	{
				theuserAgent = "IE6";
			}
			
			$.get("/inc/ajax-portfolio-content.php",{
				   userAgent: theuserAgent,
				   shortName: $(obj).attr("href").replace("/portfolio/","")
				 }, function(responseHTML) {
					$("div#content").replaceWith(responseHTML);

					$("ul.portfolio-image-nav").fadeIn();
					jQuery("ul.portfolio-image-nav li a").bind("click", function() {
						portfolioContentNavigation($(this));
					});
					
			 }); 
	}
	
	function portfolioContentNavigation(obj)	{
			
			if ($(obj).hasClass("active"))	{
				return true;
			}
			else	{
				var showThis = $(obj).parent().attr("class");
				showThis = showThis.replace("inav-","pimg-");
				var containerClass = $("div#content").attr("class");
								
				$("div.portfolio-content:visible ul.portfolio-image-nav li.active, div.portfolio-content:visible ul.portfolio-image-nav a.active").removeClass("active");

				if ($("div.portfolio-content:visible .portfolio-main-images li#"+showThis).hasClass("print")) {
					if (containerClass != "print")	{
						$("div#content").attr("class","print");
					}
				}
				else if ($("div.portfolio-content:visible .portfolio-main-images li#"+showThis).hasClass("web")) {
					if (containerClass != "web")	{
						$("div#content").attr("class","web");
					}
				}
				
				$(obj).addClass("active");
				$(obj).parent().addClass("active");
				$("div.portfolio-content:visible ul.portfolio-main-images li.active").removeClass("active");
				//$("div.portfolio-content:visible ul.portfolio-main-images li:visible").fadeOut("fast");
				FadeOrNoFade($("div.portfolio-content:visible ul.portfolio-main-images li:visible"),"out");
				FadeOrNoFade($("div.portfolio-content:visible .portfolio-main-images li#"+showThis),"in");
				//$("div.portfolio-content:visible .portfolio-main-images li#"+showThis).fadeIn("fast");
				
			}
			return false;
	}
	
	function FadeOrNoFade(jQueryObj,action)	{
			
		if (action === "out")	{
			if (jQuery.support.opacity)	{
				$(jQueryObj).fadeOut("fast");
			} else	{
				$(jQueryObj).hide();
			}
		}
		else	{
			if (jQuery.support.opacity)	{
				$(jQueryObj).fadeIn("fast");
			} else	{
				$(jQueryObj).show();
			}
		}
			
	}
	
		
		
});
	
	function mycarousel_itemFirstInCallback(carousel, item, idx, state) {
		//alert('Item #' + idx + ' is now the first item');
		var leftPos;
		switch(idx)	{
			case 1:
			  leftPos = "0px";
			  break;
			case 6:
			  leftPos = "-270px";
			  break;  
			case 7:
			  leftPos = "-270px";
			  break;
			case 13:
			  leftPos = "-545px";
			  break;
			case 18:
			  leftPos = "-700px";
			  break;
			default:
			 leftPos = "0px";
		}
		$("ul#portfolio-text").animate({ left: leftPos}, 400, "swing" );
	};

	
			
	
	