﻿Date.prototype.goto = function(n) {
    this.setTime(this.getTime() + n * 86400000);
    return this;
};

Date.prototype.DateDiff = function(endDate) {
    var ONE_DAY = 1000 * 60 * 60 * 24;
    var date1_ms = this.getTime();
    var date2_ms = endDate.getTime();
    var difference_ms = Math.abs(date2_ms - date1_ms);
    return Math.round(difference_ms / ONE_DAY);
};

$(function() {
    $('.calendar').datepicker({
        dateFormat: 'dd/mm/yy',
        firstDay: 1,
        minDate: +1,
        onSelect: function(dateText) {
            var newDate = $.datepicker.parseDate('dd/mm/yy', dateText);
            if ($('.calendar').index(this) == 0) {
                $('.calendar:last').datepicker('option', 'minDate', newDate.goto(1));
                var checkOut = $('.calendar:last').datepicker('getDate');
                if (newDate > checkOut)
                    $('.calendar:last').datepicker('setDate', newDate.goto(1));
            }
            else {
                $('.calendar:first').datepicker('option', 'maxDate', newDate.goto(-1));
                var checkIn = $('.calendar:first').datepicker('getDate');
                if (newDate < checkIn)
                    $('.calendar:first').datepicker('setDate', newDate.goto(-1));
            }

            var newStage = $('.calendar:first').datepicker('getDate').DateDiff($('.calendar:last').datepicker('getDate'));
            $('#s').val(newStage);
        }
    });

    $('#h').change(function() {
        updateRooms()
    });

    updateRooms();

    if ($('#map_widget').length > 0)
        initMaps();

    $('.hotel-sel a').click(function() {
        $('.hotel-sel-expanded').toggle();
    });

    $('.hotel-sel-expanded .collapse').click(function() {
        $('.hotel-sel-expanded').hide();
    });

    $('.lang-sel a').click(function(e) {
        $('.lang-sel-expanded').toggle();
        e.preventDefault();
    });

    NewsfeedController.init();

    ajaxSubscriptions.init();
});

function updateRooms() {
    if (!$('body').hasClass('is-destination'))
        return;
    var h = parseInt($('#h').val());
    var selbox = document.getElementById('r_1');
    selbox.options.length = 0;
    if (h == 1) {
        selbox.options[selbox.options.length] = new Option(DBSTDroom, '1');
        selbox.options[selbox.options.length] = new Option(DBHIGHroom, '2');
        selbox.options[selbox.options.length] = new Option(STESUProom, '5');
        selbox.options[selbox.options.length] = new Option(STETERRroom, '6');
        selbox.options[selbox.options.length] = new Option(STENOTERRroom, '7');
    } else if (h == 5) {
        selbox.options[selbox.options.length] = new Option(DBSTDroom, '1');
        selbox.options[selbox.options.length] = new Option(DBSUProom, '8');
    } else {
        selbox.options[selbox.options.length] = new Option(DBSTDroom, '1');
    }
}

function nobot(user) {
    document.location = 'mailto:' + user + '@' + 'grupobali.com';
}
function initMaps() {
    var latitude = $('#latitude').val();
    var longitude = $('#longitude').val();
    
    if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map_widget"), { backgroundColor: "#000000" });
        map.setCenter(new GLatLng(latitude, longitude), 16);

        map.addControl(new GSmallMapControl());
        map.addControl(new GOverviewMapControl());

        var baliIcon = new GIcon(G_DEFAULT_ICON);
        baliIcon.image = "/images/map-pin.png";
        baliIcon.iconSize = new GSize(46, 61);

        var point = new GLatLng(latitude, longitude);
        map.addOverlay(new GMarker(point, { icon: baliIcon }));
    }
}

function getAddress() {
    var originalURl = document.getElementById('hfUrl').value;
    var finalAddress = document.getElementById('saddr').value;
    document.location = originalURl + '&saddr=' + finalAddress;
}

var NewsfeedController =
{
    init: function() {
        NewsfeedController.Posts = $("#newsfeedList li");
        NewsfeedController.refreshPost();
        
        $('#nextPost').click(function(e) {
            NewsfeedController.refreshPost();
            e.preventDefault();
        });
        $('#prevPost').click(function(e) {
            e.preventDefault();
            if (NewsfeedController.CurrentPost < 0)
                return;
            --NewsfeedController.CurrentPost;
            NewsfeedController.refreshPost();
        });
    },
    refreshPost: function() {
        if (NewsfeedController.CurrentTimeOut != 'undefined') {
            clearTimeout(NewsfeedController.CurrentTimeOut);
            clearTimeout(NewsfeedController.CurrentLoadTimeOut);
        }
        NewsfeedController.Posts.slideUp('slow');
        NewsfeedController.CurrentLoadTimeOut = setTimeout(function() {
            $("#newsfeedList li:eq(" + NewsfeedController.CurrentPost + ")").slideDown('slow');
        }, 300);

        ++NewsfeedController.CurrentPost;

        if ((NewsfeedController.CurrentPost) == NewsfeedController.Posts.length)
            NewsfeedController.CurrentPost = 0;

        NewsfeedController.CurrentTimeOut = setTimeout(function() { NewsfeedController.refreshPost(); }, 6000);
    },
    Posts: null,
    CurrentPost: -1,
    CurrentTimeOut: 'undefined',
    CurrentLoadTimeOut: 'undefined'
}

var ajaxSubscriptions = {
    language: null,
    subscriber: null,
    action: null,
    subscribeMe: true,
    process: function() {
        var ajaxModal = (this.action != 'sms') ? $('#mailSubscription') : $('#smsSubscription');
        ajaxModal.find('fieldset').hide().siblings('.subscription-process').show('slow');

        $.post("/ajax/subscribe.ashx", { language: this.language, subscriber: this.subscriber, mode: this.subscribeMe, action: this.action },
            function(data) {
                $('#subscriptions .subscription-process').hide();
                if (data == 'OK')
                    ajaxModal.find('.subscription-ok').show('slow').siblings('div').hide();
                else
                    ajaxModal.find('.subscription-nok').show('slow').siblings('div').hide();
            });
    },
    processMobile: function() {
        this.subscriber = $('#smsSubscription #tbSms').val();
        this.action = 'sms';
        this.language = $('#smsSubscription #smsLang').val();
        this.process();
    },
    processEmail: function() {
        this.subscriber = $('#mailSubscription #tbEmail').val();
        this.action = 'mail';
        this.language = $('#mailSubscription #emailLang').val();
        this.process();
    },
    defaultValues: function() {
        $('#subscriptions input.textbox').each(function(i, sender) {
            var ititle = $(this).attr('title');
            $(sender).val(ititle).blur(function() { if ($(this).val() == '') $(this).val(ititle); }).focus(function() { if ($(this).val() == ititle) $(this).val(''); });
        });
    },
    init: function() {
        this.defaultValues();
        $('#subscriptions .btn-go').click(function(e) {
            e.preventDefault();
            if ($(e.target).hasClass('email-submit'))
                ajaxSubscriptions.processEmail();
            else
                ajaxSubscriptions.processMobile();
        });

        $('#subscriptions .subscription-nok a').click(function(e) {
            e.preventDefault();
            $(this).parent().hide().siblings('fieldset').show('slow');
        });
    }
};