﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("AirQuestSite");

AirQuestSite.UserInfoLabelBehavior = function(element) {
    AirQuestSite.UserInfoLabelBehavior.initializeBase(this, [element]);
}

AirQuestSite.UserInfoLabelBehavior.prototype = {

    _userid: '',
    _icon: null,
    _infobox: null,
    _infoboxcontent: null,
    _timerHide: -1,
    mouseover$handler: null,
    mouseout$handler: null,
    click$handler: null,
    documentclick$handler: null,

    initialize: function() {
        AirQuestSite.UserInfoLabelBehavior.callBaseMethod(this, 'initialize');

        var element = this.get_element();

        this._icon = document.createElement("img");
        this._icon.src = AirQuestSite.UserInfoLabelBehavior.Static.iconUrl;
        this._icon.style.position = "absolute";
        this._icon.style.right = "-13px";
        this._icon.style.top = "0px";
        this._icon.style.visibility = "hidden";
        this._icon.style.cursor = "pointer";

        element.style.position = "relative";
        element.style.cursor = "default";

        var myBounds = Sys.UI.DomElement.getBounds(element);
        element.appendChild(this._icon);

        this.mouseover$handler = Function.createDelegate(this, this.onmouseover);
        this.mouseout$handler = Function.createDelegate(this, this.onmouseout);
        this.click$handler = Function.createDelegate(this, this.onclick);

        $addHandler(element, "mouseover", this.mouseover$handler);
        $addHandler(element, "mouseout", this.mouseout$handler);
        $addHandler(this._icon, "click", this.click$handler);

    },

    onmouseover: function(e) {
        if(this._timerHide != -1) {
            window.clearTimeout(this._timerHide);
            this._timerHide = -1;
        }
        this._icon.style.visibility = "visible";
    },

    onmouseout: function(e) {
        if(this._timerHide != -1) {
            window.clearTimeout(this._timerHide);
        }

        this._timerHide = window.setTimeout(Function.createDelegate(this, this.ontimerhide), 300);
    },

    onclick: function(e) {
        var element = this.get_element();
        this._infobox = document.createElement("div");
        this._infobox.className = "userinfo_frame";

        this._infoboxcontent = document.createElement("div");
        this._infoboxcontent.className = "userinfo_content";
        this._infobox.appendChild(this._infoboxcontent);

        this._infoboxcontent.appendChild(document.createTextNode("moment, prosím ..."));

        var bounds = Sys.UI.DomElement.getBounds(element);
        Sys.UI.DomElement.setLocation(this._infobox, bounds.x, bounds.y + bounds.height + 10);
        document.body.appendChild(this._infobox);

        this.documentclick$handler = Function.createDelegate(this, this.ondocumentclick);
        $addHandler(document, "click", this.documentclick$handler);

        if(AirQuestSite.UserInfoLabelBehavior.Static._service == null) {
            AirQuestSite.UserInfoLabelBehavior.Static._service = new AirQuestSite.MapMarkerManager();
        }

        AirQuestSite.UserInfoLabelBehavior.Static._executingRequest = AirQuestSite.UserInfoLabelBehavior.Static._service.GetUserInfo(this._userid, Function.createDelegate(this, this.onProcessResponse), Function.createDelegate(this, this.onServerError));
    },

    onProcessResponse: function(resp, ctx) {
        AirQuestSite.UserInfoLabelBehavior.Static._executingRequest = null;

        this._infoboxcontent.innerHTML = resp;
    },

    onServerError: function(error) {
        AirQuestSite.UserInfoLabelBehavior.Static._executingRequest = null;        
        this._infoboxcontent.innerHTML = "Něco se nepovedlo :(";
    },

    ontimerhide: function() {
        this._timerHide = -1;
        this._icon.style.visibility = "hidden";
    },

    ondocumentclick: function(e) {

        if(this._infobox) {
            var el = e.target;
            var element = this.get_element();
            while(el != null) {
                if(el == element) {
                    return;
                }
                el = el.parentNode;
            }

            el = e.target;
            while(el != null) {
                if(el == this._infobox) {
                    return;
                }
                el = el.parentNode;
            }

            this.closeInfoBox();
        }

    },

    closeInfoBox: function() {
        var element = this.get_element();

        if(this._infobox != null) {
            document.body.removeChild(this._infobox);
            this._infobox = null;
        }
    },

    dispose: function() {

        if(this._timerHide != -1) {
            window.clearTimeout(this._timerHide);
            this._timerHide = -1;
        }

        var element = this.get_element();

        $removeHandler(element, "mouseover", this.mouseover$handler);
        $removeHandler(element, "mouseout", this.mouseout$handler);
        if(this.documentclick$handler != null) {
            $removeHandler(document, "click", this.documentclick$handler);
            this.documentclick$handler = null;
        }

        if(this._icon) {
            $removeHandler(this._icon, "click", this.click$handler);
            element.removeChild(this._icon);
            this._icon = null;
        }



        AirQuestSite.UserInfoLabelBehavior.callBaseMethod(this, 'dispose');
    }
}

AirQuestSite.UserInfoLabelBehavior.Static = {
    cssStyleNormal: '',
    cssStyleOver: '',
    iconUrl: '',
    _service: null,
    _executingRequest: null
};

AirQuestSite.UserInfoLabelBehavior.registerClass('AirQuestSite.UserInfoLabelBehavior', Sys.UI.Behavior);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

