// JavaScript Document

$(document).ready(function () 
{

	$('#buttons li a').each(function(i, n)
	{
		$(this).attr({href: '#selectedTab' + i})
		
		
		$(this).click(function()
		{
			newDisplayFunction(i);
		})
				   
									
	})
	
	var myFile = document.location.toString();
	if (myFile.match('#selectedTab')) 
	{
		// the URL contains an anchor
		// click the navigation item corresponding to the anchor
		var myAnchor = parseFloat(myFile.split('#selectedTab')[1]);
		if(myAnchor > 0 && myAnchor < 5)
		{
			newDisplayFunction(myAnchor)
		}
		
		//$('ol#nav li a[href="' + myAnchor + '"]').parent().click();
	}
	else {
		// click the first navigation item
	}
});

function newDisplayFunction(tabNumber){
	
	var i = 0;
	while(i<classNames.length)
	{
		$('div#main_container').removeClass(classNames[i]);
		i++;
	}
	
	$('div#main_container').addClass(classNames[tabNumber]);
	
	var ajaxUrl = textFiles[tabNumber];
		 $.ajax({
		 
   			type: "GET",
   			url: ajaxUrl,
   			//data: "name=John&location=Boston",
   				success: function(msg){
   				 	$('#textContent div:first').hide();
	  				$('#textContent div:first').html(msg);
					$('#textContent div:first').fadeIn("slow");
					
					if($('#textContentdiv:first').height() >= $('#page-wrap').height())
					{
						$('#page-wrap').css('height', $('#textContentdiv:first').height())
					}
					else{$('#page-wrap').css('height', 'auto')}

				   }
 		});
		 
	lastTabClicked = tabNumber;
	
	
}

var textFiles = new Array( "images/ajaxscripts/homepage/welcome.html", "images/ajaxscripts/homepage/localsourcing.html", "images/ajaxscripts/homepage/fairtrade.html", "images/ajaxscripts/homepage/community.html","images/ajaxscripts/homepage/environment.html");


var classNames = new Array("aboutUs", "localSourcing", "fairTrade", "community", "environment");
lastTabClicked = 1;


var theInt = null;
		var $crosslink, $navthumb;
		var curclicked = 0;
		
		theInterval = function(cur){
			clearInterval(theInt);
			
			if( typeof cur != 'undefined' )
				curclicked = cur;
			
			$crosslink.removeClass("active-thumb");
			$navthumb.eq(curclicked).parent().addClass("active-thumb");
				$(".stripNav ul li a").eq(curclicked).trigger('click');
			
			theInt = setInterval(function(){
				$crosslink.removeClass("active-thumb");
				$navthumb.eq(curclicked).parent().addClass("active-thumb");
				$(".stripNav ul li a").eq(curclicked).trigger('click');
				curclicked++;
				if( 5 == curclicked )
					curclicked = 0;
				
			}, 6000);
		};
		
		$(function(){
			
			$("#main-photo-slider").codaSlider();
			
			$navthumb = $(".nav-thumb");
			$crosslink = $(".cross-link");
			
			$navthumb
			.click(function() {
				var $this = $(this);
				theInterval($this.parent().attr('href').slice(1) - 1);
				return false;
			});
			
			theInterval();
		});
	var currentSelected = 0;
		function showText(i){
			var j = 1
			if (currentSelected != i)
			{
				while(j <= 3){
					$("#shownews"+j).slideUp("medium"); 	j++;
					}
				$("#shownews"+i).slideDown("medium");
				currentSelected = i;
			}
		}
		
		
