﻿function Homepage() {
    this.onInit = function () {
        this.setupTooltips();

        $('#Homepage .choose').click(this.onChoose);
        $('#Homepage .create').click(this.onCreate);
        $('#Homepage .continue').click(this.onContinue);
        $('#Homepage .retrieve').click(this.onRetrieve);
    }

    this.setupTooltips = function () {
        $('.popuptooltip').fancytooltip(stpTooltipConfig);
        $('.screenhelptip').tooltip(stpTooltipConfig);
    }

    this.onChoose = function () {
        var mode = (project != null) ? project.mode : 0;

        if (mode == 1) {
            if(project.RangeID != null)
                ConfirmProjectStart($('#Homepage #ChooseRedirect').val(), 'StartNewEasy()');
           else
               window.location = $('#Homepage #ChooseRedirect').val();
        }
        else {
            StartNewEasy();
        }
    }

    this.onCreate = function () {
        var mode = (project != null) ? project.mode : 0;

        if (mode == 2) {
            if (project.RangeID != null)
                ConfirmProjectStart($('#Homepage #CreateRedirect').val(), 'StartNewExpert()');
            else
                window.location = $('#Homepage #CreateRedirect').val();
        }
        else {
            StartNewExpert();
        }
    }

    this.onContinue = function () {
        GoToLastStep();
    }

    this.onRetrieve = function () {
        loadProject($('#Homepage #ProjectNumber').val());
    }
}

function StartNewEasy() {
    $.ajax({
        type: 'GET',
        url: '/webservices/stairplanner.svc/project/new/easy',
        dataType: 'json',
        success: function (data, success) {
            project = eval(data);
            window.location = $('#Homepage #ChooseRedirect').val();
        }
    });
}

function StartNewExpert() {
    $.ajax({
        type: 'GET',
        url: '/webservices/stairplanner.svc/project/new/expert',
        dataType: 'json',
        success: function (data, success) {
            project = eval(data);
            window.location = $('#Homepage #CreateRedirect').val();
        }
    });
}

function ConfirmProjectStart(continueUrl, newProject) {
    var $dlg = $('#finishDialog');

    if (!$dlg.data('dialogOpen')) {
        $dlg.data('dialogOpen', true);

        $('#newProject').attr('onClick', newProject);
        $('#continueProject').attr('href', continueUrl);

        jQuery.fancybox({
            'padding': 0,
            'content': $dlg.html(),
            'transitionIn': 'elastic',
            'transitionOut': 'elastic',
            'onComplete': function () { },
            'onClosed': function () {
                $dlg.removeData('dialogOpen');
            }
        });
    }
    return false;
}

//////// INIT CALL /////////
$(function () {
    var home = new Homepage();
    home.onInit();
});
