//
var UT = UT || {};

/* @group easing */
jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing, {
  def: 'easeOutQuad',
  swing: function (x, t, b, c, d) {
    //alert(jQuery.easing.default);
    return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
  },
  easeInQuad: function (x, t, b, c, d) {
    return c*(t/=d)*t + b;
  },
  easeOutQuad: function (x, t, b, c, d) {
    return -c *(t/=d)*(t-2) + b;
  },
  easeInOutQuad: function (x, t, b, c, d) {
    if ((t/=d/2) < 1) return c/2*t*t + b;
    return -c/2 * ((--t)*(t-2) - 1) + b;
  },
  easeInCubic: function (x, t, b, c, d) {
    return c*(t/=d)*t*t + b;
  },
  easeOutCubic: function (x, t, b, c, d) {
    return c*((t=t/d-1)*t*t + 1) + b;
  },
  easeInOutCubic: function (x, t, b, c, d) {
    if ((t/=d/2) < 1) return c/2*t*t*t + b;
    return c/2*((t-=2)*t*t + 2) + b;
  },
  easeInQuart: function (x, t, b, c, d) {
    return c*(t/=d)*t*t*t + b;
  },
  easeOutQuart: function (x, t, b, c, d) {
    return -c * ((t=t/d-1)*t*t*t - 1) + b;
  },
  easeInOutQuart: function (x, t, b, c, d) {
    if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
    return -c/2 * ((t-=2)*t*t*t - 2) + b;
  },
  easeInQuint: function (x, t, b, c, d) {
    return c*(t/=d)*t*t*t*t + b;
  },
  easeOutQuint: function (x, t, b, c, d) {
    return c*((t=t/d-1)*t*t*t*t + 1) + b;
  },
  easeInOutQuint: function (x, t, b, c, d) {
    if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
    return c/2*((t-=2)*t*t*t*t + 2) + b;
  },
  easeInSine: function (x, t, b, c, d) {
    return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
  },
  easeOutSine: function (x, t, b, c, d) {
    return c * Math.sin(t/d * (Math.PI/2)) + b;
  },
  easeInOutSine: function (x, t, b, c, d) {
    return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
  },
  easeInExpo: function (x, t, b, c, d) {
    return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
  },
  easeOutExpo: function (x, t, b, c, d) {
    return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
  },
  easeInOutExpo: function (x, t, b, c, d) {
    if (t==0) return b;
    if (t==d) return b+c;
    if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
    return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
  },
  easeInCirc: function (x, t, b, c, d) {
    return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
  },
  easeOutCirc: function (x, t, b, c, d) {
    return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
  },
  easeInOutCirc: function (x, t, b, c, d) {
    if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
    return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
  },
  easeInElastic: function (x, t, b, c, d) {
    var s=1.70158;var p=0;var a=c;
    if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
    if (a < Math.abs(c)) { a=c; var s=p/4; }
    else var s = p/(2*Math.PI) * Math.asin (c/a);
    return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
  },
  easeOutElastic: function (x, t, b, c, d) {
    var s=1.70158;var p=0;var a=c;
    if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
    if (a < Math.abs(c)) { a=c; var s=p/4; }
    else var s = p/(2*Math.PI) * Math.asin (c/a);
    return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
  },
  easeInOutElastic: function (x, t, b, c, d) {
    var s=1.70158;var p=0;var a=c;
    if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
    if (a < Math.abs(c)) { a=c; var s=p/4; }
    else var s = p/(2*Math.PI) * Math.asin (c/a);
    if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
    return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
  },
  easeInBack: function (x, t, b, c, d, s) {
    if (s == undefined) s = 1.70158;
    return c*(t/=d)*t*((s+1)*t - s) + b;
  },
  easeOutBack: function (x, t, b, c, d, s) {
    if (s == undefined) s = 1.70158;
    return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
  },
  easeInOutBack: function (x, t, b, c, d, s) {
    if (s == undefined) s = 1.70158; 
    if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
    return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
  },
  easeInBounce: function (x, t, b, c, d) {
    return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
  },
  easeOutBounce: function (x, t, b, c, d) {
    if ((t/=d) < (1/2.75)) {
      return c*(7.5625*t*t) + b;
    } else if (t < (2/2.75)) {
      return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
    } else if (t < (2.5/2.75)) {
      return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
    } else {
      return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
    }
  },
  easeInOutBounce: function (x, t, b, c, d) {
    if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
    return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
  }
});

/* @end */

/* @group Image Preloader */

UT.imagePreloader = function() {
  var _this = this;
  _this.cache = {};

  _this.load = function(src, callback) {
    var cacheImage = document.createElement('img');
    cacheImage.src = src;
    if(cacheImage.complete && callback && typeof(callback) == "function") {
      callback();
      return cacheImage;
    }
    if(callback && typeof(callback) == "function") {
      cacheImage.onload = callback();
    }
    _this.cache[src] = cacheImage;
    return cacheImage;
  };

  _this.loaded = function(src, callback) {
    if(_this.cache[src]) {
      return true;
    } else {
      var cacheImage = _this.load(src, callback);
      if(cacheImage.complete) {
        return true;
      }
    }
    return false;
  };

  var argsLen = arguments.length;

  if(argsLen == 2 && typeof(arguments[0]) == "string" && typeof(arguments[1]) == "function") {
    _this.load(arguments[0], arguments[1]);
    return;
  }

  for (var i = argsLen; i--;) {
    if(typeof(arguments[i]) == "string")
      _this.load(arguments[i]);
  }

};

/* @end */

/* @group Carousel */
UT.carousel = function(scope, options) {
  var carousel = this,
    o = $.extend({}, UT.carousel.defaults, options);
  
  carousel.scope = $(scope);
  carousel.viewpane = $(o.viewpane, scope);
  carousel.slider = $(o.slider, scope);
  carousel.slides = $(o.slides, scope);
  carousel.prev = $(o.prev, scope);
  carousel.next = $(o.next, scope);
  carousel.pages = $([]);
  carousel.animating = false;
  
  function init() {
    
    var count = carousel.slides.length % o.slidesPerPane;
    if(count != 0) {
      count = o.slidesPerPane - count;
      for(var i = 0; i < count; i++) {
        carousel.slider.append("<li class='blank' />");
      }
    }
    
    if(carousel.slides.length <= o.slidesPerPane) {
      carousel.scope.addClass('off');
      carousel.prev.remove();
      carousel.next.remove();
      return;
    }
    
    while($(o.slides, carousel.slider).length) {
      page = $(o.page);
      carousel.slider.find(o.slides+':lt('+o.slidesPerPane+')').appendTo(page);
      page.appendTo(carousel.viewpane);
      carousel.pages = carousel.pages.add(page);
    }
    
    carousel.slider.remove();
    carousel.current = carousel.pages.first().addClass('current');
    
    carousel.scope.bind('next', function() {carousel.advance(1);});
    
    carousel.scope.bind('prev', function() {carousel.advance(-1);});
    
    carousel.next.bind('click', function(e) {
      e.preventDefault();
      carousel.scope.trigger('next');
    });

    carousel.prev.bind('click', function(e) {
      e.preventDefault();
      carousel.scope.trigger('prev');
    });

  };
  
  carousel.advance = function(direction) {
    if(!carousel.animating) {
      carousel.animating = false;
      //var delta = direction*100,
        nextPos = carousel.current.prevAll().length + direction;

      if(nextPos >= carousel.pages.length) {
        next = carousel.pages.first();
      } else if(nextPos < 0) {
        next = carousel.pages.last();
      } else {
        next = carousel.pages.eq(nextPos);
      }
      
      next.addClass('next');

      //next.css('left', delta + '%');
      carousel.current.add(next).animate( 'slow',/* 'easeInOutQuart',*/ function() {
        carousel.animating = false;
        carousel.current.removeClass('current');
        carousel.current = next.addClass('current');
        carousel.viewpane.find('.next').removeClass('next');
      });
      
      
    }
  };
  
  
  init();
  
};

UT.carousel.defaults = {
  slidesPerPane: 3,
  viewpane: '.view-pane', 
  slider: '.view-pane ul',
  slides: 'li',
  page: '<ul class="page"></ul>',
  prev: '.prev', 
  next: '.next'
};
/* @end */

/* @group Slideshow */

UT.featureSlideshow = function(scope, slides, pagination, prev, next) {
  
  var s = this;
  s.scope = $(scope);
  
  s.showSlide = function(id, dir) {
    var staged = getSlide(id);
    
    if(!dir) {
      dir = 1;
    }
    
    if(!staged.hasClass('active')) {
      setActivePage(id);
      
      staged.addClass('staged').css({
        opacity: 0
        //left: (50 * dir) + 'px'
      }).show().animate({
        opacity: 1
        //left: '0'
      }, 500, function() {setActiveSlide(id);});
    }
    
  };
  
  function getSlide(o) {
    if(typeof(o) === 'string' && o.indexOf('#') != 0)
      var o = '#'+o;
    return $(o, s.scope);
  }
  
  function getID(o) {
    return "#" + getSlide(o).attr('id');
  }
  
  function getPage(o) {
    return $(pagination).find('a').filter(function() {
      return $(this).attr('href') === getID(o);
    }).parent();
  }
  
  function setActivePage(o) {
    getPage(o)
      .addClass('active')
      .removeClass('staged')
      .siblings('.active')
      .removeClass('active');
  }
  
  function setActiveSlide(o) {
    
    getSlide(o)
      .addClass('active')
      .removeClass('staged')
      .siblings('.active')
      .removeClass('active');
      
    s.active = getID(o);
  }
  
  function isImg(src) {
    return (/([^\s]+(?=\.(jpg|jpeg|gif|png|bmp))\.\2)/i).test(src);
  };
  
  function paginate(dir) {
    var index = getSlide(s.active).prevAll().length + dir,
      highIndex = $(slides, scope).length - 1;
    
    if(index > highIndex) {
      index = 0;
    } else if(index < 0) {
      index = highIndex;
    }
    
    s.showSlide($(slides, scope).eq(index), dir);
    
    s.pause_override = false;
    s.pause();
    s.start();
  }
  
  // Added by FB
  slide_delay = 8000;
  
  s.start = function() {
    if(s.paused) {
      s.paused = false;
      s.interval = setInterval(function() {paginate(1);}, slide_delay);
      s.timer = new UT.timer('.tab-holder', slide_delay);
      s.timer.start();
    }
  };
    
  s.pause = function() {
    if(!s.paused) {
      s.paused = true;
      clearInterval(s.interval);
      s.timer.pause();
    }
  };
  
  
  function init() {
    
    setActiveSlide(s.scope.find(slides).first());
    setActivePage(s.scope.find(slides).first());
    
    
    
    $(pagination).find('li').bind({
     click: function(e) {
        e.preventDefault();
        //s.showSlide($(this).find('a').attr('href'));
      }
    });
    
    // mouse enter event for pagination links
    $(pagination).find('li').bind('mouseenter', function(e) {
        s.showSlide($(this).find('a').attr('href'));
        s.pause_override = false;
        s.pause();
      }
    );
    
    // mouse leave event for pagination links
    $(pagination).find('li').bind('mouseleave', function(e) {s.start();});
    
    
    $(scope).hover(function() {s.pause();}, function() {
      if(s.pause_override) {s.pause();} else {s.start();}
    });
    
    $(prev).bind({
      click: function() {paginate(-1);}
    });

    $(next).bind({
      click: function() {paginate(1);}
    });
    
    s.paused = true;
    s.start();
      
  }
  
  // FB  
  s.pause_override = false;
  
  init();
  
};
/* @end */

/* @group timer */

UT.timer = function(scope, time) {
  
  var t = this,
    interval = time/3,
    index = 0;
    
  t.reset = function(bar) {
    bar.css({
      left: '-41px',
      opacity: 1
    })
  }
  
  t.start = function() {
    t.bars.stop().css({
      left: '-41px',
      opacity: 1
    });
    
    t.bars.eq(1).delay(interval+Math.random()*10);
    t.bars.eq(2).delay(2*interval+Math.random()*10);
    
    animate();
    t.interval = setInterval(animate, time);
  };
  
  t.pause = function() {
    t.bars.stop(true, true);
    t.bars.fadeOut('fast', function() {t.bars.remove();})

  };
  
  function animate() {
    
    t.bars
      .animate({ left: '0px'}, interval)
      .animate({ opacity: 0 }, interval)
      .delay(interval)
      .queue(function() {
        $(this).css({
          //left: '-40px',
          opacity: 1
        });
        $(this).dequeue();
      });

  }
  
  function init() {
    
    t.timer = $('ul.timer').length > 0 ? 
      $('ul.timer').html('<li><span></span></li><li><span></span></li><li><span></span></li>') : 
      $('<ul class="timer"><li><span></span></li><li><span></span></li><li><span></span></li></ul>');
    
    if(!($.browser.msie && $.browser.version < 8))
      t.timer.prependTo(scope);

    t.bars = t.timer.find('span');
      
    
  };
  
  init();
  
};
/* @end */

/* @group per-page execution logic */
UT.pages = {
  
  'home-simple': function() {
    
    UT.carousel('.carousel', {
      slidesPerPane: 5
    });
        
  },
  
  'home': function() {
    
    p_obj = new UT.featureSlideshow('.tab-visual .slides', 'li.tab', '.tab-visual .pagination', '.home-prev', '.home-next');
    
    $('.pagination ul').css({
      'margin-left': $('.pagination ul').outerWidth() / -2
    }).find('li:last').addClass('last');
    
    
  }
  
};

//
UTIL = {
  exec: function(page) {
    var ns = UT.pages;

    if ( page !== "" && ns[page] && typeof( ns[page] ) == "function" ) {ns[page]();}
  },

  init: function() {
    var body = document.body, 
      page = body.getAttribute( "data-page" );
      
    
    UTIL.exec( "common" );
    UTIL.exec( page );

    $(document).trigger('finalized');
  }
};

//
$(document).ready( UTIL.init );
