
function mylist_save(img, windowCloseCallback) {
    windowCloseCallback = windowCloseCallback || function() {};
    if (img.src.match("active")) {
        return false;
    }
    if (img.src.match("delete")) {
        var content = '<p>' + translate_mylist_values.ausstellervonmeinerlisteloeschen + '<' + '/p> ';
        content += '<div style="clear: both;"><' + '/div>';

        Dialog.confirm(
            content,
            {
                className: "alphacube",
                width: 400,
                okLabel: translate_mylist_values.mylist_delete,
                cancelLabel: translate_mylist_values.mylist_cancel,
                onOk: function(win) {
                    var status = 0;
                    var url = window.location.protocol + "//" + window.location.host + window.location.pathname;
                    var pars = "ajax=mylist_save&exhibitor=" + img.alt + "&status=" + status;

                    new Ajax.Request(
                        url,
                        {
                            method: "get",
                            parameters: pars,
                            onComplete: complete = function (req, obj) {
                                if (req.responseText == "0") {
                                    img.src = "img/icon_mylist.gif";
                                    img.style.margin = "0";
                                    img.style.border = "1px solid #cef1bf";
                                    img.style.background = "#e5f7dd";
                                } else {
                                    img.src = "img/icon_mylist_active.gif";
                                    img.style.margin = "0";
                                    img.style.border = "1px solid #ecb2b2";
                                    img.style.background = "#f7e0e0";
                                }
                            },
                            onException: exception = function (req, e) {
                                //var h = $H(e);
                                //alert(h.toQueryString());
                            }
                        }
                    );
                    return true;
                }
            }
        );
        return true;
    }
    var content = '<p>';
    content += '  <span class="comment_label">' + translate_mylist_values.mylist_entry_comment + '<' + '/span> ';
    content += '  <span class="comment_input"><textarea rows="4"><' + '/textarea><' + '/span>';
    content += '<' + '/p>';
    content += '<div style="clear: both;"><' + '/div>';
    Dialog.confirm(
        content,
        {
            className: "alphacube",
            width: 400,
            okLabel: translate_mylist_values.mylist_set,
            cancelLabel: translate_mylist_values.mylist_cancel,
            onOk: function(win){
                windowCloseCallback();
                var status = 1;
                var comment = $(win.content).down(".comment_input textarea").value;
                var url = window.location.protocol + "//" + window.location.host + window.location.pathname;
                var pars = "ajax=mylist_save&exhibitor=" + img.alt + "&status=" + status + "&comment=" + comment;

                new Ajax.Request(
                    url,
                    {
                        method: "get",
                        parameters: pars,
                        onComplete: complete = function (req, obj) {
                            if (req.responseText == "1") {
                                img.src = "img/icon_mylist_active.gif";
                                img.style.margin = "0";
                                img.style.border = "1px solid #cef1bf";
                                img.style.background = "#e5f7dd";
                            } else {
                                img.src = "img/icon_mylist.gif";
                                img.style.margin = "0";
                                img.style.border = "1px solid #ecb2b2";
                                img.style.background = "#f7e0e0";
                            }
                        },
                        onException: exception = function (req, e) {
                            //var h = $H(e);
                            //alert(h.toQueryString());
                        }
                    }
                );
                return true;
            },
            onCancel: function() {
                windowCloseCallback();
            }
        }
    );
}
function mylist_show_delete(img) {
    if (img.src.match("active")) {
        img.src = "img/icon_mylist_delete.gif";
    }
}
function mylist_hide_delete(img) {
    if (img.src.match("delete")) {
        img.src = "img/icon_mylist_active.gif";
    }
}


document.observe("dom:loaded", function() {
    // Domain angleichen
    var domain = window.location.hostname.replace(/^[a-z0-9]+\./, "");
    if (domain.match(/\./) != ".") {
        domain = window.location.hostname;
    }
    document.domain = domain;
    
    var lang = $$('meta[name=language]');
    if(lang[0]) {
        lang = lang[0].content;
    }else{
        lang = 'de';
    }
    
    $("content").select("a.hallplan").each(function(hplink) {
        $(hplink).href = $(hplink).href + "&open=js&lang="+lang;
    });
});

var RxHallPlanConnector = Class.create({
    initialize: function(memorize, cssfile, userid) {
        // Feature vorhanden
        this.memorize = memorize;
        this.contact = true;
        this.cssfile = cssfile;
        this.userid = userid;
    },

    memorizeExhibitor: function(exhibitor, callback, windowCloseCallback) {
        var tmpImg = $(document.createElement("img"));
        tmpImg.alt = exhibitor.id;
        tmpImg.hpCallback = false;
        tmpImg.setStyle({display: 'none'});
        document.getElementsByTagName('body')[0].appendChild(tmpImg);
        tmpImg.observe("load", function(evt) {
            var img = evt.element();
            if (!img.hpCallback && typeof callback == "function") {
                img.hpCallback = true;
                callback(img.src.match("active") ? true : false, exhibitor);
            }
        });
        mylist_save(tmpImg, windowCloseCallback);
    },

    getMemorizedExhibitors: function(callback) {
        if (typeof callback != "function") {
            return;
        }
        var url = window.location.protocol + "//" + window.location.host + window.location.pathname;
        var pars = "ajax=mylist_exhibitors";

        new Ajax.Request(
        url,
        {
            method: "get",
            parameters: pars,
            onComplete: complete = function (req, obj) {
                var exhibitors = new Array();
                if (req.responseText.isJSON()) {
                    exhibitors = req.responseText.evalJSON(true);
                }
                callback(exhibitors);
            },
            onException: exception = function (req, e) {
                //var h = $H(e);
                //alert(h.toQueryString());
            }
        });
    },

    getCSSFilePath: function() {
        if(this.cssfile == '') return false;
        return window.location.protocol + "//" + window.location.host + '/' + this.cssfile;
    },
    
    getUserid: function() {
        return this.userid;
    },

    openMyList: function() {
        window.location.href = window.location.protocol + "//" + window.location.host + window.location.pathname + "?show_mylist=1";
    },

    openExhibitor: function(exhibitor) {
        window.location.href = window.location.protocol + "//" + window.location.host + window.location.pathname + "?aid=" + exhibitor.id;
    },

    openContactForm: function(exhibitor) {
        if (this.dateWindow != null && !this.dateWindow.closed) {
            this.dateWindow.close();
        }

        var link = window.location.protocol + "//" + window.location.host + window.location.pathname + "?aid=" + exhibitor.id + "&form=date";
        var newWindow = window.open(link + "&popup=1", 'Contact', "width=500,height=480,resizable=no,scrollbars=no,top=100,left=100");
        if (!newWindow) { return; }
        
        this.dateWindow = newWindow;
        this.dateWindow.focus();
        
        //window.location.href = window.location.protocol + "//" + window.location.host + window.location.pathname + "?aid=" + exhibitor.id + "&form=date";
    }
});

