﻿
Event.observe(window, "load", OnDocumentLoad);
Event.observe(window, "unload", OnDocumentUnload);

function OnDocumentLoad(evObj) {
    new ShootingNewsExtender($("ShootingNews"), Pictures);

    $$("body")[0].select("fieldset.inboxlabel").each(function(el) {
        el.select("input.text").each(
		    function(el) {
		        new InputTextLabel(el);
		    }
	    );
        el.select("textarea.text").each(
		    function(el) {
		        new InputTextLabel(el);
		    }
	    );
		});

		
		// disable enter pressed submits
		Event.observe($$("body")[0], 'keypress', function(evObj) {
		if (evObj.keyCode == Event.KEY_RETURN)
		Event.stop(evObj);
		}
		);
		
		var Password = "ctl00_LoginContent_ctl00_LoginBox_Password_1";
		if ($(Password)) {
		    Event.observe($(Password), "keydown", function(evObj) {
		        if (evObj.keyCode == Event.KEY_RETURN) {
		            $("ctl00_LoginContent_ctl00_LoginBox_Password").setValue($("ctl00_LoginContent_ctl00_LoginBox_Password_1").getValue());
		            $("ctl00_LoginContent_ctl00_LoginBox_LoginButton").click();
		        }
		    });
		}

		if ($("UploadPicture")) {
		    new AjaxUpload($("UploadPicture"), { action: '/wingnetwork/fileuploadhandler.aspx', onComplete: function(file, response) {
		        if (response == "error")
		            $("UploadPicture").up("p").down(".validationSummary").show();
		        else
		            document.location.href = response;
		    }, onSubmit: function(file, ext) {
		        if (!(ext && /^(jpg)$/i.test(ext))) {
		            $("UploadPicture").up("p").down(".validationSummary").show();
		            return false;
		    }
		    }
		    });
		}

}

function OnDocumentUnload(evObj) {
}

function OnHtmlLoad() {
    $("ctl00_sitefinityLogo").remove();

    var viewport = new ViewportProperties();

    $("PageOverlay").hide();
    $("FeedbackHolder").hide();

    Event.observe($("OpenFeedback"), "click", function(evObj) { $("FeedbackHolder").show(); $("PageOverlay").show(); })
    Event.observe($("CloseFeedback"), "click", function(evObj) { $("FeedbackHolder").hide(); $("PageOverlay").hide(); })

    $("PageOverlay").setStyle({ width: viewport.DocWidth + "px", height: viewport.DocHeight + "px" });
    $("FeedbackHolder").setStyle({ left: (Math.floor(viewport.WindowWidth / 2 - $("FeedbackHolder").getWidth() / 2)) + "px",
        top: (Math.floor(viewport.WindowHeight / 2 - $("FeedbackHolder").getHeight() / 2)) + "px"
    });

    if ($("StatusBarOK")) {
        new TooltipExtender($("StatusBarOK"), $("StatusText").innerHTML, "tooltip", null, null, null);
        new TooltipExtender($("StatusBarNOK"), $("StatusText").innerHTML, "tooltip", null, null, null);
        new TooltipExtender($("CompetenceInfo"), $("StatusInfo").innerHTML, "tooltip", 0, 80, $("ContentColumn"));
    }
}


/********** SHOOTINGNEWS **********/
var ShootingNewsExtender = Class.create();
ShootingNewsExtender.prototype =
{
    initialize: function(holder, pics) {
        this.ImageLoaded = this.OnImageLoaded.bindAsEventListener(this);
        this.ImageChanged = this.OnImageChanged.bindAsEventListener(this);

        this.Holder = $(holder);
        this.Pics = pics;
        this.Timeout = 0.1;
        this.PicIndex = -1;

        this.CurrentImage = null;

        this.NextImage = new Image();
        this.NextImage.src = this.NextPicture();

        if (this.Holder.down("img") == undefined)
            new PeriodicalExecuter(this.ImageLoaded, this.Timeout);

        this.Timeout = 4;
    },

    NextPicture: function() {
        if (this.PicIndex == -1)
            this.PicIndex = Math.floor(Math.random() * this.Pics.length);
        else if (this.PicIndex < (this.Pics.length - 1))
            this.PicIndex++;
        else
            this.PicIndex = 0;

        return this.Pics[this.PicIndex];
    },

    OnImageLoaded: function(evObj) {
        if (this.NextImage.complete) {
            evObj.stop();

            this.Holder.appendChild(this.NextImage);

            if (this.CurrentImage == null) {
                this.OnImageChanged(null);
            }
            else {

                Effect.Fade($(this.CurrentImage), { duration: 2 });
                Effect.Appear($(this.NextImage), { duration: 2, afterFinish: this.ImageChanged });
            }
        }
    },

    OnImageChanged: function(evObj) {
        if (this.CurrentImage != null)
            this.Holder.removeChild(this.CurrentImage);

        this.CurrentImage = this.NextImage;

        $(this.CurrentImage).addClassName("visible");

        this.NextImage = new Image();
        this.NextImage.src = this.NextPicture();

        new PeriodicalExecuter(this.ImageLoaded, this.Timeout);
    }
}
/********** SHOOTING NEWS **********/

/********** INPUT TEXTLABEL **********/
var InputTextLabel = Class.create();
InputTextLabel.prototype =
{
    initialize: function(input) {
        this.FocusFound = this.OnFocusFound.bindAsEventListener(this);
        this.FocusLost = this.OnFocusLost.bindAsEventListener(this);

        this.input = $(input);
        this.textLabel = $F(input);

        Event.observe(this.input, "focus", this.FocusFound);


        if (this.input.hasClassName("password")) {
            this.passwordField = new Element("input", { type: "password" });
            this.passwordField.className = this.input.className;
            this.passwordField.addClassName("hdninput");
            this.passwordField.setAttribute("id", this.input.id + "_1");
            this.input.up("p").insert(this.passwordField);
            this.passwordField.hide();

            Event.observe(this.passwordField, "blur", this.FocusLost);
        }
        else {
            Event.observe(this.input, "blur", this.FocusLost);
        }
    },

    OnFocusFound: function(evObj) {

        if (this.passwordField != null) {
            this.passwordField.show();
            this.passwordField.focus();
            this.input.hide();
        }
        else {
            if (this.input.getValue() == this.textLabel)
                this.input.clear();

            this.input.select();
        }
    },

    OnFocusLost: function(evObj) {

        if (this.passwordField != null) {
            if (this.passwordField.getValue() == "") {
                this.passwordField.hide();
                this.input.show();
            }
            else {
                this.input.setValue(this.passwordField.getValue());
            }
        }
        else {
            if (this.input.getValue() == "")
                this.input.value = this.textLabel;
        }
    }
}
/********** INPUT TEXTLABEL **********/


/********** INPUT CALENDAR **********/
Object.extend(Date.prototype, {
    monthnames: ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"],
    daynames: ["Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"]
});

var CalendarInput = Class.create();
CalendarInput.prototype =
{
    
    initialize: function(input) {
        this.FocusFound = this.OnFocusFound.bindAsEventListener(this);
        this.FocusLost = this.OnFocusLost.bindAsEventListener(this);
        this.DateSelected = this.OnDateSelected.bindAsEventListener(this);
        this.KeyPressed = this.OnKeyPressed.bindAsEventListener(this);

        this.input = $(input);

        this.calendarHolder = new Element("div")
        this.calendarHolder.className = "calendar";

        $("Page").insert(this.calendarHolder);

        this.calendar = new scal(this.calendarHolder, this.DateSelected, {
            titleformat: "dd.MM.yyyy",
            updateformat: "dd.MM.yyyy",
            openeffect: Element.show,
            closeeffect: Element.hide
        });

        Position.clone(this.input, this.calendarHolder, {
            setHeight: false,
            setWidth: false,
            offsetTop: this.input.offsetHeight
        });
        
        this.calendarHolder.setStyle({left: ((this.calendarHolder.positionedOffset()[0]+6) + "px")});

        this.calendar.closeCalendar();

        Event.observe(this.input, "focus", this.FocusFound);
        Event.observe(this.input, "blur", this.FocusLost);
        Event.observe(this.input, "keyup", this.KeyPressed);

        if (CalendarInput.Calendars == null)
            CalendarInput.Calendars = new Array();
            
          
        CalendarInput.Calendars.push(this.calendar);



    },

    OnFocusFound: function(evObj) {
        CalendarInput.Calendars.each(function(item) {
            item.closeCalendar();
        });
        this.calendar.openCalendar();
    },

    OnFocusLost: function(evObj) {
    },

    OnDateSelected: function(evObj) {
        this.input.setValue(evObj.format("dd.MM.yyyy"));
        this.calendar.closeCalendar();
    },

    OnKeyPressed: function(evObj) {
        if (evObj.keyCode == Event.KEY_RETURN) {
            this.calendar.toggleCalendar();
        }
        if (evObj.keyCode == Event.KEY_ESC) {
            this.calendar.closeCalendar();
        }
    }
}
/********** INPUT CALENDAR **********/

/********** AUTOCOMPLETE **********/
var AutocompleteExtender = Class.create();
AutocompleteExtender.prototype =
{

    initialize: function(input, url) {
        this.ItemSelected = this.OnItemSelected.bindAsEventListener(this);
        this.KeyPressed = this.OnKeyPressed.bindAsEventListener(this);

        this.input = $(input);

        this.holder = new Element("div")
        this.holder.className = "autocomplete";

        $("Page").insert(this.holder);

        Event.observe(this.input, "keyup", this.KeyPressed);

        

        new Ajax.Autocompleter(this.input, this.holder, url,
            { paramName: "value", afterUpdateElement: this.ItemSelected });

        // disable enter pressed submits
        Event.observe($$("body")[0], 'keypress', function(evObj) {
            if (evObj.keyCode == Event.KEY_RETURN)
                Event.stop(evObj);
        }
        );


    },

    OnKeyPressed: function(evObj) {
        if (evObj.keyCode == Event.KEY_RETURN) {
            Event.fire(this.input, "item:selected");
        }
    },

    OnItemSelected: function(text, li) {
        Event.fire(this.input, "item:selected");
    }
}
/********** AUTOCOMPLETE **********/

function ExecuteSearchBoxExtender(SearchBoxID, UpdatePanelUID) {
    var SearchBox = SearchBoxID;

    if ($(SearchBox)) {
        if ($(SearchBox).value == "")
            $(SearchBox).value = "Suche / Filter";

        new InputTextLabel($(SearchBox));

        Event.observe($(SearchBox), "keypress", function (evObj) {
            if (evObj.keyCode == Event.KEY_RETURN)
                __doPostBack(UpdatePanelUID, 'OnSearch');
        });
    }
}

/********** TOOLTIP **********/
var TooltipExtender = Class.create();
TooltipExtender.prototype =
{
    initialize: function(overElement, content, tooltipCSSClass, posX, posY, parent) {
        this.MouseMove = this.OnMouseMove.bindAsEventListener(this);

        this.ItemOver = this.OnItemOver.bindAsEventListener(this);
        this.ItemOut = this.OnItemOut.bindAsEventListener(this);

        this.overElement = $(overElement);

        this.tooltip = new Element("div");
        this.tooltip.setStyle({ position: "absolute", display: "none" });
        this.tooltip.className = tooltipCSSClass;
        this.tooltip.innerHTML = content;

        ((parent)?parent:$$("body")[0]).insert(this.tooltip);

        this.PosX = posX;
        this.PosY = posY;

        this.mouseX = 0;
        this.mouseY = 0;

        Event.observe(this.overElement, "mouseover", this.ItemOver);


    },

    GetContent: function() {
        return this.tooltip.innerHTML;
    },

    OnItemOver: function(evObj) {
        var targetObj = $(Event.element(evObj));



        this.mouseX = Event.pointerX(evObj);
        this.mouseY = Event.pointerY(evObj);

        Event.observe(targetObj, "mouseout", this.ItemOut);
        Event.stopObserving(targetObj, "mouseover", this.ItemOver);

        Event.observe(document, "mousemove", this.MouseMove);

        this.tooltip.setStyle({ display: "block" });
        this.UpdatePosition();
    },

    OnItemOut: function(evObj) {
        var targetObj = $(Event.element(evObj));

        this.tooltip.setStyle({ display: "none" });

        Event.stopObserving(targetObj, "mouseout", this.ItemOut);
        Event.observe(targetObj, "mouseover", this.ItemOver);

        Event.stopObserving(document, "mousemove", this.MouseMove);
    },

    OnMouseMove: function(evObj) {
        this.mouseX = Event.pointerX(evObj);
        this.mouseY = Event.pointerY(evObj);

        this.UpdatePosition();
    },

    UpdatePosition: function() {
        var viewport = new ViewportProperties();

        var y = this.PosY;
        var x = this.PosX;

        if (y == null) {
            y = ((viewport.WindowHeight) < (this.mouseY + 10 + $(this.tooltip).getHeight() - viewport.ScrollTop))
			? this.mouseY - 10 - $(this.tooltip).getHeight()
			: this.mouseY + 10;
        }

        if (x == null) {
            x = ((viewport.WindowWidth) < (this.mouseX + 10 + $(this.tooltip).getWidth() - viewport.ScrollLeft))
			? this.mouseX - 10 - $(this.tooltip).getWidth()
			: this.mouseX + 10;
        }

        this.tooltip.setStyle({ left: x + "px", top: y + "px" });
    }
};
/********** TOOLTIP **********/



/********** VIEWPORT PROPERTIES **********/
var ViewportProperties = Class.create();
ViewportProperties.prototype = 
{
	initialize : function ()
	{
		this.WindowWidth = 0;
		this.WindowHeight = 0;
		this.ScrollLeft = 0;
		this.ScrollTop = 0;
		this.DocWidth = 0;
		this.DocHeight = 0;
		
		this.Calculate();
	},
	
	Calculate : function ()
	{
			if (window.innerWidth)
			{
				this.WindowWidth = window.innerWidth;
				this.WindowHeight = window.innerHeight;
			}
			else if (document.documentElement && document.documentElement.clientWidth)
			{
				this.WindowWidth = document.documentElement.clientWidth;
				this.WindowHeight = document.documentElement.clientHeight;
			}
			else if (document.body)
			{
				this.WindowWidth = document.body.clientWidth;
				this.WindowHeight = document.body.clientHeight;
			}

			if (window.pageYOffset)
			{
				  this.ScrollLeft = window.pageXOffset;
				  this.ScrollTop = window.pageYOffset;
			}
			else if (document.documentElement && document.documentElement.scrollTop)
			{
					this.ScrollLeft = document.documentElement.scrollLeft;
					this.ScrollTop = document.documentElement.scrollTop;
			}
			else if (document.body)
			{
				  this.ScrollLeft = document.body.scrollLeft;
				  this.ScrollTop = document.body.scrollTop;
			}
			
			if (document.body.scrollHeight > document.body.offsetHeight)
			{
				this.DocWidth = document.body.scrollWidth;
				this.DocHeight = document.body.scrollHeight;
			}
			else 
			{
				this.DocWidth = document.body.offsetWidth;
				this.DocHeight = document.body.offsetHeight;
			}
	}
	
};
/********** VIEWPORT PROPERTIES **********/