﻿/*
--------------------------------------------------

Federal Reserve Bank of Chicago
Combined Javascript Library [library.js]

Joe Morrow [joe.morrow@acquitygroup.com]
6/26/2009

Copyright   2009 Acquity Group LLC

--------------------------------------------------
*/


// browser detectors ($.browser is deprecated in jQuery 1.3 and $.support isn't helpful when targeting style rules)
function isIE6() {
	if (typeof($.isIE6) == "undefined") {
		jQuery.extend({
			isIE6: ((window.XMLHttpRequest == undefined) && (ActiveXObject != undefined)) ? true : false
		});
	}
	return $.isIE6;
}

function isIE() {
	if (typeof($.isIE) == "undefined") {
		jQuery.extend({
			isIE: (document.all) ? true : false
		});
	}
	return $.isIE;
}

function isSafari() {
	if (typeof($.isSafari) == "undefined") {
		jQuery.extend({
			isSafari: (/Safari/.test(navigator.userAgent)) ? true : false
		});
	}

	return $.isSafari;
}

function isSafari2() {
	if (typeof($.isSafari2) == "undefined") {
		jQuery.extend({
			isSafari2: ((/412|416|419/.test(navigator.appVersion)) && (/Safari/.test(navigator.userAgent))) ? true : false
		});
	}
	return $.isSafari2;
}

function isOpera8() {
	if (typeof($.isOpera8) == "undefined") {
		var ua = navigator.userAgent.toLowerCase();
		jQuery.extend({
			isOpera8: (ua.indexOf("opera 8") != -1 || ua.indexOf("opera/8") != -1) ? true : false
		});
	}
	return $.isOpera8;
}

function isLteFF30() {
	if (typeof($.isLteFF30) == "undefined") {
		if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {
			var v = new Number(RegExp.$1);
			jQuery.extend({
				isLteFF30: (v <= 3.0) ? true : false
			});
		} else {
			jQuery.extend({
				isLteFF30: false
			});
		}
	}
	return $.isLteFF30;
}

function isMac() {
	if (typeof($.isMac) == "undefined") {
		jQuery.extend({
			isMac: (/Mac/.test(navigator.platform)) ? true : false
		});
	}
	return $.isMac;
}


// Carousel Switcher
function promoSwitchTo(selected) {
	selected = selected.replace("#", "");
	var number = selected.replace(/\D/g,'');
	if ((typeof(number * 1) == "number") && (!$("#" + selected).hasClass("selected"))) {
		$("#carousel div.selected").fadeOut("medium", function() {
			$(this).removeClass("selected");
			$("#carousel li.selected").removeClass("selected").children("a").children("img").attr("src", "/admin/images/icons/carousel-dot.gif");
			var selectedScroller = $("#controller-" + number).addClass("selected");
			selectedScroller.children("a").children("img").attr("src", "/admin/images/icons/carousel-dot-on.gif");
			$("#carouselItem-" + number).fadeIn("medium", function() {
				$(this).addClass("selected");
				$("a#carouselPrev").attr("href", $("a", (selectedScroller.prev().size() > 0) ? selectedScroller.prev() : $("li:last-child", selectedScroller.parent())).attr("href"));
				$("a#carouselNext").attr("href", $("a", (selectedScroller.next().size() > 0) ? selectedScroller.next() : $("li:first-child", selectedScroller.parent())).attr("href"));
			
			});
		});
	}
}


// Utility to convert CSS property names into Javascript property names
function getJavascriptStyleName(name) {
	var nameArray = name.split("-");
	var nameJS = nameArray[0];
	if (nameArray.length > 1) {
		for (var i=1; i<nameArray.length; i++) {
			nameJS += nameArray[i].charAt(0).toUpperCase() + nameArray[i].slice(1).toLowerCase();
		}
	}
	return nameJS;
}


// Utility to get the style property units
function getStyleUnits(value) {
	var match = /[%ceimnptx]+/.exec(value);
	return (match != null) ? match : "";
}


// Text Resize Function (complicated, for ems or px)
function resizeStylePx(multiplier, properties, context, excluded, isPreset) {

	// Cache the style name lookup table for performance
	var styleNameLookup = Array();

	// Some values look better when multiplied by a fractional amount
	var multiplierFractional = ((multiplier > 1) ? 1 + ((multiplier - 1) / 10) : 1 / (1 + ((1/multiplier - 1) / 10)));

	// Preset the style values, if necessary, to limit the multiplication's scope
	if (!isPreset) {

		var elementsAll = $(":visible:not([class*=hidden], :input)", context).filter(function() {
			return ($(this).text().length > 0);
		});

		if (!isIE()) {
			elementsAll.each(function() {
				for (var i=0; i<properties.length; i++)
					$(this).css(properties[i], $(this).css(properties[i]));
			});
		} else {
			elementsAll.each(function() {
				for (var i=0; i<properties.length; i++) {
					if (styleNameLookup[properties[i]] == null)
						styleNameLookup[properties[i]] = getJavascriptStyleName(properties[i]);

					// jQuery has a known defect that incorrectly returns an extremely large font-size value for some elements, so skip these elements
					var styleUnits = getStyleUnits($(this)[0].currentStyle[styleNameLookup[properties[i]]]);	
					if ((styleUnits == "px") || (styleUnits == ""))
						$(this).css(properties[i], $(this)[0].currentStyle[styleNameLookup[properties[i]]]);
				}
			});
		}

		// Then grab only the relevant elements and cache the collection here for performance
		if (typeof($.resizeElements) == "undefined") {
			jQuery.extend({
				resizeElements: elementsAll.not(excluded)
			});
		}
	}

	// Iterate through each element
	$.resizeElements.each(function() {
		
		// Iterate through each style property
		for (var i=0; i<properties.length; i++) {
			if (styleNameLookup[properties[i]] == null)
				styleNameLookup[properties[i]] = getJavascriptStyleName(properties[i]);

			// jQuery has a known defect that incorrectly calculates some values in IE, so we're multiplying a different type of value in that case
			var value = (!isIE()) ? $(this).css(properties[i]) : $(this)[0].currentStyle[styleNameLookup[properties[i]]];
			var numeral = parseFloat(value);
			var units = getStyleUnits(value);

			if (numeral != null) {
				if (isIE()) {

					if ((units == "%") || (units == "")) 
						// For some units, we don't want as big a multiplier
						var multiplied = numeral * multiplierFractional;
					else
						// IE doesn't handle fractional pixels well, so we're rounding up to whole integers
						var multiplied = Math.ceil((numeral * multiplier) * 2) / 2;

				} else
					var multiplied = numeral * multiplier;

				// Set the new value, retaining the original units
				$(this).css(properties[i], multiplied + units);
			}
		}
	});

	// If using Cufon, reload to set the new size
	if ($(".cufon").length != 0)
		Cufon.refresh();
}


// Text Resize Function (simpler, but ems only)
function resizeStyle(multiplier, properties, context, exclude, isPreset) {

	// Cache the style name lookup table for performance
	var styleNameLookup = Array();

	var multiplierFractional = multiplier;

	// Preset the style values for any excluded elements, to limit the multiplication's scope
	if (!isPreset) {

		var excluded = $(exclude + ":visible:not([class*=hidden])", context).add(":input", context).filter(function() {
			return ($(this).text().length > 0);
		});

		if (!isIE()) {
			excluded.each(function() {
				for (var i=0; i<properties.length; i++)
					$(this).css(properties[i], $(this).css(properties[i]));
			});
		} else {
			excluded.each(function() {
				for (var i=0; i<properties.length; i++) {
					if (styleNameLookup[properties[i]] == null)
						styleNameLookup[properties[i]] = getJavascriptStyleName(properties[i]);

					// jQuery has a known defect that incorrectly returns an extremely large font-size value for some elements, so skip these elements
					var styleUnits = getStyleUnits($(this)[0].currentStyle[styleNameLookup[properties[i]]]);	
					if ((styleUnits == "px") || (styleUnits == ""))
						$(this).css(properties[i], $(this)[0].currentStyle[styleNameLookup[properties[i]]]);
				}
			});
		}

		// Then cache the collections here for performance
		if (typeof($.excludeElements) == "undefined") {
			jQuery.extend({
				excludeElements: excluded
			});
		}
		if (typeof($.resizeElements) == "undefined") {
			jQuery.extend({
				resizeElements: $(context)
			});
		}
	}

	// Iterate through each element
	$.resizeElements.each(function() {
		
		// Iterate through each style property
		for (var i=0; i<properties.length; i++) {
			if (styleNameLookup[properties[i]] == null)
				styleNameLookup[properties[i]] = getJavascriptStyleName(properties[i]);

			// jQuery has a known defect that incorrectly calculates some values in IE, so we're multiplying a different type of value in that case
			var value = (!isIE()) ? $(this).css(properties[i]) : $(this)[0].currentStyle[styleNameLookup[properties[i]]];
			var numeral = parseFloat(value);
			var units = getStyleUnits(value);
			
			if (numeral != null) {
				if (isIE()) {

					if ((units == "%") || (units == "")) 
						// For some units, we don't want as big a multiplier
						var multiplied = numeral * multiplierFractional;
					else
						// IE doesn't handle fractional pixels well, so we're rounding up to whole integers
						var multiplied = Math.ceil((numeral * multiplier) * 2) / 2;

				} else
					var multiplied = numeral * multiplier;

				// Set the new value, retaining the original units
				$(this).css(properties[i], multiplied + units);
			}
		}
	});

	// If using Cufon, reload to set the new size
	if ($(".cufon").length != 0)
		Cufon.refresh();
}


// Google Map Setup
function drawMap(mapcontainer, address) {
	if (mapcontainer) {
		map = new GMap2(document.getElementById(mapcontainer));
		geocoder = new GClientGeocoder();
		
		geocoder.getLatLng(
			address,
			function(point) {
				if (!point) {
					//alert(address + ': address not found')
				} else {
					map.setCenter(point, 14);
					marker = new GMarker(point);
					map.addOverlay(marker);
					//map.addControl(new GSmallMapControl());
				}
			}
		)
	}	
}


// Add This Setup
function setAddThis($elem) {

	addthis_options = 'email, digg, delicious, buzz, linkedin, facebook, stumbleupon, technorati';
	addthis_exclude = 'print';
	addthis_header_color = '#025E83';
	addthis_header_background = '#D3E4EE';
	addthis_hover_delay = 200;
	$elem
		.click(function(){return addthis_sendto();})
		.hover(function(){return addthis_open(this, '', '[URL]', '[TITLE]');},
			  function(){addthis_close();});

}


 
