/* Global variables */

var slideWidth, slideHeight, termsHeight,
    submitting = false;

/* Initialize everything */

$(document).ready(function () {
    // Apply png fix
    $('h2,h3,.buttonArea a,a.close,img.png').supersleight({shim: 'gfx/blank.gif'});

    // Calculate dimensions
    slideWidth = $('.vframe').width();
    slideHeight = $('#ContentFrame').height();

    var termsMargin = parseInt($('#Terms').css('marginTop'), 10);
    termsHeight = $('#Terms').innerHeight() + termsMargin;

    // Attach events
    $('a.info').click(slideToInfo);
    $('a.back').click(slideToStart);
    $('a.order').click(slideToOrder);
    $('a.send').click(submitOrder);
    $('a.backorder').click(slideBackToInfo);
    $('a.terms').click(showTerms);
    $('a.termsclose,a.close').click(hideTerms);

    // Fix positioning when tabbing to input elements
    $('#CodeForm input,#CodeForm textarea').focus(function () {
        $('#Content').css({top: '0px', left: '0px'});
        $('.vcontent').css({top: '-' + slideHeight + 'px'});
        // with IE we need to scroll back to top
        $('#ContentFrame').attr('scrollTop', 0);

        $('a.info,a.back').hide();
        $('#Code a.back').show();
    });

    $('#CouponForm input,#CouponForm textarea').focus(function () {
        $('#Content').css({top: '0px', left: '-' + (slideWidth * 2) + 'px'});
        $('.vcontent').css({top: '-' + slideHeight + 'px'});
        // with IE we need to scroll back to top
        $('#ContentFrame').attr('scrollTop', 0);

        $('a.info,a.back').hide();
        $('#Coupon a.back').show();
    });

    // Form validation and submitting
    $.validator.messages.digits =
    $.validator.messages.email =
    $.validator.messages.required = '!';

    $('#CodeForm').validate({submitHandler: sendOrder});
    $('#CouponForm').validate({submitHandler: sendOrder});
});

/* Horizontal sliding */

// Slide to code/coupon info from start position
function slideToInfo (e)
{
    e.preventDefault();
    beforeAnimate();

    var target = $(this).attr('href');
    $(target + ' a.info').hide();
    $(target + ' a.back').show();

    if (target == '#Code') {
        $('#Content').animate({left: '0px'}, 'normal', '', afterAnimate);
    } else if (target == '#Coupon') {
        $('#Content').animate({left: '-' + (slideWidth * 2) + 'px'},
                              'normal', '', afterAnimate);
    }
};

// Slide to start position and reset order slide to info slide
function slideToStart (e)
{
    e.preventDefault();
    beforeAnimate();
    $('a.back').hide();
    $('a.info').show();
    $('#Content').animate({left: '-' + slideWidth + 'px'},
                          'normal', '', resetOrder);
};
function resetOrder ()
{
    afterAnimate();
    $('.vcontent').css('top', '0px');
}
function submitOrder (e)
{
    e.preventDefault();
    var target = $(this).attr('href');
    $(target + 'Form').submit();
}
function sendOrder (form)
{
    if (submitting) return false;
    submitting = true;

    $(form).ajaxSubmit({
        success: formSentHandler($(form).is('#CodeForm') ? '#Code' : '#Coupon')
    });
    return false;
}
function formSentHandler (target) {
    return function (response) {
        submitting = false;
        if (response) {
            alert(response);
        } else {
            $(target + 'Form').resetForm();
            slideToSent(target);
        }
    };
}

/* Vertical sliding */

// Slide to code/coupon order
function slideToOrder (e)
{
    e.preventDefault();
    beforeAnimate();

    var target = $(this).attr('href');
    $(target + 'Extra .vcontent').animate({top: '-' + slideHeight + 'px'},
                                          'normal', '', afterAnimate);
}

// Slide to code/coupon order sent successfully
function slideToSent (target)
{
    beforeAnimate();
    $(target + 'Extra .vcontent').animate({top: '-' + (slideHeight * 2) + 'px'},
                                          'normal', '', afterAnimate);
}

// Slide to code/coupon info
function slideBackToInfo (e)
{
    e.preventDefault();
    beforeAnimate();

    var target = $(this).attr('href');
    $(target + 'Extra .vcontent').animate({top: '0px'},
                                          'normal', '', afterAnimate);
}

/* Info content */

// Slide terms open
function showTerms (e) {
    e.preventDefault();
    $('a.terms').hide();
    $('a.termsclose').show();
    $('#InfoFrame').animate({height: termsHeight + 'px'});
}

// Slide terms closed
function hideTerms (e)
{
    e.preventDefault();
    $('a.termsclose').hide();
    $('a.terms').show();
    $('#InfoFrame').animate({height: '0px'});
}

function beforeAnimate () {
    if (jQuery.browser.mozilla)
        $('#CouponInfo .colcontent').css('overflow', 'hidden');
}

function afterAnimate () {
    if (jQuery.browser.mozilla)
        $('#CouponInfo .colcontent').css('overflow', 'auto');
}
