$(document).ready(function() {
	//all hover and click logic for buttons
	$(".fg-button:not(.ui-state-disabled)")
	.hover(
		function(){ 
			$(this).addClass("ui-state-hover"); 
		},
		function(){ 
			$(this).removeClass("ui-state-hover"); 
		}
	)
	.mousedown(function(){
			$(this).parents('.fg-buttonset-single:first').find(".fg-button.ui-state-active").removeClass("ui-state-active");
			if( $(this).is('.ui-state-active.fg-button-toggleable, .fg-buttonset-multi .ui-state-active') ){ $(this).removeClass("ui-state-active"); }
			else { $(this).addClass("ui-state-active"); }	
	})
	.mouseup(function(){
		if(! $(this).is('.fg-button-toggleable, .fg-buttonset-single .fg-button,  .fg-buttonset-multi .fg-button') ){
			$(this).removeClass("ui-state-active");
		}
	});
	$("#myefw-link")
		.hover(
			function(){ 
				$("#myefw-login").fadeIn();
				$("#efwn-login").fadeOut(); 
			}
	)
		.click(
			function(){ 
				$("#myefw-login").fadeIn(); 
				return false;
			}
	);
	$(".loginCls").click(
		function(){ 
			$("#myefw-login").fadeOut(); 
			return false;
		}
	);
	$("#efwn-link")
		.hover(
			function(){ 
				$("#efwn-login").fadeIn(); 
				$("#myefw-login").fadeOut();
			}
	)
		.click(
			function(){ 
				//$("#efwn-login").fadeIn();
				//return false;
			}
	);
	$(".loginCls").click(
		function(){ 
			$("#efwn-login").fadeOut();
			return false;
		}
	);

    $("#submitQ, #submitRTQ").css("display", "none");
    $("#submitQImg, #submitRTQImg").css("display", "inline");
	$("label.overlabel").overlabel();
	
	$("#advancedLnk").click(function () {
		$("#advancedFrm").show("slide", { direction: "up" }, 250);
		return false;
	});
	$("#advancedCls").click(function () {
		$("#advancedFrm").hide("slide", { direction: "up" }, 250);
		return false;
	});
	$("#advancedFrm input").click(function () {
		 $("#rtqLbl").html($("label[for='" + $(this).attr("id") + "']").html()); 
	});
	
	//Striped tables
	$("table.resource1 tbody tr:odd").addClass("oddRow");
	
	//Setting pg height
	var hQ = $("#quickMenu").height();
	var hS = $("#secondary-nav").height();
	var hB = $("#body").height();
	if (hS > hB) { hB = hS; }
	if (hQ > hB) { hB = hQ; }
	$("#body").height(hB+20);
	
	$('#northAmerica-video').dialog({
		autoOpen: false,
		width: 400,
		modal: true,
		buttons: {
			"Close": function() { 
				$(this).dialog("close"); 
			}
		}
	});
	$('#northAmerica-link').click(function(){
		$('#northAmerica-video').dialog('open');
		return false;
	});
	
	$(".video").click(function(e) {
		e.preventDefault();
		var targetVideo = $(this).attr("id");
		$('body').append('<div id="video-dialog"></div>');
		$('#video-dialog').html('<img id="loading" style="margin-left:auto; margin-right:auto" src="img/loading.gif" alt="Loading..." />');
		showLoading();
		$('#video-dialog').load(targetVideo+'.html', hideLoading);
	
		$("#video-dialog").dialog({
			bgiframe: true,
			resizable: false,
			autoOpen: false,
			title: 'Video Content',
			modal: true,
			width: 400,
			buttons : {
				"Close" : function() {
					$(this).dialog("destroy");
				}
			}
		});
		$("#video-dialog").dialog("open");
	});
	
	$("#locationsLink").click(function(e) {
		e.preventDefault();
		var targetVideo = $(this).attr("id");
		$('body').append('<div id="locationsMap"></div>');
		$('#locationsMap').html('<img src="img/map-lg.png" alt="Locations" />');
	
		$("#locationsMap").dialog({
			bgiframe: true,
			resizable: false,
			autoOpen: false,
			title: 'Locations Map',
			modal: true,
			width: 1000,
			position: 'top',
			buttons : {
				"Close" : function() {
					$(this).dialog("destroy");
				}
			}
		});
		$("#locationsMap").dialog("open");
	});
	
	//show loading bar
	function showLoading(){
		$('#loading')
			.css({visibility:"visible"})
			.css({opacity:"1"})
			.css({display:"block"})
		;
	}
	//hide loading bar
	function hideLoading(){
		$('#loading').fadeTo(1000, 0);
	};
	
});

function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
	var match = regex.exec(clearString.substr(x));
	if (match != null && match.length > 1 && match[1] != '') {
		output += match[1];
	  x += match[1].length;
	} else {
	  if (clearString[x] == ' ')
		output += '+';
	  else {
		var charCode = clearString.charCodeAt(x);
		var hexVal = charCode.toString(16);
		output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
	  }
	  x++;
	}
  }
  return output;
}

function getHTTPObject() {
	if (typeof XMLHttpRequest != 'undefined') {
		return new XMLHttpRequest();
	}
	try {
		return new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			return new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
		}
	} return false;
}

function formSubmit() {
	var loginURL = '/WebTrakWT/signin.aspx?' + 
		'txtUserID=' + URLEncode(document.getElementById('txtUserID').value) + '&' + 
		'txtPassword=' + URLEncode(document.getElementById('txtPassword').value) + '&' + 
		'btnSubmit=Submit&' + 
		'__EVENTTARGET=';
	var http = getHTTPObject();
	var ajaxResponse = '';
	http.open("GET", loginURL, true);
	http.onreadystatechange = function() {
		if (http.readyState == 4) {
			ajaxResponse = http.responseText; // normally you would use this for something, but all we care about is making the request and getting the response
		}
	}
	http.send(null);
}