/*
	CircleBack scripts (utilizing jQuery 1.6.3)
*/

/* ---------- Initialize page ---------- */

$(document).ready(function(){

	// Stripe table row colors
	$("table.data tr:nth-child(even)").not(".even").not(".odd").addClass("even");

	// Insert document icons
	$("a[href$=pdf]:not(:has(img))").append('<img class="docicon" src="/images/icon_pdf.gif" width="14" height="14" alt=" (PDF)">');
	$("a[href$=doc]:not(:has(img)), a[href$=docx]:not(:has(img))").append('<img class="docicon" src="/images/icon_word.gif" width="14" height="14" alt=" (Word Document)">');
	$("a[href$=xls]:not(:has(img)), a[href$=xlsx]:not(:has(img)), a[href$=csv]:not(:has(img))").append('<img class="docicon" src="/images/icon_excel.gif" width="14" height="14" alt=" (Excel Spreadsheet)">');
	$("a[href$=ppt]:not(:has(img)), a[href$=pptx]:not(:has(img))").append('<img class="docicon" src="/images/icon_powerpoint.gif" width="14" height="14" alt=" (Powerpoint Presentation)">');

	// Column child CSS3 fix
	$(".columns>.col:first-child").addClass("first-child");
	$(".columns.three>.col:nth-child(2)").addClass("middle-child");
	$(".columns>.col:last-child:not(:first-child):not('.middle-child')").addClass("last-child");

	// Initialize video overlay
	initOverlay();

	// Homepage video link hover
	$(".home a#videolink").hover(function(){
		$("a#videobutton").addClass("hover");
	},function(){
		$("a#videobutton").removeClass("hover");
	});


	// IE6 PNG fix
	iepngfix();

});



// Initialize overlay support
function initOverlay() {

	// Links must have target="overlay"
	$("a[target=overlay]").click(function(){
		var $link = $(this);

		// Overlay padding numbers (hard-coded for accuracy)
		var widthpadding = 32;
		var heightpadding = 18;

		// Remove existing overlay
		$("#overlay-cover, #overlay-close, #overlay").remove();


		// Dynamically create cover HTML
		var $overlaycover = $('<div id="overlay-cover"></div>');
		$("body").append($overlaycover);
		$overlaycover.fadeTo("normal", .75);


		// Dynamically create overlay
		var $overlay = $('<div id="overlay"></div>');
		$overlay.width(1).height(1);

		// Position overlay over link
		var offset = $link.offset();
		$overlay
			.css({
				left: (offset.left-$(window).scrollLeft()+$link.width()/2-widthpadding/2)+"px",
				top: (offset.top-$(window).scrollTop()+$link.height()/2-heightpadding/2)+"px",

				// Set height to maximum 85% window, accounting for padding
				maxHeight: parseInt(($(window).height() - heightpadding) * .85),

				opacity: .1,
				display: "block"
				})
			.appendTo("body");


		// Animate to final position
		var width = $link.data("width") || 800;
		var height = $link.data("height") || 600;

		$overlay.animate({
			opacity: 1,

			// Center on screen
			left: parseInt(($(window).width() - (width+widthpadding)) / 2),
			top: parseInt(($(window).height() - (height+heightpadding+8)) / 2),
			width: width,
			height: height+8

		}, 750, function(){

			// Post-animate callback

			// Load iframe
			$overlay.append('<iframe src="' + $link.attr("href") + '" width="' + width + '" height="' + height + '" frameborder="0" allowfullscreen></iframe>');

			// Auto-inject close link
			$overlayclose = $('<a href="#" id="overlay-close" title="Close"></a>');
			$overlayclose
				.css({
					left: parseInt(($(window).width() - $overlay.outerWidth()) / 2 + $overlay.outerWidth()),
					top: parseInt(($(window).height() - $overlay.outerHeight()) / 2)
					})
				.appendTo("body")
				.fadeIn("fast");


			// Re-center overlay if window resizes
			$(window).resize(function() {
				if ($("#overlay").length == 1 && $("#overlay-cover").is(":visible")) {
					var $overlay = $("#overlay:visible");
					var $overlaycover = $("#overlay-close:visible");

					if ($overlay.length > 0) {
						// Same calculations as above
						var overlaypadding = parseInt($overlay.outerHeight() - $overlay.height());
						$overlay.css({
							maxHeight: parseInt(($(window).height() - overlaypadding) * .85),
							left: parseInt(($(window).width() - $overlay.outerWidth()) / 2),
							top: parseInt(($(window).height() - $overlay.outerHeight()) / 2)
						});

						$overlayclose.css({
							left: parseInt(($(window).width() - $overlay.outerWidth()) / 2 + $overlay.outerWidth()),
							top: parseInt(($(window).height() - $overlay.outerHeight()) / 2)
						});
					}
				}
			});


			// Close action
			$("#overlay-cover, #overlay-close, #overlay a[rel=close]").click(function() {
				$("#overlay-cover, #overlay-close, #overlay").fadeOut("normal", function() {
					// Remove from DOM
					$("#overlay-cover, #overlay-close, #overlay").remove();
				});
				return false;
			});
		});

		return false;
	});
}




// IE6 PNG fix
function iepngfix() {

	if (!$.browser.msie || parseInt($.browser.version) >= 7) { return; }

	// IE 5.5 and 6 PNG support (derived from youngpup.net)
	$("img[src$=png]").each(function(){
		var src = this.src;
		var div = document.createElement("div");

		// Set replacement div properties
		if (this.id) { div.id = this.id; }
		if (this.className) { div.className = this.className; }
		if (this.title) { div.title = this.title; }
		if (this.alt) {
			// Move alternate text to element text and hide
			div.innerHTML = this.alt;
			div.style.textIndent = "-9999px";
			div.style.overflow = "hidden";
			div.style.zoom = 1;
		}

		// Set alpha filter for transparent PNG
		div.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.src + "', sizing='scale')";
		div.style.width = this.width + "px";
		div.style.height = this.height + "px";

		// Replace image with transparent div
		this.replaceNode(div);
	});
}

