﻿var speed = 500;
var current = 0;
var properties = [];
var animating = false;
var slideshowTime = 5000;
var timer = 0;
var enableAutoTimer = true;
var cacheAhead = 4;
var cacheBehind = 2;

function setDefaults(target, propertList, timeEnabled, timerDelay, transitionTime, setCacheAhead, setCacheBehind) {
    var slides = $('.slideframe1');

    speed = transitionTime;
    slideshowTime = timerDelay;
    enableAutoTimer = timeEnabled;
    properties = propertList;
    current = target;
    cacheAhead = setCacheAhead;
    cacheBehind = setCacheBehind;

    slides.each(function (index) {
        var id = $(this).attr('rel');
        var opacity = (id != 'slide_' + target ? 0 : 1);
        var zIndex = (id != 'slide_' + target ? slides.length - index + 10 : slides.length + 10);

        $(this).css({ opacity: opacity, 'z-index': zIndex });

        properties[index].loaded = (index >= current - cacheBehind && index <= current + cacheAhead);
    });

    var left = properties.length < 4 || target < 2 ? 0 : (target < properties.length - 2 ? (target - 1) * 185 : (properties.length - 4) * 185);

    $('.footer .frame .center .sleeve').css({ width: (properties.length * 185) + 'px', marginLeft: -left + 'px' });

    showTime();
    move(target);
}

function move(target) {
    if (animating)
        return;

    setAdditonalBanners();

    var slides = $('.slideframe1');

    if (properties[target].StyleID == 1)
        $('.header_0').removeClass('header_0').addClass('header_1');
    else
        $('.header_1').removeClass('header_1').addClass('header_0');

    $('.footer .frame .center .button').each(function (index) {
        $(this).removeClass('selected');

        if (index == target)
            $(this).addClass('selected');
    });

    if (target == 0)
        $('#_Previous').removeClass('left').addClass('left_disabled');
    else
        $('#_Previous').removeClass('left').removeClass('left_disabled').addClass('left');

    if (target == properties.length - 1)
        $('#_Next').removeClass('right').addClass('right_disabled');
    else
        $('#_Next').removeClass('right').removeClass('right_disabled').addClass('right');

    var left = properties.length < 4 || target < 2 ? 0 : (target < properties.length - 2 ? (target - 1) * 185 : (properties.length - 4) * 185);

    $('.footer .frame .center .sleeve').animate({ marginLeft: -left + 'px' }, 500);
    
    document.title = 'Vectorform - ' + properties[target].Title.replace('&amp;', '&').replace('&trade;', '').replace('&reg;', '');

    if (target == current)
        return;

    slides.each(function (index) {
        var id = $(this).attr('rel');
        var opacity = (id != 'slide_' + target && id != 'slide_' + current ? 0 : 1);
        var zIndex = (id != 'slide_' + target && id != 'slide_' + current ? slides.length - index + 10 : id == 'slide_' + target ? slides.length + 10 : slides.length + 15);

        $(this).css({ opacity: opacity, 'z-index': zIndex });
    });

    animating = true;

    $(slides[current]).animate({ opacity: 0 }, speed, function () {
        $(slides[target]).css({ 'z-index': slides.length + 50 });
        current = target;
        animating = false;
        showTime();
    });
}

function showTime() {
    if (!enableAutoTimer)
        return;

    var target = (current + 1 > properties.length - 1 ? 0 : current + 1);

    clearInterval(timer);

    timer = setInterval(function () {
        move(target);
    }, slideshowTime);
}

function previous() {
    if (current > 0)
        move(current - 1);
}

function next() {
    if (current < properties.length - 1)
        move(current + 1);
}

function Trim(str) {
    try {
        if (!str || str == 'undefined' || str == null) {
            return '';
        } else {
            str = String(str);
            return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
        }
    } catch (e) {
        return '';
    }
}

function setAdditonalBanners() {
    var start = current - cacheBehind < 0 || current == properties.length - 1 ? 0 : current - cacheBehind;
    var end = current + cacheAhead < properties.length ? current + cacheAhead : cacheAhead;
    var slides = $('.slideframe1');

    slides.each(function (index) {
        var id = $(this).attr('rel');
        var o = properties[index];
        
            if (!o.loaded && index >= start && index <= end) {
                o.loaded = true;

                $(this).css({ 'background-image': 'url(' + o.BannerBackgroundImage + ')' });
                $(this).find('.slideframe2').css({ 'background-image': 'url(' + o.BannerForegroundImage + ')' });
                $(this).find('.slideframe2').find('.slidecontent').css({ 'background-image': 'url(' + o.AdCopyImage + ')' });
            }
    });
}

function isValidEmail(address) {
    return (address.indexOf('.') > 2) && (address.indexOf('@') > 0);
}

function submitInquery() {
    var firstName = $('.contactform .firstname');
    var lastName = $('.contactform .lastname');
    var phone = $('.contactform .phone');
    var fax = $('.contactform .fax');
    var email = $('.contactform .email');
    var type = $('.contactform .type');
    var comment = $('.contactform .comment');
    var valid = true;
    var normal = '#000000';
    var highlight = '#ed1e79';
    var alphaFilter = /^[0-9\-\+\(\)]+$/;

    firstName.css({ color: normal });
    lastName.css({ color: normal });
    phone.css({ color: normal });
    fax.css({ color: normal });
    email.css({ color: normal });

    if (Trim(firstName.val()) == '' || firstName.val() == 'First Name') {
        firstName.css({ color: highlight });
        valid = false;
    }
    if (Trim(lastName.val()) == '' || lastName.val() == 'Last Name') {
        lastName.css({ color: highlight });
        valid = false;
    }
    if (Trim(phone.val()) == '' || phone.val() == 'Phone' || !phone.val().match(alphaFilter)) {
        phone.css({ color: highlight });
        valid = false;
    }
    if (Trim(phone.val()) == '' || phone.val() == 'Phone' || !phone.val().match(alphaFilter)) {
        phone.css({ color: highlight });
        valid = false;
    }
    if (Trim(fax.val()) != '' && Trim(fax.val()) != 'Fax' && !fax.val().match(alphaFilter)) {
        fax.css({ color: highlight });
        valid = false;
    }
    if (Trim(email.val()) != '' && Trim(email.val()) != 'Email Address' && !isValidEmail(Trim(email.val()))) {
        email.css({ color: highlight });
        valid = false;
    }
    
    if (!valid) {
        if (Trim(firstName.val()) == '' || firstName.val() == 'First Name') {
            alert('Your first name is required to continue.');
            firstName.click();
            firstName.focus();
        } else if (Trim(lastName.val()) == '' || lastName.val() == 'Last Name') {
            alert('Your last name is required to continue.');
            lastName.click();
            lastName.focus();
        } else if (Trim(phone.val()) == '' || phone.val() == 'Phone' || !phone.val().match(alphaFilter)) {
            alert('A valid phone number is required to continue.');
            phone.click();
            phone.focus();
        } else if (Trim(fax.val()) != '' && Trim(fax.val()) != 'Fax' && !fax.val().match(alphaFilter)) {
            alert('A valid fax number is required to continue.');
            fax.click();
            fax.focus();
        } else if (Trim(email.val()) != '' && Trim(email.val()) != 'Email Address' && !isValidEmail(Trim(email.val()))) {
            alert('A valid email address is required to continue.');
            email.click();
            email.focus();
        }
        return;
    }


    var faxNumber = fax.val() == 'Fax' ? 'n/a' : fax.val();
    var emailAddress = email.val() == 'Email Address' ? 'n/a' : email.val();
    var subjectLine = type.val() == 'Type of Inquery' ? 'n/a' : type.val();
    var theComments = comment.val() == 'Comment' ? 'n/a' : comment.val();

    var obj = {
        FirstName: firstName.val(),
        LastName: lastName.val(),
        Phone: phone.val(),
        Fax: faxNumber,
        Email: emailAddress,
        Subject: subjectLine,
        Comment: theComments
    }

    var json = $.toJSON(obj);

    var o = {
        async: true,
        type: 'POST',
        url: '/services',
        cache: false,
        data: 'c=sendinquery&json=' + json,
        error: function (msg) {
            alert('Thank you for your interest in Vectorform!');

            resetContactForm();
        },
        success: function (msg) {
            alert('Thank you for your interest in Vectorform!');

            resetContactForm();
        }
    };

    $.ajax(o);
}

function resetContactForm() {
    var firstName = $('.contactform .firstname');
    var lastName = $('.contactform .lastname');
    var phone = $('.contactform .phone');
    var fax = $('.contactform .fax');
    var email = $('.contactform .email');
    var type = $('.contactform .type');
    var comment = $('.contactform .comment');

    firstName.val('First Name');
    lastName.val('Last Name');
    phone.val('Phone');
    fax.val('Fax');
    email.val('Email Address');
    type.val('Type of Inquiry');
    comment.val('Comment');
}
