// Hume RICS JavaScript Document

$(document).ready( function() {
	
	// Load cool fonts, but only if font smoothing is on.
	TypeHelpers.insertClasses();
	/*
	var isFontSmoothingOn = TypeHelpers.hasSmoothing();
	if (isFontSmoothingOn == true) {
		$('head').append('<link href="css/fonts/stylesheet.css" rel="stylesheet" media="screen" type="text/css" />');
	}
	*/
	
	$('#toggle_directory').click( function (ev) {
		ev.preventDefault();
		$('#directory_launcher ul').animate({opacity: 'toggle', height: 'toggle'}, 300);
	});
	
	// Attach click events to slideshow images
	$('#slideshow img').each( function() {
		if ($(this).attr('rel')) {
			$(this).click( function() { window.location = $(this).attr('rel'); } );
		}
	});
	// Animate slideshow. 
	$('#slideshow img:gt(0)').hide();
	setInterval(function(){
		$('#slideshow :first-child').fadeOut(2000).next('img').fadeIn(2000).end().appendTo('#slideshow');
	}, 8000);

	
	// Drop-down menus
	$('#header ul#nav, #header ul#secondary_nav').superfish({ 
		delay:       1000,
		animation:   {opacity:'show',height:'show'},
		speed:       'fast',
		autoArrows:  false,
		dropShadows: true
	});
	
	$('#cntnt01searchinput').focus( function(ev) {
		if (this.value == this.defaultValue) {
			this.value = '';
			$(this).removeClass('default');
		}
	});
	$('#cntnt01searchinput').blur( function(ev) {
		if (this.value == '') {
			this.value = this.defaultValue;
			$(this).addClass('default');
		}
	});

});

var TypeHelpers = new function(){

   // I use me instead of this.  For reasons why, please read:
   // http://w3future.com/html/stories/callbacks.xml
   var me = this;

   me.hasSmoothing = function(){

      // IE has screen.fontSmoothingEnabled - sweet!
      if (typeof(screen.fontSmoothingEnabled) != "undefined") {
         return screen.fontSmoothingEnabled;
      } else {

         try {

            // Create a 35x35 Canvas block.
            var canvasNode = document.createElement('canvas');
            canvasNode.width = "35";
            canvasNode.height = "35"

            // We must put this node into the body, otherwise
            // Safari Windows does not report correctly.
            canvasNode.style.display = 'none';
            document.body.appendChild(canvasNode);
            var ctx = canvasNode.getContext('2d');

            // draw a black letter 'O', 32px Arial.
            ctx.textBaseline = "top";
            ctx.font = "32px Arial";
            ctx.fillStyle = "black";
            ctx.strokeStyle = "black";

            ctx.fillText("O", 0, 0);

            // start at (8,1) and search the canvas from left to right,
            // top to bottom to see if we can find a non-black pixel.  If
            // so we return true.
            for (var j = 8; j <= 32; j++) {
               for (var i = 1; i <= 32; i++) {

                  var imageData = ctx.getImageData(i, j, 1, 1).data;
                  var alpha = imageData[3];

                  if (alpha != 255 && alpha != 0) {
                     return true; // font-smoothing must be on.
                  }
               }

            }

            // didn't find any non-black pixels - return false.
            return false;
         }
         catch (ex) {
            // Something went wrong (for example, Opera cannot use the
            // canvas fillText() method.  Return null (unknown).
            return null;
         }
      }
   }

   me.insertClasses = function(){
      var result = me.hasSmoothing();
      var htmlNode = document.getElementsByTagName('html')[0];
      if (result == true) {
         htmlNode.className += " hasFontSmoothing-true";
      } else if (result == false) {
            htmlNode.className += " hasFontSmoothing-false";
      } else { // result == null
            htmlNode.className += " hasFontSmoothing-unknown";
      }
   }

}


