﻿$(document).ready(function () {
    StartAnalysis();
    StartSchedule();
    StartPoll();
    StartMailing();
});

//*** Analysis ***//
function StartAnalysis() {
    var currentPosition = 0;
    var slideWidth = 220;
    var slides = $('.HomePage .Analysis .Box');
    var numberOfSlides = slides.length;
    var myTimeout;
    var myTimeoutInterval = 7000;

    // Remove scrollbar in JS
    $('.HomePage .Analysis .Container').css('overflow', 'hidden');

    // Wrap all .slides with #slideInner div
    slides.wrapAll('<div id="slideInner"></div>')

    // Float left to display horizontally, readjust .slides width
	.css({
	    'float': 'left',
	    'width': slideWidth
	});

    // Set #slideInner width equal to total width of all slides
    $('#slideInner').css('width', slideWidth * numberOfSlides);

    //*** Controls
    $('.HomePage .Analysis .Control .Item').css({ opacity: .4 });
    $('.HomePage .Analysis .Control .Item:first').css({ opacity: 1 });

    $('.HomePage .Analysis .Control .Item').mouseenter(function (e) {
        currentPosition = $(this).index();
        if (myTimeout) clearTimeout(myTimeout);
        ShowProgram();
    });

    $('.HomePage .Analysis .Control .LeftArrow').click(function (e) {
        currentPosition -= 1;
        if (currentPosition < 0) { currentPosition = $('.HomePage .Analysis .Control .Item:last').index(); }

        if (myTimeout) clearTimeout(myTimeout);
        ShowProgram();
    });

    $('.HomePage .Analysis .Control .RightArrow').click(function (e) {
        currentPosition += 1;
        if (currentPosition > $('.HomePage .Analysis .Control .Item:last').index()) { currentPosition = 0; }

        if (myTimeout) clearTimeout(myTimeout);
        ShowProgram();
    });

    myTimeout = setTimeout(GetNextProgram, myTimeoutInterval);
    function GetNextProgram() {
        currentPosition += 1;
        if (currentPosition > $('.HomePage .Analysis .Control .Item:last').index()) { currentPosition = 0; }

        ShowProgram();
        myTimeout = setTimeout(GetNextProgram, myTimeoutInterval);
    }

    function ShowProgram() {
        $('.HomePage .Analysis .Control .Item').css({ opacity: .4 });
        $('.HomePage .Analysis .Control .Item:eq(' + currentPosition + ')').css({ opacity: 1 });

        $('#slideInner').animate({
            'marginLeft': slideWidth * (-currentPosition)
        }, { queue: false });
    }
}

//*** Schedule ***//
function StartSchedule() {
    var div = $('div.HomeScheduleScroll');
    var ul = $('ul.HomeScheduleScroll');

    var divHeight = div.height();
    var lastLi = ul.find('li:last-child');

    div.css({ overflow: 'hidden' });

    //*** Mouse Scroll
    div.mousemove(function (e) {
        var ulHeight = lastLi[0].offsetTop + lastLi.outerHeight();
        var top = ((e.pageY - div.offset().top) * (ulHeight - divHeight) / divHeight);

        div.scrollTop(top);
    });

    //*** Hover efect
    $('li').mouseenter(function () {
        $(this).css('background-color', '#ffffff');
    });

    $('li').mouseleave(function () {
        $(this).css('background-color', '');
    });
}

//*** Poll ***//
function StartPoll() {
    $('#btnPoll').click(function (e) {
        var idPoll = $('.HomePage .Poll .Question').attr('rel');
        var vote = $('.HomePage .Poll .Option input:checked').val();

        if ((vote == '1') || (vote == '2') || (vote == '3') || (vote == '4') || (vote == '5') || (vote == '6') || (vote == '7') || (vote == '8') || (vote == '9')) {
            $('.HomePage .Poll .BoxPoll').css({ display: 'none' });
            $('.HomePage .Poll .Loading').css({ display: 'block' });

            $.post('/Poll/Vote.ashx', {
                IdPoll: idPoll,
                Vote: vote

            }, function (data) {
                $('.HomePage .Poll .Loading').css({ display: 'none' });

                if (data.Status == 'error') {
                    $('.HomePage .Poll .BoxPoll').css({ display: 'block' });

                } else {
                    $('.HomePage .Poll .Result').css({ display: 'block' });
                    $('.HomePage .Poll .Result').html(data.Message);
                }

            }, 'json');
        } else {
            alert('Selecione uma opção.');
        }
    });
}

//*** Mailing ***//
function StartMailing() {
    $('.HomePage .Newsletter .TextBox').focusout(function () { FieldTrim(this); });
    $('.HomePage .Newsletter .Required').focusout(function () { FieldRequired(this); });
    $('.HomePage .Newsletter .MaskMail').focusout(function () { FieldMail(this); });

    $('#btnMailing').click(function (e) {
        if (IsHomeFormValid()) {
            $('.HomePage .Newsletter .BoxNews').css({ display: 'none' });
            $('.HomePage .Newsletter .Loading').css({ display: 'block' });

            $.post('/Register/Mailing.ashx', {
                Name: $('#txtNameMailing').val(),
                Mail: $('#txtMailMailing').val(),
                Theme: $('#btnMailing').attr('rel')

            }, function (data) {
                if (data.Status == 'error') {
                    $('.HomePage .Newsletter .BoxNews').css({ display: 'block' });
                    $('.HomePage .Newsletter .Loading').css({ display: 'none' });

                } else {
                    location.href = '/cadastro';
                }

            }, 'json');
        }
    });
}

function IsHomeFormValid() {
    IsValid = true;

    $('.HomePage .Newsletter .TextBox').each(function (i) { FieldTrim(this); });
    $('.HomePage .Newsletter .Required').each(function (i) { FieldRequired(this); });
    $('.HomePage .Newsletter .MaskMail').each(function (i) { FieldMail(this); });

    if (!IsValid) {
        $('.HomePage .Newsletter .TextBoxInvalid:first').focus();
    }

    return IsValid;
}
