var homeBgs = Array(
	{pic:'images/work/vsac1.jpg', cap:"<span>Resource Center, VSAC</span>"},
	{pic:'images/large/residential.jpg', cap:'<span>Spiral Staircase, Colchester Residence</span>'},
	{pic:'images/work/stowe2.jpg', cap:'<span>Window Detail, Stowe Residence</span>'},
	{pic:'images/work/big/YA9V9635.jpg', cap:'<span>Tram Haus Lodge, Jay Peak</span>'},
	{pic:'images/work/whiteface1.jpg', cap:'<span>Whiteface Lodge, Lake Placid</span>'}
);
	
//	For the whole site

	//from the dev site

	//set initial include

	var thisTarget = "includes/index.php";

	//set initial marker value

	var big = false;
	var bgInterval = setInterval ("changeHomeBG()", 6000 );
	

	//subtitute for replace

var currentImg = 0;
var barOpen = false;		

function closeBar(){

	if (barOpen){

		$("#bar-content").animate({

			width: '1px'

		}, 1000, "easeOutExpo", function(){

				$('#bar-close').toggleClass("open");

			} );

			$("#ajax-content").fadeOut();

			//$("#sub-container").fadeTo(750, .01);



		barOpen = false;
		return true;

	}else{
		return false;
	}

}

function openBar(){
	if (!barOpen){

		$("#bar-content").animate({

			width: '650px'

		}, 1000, "easeOutExpo", function(){

				$('#bar-close').toggleClass("open");

		} );

		$("#ajax-content").fadeIn();

		//$("#sub-container").fadeTo(750, 1);

		barOpen = true;
		return true;

	}else{
		return false;
	}

}

function toggleBar(){
	if (barOpen) return closeBar();

	else return openBar();

}

function changeHomeBG(){
	var img = homeBgs[(currentImg % homeBgs.length)];
	
	$('#bottom-caption').fadeTo(1000,0.01, function(){
		document.getElementById('bottom-caption').innerHTML = img.cap;
		$('#bottom-caption').fadeTo(1000,1);
	});
	currentImg++;

	$('#content').fadeTo(1500, 0.01, function(){
		$("#content>img").attr({"src": img.pic}).load(function(){
			$('#content').fadeTo(100, 1,function(){
				//	Preload the next one
				$("#background-preload>img").attr({"src": homeBgs[((currentImg) % homeBgs.length)].pic});
			});
		});
	

	});

	//	Fading content out shows background on background preload
	
	
	
	
}
function chooseNextBackground(){
	//currentImg++;
	return homeBgs[(currentImg % homeBgs.length)];
}
function chooseRandomBackground(){
	var current = $('#content img').attr('src');
	var rand = Math.round(Math.random() * homeBgs.length);
	if (current.indexOf(homeBgs[rand].pic) == -1){
		return homeBgs[rand];
	}else{
		return chooseRandomBackground();
	}
}



var currentPage = '';

function loadNav(page){

	if (page != currentPage){
		clearInterval(bgInterval);

		
		
		if (page != 'index.php'){
			$('#background-preload').fadeTo(10,0.01);
		}
		$('#content').fadeTo(1000,0.01);

		
		if (currentPage == 'news.php'){
			// if the previous page was news, we need to remove the content-container div
			// the blog sits in this element which no other pages use.
			// it only exists on the news.php page.
			$('#bar-container').fadeTo(750,1);
			$('#bar-content-news').fadeTo(750,0.01,function(){
				$('#bar-content-news').remove();
			});
			$('#logo').fadeTo(750 , 0.01, function(){
				$('#logo>a>img').attr({'src': 'images/GKALogo.png'});
				$('#logo').fadeTo(750 , 1);
			});
			
			
		}
		if (currentPage == 'clients.php'){
			document.getElementById('sub-container').innerHTML = '';
		}
		
		currentPage = page;
		
		//	Clear Bar Content
		$("#ajax-content").fadeOut(200, function(){
			document.getElementById('ajax-content').innerHTML = '';
		});

		//	Close the Bar
		closeBar();
		
		$("#sub-container").fadeTo(750, .01);

		//	Change BG-Image
		if (page == 'index.php') {
			setBackground(	{pic:'images/work/whiteface1.jpg', cap:'<span>Lodge Exterior, Whiteface Lodge</span>'});
			currentImg = 0;
			$('#background-preload').fadeTo(3000,0.02,function(){
				$("#background-preload>img").attr({"src": homeBgs[0].pic});
				$('#background-preload').fadeTo(1000,1);
			});
			$('#bottom-caption').fadeTo(1000,1);
			bgInterval = setInterval ("changeHomeBG()", 6000 );

		} else if (page == 'about.php') {
			setBackground({pic:"images/large/about.jpg", cap: '' });
		} else if (page =='news.php'){
			setBackground({pic:"images/large/e8.jpg", cap: '' });
		} else if (page == 'clients.php') {		
			setBackground({pic:"images/large/clients.jpg", cap: '' });
		} else if (page == 'work.php') {
			setBackground({pic:"images/large/work.jpg", cap: '' });	
		} else if (page == 'contact.php') {
			setBackground({pic:"images/large/contact.jpg", cap: '' });
		};

		//	Load new stuff into panels
		if (page == "clients.php") {		

			$("#sub-container").load("includes/clients.php", function(){

				$("#sub-container").fadeTo(1000, 1);

			});

		};
		
		if (page == "index.php") {
			$("#ajax-content").load("includes/index.php");	
		}
		
		//contact
		if (page == "contact.php") {		

			$("#ajax-content").load("includes/contact.php");

		};
		
		// if the target is news.php, fade out the sub container
		// pull in the news.php file and load it into the content div
		if (page == "news.php") {	
			$('#logo').fadeTo(750 , 0.01, function(){
				$('#logo>a>img').attr({'src': 'images/GKALogo-dark.png'});
				$('#logo').fadeTo(750 , 1);
			});	
			$("#sub-container").fadeTo(750, 0.01);
			$('#bar-container').fadeTo(750,0.01);
			$("#content-container").fadeTo(750,0.01,function(){
				$('#content').append("<div id='news-section'></div>");
				$("#news-section").load("includes/news.php", function(){
					$("#content-container").fadeTo(750,1);
				});
			});	
		};

		
		//	Load the nav
		$("#bar-nav-inner").fadeTo(100, .01, function() {		

				$("#bar-nav-inner").load("includes/"+page.replace(/.php/, '')+"-nav.php");

		}).fadeTo(1000, 1);
		


	}

}

function setBackground(img){
	//	Fixed by Ferrante on 10/26/10 - should be working now but maybe not on IE
	/*
	var bgImage = new Image();
	bgImage.src = img.pic;
	bgImage.onload = function(){
		$('#bottom-caption').fadeTo(500,0.01, function(){
			document.getElementById('bottom-caption').innerHTML = img.cap;
		});
		$('#content').fadeTo(1000, 0.01, function(){
			$("#content>img").attr({"src": bgImage.src});
			$('#content').fadeTo(1000, 1);
			$('#bottom-caption').fadeTo(500,1);
			
		});
	}
	*/
	// Changed to below on 12/7/10 for IE -- too slow, doesn't work right
		// Preload the image
		/*
	$("#background-preload>img").attr({"src": img.pic});
	
	$('#bottom-caption').fadeTo(1000,0.01, function(){
		document.getElementById('bottom-caption').innerHTML = img.cap;
	});

	$('#content').fadeTo(1000, 0.01, function(){
		$("#content>img").attr({"src": img.pic});
			$('#content').fadeTo(1000, 1);
			$('#bottom-caption').fadeTo(1000,1);
		
	});
	*/
	//	One more time
	
	var pic=new Image();
	pic.src= img.pic;
	
	$('#bottom-caption').fadeTo(1000,0.01);
	$('#content').fadeTo(1000, 0.01, function(){
		document.getElementById('bottom-caption').innerHTML = img.cap;
		if(pic.complete){
				$("#content>img").attr({"src": pic.src});
				$('#content').fadeTo(1000, 1);
				$('#bottom-caption').fadeTo(1000,1);
			
		}
		else pic.onload= function(){
				$("#content>img").attr({"src": pic.src});
				$('#content').fadeTo(1000, 1);
				$('#bottom-caption').fadeTo(1000,1);
			
		};
	});
	
	

	
	
}

function setBar(target){
	$("#ajax-content").fadeTo(750, 0.01, function() {
		//	Load HTML from the target			
		$(this).load(target, function(){
			//	Vertically Center Description Div
			var paddingNeeded = ((102 - $('#description').height()) / 2);
			if (paddingNeeded > 0){
				$("#description").css('padding',paddingNeeded + "px 0 0 10px");	
			}else{
				$("#description").css('padding',"0 0 0 10px");
			}
			
			//	If there is a carousel to be loaded, load it
			startThumbsCarousel();
			$("#ajax-content").fadeTo(750, 1);
		});
		
	});
}

//	Carousel Functions
function startWorkCarousel(){

}
function startThumbsCarousel(){
	if (($('#thumbsCarousel li').length -2) < 3 ){
		jQuery('#thumbsCarousel').jcarousel({
			initCallback : workCarouselCallback,
			visible: 3,
			//auto: 5,
			scroll: 1,
			start: 1,
			wrap: 'both',
			animation: 'slow',
			buttonPrevHTML: null,
			buttonNextHTML: null,
			itemFirstInCallback:  lightController
		});
	}else{
		jQuery('#thumbsCarousel').jcarousel({
			initCallback : workCarouselCallback,
			visible: 3,
			//auto: 5,
			scroll: 1,
			start: 2,
			wrap: 'both',
			animation: 'slow',
			buttonPrevHTML: null,
			buttonNextHTML: null,
			/*
			itemFirstInCallback: {
				onAfterAnimation: lightController
			}	
			*/
			itemFirstInCallback: lightController
		});
	}

}

function workCarouselCallback(carousel){
	$('.carouselController .controlButton').livequery('click',function(event){
		var t = this.className.split(' ');
		
		var s = t[1];
		carousel.unlock();
		carousel.scroll(jQuery.jcarousel.intval(s));
		carousel.lock();
	});
	$('.pictureControlButton').livequery('click',function(event){
		var t = this.className.split(' ');
		
		var s = t[2];
		carousel.unlock();
		carousel.scroll(jQuery.jcarousel.intval(s));
		carousel.lock();
	});
	
	return false;
	
}

function lightController(carousel,item, index, state){
	//alert(index);
	//alert($('.controlButton.current').attr('class'));
	/*
	var currentClass = $('.controlButton.current').attr('class');
	currentClass = currentClass.split(' ');
	var oldIndex = currentClass[1];
	
	if (oldIndex != index){
		$('.controlButton').removeClass('current');
		$('.controlButton.'+(index)).addClass('current');
	}
	*/
	$('.controlButton').removeClass('current');
	$('.controlButton.'+(index)).addClass('current');
	/*
	for (var i = index; i < index + 3; i++){
		$('.controlButton.'+i).addClass('current');
	}
	*/
	//alert(index);
}





$(document).ready(function() {	

	//set initial bar status

//	$.historyInit(pageload);
$('#bar-close').fadeTo(10,.85);
	$("#content>img").load(function () {

		$(this).fadeIn("slow");
		

		//$("#content>img:hidden").fadeIn(1000);

	});

	//hide elements on page load - for home

	$("#bar-nav").css("width", "0px");

	$("#bar-content").css("width", "0px");

	//$("#bar-content").hide();

	$("#bar-nav-inner").hide();

	$("#ajax-content").hide();

	$('#bar-close').addClass('open');

	

	//initial animate open

	$("#bar-nav").animate({

			width: '140px'

		}, 2000, "easeOutExpo" );	

	$("#bar-nav-inner").fadeTo(1000, 1).fadeIn();	

	//ajax setup	

	$.ajaxSetup({

	  url: "/xmlhttp/",

	  global: false,

	  type: "POST"

	});	



	// ajax link load and animation

	 $('.ajax').livequery('click', function(event) {

	 	//	Change Class for styling navigation

		thisTarget = $(this).attr('href');

		$('#top-nav>ul>li>a').removeClass('current');

		$(this).addClass('current');

		loadNav(thisTarget);	

		return false;

	});	
	//end ajax link load and animation

	 

	//load client sub content

	$('.ajax-client').livequery('click', function(event) { 	

		

		thisTarget = $(this).attr('href');
		$('#client-list>li>a').removeClass('current');
		$(this).addClass('current');
		
		
		

		$("#ajax-content").fadeTo(500, .01, function() {									

			$(this).load(thisTarget);
			openBar();		

		}).fadeTo(750, 1);

		return false;

	});
	
	//end client stuff



	// close link
	$("#bar-close:not(.open)").livequery('click', function(event) {
		//alert('closing');
		closeBar();

	});
	$("#bar-close.open").livequery('click', function(event) {
		//alert('opening');
		openBar();

	});

	// end close link

	

	//leftnav tab clicked
	//Load background image too

	 $('.leftnav, .back').livequery('click', function(event) {

		thisTarget = $(this).attr('href');
		
		//only load the image if the current section is the work section
		var section = $(this).attr('class');
		var classes = section.split(' ');
		
		var capt = '';
		switch (classes[1]){
			case('commercial'):
				capt = "<span class='dark'>Entry Gate, Champlain Valley Expo</span>";
				break;
			case('educational'):
				capt = "<span>Emergent Media Center, Champlain College</span>";
				break;
			case('resort'):
				capt = '<span>Lodge Interior, Jay Peak</span>';
				break;
			case('residential'):
				capt = '<span>Spiral Staircase, Colchester Residence</span>';
				break;
			default:
		}
		
		if (classes.length > 1){
			setBackground({pic:"images/large/"+classes[1]+".jpg", cap:capt});
			//$("#content>img").attr({"src": "images/large/"+classes[1]+".jpg"});
		}
		/*
		$('#bottom-caption').fadeTo(100, 0.02, function(){
			document.getElementById('bottom-caption').innerHTML = caption;
			$('#bottom-caption').fadeTo(750, 1);
		});
		*/		
		
		//set a short timeout to prevent pages being loaded prematurely
		//setTimeout(" ",500);
		
		if (barOpen){
			$("#ajax-content").fadeTo(250, .01, function() {
				$(this).load(thisTarget,function(){
					startWorkCarousel();
					$("#ajax-content").fadeTo(750, 1);
					
				});
			});
		}else{
			$("#ajax-content").fadeTo(10,0.01);
			openBar();
			$("#ajax-content").load(thisTarget,function(){
					startWorkCarousel();
					$("#ajax-content").fadeTo(750, 1);
				});

		}

		return false;		

	});	

	 

	//open slider, but do not load content

	 $('.slide-open').livequery('click', function(event) {
			openBar();
		return false;		

	});		 

	 

	 //----------------------------------------------------	 
	 //breadcrumb for left-navigation
	 // changes color of links
	 // also swaps out background image
	

	$('#left-nav>li>a').livequery('click', function(event){
		
		// grab the class name other than leftnav, and select the correct background image
		// to load based on that class

		$('#left-nav>li>a').css('color','#53738D');
		
		$(this).css('color','#000');


		return false;

	});

	//----------------------------------------------------	 

	 

	 //greybox

	$("a.greybox").livequery('click', function(event) {

	  var t = "Bll Gardner";

	  //var t = this.title || this.innerHTML || this.href;

	  var target = "includes/about/bill-bio.php";

	  GB_show(t,target,480,520);

	  return false;

	});	

	

	$("a.greybox2").livequery('click', function(event) {

	  var t = "Liza Kilcoyne";

	  //var t = this.title || this.innerHTML || this.href;

	  var target = "includes/about/liza-bio.php";

	  GB_show(t,target,480,520);

	  return false;

	});

	

	$("a.greyboxvideo").livequery('click', function(event) {

	  var t = "RTA";

	  //var t = this.title || this.innerHTML || this.href;

	  var target = "rta-video.html";

	  GB_show(t,target,480,660);

	  return false;

	});	

	

	$("a.greyboxphoto").livequery('click', function(event) {

	  var t = "PhotoCredits";

	  //var t = this.title || this.innerHTML || this.href;

	  var target = "photo-credits.php";

	  GB_show(t,target,480,660);

	  return false;

	});	
	
	
	//	For each client, load their work on work page
	$('#ajax-content a.work').livequery('click', function(event){
		//alert('clicked');
		thisTarget = $(this).attr('href');
		setBackground({pic:$('img',this).attr('alt'), cap:''});
		setBar(thisTarget);
		return false;	
	});

	//	Carousel Image Clicked
	$("a.full-photo:not(.greyboxvideo)").livequery('click', function(event) {
		//alert('hit');
		setBackground({pic: $(this).attr('href'), cap: ''});
		altText = $('img', this).attr('alt');
		$("#alt-text").fadeTo(750, .1, function() {

				$(this).html(altText);
				//	Vertically Center Description Div
				var paddingNeeded = ((140 - $('#description').height()) / 2) - 20;
				$("#description").css('padding',paddingNeeded + "px 0 0 10px");

		}).fadeTo(750, 1);			
			

		return false;

	});
	$("a.full-photo.greyboxvideo").livequery('click', function(event) {
		//alert('hit');
		//setBackground({pic: $(this).attr('href'), cap: ''});
		altText = $('img', this).attr('alt');
		$("#alt-text").fadeTo(750, .1, function() {

				$(this).html(altText);
				//	Vertically Center Description Div
				var paddingNeeded = ((140 - $('#description').height()) / 2) - 20;
				$("#description").css('padding',paddingNeeded + "px 0 0 10px");

		}).fadeTo(750, 1);			
			

		return false;

	});
	
	//	Carousel Controller Clicked
	$('.carouselController .controlButton').livequery('click',function(event){
		var t = this.className.split(' ');
		var s = t[1];
		var elem = $('a.full-photo.'+s+':not(.greyboxvideo)');
		if ($(elem).attr('href')){
			setBackground({pic: $(elem).attr('href'), cap: ''});
		}else{
			elem = $('a.full-photo.'+s+'.greyboxvideo');
			
			var t = "RTA";
	  		var target = "rta-video.html";
	  		GB_show(t,target,480,660);
		}
		
		altText = $('img', elem).attr('alt');
		$("#alt-text").fadeTo(750, .1, function() {

				$(this).html(altText);
				//	Vertically Center Description Div
				var paddingNeeded = ((140 - $('#description').height()) / 2) - 20;
				$("#description").css('padding',paddingNeeded + "px 0 0 10px");

		}).fadeTo(750, 1);	
		
		
	});
	
		

	$('a').click(function() {

           this.blur();

     });
});
