// JavaScript Document
// Customer quotes
var quoteId = -1;
var quotesContentId = "customerQuotes";
var nextQuoteLinkId = "nextQuoteLink";
var previousQuoteLinkId = "previousQuoteLink";
var customerQuotes;

$(document).ready(function () {
    if (!customerQuotes) return;
    //if (customerQuotes.length == 0) return;
    
    $("#" + nextQuoteLinkId).click(nextQuote);
    $("#" + previousQuoteLinkId).click(previousQuote);
    nextQuote();
});

function nextQuote(){
  quoteId++;
  if (quoteId == customerQuotes.length) quoteId = 0;
  $("#" + quotesContentId).hide();
  $("#" + quotesContentId).html(customerQuotes[quoteId]);
  $("#" + quotesContentId).fadeIn(500);
}


function previousQuote(){
  quoteId--;
  if (quoteId < 0) quoteId = customerQuotes.length - 1;
  $("#" + quotesContentId).hide();
  $("#" + quotesContentId).html(customerQuotes[quoteId]);
  $("#" + quotesContentId).fadeIn(500);
}

// Featured projects

var featuredProjectId = -1;
var featuredProjectContentId = "featured_project_";
var nextFeaturedProjectLinkId = "nextFeaturedProjectLink";
var previousFeaturedProjectLinkId = "previousFeaturedProjectLink";
var featuredProjects;
var resourceURL;

$(document).ready(function () {
    if (!featuredProjects) return;
    //if (featuredProjects.length == 0) return;
    
    $("#" + nextFeaturedProjectLinkId).click(nextFeaturedProject);
    $("#" + previousFeaturedProjectLinkId).click(previousFeaturedProject);
    nextFeaturedProject();
    setInterval(nextFeaturedProject, 2000);
});

function nextFeaturedProject(){
  $("#" + featuredProjectContentId + featuredProjectId).hide();

  featuredProjectId++;

  if (featuredProjectId == featuredProjects.length) featuredProjectId = 0;
  //$("#" + featuredProjectContentId + featuredProjectId).html(data);
  $("#" + featuredProjectContentId + featuredProjectId).fadeIn(500);
}


function previousFeaturedProject(){
  $("#" + featuredProjectContentId + featuredProjectId).hide();

  featuredProjectId--;

  if (featuredProjectId < 0) featuredProjectId = featuredProjects.length - 1;
  //$("#" + featuredProjectContentId + featuredProjectId).html(data);
  $("#" + featuredProjectContentId + featuredProjectId).fadeIn(500);
}

// Book Club

var bookId = -1;
var bookContentId = "book_";
var nextBookLinkId = "nextBookLink";
var previousBookLinkId = "previousBookLink";
var books;
var resourceURL;

$(document).ready(function () {
    if (!books) return;
    
    $("#" + nextBookLinkId).click(nextBook);
    $("#" + previousBookLinkId).click(previousBook);
    nextBook();
});

function nextBook(){
  $("#" + bookContentId + bookId).hide();

  bookId++;

  if (bookId == books.length) bookId = 0;
  $("#" + bookContentId + bookId).show();
//  $("#" + bookContentId + bookId).fadeIn(500);
}


function previousBook(){
  $("#" + bookContentId + bookId).hide();

  bookId--;

  if (bookId < 0) bookId = books.length - 1;
  $("#" + bookContentId + bookId).show();
//  $("#" + bookContentId + bookId).fadeIn(500);
}

// Slideshow

var slideshowImageId = 0;
var currentImage = null;
var slideshowImagesCount = 0;
var slideshowImages = null;
var position;
var slideshowContainer;
var imageX;
var imageY;

$(document).ready(function () {
  slideshowContainer = $("#slideshow_container");
  slideshowImagesCount = $("#slideshow_container > img").length;

  if (!slideshowImagesCount) return;

  position = slideshowContainer.position();
  //position = slideshowContainer.offset();
  imageX = cutPX(slideshowContainer.css("margin-left")) + cutPX(slideshowContainer.css("padding-left")) + cutPX(position.left) + "px";
  imageY = cutPX(slideshowContainer.css("margin-top")) + cutPX(slideshowContainer.css("padding-top")) + cutPX(position.top) + "px";

  if (jQuery.browser.safari) imageY = cutPX(imageY) + "px"

  rewind();
  showCurrentImage();
  setInterval(nextSlideshow, 5000);
});

function cutPX(val) {
  var str = val + "";
  var pxPos = str.indexOf("px");
  if (pxPos != -1) str = str.substr(0, pxPos);
  else str = val;
  
  return Number(str);
}

function rewind() {
  slideshowImageId = 0;
  currentImage = $("#slideshow_container > img:first");
}

function nextSlideshow(){
  hideCurrentImage();

  slideshowImageId++;

  if (slideshowImageId >= slideshowImagesCount) { 
    rewind();
  }
  else {
    currentImage = currentImage.next();
  }
  
  showCurrentImage();
}

function showCurrentImage() {
  currentImage.css("z-index", 10);
  currentImage.css("left", imageX);
  currentImage.css("top", imageY);
  currentImage.fadeIn(1000);
}

function hideCurrentImage() {
  currentImage.css("z-index", 1);
  currentImage.fadeOut(1000);
}

