﻿$(document).ready(function () {

    // Home Banners
    var bannercount = $('#banners .banner:not(.default)').length;
    var bannermoving = true;
    var bannertimeout;
    var bannerfxspeed = 200;
    $('#banners .banner:not(.active)').hide();
    $('#banners .default').delay(6000).fadeOut(bannerfxspeed, function () {
        $('.arrow').fadeIn(bannerfxspeed);
        $('#underline').css('height', '4px').animate({
            opacity: 1
        }, 500);
        $('#logo').animate({
            top: '10px'
        }, 500);
        changebanner(0);
        $(this).remove();
    });
    function changebanner(bindex) {
        bannermoving = true;
        // validate bindex
        if (bindex < 0) {
            bindex = bannercount - 1;
        } else if (bindex >= bannercount) {
            bindex = 0;
        }
        var oldbanner = $('#banners .active').removeClass('active').fadeOut(bannerfxspeed);
        var newbanner = $('#banners .banner:not(.default):eq(' + bindex + ')').addClass('active');
        $('#banners').stop(true, true).delay(bannerfxspeed).animate({
            backgroundColor: newbanner.attr('data-bgcolor')
        }, bannerfxspeed, function () {
            newbanner.fadeIn(bannerfxspeed, function () {
                bannermoving = false;
            });
        });
        bannertimeout = setTimeout(function () { changebanner(bindex + 1) }, newbanner.attr('data-delay'));
    }
    // Next and Prev buttons
    $('.arrow').click(function () {
        if (!bannermoving) {
            clearTimeout(bannertimeout);
            var oldindex = $('#banners .active').index();
            var newindex = ($(this).is('.right')) ? oldindex + 1 : oldindex - 1;
            changebanner(newindex);
        }
    });

    // Random Testimonial
    $('#testimonial').hide();
    var txml;
    var tdelay = 10000;
    var tfxspeed = 300;
    $.ajax({
        type: "GET",
        url: "/data/testimonials.xml",
        dataType: "xml",
        success: function (xml) {
            txml = xml;
            setTimeout(randomTestimonial, 1000);
        }
    });
    function randomTestimonial() {
        var num = $(txml).find('testimonial').length;
        var randNum = Math.floor(Math.random() * num);
        var node = $(txml).find('testimonial:eq(' + randNum + ')');
        var firstQMark = $('#testimonial .quotationmark:first-child');
        $('#testimonial #quote').html($.trim($(node).find('quote').text()));
        $('#testimonial #author').html('~ ' + $.trim($(node).find('author').text()));
        $('#testimonial #credentials').html($.trim($(node).find('credentials').text()));
        $('#testimonial #company').html(' - ' + $.trim($(node).find('company').text()));
        $('#testimonial').css({ 'position': 'absolute', 'visibility': 'hidden', 'display': 'block' });
        firstQMark.css('marginBottom', $('#quote').height() + 'px');
        $('#testimonial').css({ 'position': 'static', 'visibility': 'visible', 'display': 'none' });
        $('#testimonial').fadeIn(tfxspeed).delay(tdelay).fadeOut(tfxspeed, randomTestimonial);
    }

});

