/*
 * sticky footer plugin for jquery
 */
;(function( $, window, document, undefined ){
(function(){
		var interval;
		jQuery.event.special.heightchange = {
		setup: function(){
			var self = this,
			$this = $(this),
			$originalHeight = $this.height();
			interval = setInterval(function(){
				if($originalHeight != $this.outerHeight()) {
						$originalHeight = $this.outerHeight();
					jQuery.event.handle.call(self, {type:'heightchange'});
				}
			},500);
		},
		teardown: function(){
			clearInterval(interval);
		}
	};
})();

$.stickyfooter = function (element, options) {
	var self = this;

	self.settings = {};

	var $footer = $(element),
		footer = element,
		$window = $(window);
		
		$.extend(self, {
		init : function () { 
			self.settings = $.extend({}, $.stickyfooter.defaults, options);
			$footer.parent().css({'width': '100%'}).parent().css({'width': '100%', 'height':$footer.height()});
			setFooterPosition();
			$window.bind('resize.stickyfooter', function () {
				setFooterPosition();
			});
			$('#content-area').bind('heightchange',function(){
				setFooterPosition();			
			});

		}
	});
	var setFooterPosition = function() {
		var $page = $('#content-area');
		var documentHeight,menuHeight,headingHeight,
		windowHeight = $window.height(),
		pageHeight = $page.outerHeight(),
		htmlMargin = parseInt($('html').css('margin-top'));
		
		if($('#menu-logo').length >0)
			menuHeight = $('#menu-logo').outerHeight();
		else
			menuHeight = 0;

		if($('#heading').length >0)
			headingHeight = $('#heading').outerHeight();
		else
			headingHeight = 0;

		if($.browser.msie && parseInt(jQuery.browser.version, 10) == 8 && document.documentMode == 8){
			documentHeight = document.documentElement.scrollHeight;
		}else{
			documentHeight = $(document).height();
		}
		if($footer.size()>0){
			var paddingbottom = parseInt($('#content-area>.wrap-center').css('paddingBottom').replace("px", ""));
			$footer.css({width: '100%',margin:'0 auto'});
			
			if ( documentHeight - windowHeight <= 0) {
				$footer.parent().css({position: 'fixed', left: '0px', bottom: 0});
				extra = windowHeight-$footer.height()-menuHeight-headingHeight-pageHeight-htmlMargin;
				console.log(extra);
				console.log(paddingbottom);
				$('#content-area>.wrap-center').css('paddingBottom',extra + paddingbottom);
			} else {
				if(paddingbottom > 0 && (documentHeight - windowHeight)< paddingbottom ){
					$('#content-area>.wrap-center').css('paddingBottom',paddingbottom - documentHeight + windowHeight);
				}else{
					$footer.parent().stop().css({position: 'relative', bottom: 'auto'});
					$('#content-area>.wrap-center').css('paddingBottom',0);
				}
			}
		}else{
			if ( document_height - window_height <= 0) {
				extra = window_height-menuHeight-headingHeight-pageHeight-htmlMargin;
				$('#content-area>.wrap-center').css('paddingBottom',extra + paddingbottom);
			} else {
				$('#content-area>.wrap-center').stop().css('paddingBottom',0);
			}
		}
	};


	self.init();
};

$.stickyfooter.defaults = {

};

$.fn.stickyfooter = function (options) {
	return this.each(function () {
		$.stickyfooter(this, options);
	});
};

})( jQuery, window , document );

jQuery(window).load(function($) {
	if(jQuery('#boxstyle-wrap').length==0)
		jQuery('#footer').stickyfooter();
});

