﻿$(function () {

    var suggestions = [];

    $(".zipcode").autocomplete({
        source: function (term, add) {
            $.getJSON("/Handlers/ZipCodeAutoCompleteHandler.ashx", term, function (data) {
                suggestions = new Array();
                $.each(data, function (i, val) {
                    suggestions.push(val);
                });
                //pass array to callback  
                add(suggestions);
            });
        },
        select: function (event, ui) {
            $(event.currentTarget).closest('.registerAddressBlock').find('.zipcode').val(ui.item.value);
            $(event.currentTarget).closest('.registerAddressBlock').find('.zipcodeName').val(ui.item.text);
        },
        minLength: 1
    }).blur(function (event) {
        var invalidPostcode = 'skriv inn et gyldig postnummer over';
        var zipcode = $(this).val();

        // no point checking if we don't have a zipcode
        if (!$(this).val().match(/^[0-9]{4}$/)) {
            $(event.target).closest('.registerAddressBlock').find('.zipcodeName').val(invalidPostcode);
            return;
        }

        $.getJSON("/Handlers/ZipCodeAutoCompleteHandler.ashx?term=" + $(this).val(), null, function(data) {
            suggestions = new Array();
            $.each(data, function(i, val) {
                suggestions.push(val);
            });
            $(event.target).closest('.registerAddressBlock').find('.zipcodeName').val(suggestions.length == 1 ? suggestions[0].text : invalidPostcode);
        });
    });

    // put in stuff in boxes on load 
    $(".zipcode").each(function (i, elm) { $(elm).trigger('blur'); });

});


var TimeOutObjects = {};

TimerEnableCollection = function (timeOut) {
    var maxTimeout = timeOut - new Date() + 500; // add 500ms to make sure the sale has really finished so we reload the right values.
    if (maxTimeout > 2147483000) // about int32 less a little for safety. FF and Chrome only use 32bit ints for window.settimeout.
    {
        maxTimeout = 2147483000;
    }
    window.setTimeout('location.reload()', maxTimeout);

    TimeRemaining();
};

TimeRemaining = function () {

    for (var product in TimeOutObjects) {
        var div = '.thefinalcountdown' + product;
        var date = TimeOutObjects[product];
        var diff = (date - new Date());

        if (diff < 0) {
            $(div).text(""); // we shouldn't see anything here.
        }
        else if (diff < 120000) {

            var timeOne = isNaN(diff) ? NaN : Math.floor(diff / 1000) + ' sek igjen';
            $(div).text(timeOne);
        }
        else {
            var timeTwo = isNaN(diff) ? NaN :
                (diff > 86400000 ? Math.floor(diff / 86400000) + ' d ' : '') +
                    (diff > 3600000 ? Math.floor(diff / 3600000 % 24) + ' t ' : '') +
                        (Math.floor(diff / 60000 % 60) + ' min igjen');
            $(div).text(timeTwo);
        }
    }
    window.setTimeout("TimeRemaining()", 100);

};

