﻿Event.observe(window, "load", OnDocumentLoad);
Event.observe(window, "unload", OnDocumentUnload);

function OnDocumentLoad ( evObj )
{
    CorrectLayout();
    Event.observe(window, "resize", OnWindowResize);


    new ReferenceExtender();

    new ArchiveExtender();
}

function InitializeStoredObjectPanel(show)
{
}

function OnDocumentUnload ( evObj )
{
}

function OnWindowResize ( evObj )
{
    CorrectLayout();
}

function CorrectLayout ()
{
    var viewport = new ViewportProperties();
    if (viewport.DocHeight < viewport.WindowHeight) {
        $("FooterHolder").setStyle({ position: "absolute" });
        $("ContentHolder").setStyle({ paddingBottom: $("FooterHolder").getHeight() + "px" });
    }
    else {
        $("FooterHolder").setStyle({ position: "relative" });
        $("ContentHolder").setStyle({ paddingBottom: "0px" });
    }
}


/********** REFERENCE EXTENDER **********/
var ReferenceExtender = Class.create();
ReferenceExtender.prototype =
{
    initialize: function() {
        this.ItemOver = this.OnItemOver.bindAsEventListener(this);
        this.ItemOut = this.OnItemOut.bindAsEventListener(this);
        this.ItemClick = this.OnItemClick.bindAsEventListener(this);
        this.CloseClick = this.OnCloseClick.bindAsEventListener(this);

        if ($("References") != null) {
            this.ReferenceInfo = $("ReferenceInfo");

            $$("#References>img").each(function(item) {
                Event.observe(item, "mouseover", this.ItemOver);
                Event.observe(item, "mouseout", this.ItemOut);
                //Event.observe(item, "click", this.ItemClick);
            } .bind(this));

            Event.observe(this.ReferenceInfo.down(".header").down("img"), "click", this.CloseClick);
        }

    },

    OnItemOver: function(evObj) {
        var targetObj = $(Event.element(evObj));
        targetObj.className = "over";
    },

    OnItemOut: function(evObj) {
        var targetObj = $(Event.element(evObj));
        targetObj.className = "";
    },

    OnItemClick: function(evObj) {
        var targetObj = $(Event.element(evObj));
        this.ReferenceInfo.setStyle({ display: "block" });

        this.ReferenceInfo.down(".header").down("strong").innerHTML = targetObj.alt;

        this.ReferenceInfo.down(".content").down("img").src =
            targetObj.src.replace("/thumb/", "/full/");


    },

    OnCloseClick: function(evObj) {
        var targetObj = $(Event.element(evObj));
        this.ReferenceInfo.setStyle({ display: "none" });
    }
}
/********** REFERENCE EXTENDER **********/

/********** ARCHIVE EXTENDER **********/
var ArchiveExtender = Class.create();
ArchiveExtender.prototype =
{
    initialize: function() {
        this.ItemClick = this.OnItemClick.bindAsEventListener(this);
	this.AfterFinish = this.OnAfterFinish.bindAsEventListener(this);
        if ($("Archive") != null) {
            this.Archive = $("Archive");

            this.Archive.hide();
            Event.observe($("ArchiveLink"), "click", this.ItemClick);
        }

    },

    OnItemClick: function(evObj) {
        var targetObj = $(Event.element(evObj));
        targetObj.hide();
	this.Archive.show();
        //Effect.SlideDown(this.Archive, { duration: 1.0, afterFinish:this.AfterFinish });
    },
	OnAfterFinish: function(evObj)
{
          $$("#Archive>h2").each(function(item) {
		item.setStyle({visibility: "visible", display: "block"});
            } .bind(this));

}
}
/********** ARCHIVE EXTENDER **********/


/********** 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 **********/
