var Site = {
  Common: {
    init: function(e) {
      $('body').addClass('js');
    },
    functionalLinks: function(e) {
      $('a.back').click(function(){
        history.go(-1);
        return false;
      })
    },
    /* Label propagator */
    accessibleInputValues: function(e) {
      labeltxt = $(e).attr('rel');
      if ($(e).val() == '') {
        $(e).val(labeltxt);
      };
      $(e)
        .focusin(function() {
          labeltxt = $(this).attr('rel');
          if ($(this).val() == labeltxt) $(this).addClass('focus').val('');
        }).focusout(function() {
          if ($(this).val() == '') $(this).removeClass('focus').val(labeltxt);
        });
    },
    datePickerInit: function(e) {
      if (typeof $.datepicker != 'undefined') {
        $('.datepicker input').datepicker();
      }
    },
    customScroll: function(e) {
      if (typeof $.fn.jScrollPane != 'undefined') {
        $(e).jScrollPane({showArrows:true, scrollbarWidth:15, scrollbarMargin:0});
      }
    },
    cufonize: function(e) {
      if (typeof Cufon != 'undefined') {
        Cufon.replace('#secondary-navigation li a', {hover: true});
      }
    },
    photoGallery: function() {
      var $image = $('#sidebar img');
      var $thumbs = $('#content ul.thumbs a');
      var $logo = $('#sidebar a.logo');
      var $viewer = $('#viewer');
      
      if($thumbs.length) {
        $logo
          .mouseover(function() {
            $(this).animate({ right: 0 }, { queue: false, duration: 'fast'});
          })
          .mouseout(function() {
            $(this).animate({ right: -255 }, { queue: false, duration: 'fast'});
          })
          .animate({ right: -255 }, { queue: false, duration: 'fast'});
      }
      
      $thumbs.click(function() {
        $viewer.hide();
        $image.attr('src', $(this).attr('href')).show();
        $("#content ul.thumbs li").removeClass('active');
        $(this).parent().addClass('active');
        return false;
      });
    },
    
    panoramicViewer: function() {
      var $image = $('#sidebar img');
      var $trigger = $('h2.panoramic_viewer a');
      
      if($trigger.length == 0) return false;
        
      var $viewer = $('<div id="viewer"></div>');
      $('#sidebar').append($viewer);
      
      $trigger.click(function() {
        
        $viewer.show();
        $image.hide();
        var params = {
          allowfullscreen: 'true',
          allowscriptaccess: 'always',
          wmode: 'opaque'
        }
        swfobject.embedSWF($(this).attr('href'), 'viewer', '541', '458', '9', params);
      
        return false;
      });
    },
    homeFlash: function() {
      if(document.getElementById('FlashID')) {
        swfobject.registerObject("FlashID");
      }
    }
  }
};

$(document).ready(function() {
  Site.Common.init();
  Site.Common.cufonize();
  Site.Common.functionalLinks();
  Site.Common.homeFlash();
  Site.Common.datePickerInit();
  Site.Common.accessibleInputValues('.places input.autoclear');
  Site.Common.accessibleInputValues('.time input.autoclear');
  Site.Common.customScroll('.scrollable');
  Site.Common.panoramicViewer();
  Site.Common.photoGallery();
  
});

