$(document).ready(function(){

  Navigation.ready();
  Video.ready();
  Logo.ready();

});

var Navigation = new function(){

  this.home = false;

  this.ready = function(){
    var path = location.pathname.substring(1);
    if(path){
      var home = $('#nav-home').attr('href');
      if(path == home){
        this.home = true;
        HomePage.ready();
      }
      else{
        this.home = false;
        SidebarAccordion.ready();
        Callouts.ready();
      }
    }
    else{
      this.home = true;
      HomePage.ready();
    }
  };

};

var Callouts = new function(){

  var self = this;

  this.ready = function(){
  
    $('#page-callouts-copy').hide();
    $('#page-callouts-links').delegate('a', 'click', self.click);
    $('#page-callouts-close-btn').click(self.clear);
    
    if(window.location.hash){
      self.set(window.location.hash);
    }
    else{
      var first = $('#page-callouts-links a:first').attr('href');
      self.set(first)
    }
    
  };

  this.click = function(){
    var selector = $(this).attr('href');
    if(selector == $('.arrow-link-current').attr('href')){
      self.clear();
    }
    else{
      self.set(selector);
    }
  };

  this.clear = function(){
    $('#page-callouts-close-btn').hide();
    $('#page-callouts-copy').hide();
    $('.arrow-link-current').removeClass('arrow-link-current');
    $('.page-callout-current').removeClass('page-callout-current');
    return false;
  };

  this.set = function(selector){
    $('#page-callouts-close-btn').show();
    $('#page-callouts-copy').show();
    $('.arrow-link-current').removeClass('arrow-link-current');
    $('a[href='+selector+']').addClass('arrow-link-current');
    $('.page-callout-current').removeClass('page-callout-current');
    $(selector+'-list').addClass('page-callout-current');
  };

};

var Video = new function(){

  var self = this;
  var first = true;

  this.ready = function(){
    var container = $('#video-container');
    if(container.length == 0){
      return;
    }
    
    if(Navigation.home == true){
      self.expand();
    }
    
    flowplayer('video', '/flash/flowplayer.commercial-3.2.2.swf', {
      key: '#$75f1a8e36167d5666f1',
      playlist: [
        {
          url: '/images/video-preview.jpg',
          scaling: 'fit'  
        },
        {
          url: $('#video-links a:first').attr('href'),
          autoPlay: false,
          autoBuffering: true
        }
      ],
      plugins:{
      		controls:  {
      			backgroundColor: '#000000',
      			backgroundGradient: 'none',			
      			all: false,
      			scrubber: true,
      			fullscreen: true,
      			play: true,
      			mute: true,
      			height: 30,
      			progressColor: '#009de9',
      			bufferColor: '#333333',
      			autoHide: false
      		}
      },
      onLoad:self.onLoad,
      onFinish:self.onFinish
    });
    
    $('#video-links').delegate('a', 'click', self.click);
    $('#video-links a:first').addClass('arrow-link-current');
    $('.video-expand').live('click', self.expand);
    $('.video-shrink').live('click', self.shrink);
  };

  this.shouldAutoPlay = function(){
    if(Navigation.home && first){
      return true;
    }
    else{
      return false;
    }
  };

  this.onLoad = function(){
    
    $f(0).setVolume(25);
    
    if(self.shouldAutoPlay()){
      $('#video-links a:first').click();
    }
    
  };

  this.onFinish = function(){
    if(Navigation.home && first){
      first = false;
      self.shrink();
    }
  };

  this.click = function(event){
    var video = $(this).attr('href');
    if(video == "/flash/30sec_knaufecosealweb.flv"){
      $f(0).setVolume(25);
    }
    else{
      $f(0).setVolume(100);
    }
    event.preventDefault();    
    $('.arrow-link-current').removeClass('arrow-link-current');
    $(this).addClass('arrow-link-current');
    $f(0).setClip(video);
    $f(0).play();
  };
  
  this.expand = function(callback){
    $('.sidebar-grad-box').animate(
      {width: 416}, 100
    );
    $('#video-container').animate(
      {width:416, height:264}, 100, function(){
        if(typeof callback == 'function'){
          callback();
        }
      }
    );
    $('#video-expand').attr('class', 'video-shrink').text('Shrink').animate(
      {top: 280}, 100
    );
    return false;
  };
  
  this.shrink = function(){
    $(this).removeClass().addClass('video-expand');
    $('.sidebar-grad-box').animate(
      {width: 266}, 100
    );
    $('#video-container').animate(
      {width:266, height:169}, 100
    );
    $('#video-expand').attr('class', 'video-expand').text('Expand').animate(
      {top: 189}, 100
    );
    return false;
  };

};

var Logo = new function(){

  this.ready = function(){
    swfobject.embedSWF("/flash/ecoseal.swf", "swfobject", "280", "70", "9.0.0");
  } 
};

var HomePage = new function(){

  this.ready = function(){
    swfobject.embedSWF("/flash/home-resized.swf", "home-flash-container", "684", "657", "9.0.0", false, false, {wmode:'opaque'});
  }
};

var SidebarAccordion = new function(){

  var self = this;

  this.ready = function(){
    $('#kiss-sidebar-accordion p>span').hide();
    $('#kiss-sidebar-accordion a').mouseenter(self.show);
  };

  this.show = function(){
    var span = $(this).next('p>span');
    if(span.hasClass('kiss-sidebar-visible')){
      return;
    }
    else{
      $('.kiss-sidebar-visible').removeClass().slideUp(200);
      span.addClass('kiss-sidebar-visible').slideDown(200);
    }
    return false;
  };
};
