﻿// For IntelliSense. Requires '-vsdoc' file
/// <reference path="/Scripts/MooTools/mootools-1.2.2-core-yc.js"/> 
// We could add a reference to the JQuery -vsdoc file here but it assumes $ is JQuery, not $j, so it kinda messes up MooTools IntelliSense :)

//var _restaurantsShown = 6;
//var _restaurantScrollSpeed = 300;
//var _restaurantThumbWidthPlusSpacing = 155;

var dialogRedirectTo = null;

function topImageMenuButtonClick(sender, panelId) {
    var button = $j(sender);

    // Remove highlight for the other buttons (siblings() doesn't select itself)
    button.siblings().each(function (index) {
        $j(this).removeClass('on');
    });

    // Set highlight for this one
    button.addClass('on');

    // Set visibility for panels
    $j('.topImageMenuPanel').each(function (index) {
        var ele = $j(this);

        if (ele.attr('Id') == panelId)
            ele.show();
        else
            ele.hide();
    });
}

//function restScrollRight()
//{
//    var elScrolledElements = $('hdnScrolledElements');
//    var scrolledElements = parseInt(elScrolledElements.value, 10);
//    var restaurants = $('ulRestaurants').getElements('li').length - 1; // Minus 1 since one of the li's is the one saying if no restaurants are open
//    if (restaurants - scrolledElements <= _restaurantsShown) return;

//    scrolledElements += 1;
//    var myFx = new Fx.Scroll('ulRestaurants', { duration: _restaurantScrollSpeed });
//    myFx.start(scrolledElements * _restaurantThumbWidthPlusSpacing, 0);

//    elScrolledElements.value = scrolledElements;
//}

//function restScrollLeft()
//{
//    var elScrolledElements = $('hdnScrolledElements');
//    var scrolledElements = parseInt(elScrolledElements.value, 10);
//    if (scrolledElements < 1) return;

//    scrolledElements -= 1;
//    var myFx = new Fx.Scroll('ulRestaurants', { duration: _restaurantScrollSpeed });
//    myFx.start(scrolledElements * _restaurantThumbWidthPlusSpacing, 0);

//    elScrolledElements.value = scrolledElements;
//}

function scrollToCategory(categoryId)
{
    new Fx.Scroll(window).toElement(categoryId);
}

function toggleBasketButtons(editButton)
{
    var buttonContainer = $(editButton).getParent().getNext('tr');
    var image = editButton.getChildren('img')[0];

    if (buttonContainer.style.display == 'none')
    {
        buttonContainer.style.display = '';
        image.src = '/Images/Basket_Confirm.gif';
    }
    else
    {
        buttonContainer.style.display = 'none';
        image.src = '/Images/Basket_Edit.gif';
    }
}
/*
// This makes sure the dialog initialization occurs after load or IE crashes
function initDialog(initFunction)
{
    window.addEvent('load', function()
    {
        initFunction();
    });  
}

function showDialog(id)
{
    $j('#' + id).dialog('open');
}
*/

function showDialogOnLoad(showFunction) // This method actually only needs to be run when registering a dialog during postback
{
    // This is IE safe. $(document).ready does not work when registered during postback
    $j(window).bind('load', function()
    {
        if (showFunction) // This fixes "showFunction is not a function" JS error when showDialogOnLoad is registered during postback
            showFunction();
    });
}

function uncheckOtherRadioButtons(radioButton)
{
    $$('input[type=radio]').each(function(item, index)
    {
        if (item != radioButton)
            item.checked = false;
    });
}

function showMessageBox(title, text, width, redirectTo) {
    $j(document).ready(function() // This is needed to avoid the IE8 error (KB927917)
    {
        /* This uses the jquery.dialogwrapper, but I'm thinking it's not really necessary
        $j.createDialog(title, text, {
            width: width != null && width != 'null' ? width : '300px',
            buttons: {
                'Ok': function () { $j(this).dialog('close'); }
            }
        });

        if (redirectTo != null && redirectTo != 'null') { // This can be the text string "null" because of the way we show popups from code behind :)
            dialogRedirectTo = redirectTo; // We can't add this to the dialog.close event as before since it's overwritten with the dialogwrapper we now use
        } 
        */

        var dialogObj = $j("#divMessageBox");

        $('pBody').set('html', text);

        dialogObj.dialog({
            title: title,
            width: width != null && width != 'null' ? width : '300px',
            buttons: {
                'Ok': function()
                {
                    $j(this).dialog('close');
                    $j(this).dialog('destroy');
                }
            },
            close: function () { if (redirectTo != null && redirectTo != 'null') { document.location.href = redirectTo; } } // This can be the text string "null" because of the way we show popups from code behind :)
        });
    });
}

function showTerms(title)
{
    var dialogObj = $j("#divTerms");

    // Make sure it's destroyed each time so we can show it multiple times
    dialogObj.dialog('destroy');

    dialogObj.dialog({
        title: title,
        width: '550px',
        buttons: {
            'Ok': function()
            {
                $j(this).dialog('close');
            }
        }
    });
}

function showLogin()
{
    var dialogObj = $j("#divLogin");
    var trLoginError = $j('#trLoginError');

    // Make sure it's destroyed each time so we can show it multiple times (doesn't work currently. $j('trLoginError') can't be found)
    dialogObj.dialog('destroy');
    
    // Also, reset the error message
    trLoginError.css('display', 'none');

    dialogObj.dialog({
        width: '350px',
        buttons: {
            'Log ind': function()
            {
                WebMethods.AccountLogin($j('#txtLoginEmail').val().trim(), $j('#txtLoginPassword').val().trim(), $j('#chkAutoLogin').attr('checked'), function(result)
                {
                    if (result)
                    {
                        dialogObj.dialog('close');
                        document.location.reload(true);                 
                    }
                    else
                        trLoginError.css('display', '');
                });
            },
            'Fortryd': function() 
            {
                dialogObj.dialog('close');
            }
        }
    });
}

function showForgotPassword(email)
{
    var dialogObj = $j("#divResetPassword");

    // Make sure it's destroyed each time so we can show it multiple times (doesn't work currently. $('txtResetPasswordEmail') is null)
    dialogObj.dialog('destroy');

    $('txtResetPasswordEmail').value = email;

    dialogObj.dialog({
        buttons: {
            'Send email': function () {
                WebMethods.SendResetPasswordEmail($('txtResetPasswordEmail').value.trim(), function (result) {
                    if (result) {
                        dialogObj.dialog('close'); // Must do this first or it's the other messagebox that gets closed before we see it :)
                        showMessageBox('Email sendt', 'Emailen er sendt til dig. Følg instruktionerne deri for at ændre din adgangskode.');
                    }
                    else {
                        showMessageBox('Email ikke fundet', 'En konto med den indtastede email kunne ikke findes. Prøv venligst at skrive den igen.');
                    }
                });
                //$j(this).dialog('close'); // Must close before click
            },
            'Fortryd': function () {
                dialogObj.dialog('close');
            }
        }
    });
}

function showClosed()
{
    $j("#divClosed").dialog({
        width: '440px',
        buttons: {
            'Ok': function()
            {
                $j(this).dialog('close');
            }
        }
    });
}

/*
function equalHeight(group) {
    var tallest = 0;
    group.each(function () {
        var thisHeight = $j(this).height();
        if (thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.height(tallest);
}
*/
