/* ---------------------------------------------------
CVS: $ $
Title: publicworks.css
Copyright: (c) 2010
Author: Donovan Mueller - donovan@rhinointernet.com
Company: Rhino Internet
Description: Effect controller for Phoenix Department
   of Public Works
--------------------------------------------------- */

$(document).ready(function(){
   hero.initialize();
   toplevelpages();
});//document.ready





var hero = {
   maxSlideWidth:  735, //px
   slideDuration:  500, //ms
   slideLife:     3000, //ms until slide death
   initialSlide:     0, //index for this.slides
   
   initialize: function() {
      var _parent = this;
      

      $('body').load(function(){
         _parent.setSlideWidth();
         _parent.goTo(_parent.activeSlide, 0);
      });


      this.viewport    = $('.hero');
      if (this.viewport.length <= 0) {
         return false;
      }
      
      this.slideHolder = this.viewport.children('.questions');
      this.slides      = this.slideHolder.children('li');
      this.activeSlide = this.slides.eq(this.initialSlide);
      this.controlBox  = this.viewport.children('.controls');
      this.controlList = this.controlBox.children('ul');
      
      //setup viewport
      this.viewport.addClass('js_enabled');
      
      //setup slides
      this.setSlideWidth();
      
      //setup controls
      this.controlBox.prepend('<a class="previous">&laquo; Previous</a>');
      this.controlBox.append('<a class="next">Next &raquo;</a><div class="highlighter"></div>');
      this.controls    = this.controlBox.find('a');
      this.highlighter = this.controlBox.children('.highlighter');
      this.setControlWidth();
      
      this.controls.click(function(){
         var slide = ($(this).hasClass('next'))
            ? _parent.next()
            : ($(this).hasClass('previous'))
               ? _parent.prev()
               : _parent.slides.filter($(this).attr('href'));
         
         if (slide.length > 0) {
            _parent.goTo(slide);
         }
         
         return false;
      });
      
      this.goTo(this.activeSlide, 0);
      
      //setup auto slideshow
      this.startLooper();
      this.viewport.hover(
         function() {
            clearInterval(_parent.looper);
         },
         function() {
            clearInterval(_parent.looper);
            _parent.startLooper();
         }
      );
      
      $(window).resize(function(){
         _parent.setSlideWidth();
         _parent.goTo(_parent.activeSlide, 0);
      });
      
      // On initial load sometimes the highlighter is a few pixels off, this fixes it.
      setTimeout(function() { _parent.positionControlHighlighter(0) },  100);
      setTimeout(function() { _parent.positionControlHighlighter(0) }, 1000);
   },//initialize()
   
   startLooper: function() {
      var _parent = this;
      
      this.looper = setInterval(
         function(){ _parent.goTo(_parent.next()); },
         this.slideLife
      );
   },//startLooper()
   
   setSlideWidth: function() {
      var view_width = this.viewport.innerWidth();
      if (view_width <= this.maxSlideWidth) {
         this.slides.css('margin', 0);
         this.slides.css('overflow', 'hidden');
      } else {
         var diff         = view_width - this.maxSlideWidth;
         var left_margin  = Math.floor(diff / 2);
         var right_margin = left_margin;
         if (diff % 2 > 0) {
            right_margin++;
         }
         this.slides.css({marginLeft: left_margin, marginRight: right_margin});
         this.slides.css('overflow', 'visible');
      }
      this.slides.css('width', view_width);
      this.slideHolder.css('width', this.slides.length * view_width * 2);
   },//setSlideWidth()
   
   setControlWidth: function() {
      this.controlList.css('width', 'auto');
      var width = 0;
      this.controlList.children('li').each(function(){
         width += $(this).outerWidth();
      });
      this.controlList.css('width', width);
   },//setControlWidth()
   
   goTo: function(slide) {
      var howfast = (arguments.length > 1) ? arguments[1] : this.slideDuration;
      //move slide into view
      this.slideHolder.animate({left: -1 * slide.position().left}, howfast);
      
      this.activeSlide = slide;
      this.positionControlHighlighter(howfast);
   },//goTo()
   
   next: function() {
      var next = this.activeSlide.next();
      if (next.length < 1) {
         next = this.slides.eq(0);
      }
      
      return next;
   },//next()
   
   prev: function() {
      var prev = this.activeSlide.prev();
      if (prev.length < 1) {
         prev = this.slides.eq(this.slides.length - 1);
      }
      
      return prev;
   },//prev()
   
   positionControlHighlighter: function() {
      var howfast = (arguments.length > 0) ? arguments[0] : this.slideDuration;
   
      //move control highlighter
      var id          = this.activeSlide.attr('id');
      var control     = this.controls.filter('a[href=#' + id + ']').parent('li');
      var listposleft = this.controlList.position().left;
      if (!$.browser.msie || $.browser.version > 8) {
         var listleftmargin = this.controlList.css('margin-left');
         listleftmargin     = listleftmargin.substring(0, listleftmargin.length - 2);
         listposleft += Number(listleftmargin);
      }
      
      var left = 
         + listposleft
         + control.position().left
         + Math.floor(control.outerWidth(true) / 2)
         - Math.ceil(this.highlighter.outerWidth() / 2);
      this.highlighter.animate({left: Number(left)}, howfast);
   }//positionControlHighlighter()
}; //hero{}


function toplevelpages(){
   if ($('.PHXNavMultiVertical-focus-2').length) {
      if ($('.PHXNavMultiVertical-focus-3').length) {
        $('#content').removeClass('toplevel');
      }; //if
   }; //if
}; //toplevelpages





