/*
 Author: arnabc@quadnode.com

 */

( function () {
    var carousel;

    $(document).ready(function () {
        var carouselControl = $('#carousel-external-controls');

        function selectControl(index) {
            carouselControl.find('span.selected').removeClass('selected');
            carouselControl.find('li:eq(' + index + ') span').addClass('selected');
        }

        // initialize callback
        function carouselInitCallback(carousel) {
            carouselControl.find('span').click(function () {
                var index = $(this).parent().index();

                selectControl(index);
                carousel.scroll(index + 1, true);
            });
        }

        // if mangoreader page
        if ($(document.body).hasClass('mangoreader')) {
            carousel = $('#carousel').jcarousel({
                animation: 'slow',
                scroll: 1,
                wrap: 'both',
                auto: 10,
                itemVisibleInCallback: function (carousel, li, index, state) {
                    $('#carousel-info').find('.item').hide().siblings('.item-' + index).fadeIn(800, 'swing');
                    selectControl(index - 1);
                },
                initCallback: carouselInitCallback,
                buttonNextHTML: null,
                buttonPrevHTML: null
            });

            $('#carousel').css('visibility', 'visible');
        }


        var invite_form = $('#invite-form'), label, input, placeholder, isSupported;
        if (invite_form.length) {

            // feature detect HTML5 'placeholder' attribute is present in the browser or not
            isSupported = !!( 'placeholder' in document.createElement( 'input' ) );

            label = invite_form.find('label');
            input = invite_form.find('input[type=text]');

            if ( !isSupported ) {

                placeholder = input.attr('placeholder');

                label.click(function () {
                    $(this).hide();
                });

                input.blur(function () {
                    var v = $(this).val();

                    if (!v || v == placeholder) {
                        $(label).show();
                        $(this).val('');
                    }
                });
            } else {
                label.hide();
            }


            // beta invite
            $('#invite-form').submit(function () {
                var email = $(this).find('input[type=text]').val(), form = $(this);

                $('.error', $(this).parent()).css('color', 'green').hide();

                if (!/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(email)) {
                    $('.error', $(this).parent()).html('Not a valid email id, please enter a valid email').css('color', 'red').show();
                    return false;
                }

                function showMessage(message, messageColor) {
                    messageColor = messageColor || 'green';

                    var el = $('.error', $(form).parent()).css('color', messageColor).html(message).show();
                    showMessage.timeout = setTimeout(function () {
                        el.hide('slow');
                    }, 5000);
                }

                showMessage('Sending Email, please wait...', '#444');

                $.ajax({
                    type: 'post',
                    url: $(this).attr('action'),
                    data: { email: email },
                    success: function (data, status, xhr) {
                        clearTimeout( showMessage.timeout );

                        $( form.parent() ).replaceWith( data );
                    },
                    error: function (xhr, status, errThrown) {
                        showMessage(xhr.responseText, 'red');
                        form[0].reset();
                    }
                });

                return false;
            });

        }

    });


} )();
























