var containerObj;

(function($) {
	$.fn.ezBgResize = function(options) {
		// First position object
		containerObj = this;
		
		containerObj.css("visibility","hidden");
		
		$(window).load(function() {
			resizeImage();
			maximizeMenu();
		});


        $(window).bind("resize",function() {
            resizeImage();
			maximizeMenu();
        });
	};
	
	function resizeImage() {
		containerObj.css({
			"position":"fixed",
			"top":"0px",
			"left":"0px",
			"z-index":"10",
			"overflow":"hidden",
			"width":getWindowWidth() + "px",
			"height":getWindowHeight() + "px"
		});
		
	
		var iw = containerObj.children('img').width();
		var ih = containerObj.children('img').height();

        var windowHeight = getWindowHeight();
        var windowWidth  = getWindowWidth();

		if (windowWidth > windowHeight) {
			if (iw > ih) {
				var fRatio = iw/ih;
				containerObj.children('img').css("width",windowWidth + "px");
				containerObj.children('img').css("height",Math.round(windowWidth * (1/fRatio)));

				var newIh = Math.round(windowWidth * (1/fRatio));

				if(newIh < windowHeight) {
					var fRatio = ih/iw;
					containerObj.children('img').css("height",windowHeight);
					containerObj.children('img').css("width",Math.round(windowHeight * (1/fRatio)));
				}
			} else {
				if(iw < windowWidth) {
					var fRatio = ih/iw;
					containerObj.children('img').css("height",windowHeight);
					containerObj.children('img').css("width",Math.round(windowHeight * (1/fRatio)));
				} else {
					var fRatio = iw/ih;
					containerObj.children('img').css("height",Math.round(windowWidth * (1/fRatio)));
					containerObj.children('img').css("width",windowWidth);
				}
			}
		} else {
			if(iw < windowWidth) {
				var fRatio = ih/iw;
				containerObj.children('img').css("height",windowHeight);
				containerObj.children('img').css("width",Math.round(windowHeight * (1/fRatio)));
			} else {
				var fRatio = iw/ih;
				containerObj.children('img').css("height",Math.round(windowWidth * (1/fRatio)));
				containerObj.children('img').css("width",windowWidth);
			}
		}
		containerObj.children('img').css("visibility","visible");
	}

	
	function getWindowHeight() {
        return $('body').height();
	}
	
	function getWindowWidth() {
		return $('body').width();
	}

	// Scrolling for buttons to go to top or bottom
	$(document).ready(function() {
		if($.browser.msie && $.browser.version=='6.0') {
/*
			$(document).remove('#backgroundImage');
			var bgImg = $("#backgroundImage img").attr('src');
			$('body').css('background','url('+bgImg+')');
*/
		}
		else {
			$('#backgroundImage').ezBgResize();
			$('#backgroundImage').fadeIn();
		}
	});

	jQuery(window).scroll(function() {
		if(jQuery(window).scrollTop() > 285) {
			var _top = jQuery(window).scrollTop()+20;
		} else {
			var _top = 305;
		}
		jQuery('#navigationButtons').stop();
		jQuery('#navigationButtons').animate({
			top: _top
		}, 250, 'easeOutCubic');
	});
})(jQuery);

function maximizeMenu() {
	jQuery('#startNav ul ul').each(function(index) {
		var ul = jQuery(this);
		var _padding = _left = this.parentNode.offsetLeft;
		_left = _left * -1;
		var a = ul.find('a');
		ul.css({
			left: _left,
			width: jQuery('body').width()
		});
		a.css({
			paddingLeft: _padding
		});
	});
}
