var _IMAGEPATH = "/images/";
var SHAREITACTIVE = false;
var _SHAREIT = ".bottomnav .popup";
var t1; // timeouts
var t2;
var t3;
var _PAGETITLE = "Stride&reg;";
var _CURRENTPAGE = "other";
var _URL = "";
var _subNavID = ".subNav";
var _leaderboardClass = ".leaderboard";
var _interstitial = "#interstitialok";
var _zapataur = "#zapataurFlash";
var _nominateFormID = "#nominate_form";

var _leaderboardEnabled = false;

var limitMax = 500; //character limit for nomination contact info and why this arcade.
var limitField = "#limit";

$(document).ready(function() {

	_PAGETITLE = document.title;
	_URL = window.location.href;
	
	bindNavRollovers();
	bindShareItRollovers();
	
	if( $("#vault_idea").length > 0 ) {
		initCharCounter();
	}
	if( $(_interstitial).length > 0 ) {
		bindInterstitialOkButton();
	}
	if( $(_subNavID).length > 0 ) {
		_URL = window.location.toString();
		setCurrentSubnav();
	}
	
});


//binds topnav roll over states
function bindNavRollovers() {
	var objClass = "";
	
	$(".click_to_enter_btn").hover(
		function() {
			$(".click_to_enter_btn span").css("background", "url('/images/click_to_enter_btn_on.png')");
		},
		function() {
			$(".click_to_enter_btn span").css("background", "url('/images/click_to_enter_btn_off.png')");
		}
	);
	
	$(".nav_thevault a").hover(
		function() {
			$(".nav_thevault a span").css("background", "url('/images/thevault_btn_on.png')");
		},
		function() {
			if(
			   _URL.indexOf(".com/agecheck") >= 0 || 
			   _URL.indexOf(".com/legal") >= 0  || 
			   _URL.indexOf(".com/privacy") >= 0  || 
			   _URL.indexOf(".com/staf") >= 0  || 
			   _URL.indexOf(".com/share") >= 0  || 
			   _URL.indexOf(".com/reg") >= 0  ||
			   _URL.indexOf(".com/interstitial") >= 0 || 
			   _URL.indexOf(".com/enter") >= 0 || 
			   _URL.indexOf(".com/winnersgallery") >= 0 || 
			   _URL.indexOf(".com/rules") >= 0
			   ) {
				$(".nav_thevault a span").css("background", "url('/images/thevault_btn_off.png')");
			}
		}
	);
	$(".nav_enter a").hover(
		function() {
			$(".nav_enter a span").css("background", "url('/images/enter_btn_on.png')");
		},
		function() {
			if(_URL.indexOf("/enter") == -1) {
				$(".nav_enter a span").css("background", "url('/images/enter_btn_off.png')");
			}
		}
	);
	$(".nav_winnersgallery a").hover(
		function() {
			$(".nav_winnersgallery a span").css("background", "url('/images/winnersgallery_btn_on.png')");
		},
		function() {
			if(_URL.indexOf("/winnersgallery") == -1) {
				$(".nav_winnersgallery a span").css("background", "url('/images/winnersgallery_btn_off.png')");
			}
		}
	);
	$(".nav_rules a").hover(
		function() {
			$(".nav_rules a span").css("background", "url('/images/rules_btn_on.png')");
		},
		function() {
			if( _URL.indexOf("/rules") == -1 ) {
				$(".nav_rules a span").css("background", "url('/images/rules_btn_off.png')");
			}
		}
	);
	$("input[name=submit_btn]").hover(
		function() {
			$(this).attr("src",_IMAGEPATH + "submit_btn_on.gif");
		},
		function() {
			$(this).attr("src",_IMAGEPATH + "submit_btn_off.gif");
		}
	);
	$(".cancel_btn").hover(
		function() {
			$(".cancel_btn img").attr("src",_IMAGEPATH + "cancel_btn_on.gif");
		},
		function() {
			$(".cancel_btn img").attr("src",_IMAGEPATH + "cancel_btn_off.gif");
		}
	);
	$(".ok_btn").hover(
		function() {
			$(".ok_btn img").attr("src",_IMAGEPATH + "ok_btn_on.gif");
		},
		function() {
			$(".ok_btn img").attr("src",_IMAGEPATH + "ok_btn_off.gif");
		}
	);
}

//set the initial sub navigation page
function setCurrentSubnav() {
	
	if(_URL.indexOf(".com/winnersgallery") >= 0) {
		$(".nav_winnersgallery a span").css("background","url('/images/winnersgallery_btn_on.png')");
		$(".nav_winnersgallery a").click(function(){ return false; });
	} else if (_URL.indexOf(".com/enter") >= 0) {
		$(".nav_enter a span").css("background", "url('/images/enter_btn_on.png')");
		$(".nav_enter a").click(function(){ return false; });
	} else if (_URL.indexOf(".com/rules") >= 0) {
		$(".nav_rules a span").css("background", "url('/images/rules_btn_on.png')");
		$(".nav_rules a").click(function(){ return false; });
	} else if (
			   _URL.indexOf(".com/agecheck") == -1 && 
			   _URL.indexOf(".com/legal") == -1 && 
			   _URL.indexOf(".com/privacy") == -1 && 
			   _URL.indexOf(".com/staf") == -1 && 
			   _URL.indexOf(".com/share") == -1 && 
			   _URL.indexOf(".com/reg") == -1 && 
			   _URL.indexOf(".com/interstitial") == -1
							) {
		$(".nav_thevault a span").css("background", "url('/images/thevault_btn_on.png')");
		$(".nav_thevault a").click(function(){ return false; });
	}
}

// binds the Share This rollover state
function bindShareItRollovers() {
	var target = ".bottomnav .share";
	var panelTarget = ".bottomnav .popup";
	$(target).click(function(e) {
		return false;
	});
	
	// ON
	$(target).mouseover(function(e) {
		SHAREITACTIVE = true;
		t1 = setTimeout(function() {
			if(SHAREITACTIVE) {
				$(_SHAREIT).css('visibility','visible');
				clearTimeout(t1);
			}
		},100);
	});
	// OFF
	$(target).mouseout(function(e) {
		SHAREITACTIVE = false;
		t2 = setTimeout(function() {
			if(!SHAREITACTIVE) {
				$(_SHAREIT).css('visibility','hidden');
				clearTimeout(t2);
			}
		},500);
	});
	
	$(panelTarget).mouseover(function(e) {
		SHAREITACTIVE = true;
	});
	$(panelTarget).mouseout(function(e) {
		SHAREITACTIVE = false;
		t3 = setTimeout(function() {
			if(!SHAREITACTIVE) {
				$(_SHAREIT).css('visibility','hidden');
			}
		},500);
	});
}

// bind the OK button on the interstitial page
function bindInterstitialOkButton() {
	
	$("#interstitialok").click(function(e) {
		history.back();
	});
}

function initCharCounter() {
	var _txt2 = "";
	var _count2 = 0;
	if( $("#vault_idea").html().length > 0 ) {
		_txt2 = $("#vault_idea").html();
		_count2 = _txt2.length;
		$("#idea_char_count").html( limitMax - _count2 + " characters remaining." );
	}
	$("#vault_idea").keyup(function() {
		_txt2 = $(this).val();
		_count2 = _txt2.length;
		displayRemainingChars(_count2, "#idea_char_count");
	});
	$("#vault_idea").keydown(function() {
		_txt2 = $(this).val();
		_count2 = _txt2.length;
		displayRemainingChars(_count2, "#idea_char_count");
	});
	$("#vault_idea").keypress(function() {
		_txt2 = $(this).val();
		_count2 = _txt2.length;
		displayRemainingChars(_count2, "#idea_char_count");
	});
}

// displays the remaining characters
function displayRemainingChars(n,f) {
	
	var _r = limitMax-n;
	var _m = "";
	if ( _r <= 0 ) { _r = 0 }
	if ( _r == 1 ) {
		_m = " character remaining.";
	} else {
		_m = " characters remaining.";
	}
	$(f).html( _r + _m );
}

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)
