/*
 * Authors: Spencer Shimko <sshimko@tresys.com>
 *          Jason Dana <jdana@tresys.com>
 *
 * Copyright 2008 Tresys Technology
 */

/* use scriptaculous' appear effect to show hide element i 
 * i = element id 
 */
function toggleShowHide(i)
{
        var e = document.getElementById(i)
        if (e.style.display == 'none') {
		new Effect.toggle(e, 'appear');
        } else {
		new Effect.toggle(e, 'appear');
        }

}

/* use scriptaculous' appear effect to show element i 
 * i = element id 
 */
function show(i)
{
        var e = document.getElementById(i)
        if (e.style.display == 'none') {
		new Effect.toggle(e, 'appear');
        } 
}

/* use scriptaculous' appear effect to hide element i 
 * i = element id 
 */
function hide(i)
{
        var e = document.getElementById(i)
        if (e.style.display != 'none') {
		new Effect.toggle(e, 'appear');
        } 
}

/* toggle bg position between two positions 
 * use it onclick so you can pass the same values each time 
 * i = element ID, pos1 = bg position 1, pos2 = bg position 2
 */
function toggleBGPos(i, pos1, pos2)
{
       var e = document.getElementById(i)
        if (e.style.backgroundPosition == pos1) {
                e.style.backgroundPosition = pos2;
        } else {
                e.style.backgroundPosition = pos1;
        }

}

/* swap between two classes 
 * i = element id, c1 = class 1, c2 = class 2
 */
function toggleClass(i, c1, c2)
{
       var e = document.getElementById(i)
        if (e.className == c1) {
                e.className = c2;
        } else {
                e.className = c1;
        }

}

/* get a list of child anchor nodes of element i then 
 * try to find an element linking to our current URL
 * i = element id, c = class to set on the anchor element if found
 */
function findAndSetCurrentURLClass(i,c)
{
	var e = document.getElementById(i)
	var url = document.location.toString();
        if (url.match('#')) {
        	url = url.split('#')[0]; 
	}
	var a = e.getElementsByTagName("a");
	for (x=0; x < a.length; x++) {
		if (a[x].toString().match(url)) {
			a[x].className = c;
		}
	}
}

/* get a list of child anchor nodes of element i then 
 * try to find an element linking to our current URL
 * i = element id, i_new = new id
 */
function findAndSetCurrentURLID(i,i_new)
{
	var e = document.getElementById(i)
	var url = document.location.toString();
        if (url.match('#')) {
        	url = url.split('#')[0]; 
	}
	var a = e.getElementsByTagName("a");
	for (x=0; x < a.length; x++) {
		if (a[x].toString().match(url)) {
			a[x].id = i_new;
		}
	}
}

function showIDIfAnchored(a,i) 
{
        var url = document.location.toString();
        if (url.match('#')) {
                var anchor = url.split('#')[1]; 
                if (anchor.match(a)) {
                        var e = document.getElementById(i);
                        e.style.display = 'block';
		}
	}
}

function hideIDIfAnchored(a,i) 
{
        var url = document.location.toString();
        if (url.match('#')) {
                var anchor = url.split('#')[1]; 
                if (anchor.match(a)) {
                        var e = document.getElementById(i);
                        e.style.display = 'none';
		}
	}
}

function bgPosIfAnchored(a,i,p)
{
        var url = document.location.toString();
        if (url.match('#')) {
                var anchor = url.split('#')[1]; 
                if (anchor.match(a)) {
                        var e = document.getElementById(i);
                        e.style.backgroundPosition = p;
		}
	}
} 

function setClassIfAnchored(a,i,c)
{
        var url = document.location.toString();
        if (url.match('#')) {
                var anchor = url.split('#')[1]; 
                if (anchor.match(a)) {
                        var e = document.getElementById(i);
                	e.className = c;
		}
	}
}

jQuery.fn.center = function () {
	this.css("position","absolute");
	this.css("top", 25+$(window).scrollTop() + "px");
	this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
	return this;
}

$(document).ready(function() {
	$("#mainNav li").hover(
		function() {
			$(this).addClass("over");
		},
		function() {
			$(this).removeClass("over");
		}
	);
	$(".hidden").hide();
	$(".toggle").click(function() {
		var $toggle = $(this);
		var $hidden = $toggle.siblings('.hasHidden');
		var open, close;
		if ($toggle.attr('id') == 'news') {
			open = "More News Articles";
			close = "Fewer News Articles";
		}
		else if ($toggle.attr('id') == 'press') {
			open = "More Press Releases";
			close = "Fewer Press Releases";
		}
		else {
			open = 'Expand';
			close = 'Collapse';
		}
		if ($toggle.hasClass("more")) {
			$hidden.children('.hidden').removeClass("hidden").addClass("shown").fadeIn('slow');
			$toggle.removeClass("more").addClass("less").html(close);
			$toggle.siblings('.show').removeClass('show').addClass('hide');
		}
		else if ($toggle.hasClass("less")) {
			$hidden.children('.shown').fadeOut('slow', function() {
				$(this).removeClass("shown").addClass("hidden");
				$toggle.removeClass("less").addClass("more").html(open);
			});
			$toggle.siblings('.hide').removeClass('hide').addClass('show');	
		}
		else if ($toggle.hasClass("show")) {
			$hidden.children(".hidden").removeClass("hidden").addClass("shown").fadeIn('slow');
			$toggle.removeClass('show').addClass('hide');
			$toggle.siblings('.more').removeClass('more').addClass('less').html(close);
		}
		else if ($toggle.hasClass('hide')) {
			$hiddene.children('.shown').fadeOut('slow', function() {
				$(this).removeClass('shown').addClass('hidden');
				$toggle.siblings('.less').removeClass('less').addClass('more').html(open);
			});
			$toggle.removeClass('hide').addClass('show');
		}
	});
	$(".magnify").click(function() {
		var $this = $(this);
		var $overlay = $("<div/>");
		$overlay
			.attr('id', 'overlay')
			.css({'opacity': '0'})
			.animate({'opacity': '0.8'}, 400);
		$('body').append($overlay);
		var img_width, img_height;
		var $bg = $("<div/>");
		var $img = $("<img/>");
		var caption = $this.next(".caption");
		$img
			.attr('src', $(this).attr('src')+"?x="+Date())
			.attr('id', 'magnified')
			.load(function() {
				img_width = this.width;
				img_height = this.height;		
				$bg
					.attr('id', 'imgcont')
					/*.animate({
						'opacity': '1.0'
					}, 200)*/
					.css({
						'width': img_width + 40,
						'min-height': img_height + 40,
						'position': 'fixed',
						'text-align': 'center',
						'z-index': '1000',
						'background-color': 'white'
					});
				$bg.center();
				$('body').append($bg.fadeIn());
				$bg.append($img)
				$($overlay).bind('click', function() {
					$(this).fadeOut(500, function() {
						$(caption).insertAfter($this).hide();
					});
					$('div#overlay').fadeOut(500, function() {
						$(this).remove();
					});
					$('div#imgcont').fadeOut(500, function() {
						
						$(this).remove();
					});
				});

				$(this)
					.fadeIn(400, function() {
						$(this)
							.bind('click', function() {
								$(this)
									.fadeOut(500, function() {
										$(caption).insertAfter($this).hide();
									});
								$('div#overlay')
									.fadeOut(500, function() {
										$(this).remove();
									});
								$('div#imgcont')
									.fadeOut(500, function() {
									$(this).remove();
								});
							});
						$(caption).detach();
						$bg.append(caption);
						$(caption).show();
					})
					.css({
						'margin': '10px',
						'z-index': '1001'
					});
			});
	});
});

