﻿var totalChildren = 1;
var child = 1;
var cnter = 1;

$(document).ready(function () {
    LoadChild();
    newsTicker();
});


this.newsTicker = function() {
    var pause = 20000;
    var length = $('ul#newsFeed li').length;
    var temp = -1;

    this.show = function () {

        if (cnter > length)
            cnter = 1;

        var ran = cnter;

        $('ul#newsFeed li').hide();
        $('ul#newsFeed li:nth-child(' + ran + ')').fadeIn(1500);
        cnter++;
    };

    show();

    if (length > 1)
        setInterval(show, pause);
}

function childNext() {
    if (child == totalChildren) {
        return false;
    } else {
        child++;
        LoadChild();
    }
}

function childPrev() {
    if (child == 1) {
        return false;
    } else {
        child--;
        LoadChild();
    }
}

function LoadChild() {
    $.ajax({
        type: "POST",
        url: "Default.aspx/LoadChild",
        data: '{"child":"' + child + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: true,
        cache: false,
        success: function (msg) {
            var items = msg.d;
            if (items != null) {
                totalChildren = parseInt(items[0]['RowCount']);
                $('#childDesc').html(items[0]['Description']);
                $('#childImg').attr('src', items[0]['Image']);
                $('#childName').html(items[0]['Name']);
                $('#childVillage').html('Rafiki Village: ' + items[0]['Village']);
                $('#childAge').html('Age: ' + items[0]['YearsOld']);
                $('#childNumber').html('Child Number: ' + items[0]['Number']);
                $('.childLink').attr('href', items[0]['DonationLink']);
                $('.childLink').html('Sponsor ' + items[0]['Name'] + ' Today!');
            }
        },
        error: function (msg) {
        },
        complete: function () {
        }
    });
}

