//halls
$(document).ready(function(){
  $('.fan_form').submit(function(){
    //pause the cycling billboard
    if($('#billboard').length){
      $('#billboard').cycle('pause');
    }
    var f = $(this);
    var z = $(this).children('#zip');
    z.val(z.val().replace(/\s/,'').replace(/^(...)/,'$1 '));
    $('#fan_result').html("<img src='images/movewait.gif' />");
    $('#fan_overlay').show();
    backgroundInit();
    $.ajaxSetup({
    	url:f.attr('action'),
    	global:false,
    	type: "GET"
    });
    
    pageTracker._trackPageview('/flu_utility');
    
  	$.ajax({
  		data:f.serialize(), 
  		success: function(data){
  		  pageTracker._trackPageview('/flu_utility/success');
    		var d = data.split('|');
    		var level = d[2];
    		var descr;
    		if(d[2]!=undefined){
    	  	switch(level.replace(/^ */,"").replace(/ *$/,"")){
      		  case "Low" :
      		    descr = "Less than or equal to 5% of population affected";
      		    break;
      		  case "Moderate":
      		    descr = "Greater than 5% to less than 10% of population affected";
      		    break;
      		  case "High":
      		    descr = "Greater than or equal to 10% to less than 15% of population affected";
      		    break;
      		  case "Very High":
      		    descr = "Greater than or equal to 15% of population affected";
      		    break;
      		  default:
      		    descr = level;
      		    break;
      		}
      		//$('#fan_result').html('<h2 class="fan_h2">The current cold and flu level in<br/> <span class="city">' + d[0] + '</span>on ' + d[1] + ' is:</h2><h1>' + d[2] + '</h1><p class="fan_description">'+ descr +'</p>');
      		$('#fan_result').html('<h2 class="fan_h2">The current cold and flu level in your region <br/>on ' + d[1] + ' is:</h2><h1>' + d[2] + '</h1><p class="fan_description">'+ descr +'</p>');
      	} else {
      	  $('#fan_result').html('<h2 class="fan_h2">Please enter a valid postal code</h2>');
      	}
     	},
     	error: function(data){
     	  pageTracker._trackPageview('/flu_utility/error');
     		$('#fan_result').html('<h2 class="fan_h2">Please enter a valid postal code</h2>');
     	}
    });
    return false;
  });
  
  $('#overlay').click(function(){
    $('#fan_overlay').hide();
  });
});

function backgroundInit() {

  var xScroll, yScroll;
  if (window.innerHeight && window.scrollMaxY) {
    xScroll = window.innerWidth + window.scrollMaxX;  
    yScroll = window.innerHeight + window.scrollMaxY;
  } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
    xScroll = document.body.scrollWidth;
    yScroll = document.body.scrollHeight;
  } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
    xScroll = document.body.offsetWidth;
    yScroll = document.body.offsetHeight;
  }

  var windowWidth, windowHeight;

  if (self.innerHeight) { // all except Explorer
    if(document.documentElement.clientWidth){
      windowWidth = document.documentElement.clientWidth;
    } else {
      windowWidth = self.innerWidth;
    }
    windowHeight = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
  } else if (document.body) { // other Explorers
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
  }

    // for small pages with total height less then height of the viewport
  if(yScroll < windowHeight){
    pageHeight = windowHeight;
  } else {
    pageHeight = yScroll;
  }
  // for small pages with total width less then width of the viewport
  if(xScroll < windowWidth){
    pageWidth = xScroll;
  } else {
    pageWidth = windowWidth;
  }

  var backgroundObj = document.getElementById("overlay");
  backgroundObj.style.height = pageHeight + "px";
}