jQuery( function( $ )
  {
    if( $( '#quick-links' ).length )
      $( '#quick-links' ).MCAccordion( { toggle: 'h3', show: 0, clickClose: false } );

    if( $( '#map-canvas' ).length )
      loadScript( );

    if( $( '.virtual-tour-cb' ).length )
      $( '.virtual-tour-cb' ).colorbox( { iframe: true, innerWidth: 640, innerHeight: 365, opacity: 0.5 } );

	if( $( '.awards-cb' ).length )
      $( '.awards-cb' ).colorbox( { opacity: 0.5 } );

    if( $( 'textarea[placeholder]' ).length )
      $( 'textarea[placeholder]' ).placeholder( );

    var finalDate = new Date( );
    finalDate.setFullYear( 2012, 8, 29 );
    finalDate.setHours( 6, 0, 0, 0 );
    
    var countDownInterval;
    if( $( '#counter' ).length )
    {
      updateCountDown( );
      countDownInterval = setInterval( updateCountDown, 1000 );
    }

    function updateCountDown( )
    {
      var now = new Date( );
      var delta = finalDate.getTime( ) - now.getTime( );

      if( delta < 0 )
      {
        clearInterval( countDownInterval );
        return null;
      }

      var days = Math.floor( delta / ( 1000 * 60 * 60 * 24 ) );
      var hours = Math.floor( delta / ( 1000 * 60 * 60 ) ) % 24;
      var minutes = Math.floor( delta / ( 1000 * 60 ) ) % 60;
      var seconds = Math.floor( delta / ( 1000 ) ) % 60;

      var counter = $( '#counter' );
      counter.find( '.days .num' ).html( padTime( days.toString( ) ) );
      counter.find( '.hours .num' ).html( padTime( hours.toString( ) ) );
      counter.find( '.minutes .num' ).html( padTime( minutes.toString( ) ) );
      counter.find( '.seconds .num' ).html( padTime( seconds.toString( ) ) );
    }

    var slideshow = $( '.slideshow li' );
    var slideindex = $( '.slideshow-index li' );
    var slidetext = $( '.slideshow-text li' );
    var slideCurrentIndex = 0;
    var slideShowInterval;

    slideindex.each( function( n )
      {
        $( this ).data( 'index', n ).click( function( )
          {
            clearInterval( slideShowInterval );
            selectSlideShow( $( this ).data( 'index' ) );
          }
        );
      }
    );

    slideShowInterval = setInterval( slideShowNext, 5000 );

    function slideShowNext( )
    {
      slideCurrentIndex++;
      slideCurrentIndex = slideCurrentIndex >= slideshow.length ? 0 : slideCurrentIndex;
      selectSlideShow( slideCurrentIndex );
    }

    function selectSlideShow( index )
    {
      slideshow.each( function( n )
        {
          if( n != index )
            $( this ).fadeOut( 'slow' );
          else
            $( this ).fadeIn( 'slow' );
        }
      );
      slideindex.each( function( n )
        {
          if( n != index )
            $( this ).find( 'img' ).attr( 'src', 'css/img/slideshow.index.gif' );
          else
            $( this ).find( 'img' ).attr( 'src', 'css/img/slideshow.index.select.gif' );
        }
      );
      /*
      slidetext.each( function( n )
        {
          if( n != index )
            $( this ).fadeOut( 'slow' );
          else
            $( this ).fadeIn( 'slow' );
        }
      );
      */
    }
  }
);

function padTime( $str )
{
  if( $str.length >= 2 )
    return $str;
  return padTime( '0'+$str );
}


function initialize() {
  // West
  var myLatLng = new google.maps.LatLng( 40.1507604414267, -75.3424041528524 );

  var myOptions = {
    zoom: 16,
    center: myLatLng, 
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };

  var map = new google.maps.Map(document.getElementById('map-canvas'),
      myOptions);

  var marker = new google.maps.Marker({
        position: myLatLng, 
        map: map,
        title:"Einstein Montgomery"
    });
}

function loadScript() {
  var script = document.createElement('script');
  script.type = 'text/javascript';
  script.src = 'http://maps.googleapis.com/maps/api/js?sensor=false&' +
      'callback=initialize';
  document.body.appendChild(script);
}

