﻿// AddThis instellen
// URL hard instellen zodat de testomgeving URL niet gebruikt wordt
var addthis_share = {
    url: 'http://www.voordeelmobiel.nl/weekdeal.html',
    title: 'Voordeelmobiel Weekdeal',
    description: 'Elke week op Voordeelmobiel een superaanbieding voor een superscherpe prijs!',
    templates: {
        twitter: 'Check deze vette deal op {{url}}!'
    }
};

$(function() {
    $('div#wdOrder').click(function() {
        var deeplink = $(this).data('deeplink')

        if (deeplink !== '') {
            goTo(deeplink);
        }
        else {
            goTo('/weekdeal/bestellen.html');
        }
    });

    $('div#wdMoreInfo').click(function() {
        goTo($('a#wdMoreInfoLink').attr('href'));
    });

    $('div#wdEmailBtn').click(function() {
        $('div#wdEmailBtn').hide();

        $.ajax({
            url: '/weekdeal.aspx/SendWeekdealRegistration',
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            data: '{ "email": "' + $('input#weekdealMail').val() + '" }',
            dataType: 'json',
            success: function(response) {
                var data = typeof response.d === 'string' ? eval('(' + response.d + ')') : response.d;
                if (data.success === true) {
                    $('div#wdEmail').empty().append('<span>Bedankt voor uw aanmelding.</span>');
                }
                else {
                    $('div#wdEmail span').text(data.error);
                }
            },
            complete: function() {
                // Eventueel de knop weer tonen, als deze er nog is
                // Als de knop weg is, doet deze functie niets
                $('div#wdEmailBtn').show();
            }
        });
    });

    if ($('#weekdeal').length === 1) {
        // Weekdeal countdown instellen
        var targetDate = $('div#weekdeal').data('enddate');
        var dateParts = targetDate.split('-');
        if ($('div#wdSoldOut').length === 0) {
            $("#wdTimeLeft").countDown({
                targetDate: {
                    day: dateParts[0],
                    month: dateParts[1],
                    year: dateParts[2],
                    hour: 23,
                    min: 59,
                    sec: 59
                },
                omitWeeks: true,
                onComplete: weekdealDateDue
            });
        }
        else {
            $("#wdTimeTillNext").countDown({
                targetDate: {
                    day: dateParts[0],
                    month: dateParts[1],
                    year: dateParts[2],
                    hour: 0,
                    min: 0,
                    sec: 0
                },
                omitWeeks: true,
                onComplete: weekdealDateDue
            });
        }
    }

    $('div.phoneBlock a.btn2.fRight').click(function(e) {
        e.preventDefault();

        if ($(this).attr('href') == '#') {
            var values = 'subscriptionOffer:true' +
                '~subscriptionId:' + $(this).closest('div.phoneBlock').data('prodid') +
                '~subscriptionType:' + $(this).closest('div.phoneBlock').data('aboType');

            $.ajax({
                type: 'POST',
                url: '/ajax.aspx/UpdateOrder',
                data: '{ "choicesString": "' + values + '" }',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function() {
                    goTo('/bundelselectie.html');
                }
            });
        }
        else {
            goTo($(this).attr('href'));
        }
    });
});

function weekdealDateDue() {
    $('div#weekdeal div#wdButtonRow').empty();
    location.reload();
}

