$( function() {
    // clock
    var updateClock = function ( ) {
      var currentTime = new Date ( ),
            currentHours = currentTime.getHours ( ),
            currentMinutes = currentTime.getMinutes ( );
            //currentSeconds = currentTime.getSeconds ( );

      // Pad the minutes and seconds with leading zeros, if required
      currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
      //currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;

      // Choose either "AM" or "PM" as appropriate
      var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";

      // Convert the hours component to 12-hour format if needed
      currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;

      // Convert an hours component of "0" to "12"
      currentHours = ( currentHours == 0 ) ? 12 : currentHours;

      // Compose the string for display
      //var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;
      var currentTimeString = currentHours + ":" + currentMinutes  + " " + timeOfDay;

      // Update the time display
      $('div.theme-control div.themec span:first').text(currentTimeString);
    };
    
    setInterval(function() { updateClock(); }, 1000 )

	
    // theming
	var changeTheme = function(isNight) {
			var _src = (isNight) ? 'css/night.css' : 'css/day.css';
			$('link').eq(1).attr('href', _src);
		};
	
	var _currentTime = (new Date()).getHours(),
		_firstA = $('div.theme-control div.themec em a:first');
        _secondA = $('div.theme-control div.themec em a:last');
	$('div.theme-control div.themec em a').removeClass('active');
	// auto night on 6pm
	if ( _currentTime >=  18) {
		changeTheme(true);
		_secondA.addClass('active');
	} else  {
		changeTheme(false);
		_firstA.addClass('active');
	}
    
    var _pos = _firstA.position(),
		_bubble = $('div.theme-control div.themec strong.bubble:first'),
        _bubble2 = _bubble.clone().appendTo('div.theme-control div.themec em').text('night'),
        _pos2 = _secondA.position();
        
	 _bubble.hide().css({
			top : _pos.top - 23, left: _pos.left - 6
	}); 
    
    _bubble2.hide().css({
			top : _pos2.top - 23, left: _pos2.left - 6
	});
	
	$('div.theme-control div.themec em a').each( function(i) {
		$(this).click( function() {
				var isNight = (i==0) ? false : true;
				changeTheme(isNight);
				$(this).parent().find('a.active').removeClass('active');
				$(this).addClass('active');
				
				return false;
			});
            
        $(this).hover(
            function() {
                if ( i==0 )
                    _bubble.show();
                else
                    _bubble2.show();
            },
            function() {
                if ( i==0 )
                    _bubble.hide();
                else
                    _bubble2.hide();
            }
        );
	});
	
	// scroll to
	$('ul.navigation li a').click(function() {
		$(window).scrollTo($(this).attr('href'), 800);
		return false;
	});
	
	// fixed position is not working in IE6 - must fix this bug
	if ( !$.support.cssFloat ) {
		if ( $.browser.version == '6.0' ) {
			$("div.theme-control").fixedPosition();
		}
	}
})
