skiptrip_namespace("skiptrip.tools");
skiptrip.tools = {
    registerHandlers: function() {
       $("#location-search").focus().autocomplete("https://www.skiptrip.se/js/api/locations.jsf", {
            width: "300px",
            formatItem: function(row) {
                var displayName = row[5];
                return "<div title='" +displayName+ "'>" +displayName+ "</div>";
            },
            formatResult: function(row) {
                var displayName = row[5];
                return skiptrip.util.entityDecode(displayName);
            }
        });
        $("#location-search").result(function(event, row) {
            if (row) {
                var id = row[0];
                $(this).next().val(id);
            }
         });
       $("#booking-search").focus().autocomplete("https://www.skiptrip.se/js/api/bookings.jsf", {
            width: "300px",
            max: 100,
            formatItem: function(row) {
                var number = row[1];
                var name = row[4];
                var cities = skiptrip.util.entityDecode(row[2]);
                var date = row[3];
                return "<div>" +number+ " (" +name+ ")<br/>" +date+ "<br/>" +cities+ "</div>";
            },
            formatResult: function(row) {
                var number = row[1];
                return number;
            }
        });
        $("#booking-search").result(function(event, row) {
            if (row) {
                var id = row[0];
                $(this).next().val(id);
            }
         });
        $("#booking-search-form").submit(function() {
            $("input[id^=studio-search]").each(function() {
                $(this).val($(this).val().replace(" (" +skiptrip.util.text("privateSystem.label").toLowerCase()+ ")", ""));
            });
        });
        $("input[id^=studio-search]").each(function() {
            $(this).change(function() {
                if (!$(this).val()) {
                    $(this).prev().val(null);
                }
                
            });
            $(this).blur(function() {
                var id = $(this).prev().val();
                var value = $(this).val();
                if (id == -1) {
                     $(this).val(value +" (" +skiptrip.util.text("privateSystem.label").toLowerCase()+ ")");
                }
            });
            $(this).focus(function() {
                if ($(this).val().indexOf(" (" +skiptrip.util.text("privateSystem.label").toLowerCase()+ ")") >= 0) {
                    $(this).val($(this).val().replace(" (" +skiptrip.util.text("privateSystem.label").toLowerCase() +")", ""));
                    var pos = $(this).val().length;
                    $(this).selectRange(pos, pos);
                }
            });
            $(this).autocomplete("http://www.skiptrip.se/js/api/studios.jsf", {
                width: "300px",
                matchCase: true,
                max: 100,
                formatItem: function(row, i, max, value, term) {
                    var displayName = row[7];
                    var result = "";
                    if (row[0] == -1) {
                        result = "<div style='border-bottom: dotted 1px #cccccc; height: 5px; margin-bottom: 5px;'/>";
                    }
                    result += "<div class='left' title='" +displayName+ "'>" +displayName;
                    if (row[0] == -1) {
                        result += " (<span style='font-style: italic;'>" +skiptrip.util.text("privateSystem.label").toLowerCase()+ "</span>)";
                    }
                    result += "</div>";
                    if (row[0] == -1) {
                        result += "<div class='right venue-icon1b' /><br/><div class='venue-info-boxb'/>";
                    }
                    return result;
                },
                formatResult: function(row) {
                    var displayName = row[7];
                    return skiptrip.util.entityDecode(displayName);
                },
                formatCompleted: function() {
                    $(".venue-icon1b").mouseenter(function() {
                        $(this).addClass("venue-icon2b");
                        var box = $(this).next().next();
                        box.show();
                        box.html(skiptrip.util.text('privateSystem.infoText'));
                    });
                    $(".venue-icon1b").mouseleave(function() {
                        $(this).removeClass("venue-icon2b");
                        var box = $(this).next().next();
                        box.hide();
                    });
                }
            });
            $(this).result(function(event, row) {
                if (row) {
                    var id = row[0];
                    if (id == -1) {
                        $(this).removeClass("inputSize9").addClass("inputSize5");
                        $(this).next().show();
                    } else {
                        $(this).removeClass("inputSize5").addClass("inputSize9");
                        $(this).next().hide();
                    }
                    
                    var displayName = row[7];
                    var idInput = $(this).prev();
                    idInput.val(id);
                    var typeInput = idInput.prev();
                    typeInput.val(id == -1 ? 'Private' : 'Studio');
                    $(this).attr("title", skiptrip.util.entityDecode(displayName));
                }
             });
         });
        $("input[id^=reference-search]").each(function() {
            $(this).autocomplete("http://www.skiptrip.se/js/api/references.jsf", {
                width: "300px",
                formatItem: function(row) {
                    var displayName = "";
                    var externalRef = row[1];
                    var s = "";
                    if (externalRef) {
                        displayName += externalRef;
                        s = ", ";
                    }
                    var companyName = row[2];
                    if (companyName) {
                       displayName = displayName +s+ companyName;
                       s  = ", ";
                    }
                    var contactName = row[3];
                    if (contactName) {
                        displayName = displayName +s+ contactName;
                        s = ", ";
                    }
                    return "<div title='" +displayName+ "'>" +displayName+ "</div>";
                },
                formatResult: function(row) {
                    var displayName = row[1];
                    return skiptrip.util.entityDecode(displayName);
                }
            });
            $(this).result(function(event, row) {
                if (row) {
                    var id = row[0];
                    $(this).next().val(id);
                    var displayName = row[1];
                    $(this).attr("title", skiptrip.util.entityDecode(displayName));
                }
             });
         });
         $(".venue-info").click(function() {
             $(this).toggleClass("venue-icon2a");
             var box = $(this).next();
             box.html(skiptrip.util.text('privateSystem.infoText'));
             box.slideToggle('fast');
         });
    }
};

$().ready(function() {
    skiptrip.tools.registerHandlers();
    $("input[id^=studio-search]").each(function() {
        if ($(this).prev().prev().val() == 'Private' && $(this).val().length > 0) {
            $(this).val($(this).val() +" (" +skiptrip.util.text("privateSystem.label").toLowerCase() +")");
        }
    });
});

