// $Id: jquery_x.js 9 2009-11-26 17:50:22Z Ian.H $

// Init
$(function() {
    // Display main video / info
    if ($('div.testimonial-text').length < 1) {
        var autoplay = true;

        if ($('#frm-contact').length > 0) {
            autoplay = false;
        }

        displayMainVideo(autoplay, 50);
    } else {
        getTestimonial(1, true, 50);
    }

    // Testimonial selector
    $('div.testimonial-pager span').click(function() {
        var data = $(this).attr('rel');
        var tData = data.split('||');

        getTestimonial(tData[0], tData[1], 50);
    });

     // Contact us field focus
//    if ($('input#name').length > 0) {
//        $('input#name').focus();
//    }

    // Contact us form submittal
    $('#btn-send').click(function() {
        return submitContactForm();
    });

    // Contact us form type selector
    $('#form-type-contact').click(function() {
        $('div.order-fields').hide();
        $('div.contact-fields').fadeIn();
    });
    $('#form-type-order').click(function() {
        $('div.contact-fields').hide();
        $('div.order-fields').fadeIn();
    });

    // Ajax msgbox hide
    $('div.msgbox-ajax').click(function() {
        $(this).hide();
    });
});

// Display testimonial video
function getTestimonial(id, autoPlay, initVolume) {
    $.ajax({
        url:        'get-testimonial.php?id=' + id,
        success:    function(data) {
            var docRoot = '';
            var tData = new Array();
            tData = data.split('||');

            docRoot = tData[5];

            // Video
            $('div.testimonials-video-container').empty();
            $('div.testimonials-video-container').flash({
                src:                    'video.swf',
                width:                  512,
                height:                 354,
                allowFullScreen:        true,
                wmode:                  'transparent',
                flashvars:              {
                    flvpVideoSource:    docRoot + '_video/media/' + tData[1],
                    flvpAutoStartMovie: autoPlay,
                    flvpInitVolume:     initVolume
                }
            });

            // Still
            $('div.testimonials-video-still').html('<img src="' + tData[5] + '_video/thumbs/_' + tData[2] + '.png" alt="" />');
            $('div.testimonials-video-still').show();

            // Title
            $('.testimonial-header').html(tData[3]);

            // Text
            $('div.testimonial-text').html(tData[4]);
        }
    });
}

// Display main video
function displayMainVideo(autoPlay, initVolume) {
    // Video
    $('div.testimonials-video-container').empty();
    $('div.testimonials-video-container').flash({
        src:                    'video.swf',
        width:                  512,
        height:                 354,
        allowfullscreen:        true,
        wmode:                  'transparent',
        flashvars:              {
            flvpVideoSource:    '_video/media/svs.flv',
            flvpAutoStartMovie: autoPlay,
            flvpInitVolume:     initVolume
        }
    });

    // Still
    $('div.testimonials-video-still').hide();
    $('div.testimonials-video-still').empty();
}

// Submit contact us form
function submitContactForm() {
    var formType;
    var errorField;
    var formError = false;

    // Determine form type
    if ($('#form-type-contact').attr('checked') == true) {
        formType = 'contact';
    }
    if ($('#form-type-order').attr('checked') == true) {
        formType = 'order';
    }

    if (formType == 'contact') {
        var reqFields = new Array('name', 'email', 'phone', 'body');

        for (var i = 0; i < reqFields.length; i++) {
            if ($('#' + reqFields[i]).val() == '') {
                formError = true;
                errorField = reqFields[i];
                break;
            }
        }
    } else if (formType == 'order') {
        var reqFields = new Array('name', 'email', 'phone', 'num-dvds', 'address');

        for (var i = 0; i < reqFields.length; i++) {
            if ($('#' + reqFields[i]).val() == '') {
                formError = true;
                errorField = reqFields[i];
                break;
            }
        }
    }

    if (formError) {
        clearErrorFields();
        setErrorField(errorField);
        showErrorMsg('Please complete all required fields.', 'error');
        return false;
    }

    return true;
}

// Clear fields of error status
function clearErrorFields() {
    var fields = new Array(
        'name',
        'email',
        'phone',
        'company',
        'num-dvds',
        'title',
        'body',
        'address'
    );

    for (var i = 0; i < fields.length; i++) {
        $('#' + fields[i]).removeClass('error-field');
    }

    $('span.error-req-field').remove();
}

// Set field as containing an error
function setErrorField(field) {
    $('#' + field).addClass('error-field');
    $('#' + field).after('<span class="error-req-field">required field</span>');
    $('#' + field).focus();
}

// Contact form error message
function showErrorMsg(msg, type) {
    var style;

    if (type == 'error') {
        style = 'errormsg';
    } else if (type == 'success') {
        style = 'successmsg';
    }

    $('div.msgbox-ajax').addClass(style);
    $('div.msgbox-ajax').html(msg);
    $('div.msgbox-ajax').fadeIn();
}
