var JonHarmonDesign = JonHarmonDesign || {};

(function(lib){

  lib.toolTips = function(){
    $("a#portfolio-link").tooltip({
      tip:'#portfolio-tip',  
      effect: 'slide',
      position: "top center",
      offset: [0,0]
    });
    
    $("a#about-link").tooltip({
      tip:'#about-tip',  
      effect: 'slide',
      position: "top center",
      offset: [0,-50]
    });


    $("a#contact-link").tooltip({
      tip:'#contact-tip',  
      effect: 'slide',
      position: "top center",
      offset: [0,50]
    });
  };
  
  lib.showcase = function(){
    $('#menu').tabs("#showcase .case",{
      effect: 'fade'
    }).history();
  };
  
  lib.loadContent = function(){
    lib.showcase();
    $('#content').fadeIn('fast');    
  };
  
  lib.animateDock = function(){
    $('#menu a').hover(function(){
      var that = $(this);
      that
        .stop()
        .animate({'opacity':0.6,'top':'-55px'},250,'swing');

      that
        .parent()
        .stop()
        .animate({},300);

    },function(){
      var that = $(this);  
      that
        .stop()
        .animate({'opacity':1,'top':'-41px'},250,'swing');
    });
  };

  lib.loadDockIcons = function(){
    //deal with the order of loading the dock, and then the rest of the content
    $('#menu a:hidden:first')
      .animate({'opacity':'toggle','top':'-65px'},'swing',function(){

        $(this).animate({'top':'-40px'},'swing');
        $('#menu a:hidden:first')
          .animate({'opacity':'toggle','top':'-65px'},'swing',function(){
            $(this).animate({'top':'-40px'},'swing');

            $('#menu a:hidden:first')
              .animate({'opacity':'toggle','top':'-65px'},'swing',function(){
                $(this).animate({'top':'-40px'},'swing',function(){

                  // fire the dockIconsLoaded event
                  $('#menu').trigger('dockIconsLoaded');
                });
            });
        });
    });
  };
 
  lib.portfolio = function(){
    // assign width to the navi items, this will allow css to nicely align

    $("#branding .items img").click(function() { 
      var url  = $(this).attr("data-big"),
       wrap = $(".image_wrap"),
       img  = new Image(); 
       
      img.onload = function() { 
        wrap
          .fadeTo(0,0)
          .fadeTo(400, 1) 
          .find("img")
          .attr("src", url); 
      }; 
   
      img.src = url; 
    }).filter(":first").click();

    $('#portfolio-nav').tabs(".portfolio-panes",{
      effect: 'fade'
    });

    $('#portfolio-nav').click(function(){
      $('div.navi').each(function(){ var a = $('a', this);$(this).width(a.length * $(a[0]).width())})
    });

    $(".scroller")
      .scrollable({ size : 1 })
      .navigator(); 

    $('.navi').each(function(){ 
      var that = $(this);
      a = that.find('a'); 
      that.width(a.length * 13);
    });
  };
  lib.fadeOutLoader = function(){
    $('#viewer').css({  'background': 'none'})
  };
  lib.run = function(){
    // fade in all images
    $('img').fadeIn('slow');

    // listen for the dockLoaded event
    $(document).bind('dockIconsLoaded',function(){
      //load content
      lib.loadContent();
      lib.animateDock();
      lib.toolTips();
      lib.portfolio();
      lib.fadeOutLoader();
    });

    var background = new Image(),
        dock       = new Image();


    // wait for background to load
    $(background).load(function(){
        // then wait for the dock to load
        $(dock).load(function(){
          // the load the icons
          lib.loadDockIcons();   
        }).attr('src','images/dock.png');

      }).attr('src','images/background.jpg' );
    
    // enable portfolio
  };
})(JonHarmonDesign);

