jQuery(function() {
	
	jQuery("body").addClass("jsEnabled");
	
	/* ===========================================================
	 * Document library
	 */
	jQuery('.documentList').each(function() {
		var $this = jQuery(this);
		var root = '/';
		var ul = jQuery("> ul", $this);
		var script = 'document-library/document-library-js.scriptJsf';
		
		if ( !jQuery("body").hasClass("static") ) {
			
			if ( $this.hasClass("manual") ) {
				// Manually created library
				
				jQuery("li", ul).each(function() {
					var $liThis = jQuery(this);
					if ( !$liThis.hasClass("file") ) {
						$liThis.addClass("directory");
					}
				});
				
				ul.each(function() {
					var $ulThis = jQuery(this);
					$ulThis.fileTree({ 
						root: root,
						script: script,
						expandSpeed: 350,
						collapseSpeed: 250
						}, function(file) {
							window.open( URLUtil.getBaseURL() + "assets/documents/library/" + file);
							return false;
					});
				});
			} else {
				if (ul.size() > 0 && ul.attr('title') != '') {
					root = ul.attr('title');
				}
				$this.fileTree({ 
					root: root,
					script: script,
					expandSpeed: 350,
					collapseSpeed: 250
					}, function(file) {
						window.open( URLUtil.getBaseURL() + "assets/documents/library/" + file);
						return false;
				});
			}
			
		}
	});
	
	jQuery(".moreInfo").live("click" , function(){
		jQuery(this).next(".additionalInfo").toggle();
	});
	
	/* ==========================================================
	 * Automatically select searchbox text and submit on pressing enter
	 */
	jQuery(".searchForm .textField").focus(function(){
		jQuery(this).select();
	});
	
	jQuery('input').keydown(function(event){
		var input = jQuery(this);
	    if (event.keyCode == 13) {
	    	input.parent().submit();
	        return false;
	    }
	});

	
	/* ===========================================================
	 * Homepage
	 */
	jQuery('.contentArea').hover(
		function () {
			var contentHeight = jQuery(this).height();
			var distanceFromTop = (273 - contentHeight - 30) + "px"
			jQuery(this).stop().animate({
				top: distanceFromTop
			}, 500);
		},
		function () {
			jQuery(this).stop().animate({
				top: "210px"
			}, 500)
		}	
	);
	// Make click on .contentArea link you to ./h3/a/@href
	jQuery('.animation').click(function(){
		var link = jQuery(this).children('.contentArea').children('h3').children('a').attr("href");
		window.location = link;
	});
	
	/* ===========================================================
	 * Contact Us Map.
	 */
	// Remove tooltip when mouseing out of map
	jQuery("span.close").live("click" , function(){
		jQuery(".tooltip").remove();
	});
	
	//Static State Contact Us Map has select boxes that select different sales reps
	
	var currentProduct;
	var currentState;
	
	jQuery("#salesRepSelect").change(function(){
		jQuery(".salesRepStateSelect").hide();
		
		if ( currentProduct != undefined ) {
			jQuery(currentProduct).hide();
		}
		
		var productType = jQuery(this).val(); //Get the product type val
		var productTypeCssSelector = "." + productType; //Create a css selector with value
		
		jQuery("#" + productType).show(); //Show current select box
		
		if ( currentState != undefined ) {
			jQuery("#" + productType).val(currentState.substr(1));
		}
		
		if ( currentState != undefined && currentState != '.no-state' ) {
			jQuery(productTypeCssSelector).each(function(){
				if ( jQuery(this).hasClass(currentState) ) {
					jQuery(this).show();
				}
			});
		} else {
			jQuery(productTypeCssSelector).show();
		}
		
		currentProduct = productTypeCssSelector;
		return currentProduct;
	});
	
	jQuery(".salesRepStateSelect").change(function(){
		
		var state = jQuery(this).val(); //Get the product type val
		var stateCssSelector;
		
		jQuery(".salesRepContactInfo").hide();
		stateCssSelector = "." + state; //Create a css selector with value
		
		if ( currentProduct != undefined && stateCssSelector != '.no-state') {
			jQuery(stateCssSelector).each(function(){
				if ( jQuery(this).hasClass(currentProduct) ) {
					jQuery(this).show();
				}
			});
		} else {
			jQuery(currentProduct).show();
		}
		
		currentState = stateCssSelector;
		return currentState;
	});
	
	/* ===========================================================
	 * Misc.
	 */
	// Add hover states to submit buttons in IE6
	jQuery('.submit').hover(
		function(){
			jQuery(this).addClass('hover');
		}, function() {
			jQuery(this).removeClass('hover');
		}
	);
	
	
});

/**
 * Returns a reference to the specified flash movie.
 * @param movieName
 * @return
 */
function getFlashMovie(movieName) {
	  var isIE = navigator.appName.indexOf("Microsoft") != -1;
	  return (isIE)? window[movieName] : document[movieName];
}

/**
 * Used on contact us page when the flash is clicked.
 * 
 * Only used for sales reps
 */
function regionClick(id, xPos, yPos) {
	jQuery(".tooltip").remove();
	var flashAreaPosition = jQuery("#salesRepMap").offset();
	
	jQuery("<div class='tooltip' />")
		.appendTo("body")
		.append("<table id='reps'><tr></tr></table>");
	
	jQuery("#sales-representative-contact-area .contact-"+id).each(function() {
		jQuery("#reps tr").append("<td valign='top' />");
		jQuery(this).clone().appendTo("#reps tr td:last");
	});
	
	jQuery(".tooltip").append("<span class='close'>Close</span>");
	
	var toolTipX = flashAreaPosition.left + xPos;
	var toolTipY = flashAreaPosition.top + yPos;
	var toolTipWidth = jQuery(".tooltip").width();
	var toolTipRightPosition = toolTipWidth + toolTipX;
	var documentWidth = jQuery(document).width();
	
	if ( toolTipRightPosition >= documentWidth ) {
		toolTipX =  Math.max(10, toolTipX - toolTipWidth - 10);
	}
	jQuery(".tooltip").css({
		top: toolTipY, 
		left: toolTipX
	});
	
	jQuery(".tooltip").css({ visibility: "visible" });
}

function beforeUpdateMap() {
	jQuery(".tooltip").remove();
	//jQuery("#salesRepMap").css("visibility", "hidden");
	jQuery("#salesRepMap").fadeOut();
}

function afterUpdateMap() {
	jQuery("#salesRepMap").fadeIn();	
}

var enabledRegionIds = new Array();
var enabledRegionTitles = new Array();
/**
 * Called by the flash to get the enabled regions
 */
function getEnabledRegionIds() {
	return enabledRegionIds;
}
function setEnabledRegionIds(value) {
	enabledRegionIds = value;
}
function getEnabledRegionTitles() {
	return enabledRegionTitles;
}
function setEnabledRegionTitles(value) {
	enabledRegionTitles = value;
}
function salesRepMapUpdateRegions() {
	var salesRepMap = getFlashMovie("salesRepMap");
	if ( salesRepMap != null ) {
		salesRepMap.setupRegions();
	}
}

var URLUtil = function() {
    var public = {};
    var baseURL;
    
    public.setBaseURL = function( url ) {
            baseURL = url;
    };
    public.getBaseURL = function() {
            return baseURL;
    };
   
    return public;
}();
