/*
 *  The Footer Plugin
 *  
 * 	Copyright© 2010 Icewares
 *  
 *  Licensed under the CPL (http://www.icewares.com.br/footer/licence.html)
 */
/*
 * 	The Footer Plugin
 *  version 1.0
 *  Created by Stéfano Stypulkowski @ Icewares
 *   
 *  Require jQuery 1.4 http://www.icewares.com.br/footer/
 *  
 *  For instructions see: 
 */

var id = new String();

var height = new Number();

var footerHeight = new Number();

var styleAbsolute = { "position" : "absolute" , "bottom" : "0" , "z-index" : "200" };

var styleRelative = { "position" : "relative" , "bottom" : "auto" };

var config;

var footerObject;

var browserAdjust = 0;

(function($){
	
	$.fn.footer = function(options) {
		
		var defaults = {
			"monitorTimeOut" : 200
		};
		
		config = $.extend(defaults, options);
		
		if ( config.monitorTimeOut < 1 ){
			config.monitorTimeOut = 1;
		}
		
		if ( $(this).attr("id") == "" ){
			$(this).attr("id", "footerAutoGeneratedId");
		}
		
		id = "#" + $(this).attr("id");

		height = $(document).height();
		
		footerHeight = $(id).height();
		
		if ( $.browser.msie ){
			browserAdjust = 4;
		}
		
		setPosition();
		
		monitor();
	}
})(jQuery);


function monitor(){
	
	if ( height != $(document).height() ){
		
		setPosition();
		
		height = $(document).height();
	}
	
	if ( footerHeight != $(id).height() ){
		
		setFsmHeight();
		
		footerHeight = $(id).height();
	}
	
	setTimeout('monitor()', config.monitorTimeOut);
}

function setPosition(){
	
	if ( ($(document).height() - browserAdjust) > $(window).height() ){
	
		$("#footerSpaceManager").remove();
		
		$(id).css(styleRelative);
		
	}else{
		
		if ( $("#footerSpaceManager").size() == 0 ){
			
			$("body").append("<div id='footerSpaceManager' style='height: " + $(id).height() + "px;'></div>")
		}
		
		$(id).css(styleAbsolute);
	}
}

function setFsmHeight(){

	if ( $("#footerSpaceManager").size() > 0 && $(id).height() != $("#footerSpaceManager").height() ){
		
		$("#footerSpaceManager").height($(id).height());
	}
}

