/*********************************************
 * General Javascript variables and functions.
 *********************************************/
 
/*********************************************
 * TODO
 * The way z-index (which window is on top) for
 * the help windows is handled is not the best.
 * It will definitely cause problems if we ever
 * bring more "floaty" windows into prosite2.
 * The best would be to build a small windowing
 * framework that any float-window would
 * inherit. When the window is created, opened,
 * or clicked (or focused) it could test to see
 * if it has the highest z-index. If it doesn't
 * it would assign itself that + 1. (That's
 * essentially what the help windows do now.
 *********************************************/

var toggleTime = 500;	// .5 seconds
var waitTimeout = 5000;	// 5 seconds
var tmpButtonDisable = 1000;
var highestZIndexOnPage = 2;

$(document).ready(function() {

	/**
	 * Give focused fields a little color.
	 * Note: When IE catches up with the rest of the browser world, go to
	 * styles.css and change .focus to input:focus. Slick, eh?
	 */
	$("input, textarea, select").focus(function() {
		$(this).addClass("focus");
	});
	$("input, textarea, select").blur(function() {
		$(this).removeClass("focus");
	});

	// Table list style.
	$("table.list tbody tr:even td").addClass("listEven");
	$("table.list tbody tr:odd td").addClass("listOdd");

	/*
	$("table.list tbody tr").hoverIntent({
		interval: 200,
		over: function() {
			//$(this).find("td").addClass("listHover");
			$(this).find(".listOptions").slideDown(toggleTime);
		},
		out: function() {
			//$(this).find("td").removeClass("listHover");
			$(this).find(".listOptions").slideUp(toggleTime/2);
		}
	});
	*/
	$("table.list tbody tr").hover(
		function() {
			$(this).find("td").addClass("listHover");
		},
		function() {
			$(this).find("td").removeClass("listHover");
		}
	);

	// Close
	$(".messageBoxClose").each(function() {
		attachMessageBoxClose($(this));
	});
	
	// Highlight the nav menu element that this page is.
	if ( typeof(navigationMenuSelected) != "undefined" ) {
		//$("#" + navigationMenuSelected ).addClass("navMenuSelected");
		$("#" + navigationMenuSelected ).addClass("selected");
	}
	
	
	// Toggle any filters on page.
	$(".filterToggle").click( function() {
		toggleFilter( $(this) );
	});
	
	// Display any filter by default if it contains previous data.
	// For now previous data will be defined as not:
	//	- ""	(blank fields)
	//	- "0"	(select list defaults)
	$(".filterToggle").each( function() {
		var filterContainer = $(this).find(".belongsTo").text();
		
		
		// Search through all input (type=text), textarea and select
		// to see if they contain anything.
		var hasPreviousSearch = false;
		$("#" + filterContainer).find("input[type=text], textarea, select, input[@type=checkbox]:checked").each( function() {
			if ( $(this).val() != "" && $(this).val() != 0 ) {
				hasPreviousSearch = true;
			}
		});
        
        setFilterLink($(this));
		
	});
	
	/**
	 * Help
	 */
	$(".helpBoxContainer").each( function() {
		var helpBox = $(this).find(".helpBox").prependTo("body");
		
		// If someone clicks in a help box make sure it becomes the highest one.
		helpBox.click(function() {
			if ( $(this).css("z-index") < highestZIndexOnPage ) {
				$(this).css({"z-index": highestZIndexOnPage++});
			}
		});
		
		// Attach draggable event.
		helpBox.draggable({
			cursor: "move",
			handle: "div.proSiteHelp"
		});
		
		$(this).find(".help").click(function(e) {
			e.preventDefault();
			// Stop any click event "beneath" this one.
			e.stopPropagation();
		
			// Toggle help box open/close.
			if ( helpBox.is(":hidden") ) {
				//alert("hidden: " + $("#" + helpBoxId).css("left"));
				helpBox.css({
					"top" : e.pageY,
					"left" : e.pageX,
					"z-index" : highestZIndexOnPage++
				});
			}
			
			helpBox.slideToggle(toggleTime);
		});
		
		// Attach the close event to the box.
		helpBox.find("img.helpClose").click(function() {
			helpBox.slideToggle(toggleTime);
		});
	});
	
	$("img.iconHolder").hover(
		function() {
			$(this).removeClass("iconDefaultStyle");
			$(this).addClass("iconHolderHover");
		},
		function() {
			$(this).removeClass("iconHolderHover");
			$(this).addClass("iconDefaultStyle");
		}
	);
	
	/**
	 * Disable submit buttons on submit.
	 */
	$("form.disableOnSubmit, .filter form").submit(function() {
		$(this).find("input[type=submit]").each(function() {
			$(this).attr("disabled", "disabled");
			$(this).val( $(this).val() + "...");
		});
	});
	
	/**
	 * Processing
	 */
	/*
	$(".processingHandle").click(function(e) {
		toggleProcessingDialog(e);
	});
	*/
	
	/**
	 * Eventually we could use this for linking to another help item and
	 * have it load (Think .load()) into the current help window.
	 * Possible Problem: If separate content is loaded it will need to be
	 * "refreshed" when someone opens the help back up. Bummer.
	 */
	$(".helpInternalLink").click(function(e) {
		e.preventDefault();
		alert("You clicked: " + $(this).attr("href"));
	});
	
	/**
	 * Hide Processing Messages after waitTimeout seconds
	 */
	setTimeout("$(\".processingMessage\").slideUp(toggleTime)", waitTimeout);

});

function attachMessageBoxClose(anchorEle){
  var parentElement = anchorEle.parent();

  anchorEle.click(function(e) {
    e.stopPropagation();
    e.preventDefault();
    parentElement.hide(toggleTime);
  });
}

function toggleFilter(filterToggle) {

	// Get the element that this toggle belongs to.
	var filterContainer = filterToggle.find(".belongsTo").text();
	
	
	// Toggle the Show/Hide based on what visiblitiy the
	// filter container WILL be.
	if ( $("#" + filterContainer).is(":hidden") ) {
		filterToggle.find("span:eq(0)").html("Hide");
	} else {
		filterToggle.find("span:eq(0)").html("Show");
	}
	
	// Toggle the filter container.
	$("#" + filterContainer).slideToggle(toggleTime);
}

function setFilterLink(filterToggle) {
    // Get the element that this toggle belongs to.
	var filterContainer = filterToggle.find(".belongsTo").text();
    
	// Toggle the Show/Hide based on what visiblitiy the
	// filter container WILL be.
	if ( $("#" + filterContainer).is(":hidden") ) {
		filterToggle.find("span:eq(0)").html("Show");
	} else {
		filterToggle.find("span:eq(0)").html("Hide");
	}
	
}

function filterPageNext(pageNumberInputName, filterFormName){
    inputName = "#" + pageNumberInputName;
    $(inputName).val(parseInt($(inputName).val())+1);
    formName = "#" + filterFormName;
    $(formName).submit();
}

function filterPagePrevious(pageNumberInputName, filterFormName){
    inputName = "#" + pageNumberInputName;
    $(inputName).val(parseInt($(inputName).val())-1);
    formName = "#" + filterFormName;
    $(formName).submit();
}

function setFirstPage(pageNumberInputName){
    inputName = "#" + pageNumberInputName;
    $(inputName).val(0);
}

function toggleProcessingDialog(e) {
	if (e) {
		$("#processingDialog").css({
			"top" : e.pageY - 25 + "px",
			"left" : e.pageX - 25 + "px"
		});
	}
	$("#processingDialog").toggle(toggleTime);
}

function disableButton(button){
  button.attr("disabled", "disabled");
	button.val( button.val() + "...");
}

function enableButton(button, value){
  button.removeAttr('disabled');
	button.val(value);
}

