String.prototype.capitalize = function(){
   return this.replace( /(^|\s)([a-z])/g , function(m,p1,p2){ return p1+p2.toUpperCase(); } );
};

function cssSpriteHover (btn_id, bg_x, delta_y, orig_y) {
   // var pageNameCamelCase = pageName.capitalize();
    //if (btn_id !== "nav"+pageNameCamelCase) { // if the current page is the btn_id, don't offer hover
		if (!orig_y) {orig_y = 0;}
		$("#"+btn_id).unbind("hover");
		$("#"+btn_id).hover(
			function() {
				$(this).css({backgroundPosition: bg_x+"px "+delta_y+"px"});
			},
			function () {
				$(this).css({backgroundPosition: bg_x+"px "+orig_y+"px"});
			}
		);
	//}
	
} // end function cssSpriteHover()

function hoverIntentOver () {
	$(this).css({backgroundPosition: "0px -33px"});

} // end function hoverIntentOver()

function hoverIntentOut () {
	$(this).removeClass("navBtnHover");
	$(this).css({backgroundPosition: "0px 0px"});
} // end function hoverIntentOut()

/* !START ONREADY FUNCTION */
$(function() {
	
    /* !HEADER BEHAVIORS */
    cssSpriteHover("navHome", "-6", "-33");
    cssSpriteHover("navProducts", "0", "-33");
    cssSpriteHover("navWhatWeDo", "0", "-33");
    cssSpriteHover("navWhoWeAre", "0", "-33");
    cssSpriteHover("navPress", "-3", "-33");
        
    cssSpriteHover("productsHome", "0", "-204");
    //cssSpriteHover("productsProducts", "-40", "-204");
    cssSpriteHover("productsWhatWeDo", "0", "-284", "-80");
    cssSpriteHover("productsWhoWeAre", "0", "-324", "-120");
    cssSpriteHover("productsPress", "0", "-364", "-160");

	var hoverIntentConfig = {    
	     over: hoverIntentOver, // function = onMouseOver callback (REQUIRED)    
	     timeout: 100, // number = milliseconds delay before onMouseOut    
	     out: hoverIntentOut, // function = onMouseOut callback (REQUIRED)    
		interval:50
	};

/* 	$(".navButton").hoverIntent(hoverIntentConfig); */
	$(".navButton").click( function () {
		var page = $(this).attr("name");
		window.location = "?page="+page;
	});
	
	$(".productsNavBtn").click( function () {
		var page = $(this).attr("name");
		if ( page != "products" ){
			window.location = "?page="+page;
		}
	});
	

    
	$("#masthead").click( function () {
		window.location = "?page=home";
	});
	$("#iPadClickable").click( function () {
		window.location = "?page=products";
	});
	$("#productsLogoTop").click( function () {
		window.location = "?page=home";
	});

	$("#productsAppStoreFixed").click( function () {
		window.location = "http://itunes.apple.com/us/app/improvox/id382109101?mt=8";
	});
	$("#productsFacebook").click( function () {
		window.location = "http://www.facebook.com/pages/ImproVox/128356933874865";
	});
	$("#productsAppStoreBottom").click( function () {
		window.location = "http://itunes.apple.com/us/app/improvox/id382109101?mt=8";
	});


	
	if (BrowserDetect.browser !== "Explorer"){
		// Script RefURL: http://www.josephgiuffrida.com/jquery/jquery-fixed-sidebar-1/
		var theLoc = $('#improvoxIpad').position().top;
		if ($('#productsAppStoreFixed').length>0) {
			var productsLoc = $('#productsAppStoreFixed').position().top;
		}
		$(window).scroll(function() {
			if(theLoc >= $(document).scrollTop()) {
				if($('#improvoxIpad').hasClass('fixed')) {
					$('#improvoxIpad').removeClass('fixed');
					$('#iPadClickable').removeClass('fixed');
				}
			} else { 
				if(!$('#improvoxIpad').hasClass('fixed')) {
					$('#improvoxIpad').addClass('fixed');
					$('#iPadClickable').addClass('fixed');
				}
			}
			
			if(productsLoc >= $(document).scrollTop()) {
				if($('#productsAppStoreFixed').hasClass('fixedAppStore')) {
					$('#productsAppStoreFixed').removeClass('fixedAppStore');
					$("#improvoxApplogo").fadeOut("fast");
				}
			} else { 
				if(!$('#productsAppStoreFixed').hasClass('fixedAppStore')) {
					$('#productsAppStoreFixed').addClass('fixedAppStore');
					$("#improvoxApplogo").fadeIn();
				}
			}
		}); // end fixedSide.js
	} // end test for windows machines
	
	var pageNameCamelCase = pageName.capitalize();
	$("#nav"+pageNameCamelCase).unbind().addClass("nav"+pageNameCamelCase+"Clicked");
	
}); // ####### end onready function #######

