/* 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);
	
	$('#cd1').click(toBrochureForm);
	$('#cd2').click(toOrderForm);
	$('#CodeForm').resetForm();
	toBrochureForm();
	
    // 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});
    $('#readyCoupon').validate({submitHandler: sendOrder});
	$('#ownCoupon').validate({submitHandler: sendOrder});
	$('#pdfCoupon').validate({submitHandler: sendOrder});
});


function toBrochureForm() {
	$('#CodeForm').resetForm();
	$('#cd1').attr('checked', 'checked');
	$('#CodeForm tr').eq(2).hide();
	$('#CodeForm tr').eq(3).hide();
	$('#CodeForm tr').eq(4).hide();
	//$('#CodeForm tr').eq(8).hide();
	//$('#CodeForm tr').eq(9).hide();
	
}

function toOrderForm(e) {
	$('#CodeForm').resetForm();
	$('#cd2').attr('checked', 'checked');
	$('#CodeForm tr').show();
}
/* 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).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 && response != 'ok') {
            alert(response);
        } else {
            $('#CodeForm').resetForm();
			$('#readyCoupon').resetForm();
			$('#ownCoupon').resetForm();
			$('#pdfCoupon').resetForm();
            slideToSent(target);
        }
    };
}

/* Vertical sliding */

// Slide to code/coupon order
function slideToOrder (e)
{
    e.preventDefault();
    beforeAnimate();
	toBrochureForm();
	
	$('#CouponOrder form').hide();
	$('#CouponOrder a.send').hide();
	
	if($(this).hasClass('readyCoupon')) {
		$('#readyCoupon').show();
		$('a.readyCoupon').show();
	}
	
	if($(this).hasClass('ownCoupon')) {
		$('#ownCoupon').show();
		$('a.ownCoupon').show();
	}
		
	if($(this).hasClass('pdfCoupon')) {
		$('#pdfCoupon').show();
		$('a.pdfCoupon').show();
	}
	
	
    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');
}

