﻿var tc = {
    index : {}
};

tc.index.bindClickableElements = function() {
    $('.canclick').each(tc.index.bindClickableElement);
};

tc.index.bindClickableElement = function(clickableElement) {
    $(clickableElement).bind('click', tc.index.onClickableElementClick);
};

tc.index.onClickableElementClick = function(e) {
    var isValid = false;
    var clickableElement = $(e.target);
    if (clickableElement.length > 0) {
        isValid = clickableElement.hasClass('canclick');
        if (!isValid) {
            clickableElement = $(clickableElement.parents('.canclick:first')[0]);
            if (clickableElement.length > 0) {
                isValid = clickableElement.hasClass('canclick');
            }
        }
    }
    if (isValid) {
        var redirectionUrl = String(clickableElement.attr('href'));
        window.location.href = redirectionUrl;
    }
};

tc.index.onRegistrationClick = function() {
    if (tc.index.isRegistrationFormValid()) {
        $.post('/Offers/OfferSignup.aspx', $('form').serialize(), tc.index.indicateRegistrationSuccessful);
    }
};

tc.index.isRegistrationFormValid = function() {
    var isValid = ($('form input[name=FirstName]').val().length > 0);
    if (!isValid) {
        tc.index.showErrorMessage('A "First name" is required');
    } else {
        isValid = ($('form input[name=Surname]').val().length > 0);
        if (!isValid) {
            tc.index.showErrorMessage('A "Last name" is required');
        } else {
            isValid = ($('form input[name=Email]').val().length > 0);
            if (!isValid) {
                tc.index.showErrorMessage('An "Email Address" is required');
            }
        }
    }
    if (isValid) {
        tc.index.hideErrorMessage();
    }
    return isValid;
};

tc.index.showErrorMessage = function(errorMessage) {
    var errorMessageElement = $('form .error');
    errorMessageElement.html(errorMessage);
    errorMessageElement.show();
};

tc.index.hideErrorMessage = function() {
    var errorMessageElement = $('form .error');
    errorMessageElement.html('');
    errorMessageElement.hide();
};

tc.index.indicateRegistrationSuccessful = function(data) {
    $('form').find(':input').each(function() {
        switch (this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
                $(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
        }
    });
    tc.index.showSuccess();
};

tc.index.showSuccess = function() {
    tc.index.hideErrorMessage();
    var errorMessageElement = $('form .success');
    var signUpForm = $('form #signUpForm');
    signUpForm.hide();
    errorMessageElement.show();
};

tc.index.setupTcSearch = function() {
    var tcNameElement = $('#ctl00_Content_TcSearch_txtName');
    tcNameElement.autocomplete({
        url: '/System/TcSearchAutoComplete.ashx',
        scrollHeight: '500px',
        width: '213px',
        max: 10
    });
    tcNameElement.keyup(tc.index.onTcNameKeyUp);
    tcNameElement.parent().find('a').click(function(e) {
        tcSearchValidateName(tcNameElement.val());
        TrackJS('/TcNameSearch');
    });
    tcNameElement.watermark('Type Travel Counsellor\'s name', { css: { 'font-size': '20px'} });
    tcNameElement.data('avoidRestyling', true);
};

tc.index.setupPostcodeSearch = function() {
    var tcPostCodeElement = $('#ctl00_Content_TcSearch_txtPostcode');
    tcPostCodeElement.parent().find('a').click(function(e) {
        tcSearchValidatePostcode(tcPostCodeElement.val());
    });
    tcPostCodeElement.keyup(tc.index.onPostCodeKeyUp);
    tcPostCodeElement.data('avoidRestyling', true);
    tcPostCodeElement.watermark('Type your postcode');
};

tc.index.setupSpinner = function() {
    var container = $('#top-container-homepage .Detail');
    var selectorHtml = '<div class="home-selector">';
    var offset = 0;
    container.find('img').each(function() {
        var heroImage = $(this);
        var displayIndex = String(heroImage.attr('index'));
        heroImage.wrap('<div index="' + displayIndex + '" />');
        var heroWrapper = heroImage.parent();
        heroWrapper.hide();
        var buttonHtml = '<a href="';
        if (displayIndex == '0') {
            buttonHtml += 'javascript:void(0);" onclick="LoadVideo(\'http://www.travelcounsellors.co.uk/videos/consumer/uk/ConsumerWebVideo.flv\', \'Travel Counsellors\'); TrackJS(\'/ConsumerWebVideo\');';
        } else if (displayIndex == '1') {
        buttonHtml += '/Why-Book-With-Us/Complete-Financial-Protection';
        } else if (displayIndex == '2') {
            buttonHtml += '/Offers';
        } else if (displayIndex == '3') {
            buttonHtml += '/Why-Book-With-Us/Exceptional-Customer-Service';
        }
        buttonHtml += '" class="home-rotator-button home-rotator-button-' + displayIndex + '">&nbsp;</a>';
        heroWrapper.append(buttonHtml)
        selectorHtml += '<a style="left:' + String(offset) + 'px;" href="javascript:void(0)" jumpto="' + displayIndex + '">&nbsp;</a>';
        offset += 30;
    });
    container.show();
    selectorHtml += '</div>';
    container.append(selectorHtml);
    var intervalIdentifier = setInterval(tc.index.doNextSpinner, 4000);

    container.find('.home-selector a').click(function() {
        var linkSelector = $(this);
        var jumpTo = Number(linkSelector.attr('jumpto'));
        clearInterval(intervalIdentifier)
        tc.index.displaySpinnerIndex(jumpTo);
    });

    tc.index.doNextSpinner();
};

tc.index.doNextSpinner = function() {
    var container = $('#top-container-homepage .Detail');
    var maxIndex = 0;
    container.find('div').each(function() {
        var heroImage = $(this);
        var displayIndex = Number(heroImage.attr('index'));
        if (displayIndex > maxIndex) {
            maxIndex = displayIndex;
        }
    });

    var currentIndex = Number(container.attr('displayindex'));
    if (String(currentIndex) == 'NaN' || currentIndex == maxIndex) {
        currentIndex = 0;
    } else {
        currentIndex++;
    }
    tc.index.displaySpinnerIndex(currentIndex);
};

tc.index.displaySpinnerIndex = function(displayIndex) {
    var container = $('#top-container-homepage .Detail');
    var currentDisplayIndex = container.attr('displayindex');
    if (Number(displayIndex) != Number(currentDisplayIndex)) {
        container.attr('displayindex', displayIndex);

        var toBeDisplayed = container.find('div[index="' + String(displayIndex) + '"]');
        if (currentDisplayIndex != NaN) {
            var wasDisplayed = container.find('div[index="' + String(currentDisplayIndex) + '"]');
            wasDisplayed.attr('zIndex', 0);
            container.find('.home-selector a[jumpto="' + String(currentDisplayIndex) + '"]').removeClass('current');
            toBeDisplayed.attr('zIndex', 9999);
            container.find('.home-selector a[jumpto="' + String(displayIndex) + '"]').addClass('current');
            toBeDisplayed.fadeIn(1000, function() {
                wasDisplayed.hide();
            });
        } else {
            toBeDisplayed.fadeIn(1000);
        }
    }
};

tc.index.setupEmailForm = function() {
    var firstName = $('form input[name=FirstName]');
    var surname = isValid = $('form input[name=Surname]');
    var email = isValid = $('form input[name=Email]');

    firstName.watermark('First name');
    surname.watermark('Last name');
    email.watermark('Email address');
}

tc.index.onTcNameKeyUp = function(e) {
    cookieWrite('PreviousPage', window.location, 1);
    tcSearchNameEnter(this, e);
};

tc.index.onPostCodeKeyUp = function(e) {
    cookieWrite('PreviousPage', window.location, 1);
    tcSearchPostcodeEnter(this, e);
};

tc.index.setupBannerFade = function() {
    var animElements = $('.home-menu');
    animElements.each(function() {
        var animElement = $(this);
        var newElement = $('<div>&nbsp;</div>');
        newElement.css({
            opacity: 0
        });

        animElement.hover(function() {
            newElement.animate({
                opacity: 0.2
            }, 200);
        }, function() {
            newElement.animate({
                opacity: 0
            }, 200);
        });

        animElement.append(newElement);
    });
};

$(document).ready(function() {
    tc.index.setupSpinner();
    tc.index.bindClickableElements();
    tc.index.setupTcSearch();
    tc.index.setupPostcodeSearch();
    tc.index.setupEmailForm();

    tc.index.setupBannerFade();
});
