﻿/*
------------------------------------------------------
Checkout
------------------------------------------------------
TODO: Gör jQuery av detta istället och kapsla in alla textboxarna i en DIV-tagg eller motsvarande med ID
sedan skall det finnas en metod.
$('myDIV').setInfoMessages(infoArrayName, 'filterExpression');

Den method går sedan igenom alla matchande element baserat på filterExpression, exempelvis input="text"
och sätter en method som reagerar på focus-eventet.

Focusevent-metoden
Denna metod registrerar en metod på samma elements blur-event om det inte redan finns en registering där.
Sedan sätter metoden informationen om den finns annars en default-text.
*/
var inputFieldInformation_TimerId = null;

function clearInputFieldInfo() {
    if(inputFieldInformation_TimerId != null) {
        clearTimeout(inputFieldInformation_TimerId);
        inputFieldInformation_TimerId = null;
    }
    
    inputFieldInformation_TimerId = setTimeout('getElement("inputFieldInformation").innerHTML = inputFieldInformation_["DEFAULT"]', 1000);
}

function setDelayedInputFieldInfo(elementId) {
    if(inputFieldInformation_TimerId != null) {
        clearTimeout(inputFieldInformation_TimerId);
        inputFieldInformation_TimerId = null;
    }
    
    inputFieldInformation_TimerId = setTimeout('setInputFieldInfo(null, "' + elementId + '")', 300);
}

function setInputFieldInfo(element, valueId) {
    if(inputFieldInformation_TimerId != null) {
        clearTimeout(inputFieldInformation_TimerId);
        inputFieldInformation_TimerId = null;
    }

    var e = getElement('inputFieldInformation');
    if(inputFieldInformation_["DEFAULT"] == null) {
        inputFieldInformation_["DEFAULT"] = e.innerHTML;
    }
    
    if(valueId == null || valueId == '') valueId = element.id;

    var text = inputFieldInformation_[valueId];
    if(text == null || text == "") text = inputFieldInformation_["DEFAULT"];
    e.innerHTML = text;
}
/*
------------------------------------------------------
/Checkout
------------------------------------------------------
*/

(function ($) {
    /*
    infoArray =         The array in which the messages are listed. Not setting a DEFAULT-entry in 
                        the infoArray means that we want this function to retreive the default message
                        from the target element. Set the DEFAULT-entry to an empty string if you don't
                        want any default message.
    targetElementId =   The id for the element that should be filled with the information. Both input-
                        elements and elements that support innerHTML is supported.
    onMouse =           true / false. If onMouse is true then the information is set using the mouseover
                        and mouseout, if set to false the the focus and blur events are used.
    */
    $.fn.information = function(infoArray, targetElementId, onMouse) {
        // If the DEFAULT-message is not specified we retreive the message from the target 
        // element and store it in the array under "DEFAULT".
        if(window.informationArray == null) {
            window.informationArray = infoArray;
            
            if(window.informationArray["DEFAULT"] == null) {
                var targetElement = $("#" + targetElementId);
                if(typeof(targetElement.attr("value")) == 'undefined') {
                    window.informationArray["DEFAULT"] = targetElement.html();
                } else {
                    window.informationArray["DEFAULT"] = targetElement.val();
                }
            }
        
            //window.setInformationByElementId = function(elementId, targetElementId) {
                //alert('setInformationByElementId=' + elementId);
                //$("#" + elementId).setInformation();
            //};
            
            window.setDelayedInformation = function(elementId, targetElementId) {
                if(window.informationTimerId != null) {
                    clearTimeout(window.informationTimerId);
                    window.informationTimerId = null;
                }
                
                //window.informationTimerId = setTimeout('setInformation($("#" + ' + elementId + '))', 300);
                window.informationTimerId = setTimeout('window.setInformation("' + elementId + '","' + targetElementId + '")', 300);
            };
            
            window.setInformation = function(elementId, targetElementId) {
                var element = $("#" + elementId);
                //alert(element);
                if(window.informationTimerId != null) {
                    clearTimeout(window.informationTimerId);
                    window.informationTimerId = null;
                }
                
                var targetElement = $("#" + targetElementId);
                if(typeof(targetElement.attr("value")) == 'undefined') {
                    targetElement.html(window.informationArray[element.attr("id")]);
                } else {
                    targetElement.val(window.informationArray[element.attr("id")]);
                }
            };
            
            window.clearInformation = function(targetElementId) {
                if(window.informationTimerId != null) {
                    clearTimeout(window.informationTimerId);
                    window.informationTimerId = null;
                }
                
                var targetElement = $("#" + targetElementId);
                //alert(targetElement);
                if(typeof(targetElement.attr("value")) == 'undefined') {
                    targetElement.html(window.informationArray["DEFAULT"]);
                } else {
                    targetElement.val(window.informationArray["DEFAULT"]);
                }
            };
        }
            
        return this.each(function () {
            var element = $(this);
            
            if(onMouse) {
                element.mouseover(function() {
                    // Set the related message (to this control) when the focus event is triggered.
                    window.setDelayedInformation(element.attr("id"), targetElementId);
                    
                    // Attach mouseout only if the element has been hovered over to save resources.
                    // Use jQuery.one() to ensure that the event is only attached once.
                    element.one("mouseout", function() {
                        // Set the default message when the mouseout event is triggered.
                        window.clearInformation(targetElementId);
                    });
                });
            } else {
                element.focus(function() {
                    // Set the related message (to this control) when the focus event is triggered.
                    window.setInformation(element.attr("id"), targetElementId);
                    
                    // Attach blur only if the element has had focus to save resources.
                    // Use jQuery.one() to ensure that the event is only attached once.
                    element.one("blur", function() {
                        // Set the default message when the blur (loses focus) event is triggered.
                        window.clearInformation(targetElementId);
                    });
                });
            }
        });
    };
})(jQuery);

/*
------------------------------------------------------
Search box
------------------------------------------------------
*/
function handleSearchTextBoxKeyPress(e, submitButtonId) {
    // TODO: Lägg till stöd för AJAX autocompletion / förslag.
    
    if(isKey(e, 13) == true) {
        $('#' + submitButtonId).click();
        e.cancelBubble = true;
        if(e.stopPropagation) e.stopPropagation();
        return false;
    }
    return true;
}
    
function isKey(e, code) {
    return getKey(e) == code;
}

function getKey(e) {
    var keyCode = 0; e = e || window.event;
    if(e) {
        if(e.keyCode) {
            keyCode = e.keyCode;
        } else if(e.which) {
            keyCode = e.which;
        }
    }
    return keyCode;
}
/*
------------------------------------------------------
/Search box
------------------------------------------------------
*/

/*
------------------------------------------------------
TopFlyout
------------------------------------------------------
*/
function loadTopFlyoutContent(element, cmd) {
    var el = $("#" + topFlyoutLoadTypeId);
   
    if(el.val() == cmd) {
        var tf = $("#TopFlyout");
        if(tf.is(":hidden")) {
            showTopFlyout();
        } else {
            tf.slideUp("fast");
        }
        return;
    }
    el.val(cmd);
    lastTopFlyoutFocusElementId = null;
    __doPostBack(el.attr("id"),el.val());
}

var lastTopFlyoutFocusElementId = null;    
function showTopFlyout(focusElementId) {
    var element = $("#TopFlyout");
    var completeCallback = null;
    
    if(!focusElementId) focusElementId = lastTopFlyoutFocusElementId;
    if(focusElementId) {
        completeCallback = function() {
             $("#" + focusElementId).focus();
        }
    }
    lastTopFlyoutFocusElementId = focusElementId; 
    
    element.slideDown({
        duration:1000
        ,easing:"easeOutBounce"
        ,complete:completeCallback
    });
}

function EndRequestHandler(sender, args) {
    if(args.get_error()) {
        var errorMessage;
        if(args.get_response().get_statusCode() == '200') {
            errorMessage = args.get_error().message;
        } else {
            errorMessage = 'An unspecified error occurred.'; // Error occurred somewhere other than the server page.
        }
        args.set_errorHandled(true);
        // TODO: Log failuremessage.
        __doPostBack('DUMMY', '');
    }
}

$(document).ready(function() {
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
});

/*
------------------------------------------------------
/TopFlyout
------------------------------------------------------
*/
