// JavaScript Document
// <reference path="jquery-1.4.3.min.js"/>

var pageTracker;
var defaultFile = "default.aspx";

// google analytics tracking code
jQuery(document).ready(function($) {

    AddTrackPageAnalytics(); 
    AddDocumentAnalytics();
});

function Initializers() {
    WireMenu();
	CheckURLAndActivateMenuItem();
	randomSayings();
	//Signup('a.signup');
	$('.anchorImage').jcaption();
}

function Signup(target) {
    $(target).fancybox({
        'frameWidth': 600,
        'frameHeight': 400, 
        'centerOnScroll': true,
		'hideOnContentClick': false,
		'zoomOpacity': true,
		'zoomSpeedIn': 600,
		'zoomSpeedOut': 500
	});
	//'type': 'iframe'toptions="group = links, type = iframe, effect = fade, width = 600, height = 475, overlayClose = 1, layout = quicklook" 
}


function WireMenu() {
    $('#MainMenu li.main').removeClass('nojs');
	//$('#MainMenu li.main').children('li:last').css('border','none');
    $('#MainMenu li.main').hover(
        function () {
           // $('ul li a', this).equalWidths(); // fixes width 
            $('ul', this).slideDown({ speed: 'fast', easing: 'easeOutBack' });
            $(this).siblings('li').removeClass('current');
            $(this).addClass('current');
        },
        function() {
		//$('ul', this).slideUp({ speed: 'fast', easing: 'easeOutExpo' }, ClearCurrent(this));
        $('ul', this).hide(200);//.slideUp({ speed: 'fast', easing: 'easeOutExpo' }, ClearCurrent(this));
		ClearCurrent(this);
        }
    );
}

function ClearCurrent(target) {
    $(target).removeClass('current');
}

function CheckURLAndActivateMenuItem()
{	
    var paths =  location.pathname.substring(1).split('/');
	var isRoot = false;
	//alert("My Path " + paths);
	
	// if *.html page in root, then could be a Footer Link
	if (paths[0].indexOf("html") !=-1)
	{
			var mypath = paths[0];
        	$('#FooterWrapper li a[href*="' + mypath + '"]').parent('li').addClass('active');
			isRoot = true;
	}

    var menuName = '';
	menuName = $('#MainMenu li.main a[href*="' + paths[0] + '"]').parent('li').attr('id');
	$('#' + menuName).addClass('active');
	
	var idName = "#" + menuName;
	//put subNavigation into left menu
	if (idName != "#undefined") {
		//alert(idName);
		//$('#NavItems').append('<h1>' + $('#MainMenu ' + idName + ' a').html() + "</h1>");
		$('#NavItems').append('<ul id="ContextNav">' + $('#MainMenu ' + idName + ' ul').html() + "</ul>");
		if (paths.length > 1 ) // has children
		{
			var mypath = '/' + paths[0] + '/' + paths[1];
			$('#ContextNav li a').each(function(){
				if ($(this).attr("href") == mypath)
				  $(this).parent('li').addClass('active');
			});	
		}
	}
		$('#NavItems li:first').css('border-top', '1px solid rgb(218,239,247)');
}


$.fn.equalWidths = function (px) { //alert('candy');
    $(this).each(function () {
        var currentWidest = 0;
        $(this).children().each(function (i) {
            if ($(this).width() > currentWidest) { currentWidest = $(this).width(); }
        });
        //if (!px || !Number.prototype.pxToEm) currentWidest = currentWidest.pxToEm(); //use ems unless px is specified
        // for ie6, set height since min-height isn't supported
        if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({ 'width': currentWidest }); }
        $(this).children().css({ 'min-width': currentWidest });
    });
    return this;
};


function randomSayings()
{
	var myQuotes=new Array(13);
	for (i=0; i < 13; i++)
		myQuotes[i]=new Array(2);

	//SayingId '	ImageUrl	Content	Author
	myQuotes[0][0] = "Making the decision to have a child - it's momentous. It is to decide forever to have your heart go walking outside your body.";
	myQuotes[0][1] = "Elizabeth Stone";
	
	myQuotes[1][0] = "Children are our most valuable natural resource.";
	myQuotes[1][1] = "Herbert Hoover";
	
	myQuotes[2][0] = "Children have never been very good at listening to their elders, but they have never failed to imitate them.";	
	myQuotes[2][1] = "James Baldwin";
	
	myQuotes[3][0] = "When I approach a child, he inspires in me two sentiments; tenderness for what he is, and respect for what he may become.";
	myQuotes[3][1] = "Louis Pasteur";
	
	myQuotes[4][0] = "A Child can ask questions that a wise man cannot answer.";
	myQuotes[4][1] = "Author Unknown";
	
	myQuotes[5][0] = "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
	myQuotes[5][1] = "Albert Einstein";
	
	myQuotes[6][0] = "There's nothing that can help you understand your beliefs more than trying to explain them to an inquisitive child.";
	myQuotes[6][1] = "Frank A. Clark";
	
	myQuotes[7][0] = "The only thing worth stealing is a kiss from a sleeping child.";
	myQuotes[7][1] = "Joe Houldsdworth";
	
	myQuotes[8][0] = "Cleaning your house while your kids are still growing up is like shoveling the walk before it stops snowing.";
	myQuotes[8][1] = "Phyllis Diller";
	
	myQuotes[9][0] = "There are only two things a child will share willingly - communicable diseases and his mother's age.";
	myQuotes[9][1] = "Benjamin Spock, Dr. Spock's Baby and Child Care, 1945";
	
	myQuotes[10][0] = "Children seldom misquote. In fact, they usually repeat word for word what you shouldn't have said.";
	myQuotes[10][1] = "Author Unknown";
	
	myQuotes[11][0] = "Children are one third of our population and all of our future.";
	myQuotes[11][1] = "Select Panel for the Promotion of Child Health, 1981";
	 
	myQuotes[12][0] = "There are only two lasting bequests we can hope to give our children. One is roots; the other, wings.";
	myQuotes[12][1] = "Hodding Carter";
	
	//$("#Sayings");
	
	var n = myQuotes.length; 
	var random2 = Math.floor( Math.random()*n ); 
	var num_images= 23;
	var random3 = Math.floor(1 + Math.random()*num_images); 
 
 	$('#RandomQuote').append('<img class="childImage" src=\"/skin/children/Child' + random3 + ".jpg\" />");
	$('#RandomQuote').append('<p>&#8220;' + myQuotes[random2][0] + "&#8221;</p>");
	$('#RandomQuote').append('<em>' +myQuotes[random2][1]+ "</em>");
	
	if ($('.fasdImage').length > 0) $('.childImage').css('display','none');
}
 


function validateForm() {
	var topic = "";
	if ($("#EmailCDC input").val()=="") 
	{
		topic += "required fields: ";
		if ($("#EmailCDC input.firstName").val()=="") 
			topic += "first name";
		if ($("#EmailCDC input.firstName").val()=="") 
			topic += " last name";
		if ($("#EmailCDC input.firstName").val()=="") 
			topic += " email address";
	}

	if (topic != "") { alert(topic);  }
	else { location.href= "/newslettersignup.aspx"; }

}

function AddTrackPageAnalytics() {
    $('a').each(function() {
        var $a = $(this);
        var href = $a.attr('href');

        // see if the link is external
        if (href != null && (href.match(/^http/)) && (!href.match(document.domain))) {

            // if so, add the GA tracking code
            $a.click(function() {
                pageTracker._trackPageview('/outbound/' + href);
            });
        }
    });
}

function AddDocumentAnalytics() {
    var fileTypes = ['doc', 'xls', 'docx', 'xlsx', 'pdf', 'ppt', 'zip'];

    $('a').each(function() {
        var $a = $(this);
        var href = $a.attr('href');

        if (href != null) {
            var hrefArray = href.split('.');
            var extension = hrefArray[href.length - 1];

            if ($.inArray(extension, fileTypes) != -1) {
                $a.click(function() {
                    // get the post title if there is one
                    // and add it to the string for tracking
                    pageTracker._trackPageview('/documents/' + href);
                   // $a.attr("target", "_blank");
                });
            }
        }
    });
}
