var extraHeight = 158;
var supportsOrientationChange = "onorientationchange" in window,
orientationEvent = supportsOrientationChange ? "orientationchange" : "onresize";

if (window.addEventListener) {

	window.addEventListener(orientationEvent, function() {

		updateOrientation();

	}, false);

} else if(window.attachEvent) {

    window.attachEvent(orientationEvent, function() {

	    updateOrientation();

    }, false);
}

function updateOrientation() {


	
	switch (window.orientation) {

		case 0:
			extraHeight= 158;
			isiScroll(extraHeight);
			break;

		case -90:
			extraHeight= 100;
			isiScroll(extraHeight);
			break;

		case 90:
			extraHeight= 100;
			isiScroll(extraHeight);
			break;

		case 180:
			extraHeight= 100;
			isiScroll(extraHeight);
			break;

	}



}


function isiScroll(extraHeight) {

	var viewHeight = $(window).height();
	var docHeight = $(document).height();
	var isiHeight = $("#footer-div").height();
	var offsetValue, scrollPosition = $(window).scrollTop();
	
	$("#footer-inner-dynamic").css({'bottom':'45px'}); // changed from 200px for ipad fix - DW
	
    $("#footer-div").css("top", (scrollPosition + viewHeight)-isiHeight+'px');
    
	$(window).scroll(function() {

	    offsetValue = $("#footer-div").offset();
		scrollPosition = $(window).scrollTop();


		//alert(offsetValue.top);
		if (scrollPosition == 0) {

			var totalHeight = viewHeight - isiHeight;
			$("#footer-div").css("top", totalHeight + 'px');


		} else {

			if (navigator.userAgent.match(/(iphone|ipod|android|blackberry)/i)){
		
				var totalHeight = (scrollPosition + viewHeight)+extraHeight;
				
			} else if(navigator.userAgent.match(/(ipad)/i)) {

				var totalHeight = (scrollPosition + viewHeight)-isiHeight;
			}

			//if (totalHeight <= docHeight) {
			
				//alert(totalHeight);
				$("#footer-div").css("top", totalHeight + 'px');
				
			//}
			//else {
				
			//	$("#footer-div").css("top", docHeight-isiHeight+ 'px');
			//}

		}
	});

}
