var animationSpeed = 'normal';
var pageManager;
var preloadDone = false;
var currentQuote = 1;
var canMoveQuote = true;
var currentPageIndex;

var pages = [
  new ryybo.code.Page('home', {
    title: 'home',
    onPageUnload: onPageUnload,
    onPageLoad: onPageLoad,
    image: 'images/photo1.jpg',
	quote: 1,
	analyticsPage: 'gm/home'
  }),
  new ryybo.code.Page('gene', {
    title: 'gene',
    onPageUnload: onPageUnload,
    onPageLoad: onPageLoad,
    image: 'images/photo2.jpg',
	quote: 7,
	analyticsPage: 'gm/gene'
  }),
  new ryybo.code.Page('neighborhoods', {
    title: 'neighborhoods',
    onPageUnload: onPageUnload,
    onPageLoad: onPageLoad,
    image: 'images/photo3.jpg',
	quote: 12,
	analyticsPage: 'gm/neighborhoods'
  }),
  new ryybo.code.Page('customers', {
    title: 'customers',
    onPageUnload: onPageUnload,
    onPageLoad: onPageLoad,
    image: 'images/photo4.jpg',
	quote: 17,
	analyticsPage: 'gm/customers'
  }),
  new ryybo.code.Page('stress', {
    title: 'stress',
    onPageUnload: onPageUnload,
    onPageLoad: onPageLoad,
    image: 'images/photo5.jpg',
	quote: 23,
	analyticsPage: 'gm/stress'
  }),
  new ryybo.code.Page('service', {
    title: 'service',
    onPageUnload: onPageUnload,
    onPageLoad: onPageLoad,
    image: 'images/photo6.jpg',
	quote: 28,
	analyticsPage: 'gm/service'
  }),
  new ryybo.code.Page('contact', {
    title: 'contact',
    onPageUnload: onPageUnload,
    onPageLoad: onPageLoad,
    image: 'images/photo7.jpg',
	quote: 34,
	analyticsPage: 'gm/contact'
  })
];


function onPageUnload(page, callback) {

  callback();
};

function emailLinkClicked() {
	pageTracker._trackPageview("Email_to_Friend_From:"+pageManager.getPageNameIndex(currentPageIndex)); //GA email to friend tracking
}

function moveRight() {
    /* move quote right */
	var endCase = false;
	if (canMoveQuote == true) {
	canMoveQuote = false
	var nextQuote = currentQuote + 1;
	if (currentQuote == 39) {
		endCase = true;
	}
	pageTracker._trackPageview("quote_Change:"+currentQuote+">R>"+nextQuote); //GA quote tracking
	
    $('#quote'+nextQuote).removeClass('hiddenQuote').addClass('shownQuote');
	$('#quote'+nextQuote).css('margin-top', ((210 - $('#quote'+nextQuote).height()) / 2));
	$('#quote'+currentQuote).css('margin-top', ((210 - $('#quote'+currentQuote).height()) / 2));
      $('#quote'+nextQuote).css('left', 40);
	  $('#quote'+currentQuote).css('left', 0);
    $('#quote'+nextQuote).animate({left: -200}, {duration: 1000, easing: 'easeOutBack'});
    $('#quote'+currentQuote).animate({left: -240}, {duration: 1000, easing: 'easeOutBack', complete: function() {
    $('#quote'+currentQuote).removeClass('shownQuote').addClass('hiddenQuote');
      $('#quote'+nextQuote).css('left', 0);
	  $('#quote'+currentQuote).css('left', 0);
	  currentQuote = nextQuote;
	  canMoveQuote = true;
	  
	  if (endCase) {
		  $('#quote40').removeClass('shownQuote').addClass('hiddenQuote');
		  $('#quote1').removeClass('hiddenQuote').addClass('shownQuote');
		  $('#quote1').css('margin-top', ((210 - $('#quote1').height()) / 2));
		  currentQuote = 1;
	  }
	  }
	  }
	  );
	}
}

function moveLeft() {
    /* move quote left */
	var endCase = false;
	if (canMoveQuote == true) {
		canMoveQuote = false;
	var nextQuote = currentQuote - 1;
	if (currentQuote == 1) {
		endCase = true;
	}
	pageTracker._trackPageview("quote_Change:"+currentQuote+">L>"+nextQuote); //GA quote tracking
	
    $('#quote'+nextQuote).removeClass('hiddenQuote').addClass('shownQuote');
	$('#quote'+nextQuote).css('margin-top', ((210 - $('#quote'+nextQuote).height()) / 2));
	$('#quote'+currentQuote).css('margin-top', ((210 - $('#quote'+currentQuote).height()) / 2));
      $('#quote'+currentQuote).css('left', -200);
	  $('#quote'+nextQuote).css('left', -240);
    $('#quote'+currentQuote).animate({left: 40}, {duration: 1000, easing: 'easeOutBack'});
    $('#quote'+nextQuote).animate({left: 0}, {duration: 1000, easing: 'easeOutBack', complete: function() {
    $('#quote'+currentQuote).removeClass('shownQuote').addClass('hiddenQuote');
      $('#quote'+currentQuote).css('left', 0);
	  $('#quote'+nextQuote).css('left', 0);
	  currentQuote = nextQuote;
	  canMoveQuote = true;
	  
	  if (endCase) {
		  $('#quote0').removeClass('shownQuote').addClass('hiddenQuote');
		  $('#quote39').removeClass('hiddenQuote').addClass('shownQuote');
		  $('#quote39').css('margin-top', ((210 - $('#quote39').height()) / 2));
		  currentQuote = 39;
	  }
	  }});
	}
}

function onPageLoad(page, callback) {
	currentPageIndex = pageManager.getPageIndex(page);
  /* unselect all the tabs */
  $('#navContainer > ul > li.selected').removeClass('selected');
  /* select the tab for the loaded page */
  $('#navContainer > ul > li:eq(' + pageManager.getPageIndex(page) + ')').addClass('selected');
  
  $('#quote'+currentQuote).removeClass('shownQuote').addClass('hiddenQuote');
  currentQuote = page.quote;
  if (page.quote == null) {
	  currentQuote = 0;
  }
  $('#quote'+currentQuote).removeClass('hiddenQuote').addClass('shownQuote');
  $('#quote'+currentQuote).css('margin-top', ((210 - $('#quote'+currentQuote).height()) / 2));

  if (page.name == 'contact') {
    $('#circle').css('opacity', 0);
    $('#circle').removeClass('invisibleIMG').addClass('visibleIMG');
    $('#circle').animate({opacity: 1}, animationSpeed);
  } else {
    // if the circle was previously visible, fade it out, and hide it
	$('#circle').removeClass('visibleIMG').addClass('invisibleIMG');
  }

  /* load the text */
  $('.visibleText').removeClass('visibleText').addClass('invisibleText');
  $('#text' + capitalize(page.name)).removeClass('invisibleText').addClass('visibleText');
  if (preloadDone) {
      $('#photoContainerImage').attr('src', page.image);
  }

  pageTracker._trackPageview(page.analyticsPage); //tell GA that page is loaded
  callback();
};

function capitalize(str) {
  return str.substring(0, 1).toUpperCase() + str.substring(1);
};

function nextPage() {
  var page = pageManager.getNextPage();
  gotoPage(page.name);
};

function previousPage() {
    var page = pageManager.getPreviousPage();
    gotoPage(page.name);
};

function gotoPage(page) {
  pageManager.gotoPage(page);
};

/*
 * This method is called when the DOM is ready... (like window.onload)
 */
$(document).ready(function() { //calls function of page is ready for document
  // set up the navigation tabs
  $('#navContainer > ul > li').each(function(index) {
    
    $(this).click(function() {
      gotoPage(index);
    });
  });
  
  pageManager = new ryybo.code.PageManager(pages, {});
  $('.hiddenBody').removeClass('hiddenBody').addClass('visibleBody');

    
	//add IMAGE PRE LOAD
	if (document.images) { //if has DOM object images then preload all images
		var pic1 = new Image(599,225);
		pic1.src = "images/photo1.jpg";
		var pic2 = new Image(599,225);
		pic2.src = "images/photo2.jpg";
		var pic3 = new Image(599,225);
		pic3.src = "images/photo3.jpg";
		var pic4 = new Image(599,225);
		pic4.src = "images/photo4.jpg";
		var pic5 = new Image(599,225);
		pic5.src = "images/photo5.jpg";
		var pic6 = new Image(599,225);
		pic6.src = "images/photo6.jpg";
		var pic7 = new Image(599,225);
		pic7.src = "images/photo7.jpg";
		var hover1 = new Image(23,23);
		hover1.src = "images/button_right_hover.gif";
		var hover2 = new Image(23,23);
		hover2.src = "images/button_left_hover.gif";
	}

  // wire the logo to home page
  $('#logo, #logoCont').click(function() {
    gotoPage(0);
  });
});
