$(document).ready(function() {

    if ($('#submitted').val() == "0") {
        $('#lda_flash_wrapper').css('display', 'block');
        flashLda();
    } else {
        $('#lda_content').customFadeIn(1000);
        initializeForm();
    }

    $('input').each(function(index) {
        $(this).keydown(function() {
            onKeyDown($(this));
        });

        $(this).blur(function() {
            onBlur($(this));
        });

        $(this).keyup(function(e) {
            onKeyup($(this), e);
        });

        $('input').each(function(index) {
            $(this).keydown(function(e) {
                onFormKeyDown($(this), e);
            });
        });

        $('select').each(function(index) {
            $(this).keydown(function(e) {
                onFormKeyDown($(this), e);
            });
        });

    });
});

var previousValue;
//Swfs to preload.
var swfsForPreload = ['intro',
                      'falling_feather',
                      'grouse_fly',
                      'sound_player',
                      '/serves/grouse-and-sparkling-apple',
                      '/serves/the-famous-grouse-on-ice',
                      '/serves/the-famous-grouse-with-a-twist', 
                      '/serves/the-ginger-grouse'];
var imagesForPreload = ['/layout/lda.png',
                        '/pages/home.png',
						'/products/medium/bottle01_over.png',
						'/products/medium/bottle02_over.png',
						'/products/medium/bottle03_over.png',
						'/products/medium/bottle04_over.png',
						'/products/medium/bottle05_over.png',
						'/products/medium/bottle06_over.png',
						'/products/medium/bottle07_over.png',
						'/products/medium/bottle08_over.png',
						'/products/medium/bottle09_over.png',
                        '/pages/shop_sky.png',
                        '/pages/btl_background.png',
                        '/pages/visit_tree.png',
                        '/pages/home_boulder.png',
                        '/pages/home_crate.png',
                        '/pages/home_gate.png',
                        '/pages/home_tree.png',
                        '/pages/btl_bridge.png',
                        '/pages/btl_gate.png',
                        '/pages/btl_tree.png',
                        '/pages/shop_feathers.png',
                        '/pages/shop_grouse.png',
                        '/pages/shop_hip_flask.png',
                        '/pages/shop_label_maker.png',
                        '/pages/shop_sky.png',
                        '/pages/shop_whisky_glass.png',
                        '/pages/visit_barrels.png',
                        '/pages/visit_still.png'];
var preloadDiv;

function onKeyDown(field) {
    if (field.val() == "DD"
    || field.val() == "MM"
    || field.val() == "YYYY") {
        field.val("");
        return;
    }

    previousValue = field.val();
}

function onBlur(field){
    if (field.val() == "")
    {
        if(field.attr('name') == 'day')
        {
            field.val('DD');
        }
        if(field.attr('name') == 'month')
        {
            field.val('MM');
        }
        if(field.attr('name') == 'year')
        {
            field.val('YYYY');
        }                
    }
}

function onKeyup(field, event) {
    var chars;

    if (!testInput(field.val())) {
        field.val(previousValue);
        previousValue = "";
        return;
    }
    previousValue = "";

    if (event.which < 48
    || event.which > 57) {
        return;
    }

    if (field.attr('name') == "day"
    || field.attr('name') == "month") {
        chars = 2;
    } else {
        chars = 4;
    }

    if (field.val().length == chars) {
        if (field.attr('name') == 'day') {
            $('#month').focus();
            $('#month').select();
        } else if (field.attr('name') == 'month') {
            $('#year').focus();
            $('#year').select();
        } else {
            $('#select_country').focus();
        }
    }   
    
}

function testInput(x) {
    if (x == "") {
        return true;
    }
    if (x == "DD" || x == "MM" || x == "YYYY") {
        return true;
    }

    var re = /^(\s|\d)+$/;
    return re.test(x);
}

function flashLda() {
    
    var attributes = {
        id: 'lda_grouse',
        name: 'lda_grouse'
    };
    attributes['class'] = 'flashObject';
    swfobject.embedSWF('/Content/swf/lda_grouse.swf', 'flash_target', '955', '449', '8', false, false, false, attributes, false);
}

function ldaReady() {

    //Needs a wee delay, don't know why as it should be loaded by time it calls this!
    setTimeout(function() { thisMovie('lda_grouse').startAtStart(); }, 1);

    //Start preloading images.
    $.preload(imagesForPreload, {
        base: '/Content/images'
    });
    var currentItem = 0;
    preloadDiv = $("<div />");
    preloadSwf(swfsForPreload, currentItem);
}

function preloadSwf(swfList, itemNo) {
    nextItem = itemNo + 1;
    if (itemNo < swfList.length) {
        var swfUrl = "/Content/swf/" + swfList[itemNo] + ".swf";
        //$('#flash_preload').load(swfUrl, preloadSwf(swfList, nextItem));
        preloadDiv.load(swfUrl, preloadSwf(swfList, nextItem));
    }
}

function ldaComplete() {
    $('#lda_content').customFadeIn(5);
    $('#lda_flash_wrapper').customFadeOut(200);
    initializeForm();
}

function initializeForm() {
    $('input.focus:first').focus();
    $('input.focus:first').select();
}

function onFormKeyDown(field, event) {
    if (event.which == 13) {
        $('#lda_content').customFadeOut(1500, function() {
            $('form')[0].submit();
        });
    }
}