/// <reference path="~/js/jquery-vsdoc.js" />
var homePage = function (window, document, undefined) {
          // Global settings
          var animationDelay = 1000;

          // Promotional area animation
          var promoState = 1;
          var promoIllustration;
      
    var promoIllustrationOld;
    var promoIllustrationNew;
            
          var promoInterval;

          // FAQ animation
          var faqContainer;

          // Quotes animation
          var quotesContainer;

          var banners;


          function rotateFAQ() {
              var nextQuestion = $('dt.active+dd', faqContainer);
              if (nextQuestion.size() != 1)
                  nextQuestion = $('dd:first-child', faqContainer);

              $('.active', faqContainer).fadeOut('slow', function () {
                  $('dd', faqContainer).removeClass('active');
                  $('dt', faqContainer).removeClass('active');

                  nextQuestion.fadeIn('slow');
                  nextQuestion.addClass('active');

                  var nextAnswer = $('dd.active+dt', faqContainer);
                  nextAnswer.fadeIn('slow');
                  nextAnswer.addClass('active');
              });
          }

          function rotatePromo(state) {
              // State can be specified explicitly when calling from pager, check whether this is the case
              // and reset animation timer so that user will not see two animations in a row.

              var newContentUrl;
              if (banners.length == promoState) {
                  promoState = 1;
                  newContentUrl = banners[0]
              }
              else {
                  newContentUrl = banners[promoState]
                  promoState = promoState + 1;
                }

              // We have two blocks to animate - description and illustration. Thanks to the fadeOut
              // function implementation that does not wait for the animation to finish we can call them
              // one by one and user will not see the delay between those blocks animation.
              promoIllustration.css("display","none")
              
              promoIllustration.load(newContentUrl + ' .image > img');
              var image = $('.baner_item').attr('src');
              $('.baner_area').css("background-image","url("+image+")");
              promoIllustration.fadeIn('slow');
 
          }

          function rotateQuotes() {
              var nextQuote = $('.active+li', quotesContainer);
              if (nextQuote.size() != 1)
                  nextQuote = $('li:first-child', quotesContainer);

              $('.active', quotesContainer).fadeOut('slow', function () {
                  $('li', quotesContainer).removeClass('active');
                  nextQuote.fadeIn('slow', function () { $(this).addClass('active'); });
              });
          }

          function init(mymasiv) {
              // Elements for animation
              jQuery.parseJSON(mymasiv);
              banners=mymasiv;
              promoIllustration = $('#baner_background');
              faqContainer = $(document.getElementById('faq'));
              quotesContainer = $(document.getElementById('quotes'));
            
              if (faqContainer.size() > 0)
                  setInterval('homePage.rotateFAQ()', animationDelay);
              if (quotesContainer.size() > 0)
                  setInterval('homePage.rotateQuotes()', 200);

              // Manual animation pager initialization
              $('#promo > ul a').click(function (event) {
                  homePage.rotatePromo($(this).text() - 1);
                  event.preventDefault();
              });

          }

          return {
              rotatePromo: rotatePromo,
              rotateFAQ: rotateFAQ,
              rotateQuotes: rotateQuotes,
              init: init
          };
      
      } (window, document);

