(function($) {
	window.$fn = {
		nav: function() {
			$("#nav li").each(function() {
				var a = this.getElementsByTagName("a")[0];
				if (a.href == window.location) {
					$(this).addClass("active temple");
					$(this).parents("ul:not(#nav),li").addClass("active temple");
				}
				else if (window.location.pathname.indexOf('.') != -1) {
					if (a.href.substring(a.href.lastIndexOf('/') + 1) == window.location.pathname.substring(window.location.pathname.lastIndexOf('/') + 1)) {
						$(this).addClass("active temple");
						$(this).parents("ul:not(#nav),li").addClass("active temple");
					}
					else if (typeof npid != "undefined") {
						var t = "pid"+npid;
						if (a.id == t) {
							$(this).addClass("active temple");
							$(this).parents("ul:not(#nav),li").addClass("active temple");
						}
					}
				}
			});
			$("#nav li.active>a").addClass("active");
			$("#nav li.active>ul").addClass("active temple");
		},
		setnav: function(l1, l2) {
			var count = 0;
			var that;
			$("#nav>li").each(function() {
				if (count == l1) {
					$(this).addClass("active temple");
					that = $(this).find("li");
				}
				count++;
			});
			count = 0;
			if (that.length > 0) {
				that.each(function() {
					if (count == l2) {
						$(this).addClass("active temple");
					}
					count++;
				});
			}
			$("#nav li.active>a").addClass("active temple");
			$("#nav li.active>ul").addClass("active temple");
		},
		equal: function(col1, col2) {
			col1 = col1 || "#main";
			col2 = col2 || "#page .sidebar:eq(0)";
			p = "#page";
			h = Math.max($(col1).height(), $(col2).height(), $(p).height());
			minh = 565;
			if (h < minh) {
				$(col1).height(minh + "px");
				$(col2).height(minh + "px");
			}
			//$(col1).height($(p).height() + "px");
			//$(col2).height($(p).height() + "px");
			//if($(col1).height() <= $(col2).height()) {
				//$(col1).height($(col2).height() + "px");
			//} else {
				//$(col2).height($(col1).height() + "px");
			//}
		}
	}
	$(document).ready(function() {
		// BEGIN debtlogic form
		$('#debtlogic input.gray').each(function() {
			var $t = $(this),
			     defaultVal = $t.attr('default-value');
			if(defaultVal != $t.attr('value'))
				$t.removeClass('gray').addClass('notgray');
		});
		// Intentionally re-selects, previous operation could have removed the gray class
		$("#debtlogic input.gray").focusin(function() {
			var $t = $(this);
			if($t.is('.gray')) {
				$t.attr('value', '').removeClass('gray').addClass('notgray');
			}
		}).focusout(function() {
			var $t = $(this);
			if($t.is('.notgray') && $t.attr('value') == '') {
				$t.removeClass('notgray').addClass('gray').attr('value', $t.attr('default-value'));
			}
		});
		$("#debt_phonenumber").bind('keyup', function(e) {
			var $t = $(this),
			    orig = $t.attr('value').replace(/[^0-9]/g, ''),
			    len = orig.length,
			    output = '';
			// FIXME: if button is delete/backspace do nothing
				output = orig;
			if(len < 3)
				output = orig;
			else {
				output = '('+orig.substr(0, 3)+') ';
				if(len >= 6)
					output = output + orig.substr(3, 3) + ' - ' + orig.substr(6);
				else
					output = output + orig.substr(3);
			}
			$t.attr('value', output);
		});
		window.submitGrayedForm = function() {
			$('#debtlogic').find('input.gray').attr('value', '');
		}
		// END debtlogic form
		$("#nav a").bind("mouseover", function() {
			var that = $(this).parents("li")[0];

			var ul = that.getElementsByTagName("ul")[0];

			if(typeof ul != "undefined" || $(that).parents("li").length > 0) {
				$("#nav *").removeClass("active");
			} else {
				$fn.nav();
			}

			$(that).parents("ul,li").addClass("active");

			$(ul).addClass("active")

			if($(that).hasClass("temple") || $(that).parents("li").eq(0).hasClass("temple")) {
				$(ul).find(".temple").addClass("active");
			}

			$("#nav li.active>a").addClass("active");
		});

		$("#nav>li").bind("mouseout", function() {
			$("#nav *").removeClass("active");
			$fn.nav();
		});
		if($("#affiliations").length > 0) {
			$("#affiliations").cycle({
				//before: onBefore,
				fx: "fade",
				cleartype: false,
				speed: 1200
			});
		}
                if($('#agree-terms').length > 0) {
                    function checkCheck() {
                        if(this.checked) {
                            $('#agree-terms input[type=image]').attr('disabled', false).parent().removeClass('dim');
                        }
                        else {
                            $('#agree-terms input[type=image]').attr('disabled', true).parent().addClass('dim');
                        }
                    }
                    $('#agree-terms input[type=checkbox]').click(function() {
                        checkCheck.call(this);
                    });
                    $('#agree-terms').submit(function() {
                        if($(this).find('input[type=checkbox]').attr('checked')) {
                            return true;
                        }
                        return false;
                    });
                    checkCheck.call($('#agree-terms input[type=checkbox]')[0]);//in case of back button or refresh
                    $('#agree-terms input[type=image]').hover(function() {
                        if(this.className != 'dim') {
                            this.src = '/images/get-started-agree2.gif';
                        }
                    }, function() {
                        this.src = '/images/get-started-agree.gif';
                    });
                    $('<img>').attr('src', '/images/get-started-agree2.gif');//preload
                }
		//$fn.equal();
	});
})(jQuery)
$ = jQuery;
function getObj(id) { return document.getElementById(id); }
(function(){
    $('body').onImagesLoad({
        selectorCallback: selectorImagesLoaded
    });
    function selectorImagesLoaded($selector){
		col1 = "#main";
		col2 = "#page .sidebar:eq(0)";
		p = "#page";
		snh = 0;
		$("#page #nav ul").each(function() {
			var tmph = $(this).height();
			if (tmph > snh) {
				snh = tmph;
			}
		});
		snh = snh + 174; //offset
		h = Math.max($(col1).height(), $(col2).height(), $(p).height());
		minh = 565;
		if (snh > minh) {
			minh = snh;	
		}
		if (h < minh) {
			$(col1).height(minh + "px");
			$(col2).height(minh + "px");
		}
    }
});
function onBefore(curr,next,opts) {
    var $slide = $(next);
    var w = $slide.outerWidth();
    var h = $slide.outerHeight();
    $slide.css({
        marginTop: (90 - h) / 2,
        marginLeft: (196 - w) / 2
    });
}

$(document).ready(function() {
	if($('#debt-consolidation').length > 0) {
        $('a[href=#debt-consolidation]').click(function(){pageTracker._trackPageview('/home/popup/debt-consolidation')}).facebox($('#debt-consolidation').html());
    }
    $('body.index a[href=#debt-solutions]').click(function() {
        pageTracker._trackPageview('/home/popup/debt-solutions');
    });
    if($('#popup').length > 0) {
        $('a[href=#popup]').facebox($('#popup').html());
    }
    if($('#read-more').length > 0) {
        $('a[href=#read-more]').facebox($('#read-more').html());
    }
	
	if($('#watch-video1').length > 0) {
			var jqxhr = $.ajax({ url: "/js/video.slider.fetch.php" })
			.success(function(data) 
			{ 
				$('#video-slider').html(data);
				$('a[href=#watch-video1]').facebox($('#watch-video1').html());
			})
			.error(function() 
			{
				$('#video-slider').html('Sorry, video content could not be loaded.'); 
				$('a[href=#watch-video1]').facebox($('#watch-video1').html());
			})
    }
	if($('#watch-video2').length > 0) {
        $('a[href=#watch-video2]').facebox($('#watch-video2').html());
    }
	
	if($('#watch-video3').length > 0) {
        $('a[href=#watch-video3]').facebox($('#watch-video3').html());
    }
	
	if($('#watch-video4').length > 0) {
        $('a[href=#watch-video4]').facebox($('#watch-video4').html());
    }
	
	if($('#watch-video5').length > 0) {
        $('a[href=#watch-video5]').facebox($('#watch-video5').html());
    }
	
    if($('#debt-solutions').length > 0) {
        $('a[href=/#debt-solutions]').facebox($('#debt-solutions').html());
        if(window.location.hash == '#debt-solutions') {
            $('a[href=/#debt-solutions]').click();
        }
    }
    $('body.index a[href=#read-more]').click(function() {
        pageTracker._trackPageview('/home/popup/read-more');
    });
    if(window.location.href == 'http://www.myfinancialgoals.org/debt-consolidation-page.htm') {
        $('a[href=#read-more]').click(function() {
            pageTracker._trackPageview('/debt-consolidation-page/popup/read-more');
        });
    }
    var requestURI = window.location.href.split('myfinancialgoals.org')[1];
    $('#main.big-picture-interior-main a[href=#popup]').click(function() {
        pageTracker._trackPageview(requestURI + '/popup');
    });
});


function validateSubscribe()
{
	 var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	 
	if(reg.test(document.getElementById('mce-EMAIL').value))
	{	
		return true; 
	}
	else
	{
		$('#mce-responses').html('<span style="color: red; font-weight: bold">Enter a valid email address</span>');
		return false;
	}
}
	
function externalLinks() 
{ 
	if (!document.getElementsByTagName) return; 
	var anchors = document.getElementsByTagName("a"); 
	for (var i=0; i<anchors.length; i++) 
	{ 
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") anchor.target = "_blank"; 
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "self") anchor.target = "_self"; 
	} 
}

window.onload = externalLinks;	
	
	
	
	
	
	
	
	
	
	
	
