/* Copyright Mercy International */

function onLoad() {
    buildQuotesComponent();
    buildMenu();
    buildSponsorNowComponent();
    loadPageImages();
}
function changeFlag() {
    var currency = document.getElementById("ddlCurrency").value;
    var currencyFlag = document.getElementById("imgCurrencyFlag");
    if (currency == "USD")
        currencyFlag.src = "images/UsaFlagSmall.jpg";
    else if (currency == "EURO")
        currencyFlag.src = "images/EuroFlagSmall.jpg";
    else
        currencyFlag.src = "images/AustralianFlagSmall.jpg";

    if (document.getElementById("hCurrency"))
        document.getElementById("hCurrency").value = currency;
}
function setLabelText(id, txt) {
    var elem;
    if (document.getElementById && (elem = document.getElementById(id))) {
        if (!elem.firstChild)
            elem.appendChild(document.createTextNode(txt));
        else
            elem.firstChild.data = txt;
    }
    return false;
}
function loadPageImages() {
    // This function was added to force loading of page specific images after menu and sidebar loaded to visually improve performance
    imgs=document.getElementsByTagName('img');
    for (i = 0; i < imgs.length; i++) {
        if (imgs[i].getAttribute("imgFile"))
            imgs[i].src = imgs[i].getAttribute("imgFile");
    }
}
function shuffleArray() {
    return (Math.round(Math.random()) - 0.5);
}

/* ---- Get/Set cookies for recording currency information ---- */

function setCurrencyCookie(currency) {
    var exp = new Date();
    exp.setTime(exp.getTime() + (1000 * 60 * 60 * 24 * 364));     //set expiration 364 days ahead 
    var cookieVal = "MercyIntCurrency=" + escape(currency) + "; expires=" + exp.toUTCString();
    document.cookie = cookieVal;

}
function getCurrencyCookie() {
    var dc = document.cookie.split("; ");
    var cname = "MercyIntCurrency";
    for (var i = 0; i < dc.length; i++) {
        var aCrumb = dc[i].split("=");
        if (cname == aCrumb[0]) 
            return unescape(aCrumb[1]);
    }
    return null;
}
function displayDefaultCurrency() {
    
    var currency = getCurrencyCookie();
    if (currency == "AUD" || currency == "USD" || currency == "EURO") {
        $("#ddlCurrency").val(currency);
        changeFlag();
    }
}
function saveDefaultCurrency() {
    var currency = document.getElementById("ddlCurrency").value;
    if (currency == "AUD" || currency == "USD" || currency == "EURO") {
        setCurrencyCookie(currency);
    }
}
/* ---- Build Quotes Slideshow component ---- */

function buildQuotesComponent() {

    if (document.getElementById("quotesContainer")) {

        var containerHtml = '<div class="quoteSlideshow">';
        var quotes = [];
        quotes[0] = "'The poor do not need our condescending attitude or our pity.  They only need our love and our tenderness.' – Mother Teresa";
        quotes[1] = "'A person is at their tallest when they stoop to help another.'";
        quotes[2] = "'Find a need and fill it – find a hurt and heal it.'";
        quotes[3] = "'Without Faith it is impossible to please God.' Hebrews 11:6";
        quotes[4] = "'Faith is not believing that God can, but that He will.'";
        quotes[5] = "'The most effective kind of education is that a child should play amongst lovely things.' – Plato";
        quotes[6] = "'What I hold in my hand I loose but what I have given to God I still possess.' – Mother Teresa";
        quotes[7] = "'There is something beautiful in each and everyday – all we have to do is look for it.'";
        quotes[8] = "'Jesus said 'Let the little children come to me and do not forbid them, for of such is the Kingdom of God.' – Matthew 19:14";
        quotes[9] = "'He who has pity on the poor lends to the Lord and God will pay back what he has given.' – Proverbs 19:17";
        quotes[10] = "'Pure and undefiled religion before God the Father is this, to visit orphans and widows in their trouble and to keep oneself unspotted from the world.'– James 1:27";
        quotes[11] = "'Invest in your children, give them time, your attention, your love – they are the future.'";
        quotes[12] = "'Observe the boy and you'll see the man.'";
        quotes[13] = "'Its easier to build a boy than mend a man.'";
        quotes[14] = "'Sponsor a child and make a difference.'";
        quotes[15] = "'You can do what I cant do and I can do what you cant do, but together we can make things happen - Mother Teresa.'";
        quotes[16] = "'Never look down on anyone unless you are helping them up.'";
        quotes[17] = "'The more you do for others the more God and others will do for you.'";
        quotes[18] = "'If you teach a boy you educate an individual, if you teach a girl you educate a community.'";
        quotes[18] = "'Each year an estimated 1.2 million children are trafficked into commercial sex trade and bonded labour!'";
        quotes[19] = "'Save a child - Save a life.'";
        quotes[20] = "'Although the world is full of suffering, it is full also of the overcoming of it.' - Helen Keller";
		        

        quotes.sort(shuffleArray);

        for(i=0; i<21; i++) {

            if (i == 10)
                containerHtml = containerHtml + "<a text-align='center'>" + quotes[i] + "</a>";
            else
                containerHtml = containerHtml + "<a>" + quotes[i] + "</a>";
        }
        
        containerHtml = containerHtml + '</div>';
        document.getElementById("quotesContainer").innerHTML = containerHtml;
        
        $('.quoteSlideshow').cycle({
            timeout: 6000,
            speed: 500 
        });
    }
}

/* ---- Build SponsorNow Slideshow component ---- */

function buildSponsorNowComponent() {

    if (document.getElementById("sponsorDonateContainer")) {

        var maxImgNumber = 26;
        var imgNumber1 = Math.floor(Math.random() * (maxImgNumber + 1));
        if ((imgNumber1 < 1) || (imgNumber1 > maxImgNumber))
            imgNumber1 = 1;
        var imgNumber2 = 1;
        var imgNumber3 = 1;
        var imgNumber4 = 1;
        if (imgNumber1 < maxImgNumber)
            imgNumber2 = imgNumber1 + 1;
        if (imgNumber2 < maxImgNumber)
            imgNumber3 = imgNumber2 + 1;
        if (imgNumber3 < maxImgNumber)
            imgNumber4 = imgNumber3 + 1;

        var containerHtml = '<div class="sponsorDonateTop"><a href="SponsorNow.html" class="buttonText">Become a sponsor today</a></div>';
        containerHtml = containerHtml + ' <div class="sponsorDonateSlideshow">';
        containerHtml = containerHtml + '<a href="SponsorNow.html"><img id="imgSponsorNowSlide1" src="images/SponsorNowSlide' + imgNumber1.toString() + '.jpg" alt="Sponsor Now" title="Become a sponsor today" /></a>';
        containerHtml = containerHtml + '<a href="SponsorNow.html"><img id="imgSponsorNowSlide2" src="images/SponsorNowSlide' + imgNumber2.toString() + '.jpg" alt="Sponsor Now" title="Become a sponsor today" /></a>';
        containerHtml = containerHtml + '<a href="SponsorNow.html"><img id="imgSponsorNowSlide3" src="images/SponsorNowSlide' + imgNumber3.toString() + '.jpg" alt="Sponsor Now" title="Become a sponsor today" /></a>';
        containerHtml = containerHtml + '<a href="SponsorNow.html"><img id="imgSponsorNowSlide4" src="images/SponsorNowSlide' + imgNumber4.toString() + '.jpg" alt="Sponsor Now" title="Become a sponsor today" /></a>';
        containerHtml = containerHtml + '</div>';
        containerHtml = containerHtml + '<div class="sponsorDonateBottom"><p><a href="SponsorNow.html"><img src="images/Arrow.jpg" alt="Sponsor a child in need" style="padding:0" /></a>&nbsp;<strong>Sponsors urgently needed</strong></p>';
        containerHtml = containerHtml + '<p>Child Sponsorships, Child Education sponsorships or why not make a donation to ongoing projects such as building new homes for children or furnishing a classroom.</p>';
        containerHtml = containerHtml + '<p><a href="SponsorNow.html" class="buttonText" style="padding:35px"><img src="images/SponsorNowButton.jpg" alt="Sponsor Now" /></a></p></div>';
        document.getElementById("sponsorDonateContainer").innerHTML = containerHtml;

        $('.sponsorDonateSlideshow').cycle({
            fx: 'fade',
            timeout: 4000,
            speed: 1400
        });
    }
}

/* ---- Navigation Menu ---- */
var timeout = 500;
var closetimer = 0;
var navmenuitem = 0;

function nav_open() {
    nav_canceltimer();
    nav_close();
    navmenuitem = $(this).find('ul').css('visibility', 'visible');
}

function nav_close()
{ if (navmenuitem) navmenuitem.css('visibility', 'hidden'); }

function nav_timer()
{ closetimer = window.setTimeout(nav_close, timeout); }

function nav_canceltimer() {
    if (closetimer) {
        window.clearTimeout(closetimer);
        closetimer = null;
    }
}

function add_li(list, text) {
    var list = document.getElementById(list);
    var li = document.createElement("li");
    li.innerHTML = text;
    list.appendChild(li);

}
function buildMenu() {
    add_li("nav", "<a href='index.html'>Home</a>");
    add_li("nav", "<a href='History.html'>About Us</a><ul id='aboutUs'></ul>");
    add_li("aboutUs", "<a href='History.html'>History</a>");
    add_li("aboutUs", "<a href='Directors.html'>Directors</a>");
    add_li("aboutUs", "<a href='Admin.html'>Administration</a>");
    add_li("nav", "<a href='Sponsor.html'>Sponsor</a><ul id='sponsor'></ul>");
    add_li("sponsor", "<a href='SponsorNow.html'>Sponsor a child</a>");
    add_li("sponsor", "<a href='SponsorsStories.html'>Sponsors Stories</a>");
    add_li("nav", "<a href='Donate.html'>Donate</a>");
    add_li("nav", "<a href='GetInvolved.html'>Get Involved</a>");
    add_li("nav", "<a href='Projects.html'>Projects</a><ul id='projects'></ul>");
    add_li("projects", "<a href='ProjectsPetchabunVillage.html'>Petchabun - Childrens Village</a>");
    add_li("projects", "<a href='ProjectsPetchabunSchool.html'>Petchabun - Christian School</a>");
    add_li("projects", "<a href='ProjectsPetchabunAgriculture.html'>Petchabun - Agriculture</a>");
    add_li("projects", "<a href='ProjectsPetchabunOutreach.html'>Petchabun - Community Outreach</a>");
    add_li("projects", "<a href='ProjectsPhraeVillage.html'>Phrae - Childrens Village</a>");
    add_li("projects", "<a href='ProjectsPhraeAgriculture.html'>Phrae - Agriculture</a>");
    add_li("projects", "<a href='ProjectsPhraeOutreach.html'>Phrae - Community Outreach</a>");
    add_li("projects", "<a href='ProjectsKhonKaenHome.html'>Khon Kaen - Childrens Home</a>");
    add_li("projects", "<a href='ProjectsSisaketELS.html'>Sisaket - Mercy Language School</a>");
    add_li("nav", "<a href='News.html'>News</a><ul id='news'></ul>");
    add_li("news", "<a href='Newsletter.html'>NewsLetter</a>");
    add_li("news", "<a href='Events.html'>Events</a>");
    add_li("nav", "<a href='ContactUs.html'>Contact Us</a>");

    $('#nav > li').bind('mouseover', nav_open)
    $('#nav > li').bind('mouseout', nav_timer)
}

document.onclick = nav_close;

/* ---- end navigation---- */



