"use strict";

/*global jQuery: false */
jQuery.noConflict();

document.biz = document.biz || {};
document.biz.trevellyan = document.biz.trevellyan || {};

document.biz.trevellyan.portfolio = function (parameters) {
  var sourceIndex,
  targetId = parameters.target,
  sourceId = parameters.source;

  function getCookie(name) {
    var cookieRE, found;

    cookieRE = new RegExp("(^|; )" + name + "=([^;]*)(;|$)");
    found = document.cookie.match(cookieRE);

    return found ? found[2] : null;
  }

  function setSessionCookie(name, value) {
    document.cookie = name + "=" + value;
  }

  function replaceCounter()
  {
    var counterDiv, counterText, newText, current;

    if (parameters.counter) {
      counterDiv = jQuery("#" + parameters.counter);
      counterText = jQuery.trim(counterDiv.text());
      current = sourceIndex + 1;
      newText = counterText.replace(
        /(\D*)(\d+)(\D+)(\d+)(\D*)/,
        "$1" + current + "$3" + sourceId.length + "$5"
        );
      counterDiv.html(counterDiv.html().replace(counterText, newText));
    }
  }

  function replaceDivs()
  {
    var sourceDiv, targetDiv, i;

    sourceDiv = jQuery("div", "#" + sourceId[sourceIndex]);

    for (i = 0; i < targetId.length && i < sourceDiv.length; i += 1)
    {
      targetDiv = jQuery("#" + targetId[i]);
      targetDiv.html(jQuery(sourceDiv[i]).html());
    }
  }

  function enablePrevious() {
    var button;

    if (parameters.previous) {
      button = jQuery("#" + parameters.previous);

      if (sourceIndex === 0) {
        button.attr("disabled", true);
      } else {
        button.attr("disabled", false);
      }
    }
  }

  function enableNext() {
    var button;

    if (parameters.next) {
      button = jQuery("#" + parameters.next);

      if (sourceIndex === sourceId.length - 1) {
        button.attr("disabled", true);
      } else {
        button.attr("disabled", false);
      }
    }
  }

  function goPrevious() {
    sourceIndex -= 1;
    if (sourceIndex < 0) {
      sourceIndex = sourceId.length - 1;
    }
    setSessionCookie("portfolioIndex", sourceIndex);

    replaceCounter();
    replaceDivs();
    enablePrevious();
    enableNext();
  }

  function goNext() {
    sourceIndex = (sourceIndex + 1) % sourceId.length;
    setSessionCookie("portfolioIndex", sourceIndex);

    replaceCounter();
    replaceDivs();
    enablePrevious();
    enableNext();
  }

  if (document.location.search !== "") {
    sourceIndex = jQuery.inArray(document.location.search.substr(1), sourceId);
    setSessionCookie("portfolioIndex", sourceIndex);
    document.location.replace(document.location.href.replace(document.location.search, ""));
    return;
  }

  jQuery(function () {
    var button;

    sourceIndex = Number(getCookie("portfolioIndex") || 0);
    sourceIndex = sourceIndex < 0 ? 0 : sourceIndex % sourceId.length;

    replaceCounter();
    replaceDivs();

    if (parameters.previous) {
      button = jQuery("#" + parameters.previous);
      button.click(goPrevious);
      enablePrevious();
    }

    if (parameters.next) {
      button = jQuery("#" + parameters.next);
      button.click(goNext);
      enableNext();
    }
  });
};

