$(document).ready(function(){
    //Clean Divs before appending the new content.
    cleanContent('#footerLinks');
    cleanContent('#upcomingEvents');

    //Get links from the navigation bar as well as the side to append into the footer
    getNavlinks('#navigation a span', '#footerLinks');
    getNavlinks('#leftContent a span','#footerLinks');

    //---Append the siteMap and privacy Policy links---//
    appendExtraLinks();

    //Set the randomTestimonials
    rotateTestimonials('#testimonialsHolder');

    //Set to display only 3 testimonials at the time
    rotateTestimonials('#testimonialsDivShow');
   
    //Set the donate button background
    setLastNavItem();

    //This sets the hover animation to display hidden divs
    expandText();

    //In  the News page get most recent news and append them onto the right side
    getMostResentNews('mediaCoverageFirst', 'mediaCoverage');
    getMostResentNews('publicationFirst', 'publications');
    getMostResentNews('publicRecognitionFirst', 'publicRecognition');
    getMostResentNews('newsReleasesFirst', 'newsReleases');
    
    //Check for the Questionnaire
    if(!( $('#questionnaire').length) > 0){
        setUpcomingEvents();
        startSponsorsSlideShow();
    }else{
        $('#sponsorsFooter').remove();
    }   
});

//Val1 is the Div that clones the most recent news while val2 is the div where the news come from
function getMostResentNews(val1, val2){
    $('#' + val2 + ' p:first-child').clone().appendTo($('#' + val1));
}
function setUpcomingEvents(){
    $('#upcomingEvents').load('/upcoming-events #upcomingEventsSetter', function(){
        });
}

//This function will grab the links from the first Value and appended into Value2
function getNavlinks(value, value2){
    $(value).each(function(){
        var name = $(this).html().toLowerCase();
        var reference2 = $(this).parent().get();
        var link2 = '<a href="'+reference2+'">'+name+'</a>&nbsp|&nbsp;';
        $(value2).append(link2);
    });
}
function appendExtraLinks(){
    $('#footerLinks').append('<a style="font-weight: bold; color: #fd852b;" href="/site-map">site map</a> | <a href="/privacy-policy">privacy policy</a> | <a href="/continuum-questionnaire">Technology Questionnaire</a>');
}

function cleanContent(value){
    $(value).empty();
}
function rotateTestimonials(holder){
    //    $(holder).load('/testimonials .testimonials p', function(){
    $(holder).load('/testimonials .testimonials p', function(){
        $(holder).children().each(function(){
            $(this).hide();
        });
        var size = $(holder).children().length;
        var randNum;
        var randNum2;
        //        This is the home page holder
        if(holder == '#testimonialsHolder'){
            if (size > 2) {
                randNum = Math.floor(Math.random() * size + 1);
                randNum2 = Math.floor(Math.random() * size + 1);
                while (randNum2 == randNum) {
                    randNum2 = Math.floor(Math.random() * size + 1);
                }
            }
            else {
                randNum = 1;
                randNum2 = 2;
            }
            $('#testimonialsHolder img').each(function(){
                $(this).hide();
            });
            $(holder+' p:nth-child(' + randNum + ')').show().prepend('<img src="/image/image_gallery?uuid=47d6d3f0-145e-4801-b7ef-f24e03db6097&groupId=65251&t=1308661846993" />');
            $(holder+' p:nth-child(' + randNum2 + ')').show().prepend('<img src="/image/image_gallery?uuid=47d6d3f0-145e-4801-b7ef-f24e03db6097&groupId=65251&t=1308661846993" />');
        }
        else{
            if (size > 3) {
                randNum = Math.floor(Math.random() * size + 1);
                randNum2 = Math.floor(Math.random() * size + 1);
                randNum3 = Math.floor(Math.random() * size + 1);
                while(randNum == randNum2 || randNum == randNum3 || randNum2 == randNum3){
                    randNum2 = Math.floor(Math.random() * size + 1);
                    randNum3 = Math.floor(Math.random() * size + 1);
                    
                }
            }
            else {
                randNum = 1;
                randNum2 = 2;
                randNum3 = 3;
            }
            $(holder+' p:nth-child(' + randNum + ')').show().append('<img style="width:100%; height: 6px;" src="/image/image_gallery?uuid=fc832cfb-8fc3-4ac1-8229-d0464e08fd17&groupId=65251&t=1310407592079" />');
            $(holder+' p:nth-child(' + randNum2 + ')').show().append('<img style="width:100%; height: 6px;" src="/image/image_gallery?uuid=fc832cfb-8fc3-4ac1-8229-d0464e08fd17&groupId=65251&t=1310407592079" />');
            $(holder+' p:nth-child(' + randNum3 + ')').show().append('<img style="width:100%; height: 6px;" src="/image/image_gallery?uuid=fc832cfb-8fc3-4ac1-8229-d0464e08fd17&groupId=65251&t=1310407592079" />');
        }
    });
}

function expandText(){
    $('.hiddenText').hide();
    $('.showText').click(function(){
        if($(this).attr('class')== 'showText selectedArrow')
        {
            $(this).removeClass('selectedArrow');
            $(this).addClass('unselectedArrow');
        }
        else if($(this).attr('class')== 'showText unselectedArrow')
        {
            $(this).removeClass('unselectedArrow');
            $(this).addClass('selectedArrow');
        }
        var val = $(this).attr("href");
        $(val).slideToggle('slow');
        return false;
    });
}

/*---This method set up the background for the make a donation button*/
function setLastNavItem(){
    $('#navigation li a span:contains("MAKE A DONATION")').parent().parent().css({
        "background-image":"url(/GroundWork-group-theme/GroundWork-group-Theme/images/gwg/menu-buttonOrange.png)",
        "background-repeat":"no-repeat"
    });
    $('#navigation li a span:contains("MAKE A DONATION")').css('color','#748799');

    $('#navigation li a span:contains("MAKE A DONATION")').hover(function(){
        $(this).css('color','#08397b')
    },function(){
        $(this).css('color','#748799');
    });
}

function startSponsorsSlideShow(){
    $('#sponsorsSlideShow').cycle({
        fx: 'fade',
        speed: '5500'
    });
}

/*---This method is for the Sponsors slide show---*/
//function getMostResentNews(val1, val2){
//    $('#' + val2 + ' p:first-child').clone().appendTo($('#' + val1));
//}

//Do Not DELETE
//This method will clean the left section of the Theme to fit the questionnaire page.
//This section is to be pasted directly in the desired page (Manage page/ click on the page desired/ look and feel/ css
//
//function setQuestionnairePage(){
//    $('#content #leftContent').remove();
//    $('#content #rightContent').css('width','99%')
//}


