var novaloc = function() {
  var this_ = this;
  
  this_.fadeTime = 500;
  this_.block = false;
  this_.toggled = false;
  this_.slideStyles = "";
  this_.fadingType;
  this_.fadeOutParams;
  this_.fadeInParams;

  function startSlideShow(slide, slides, timeout, translation) {
    this_.slideStyles = $(slides).eq(0).css('display'); // same display set to all slides
    if (translation) {
      this_.fadingType = 'animate';
      this_.fadeOutParams = {opacity: 0.0, marginLeft: translation};
      this_.fadeInParams = {opacity: 1.0, marginLeft: 0};
    } else {
      this_.fadingType = 'fade';
    }
    slideShow(slide, slides, timeout, translation);
  }
  
  function slideShow(slide, slides, timeout, translation) {
    setTimeout(function() {
      if (!this_.block) {
        $(slides[slide]).css('display', this_.slideStyles);
        
        function callback () {
          var oldDisplay = $(this).css('display');
          $(this).css('display', 'none');
          if (translation) {
            $(this).css('margin-left', -translation);
          }
          var next = slide + 1 < slides.length ? slide + 1 : 0;
          var controls = $(slides).parents('[title="slideshow"]').eq(0).find('ul.slide-controls li');
          $(controls).removeClass('active');
          $(controls[next]).addClass('active');
          $(slides[next]).css('display', oldDisplay);
          
          if (this_.fadingType == 'animate') {
            $(slides[next]).animate(this_.fadeInParams, this_.fadeTime, "swing", function() {
              slideShow(next, slides, timeout, translation);
            });
          } else {
            $(slides[next]).fadeIn(this_.fadeTime, function() {
              slideShow(next, slides, timeout, translation);
            });
          }
        }
        if (this_.fadingType == 'animate') {
          $(slides[slide]).animate(this_.fadeOutParams, this_.fadeTime, "swing", callback);
        } else {
          $(slides[slide]).fadeOut(this_.fadeTime, callback);
        }
      } else {
        slideShow(slide, slides, timeout, translation);
      }
    }, timeout);
  }
  
  function toggleBlock(val) {
    this_.toggled = val;
    this_.block = val;
  }
  
  function setBlock(status) {
    if (!this_.toggled) {
      this_.block = status;
    }
  }
  
  function isToggled() {
    return this_.toggled;
  }
  
  function showSlide(slides, control, doCheck) {
    // Show correct slide
    var allow = !this_.toggled;
    allow = doCheck ? allow : true;
    if (allow) {
      var slide = parseInt($(control).attr('title'));
      $(slides).filter(function() {
        if ($(this).css('display') != 'none') {
          return true;
        } else { 
          return false; 
        }
      }).each(function() {
        if (this_.fadingType == 'animate') {
          $(this).css({opacity: 0.0, display: 'none'});
        } else {
          $(this).fadeOut(0);
        }
      });
      $(slides).filter(function() {
        if ($(this).attr('title').search(slide+1) != -1) {
          return true;
        } else {
          return false;
        }
      }).each(function() {
        if (this_.fadingType == 'animate') {
          $(this).css({opacity: 1.0, display: 'block'});
        } else {
          $(this).fadeIn(0);
        }
      });
    }
  }
  
  return {
    slideShow: startSlideShow,
    showSlide: showSlide,
    toggleBlock: toggleBlock,
    setBlock: setBlock,
    isToggled: isToggled
  }
}();

$(window).load(function() {
  /**
   * used to save OO like attributes
   */
  var this_ = this;
   
   /**
    * Add relevant controls if required
    */
  $('[title="slideshow"], [title="slideshow-no-controls"]').each(function() {
    var slides = $(this).find('[title*="slide"]');
    if ($(this).attr('title') == "slideshow-no-controls") {
      $(slides).each(function(index) {
        if (index > 0) {
          $(this).css('opacity', 0.0); 
        }
      });
    }
    if ($(this).attr('title') == "slideshow") {
      var controls = $('<ul class="slide-controls"></ul>');
      $(slides).each(function(index) {
        $(controls).append('<li class="slide-control-item" title="' + index + '">' +
         '<a href="#" title="' + index + '">' + (index+1) + '</a></li>');
      });
      $(controls).children().eq(0).addClass('active');
      $(controls).find('a').mouseenter(function(e) {
        e.preventDefault();
        $(this).parent().addClass('focus');
        this_.slideStore = $(controls).find('li.active a');
        novaloc.showSlide(slides, this);
        novaloc.setBlock(true);
      });
      $(controls).find('a').mouseout(function(e) {
        e.preventDefault();
        $(this).parent().removeClass('focus');
        novaloc.showSlide(slides, this_.slideStore, this_.preventChange);
        this_.preventChange = false;
        novaloc.setBlock(false);
      });
      $(controls).find('a').click(function(e) {
        e.preventDefault();
        if (!($(this).parent().hasClass('active')) 
              || $(this).parent().hasClass('active') && !novaloc.isToggled()) {
          $(this).parent().siblings().removeClass('active');
          $(this).parent().addClass('active');
          novaloc.toggleBlock(true);
          this_.preventChange = true;
          novaloc.showSlide(slides, this);
        } else {
          novaloc.toggleBlock(false);
        }
      });
      $(this).prepend(controls);
      
      /**
       * Activate slideshow
       */
      novaloc.slideShow(0, slides, 7000);
    } else {
      var translationAmount = 60;
      $(slides).each(function(index) { 
        if (index > 0) {
          $(this).css({'display': 'none', 'margin-left': -translationAmount}); } 
        }
      );
      
      /**
       * Activate slideshow with translation animation
       */
      novaloc.slideShow(0, slides, 7000, translationAmount);
    }
  });
  
  /**
   * Make search box text to disappear when gets focus and return that text if
   * focus is lost and no text is in the box
   */
  this_.searchText = $('#search input').attr('value');
  $('#search input[name="searchfor"]').focus(function() {
    if ($(this).attr('value') == this_.searchText) {
      $(this).attr('value', '');
    }
  });
  $('#search input[name="searchfor"]').blur(function() {
   if ($(this).attr('value') == '') {
    $(this).attr('value', this_.searchText);
   }
  });
  
  /**
   * Ensure that footer is always attached to the bottom of the browser window even
   * if the content area isn't tall enough to fill the whole space given by the browser
   */
   function footerCheck() {
    var windowHeight = $(window).height();
    var emptySpace = windowHeight - this_.footerHeight - this_.footerOffset;
    
    if (emptySpace > 0) {
      $('#content').height(this_.contentHeight + emptySpace);
    }  else {
      $('#content').height(this_.contentHeight);
    }
   }
   /**
    * Magic number for Opera and IE6. If you happen to read this source code and know why
    * Opera returns wrong offset value and IE6 wrong height value, please let us know, thanks!
    */
   var magicOpera = 37; 
   var magicIE6 = ($('#header').height() + $('#banner').height() - $('#search').height() + 5);
   
   this_.footerOffset = jQuery.browser.opera ? $('#footer').offset().top + magicOpera : $('#footer').offset().top;
   this_.footerHeight = $('#footer').height();
   this_.contentHeight = $('#content').height();
   if (jQuery.browser.msie && jQuery.browser.version == "6.0") {
    this_.contentHeight -= magicIE6;
   }
   
   footerCheck();
   $(window).resize(footerCheck);
   
  // IE6 fix, change transparent-pixel.png to gif version which doesn't have
  // transparency (solid color), this is due to problem with two transparent
  // pngs on top of each other and the pngFix which causes cropping.
  // Also change #search and .slide-control-item backgrounds to gif version
  $('div.smallInfo, div.largeInfo, #search, .slide-control-item').each(function() {
    // Yes, this is a deprecated way, but we want to target this effect
    // _only_ to IE6, not also to IE7 (jQuery doesn't provide a way to
    // check if the browser is capable of rendering png alphas)
    if (jQuery.browser.msie && jQuery.browser.version == "6.0") {
      var img = $(this).css('background-image');
      img = img.replace(/png/, "gif");
      $(this).css('background-image', img);
    }
  });
  
  $(document).pngFix();
  
  /**
   * Add shadows to elements with emphasize-class after they've been loaded
   */
  $('.emphasize').dropShadow({left: 0, top: 0, blur: 3});
});
