Event.observe(window, "load", OnDocumentLoad);
Event.observe(window, "unload", OnDocumentUnload);

function OnDocumentLoad ( evObj )
{
	new LayoutCorrection();
	new BackgroundExtender(8);
	new LinkExtender();
}

function OnDocumentUnload ( evObj )
{
}


/********** LayoutCorrection  **********/
var LayoutCorrection = Class.create();
LayoutCorrection.prototype =
{
	initialize : function ()
	{
		this.WindowResize = this.OnWindowResize.bindAsEventListener(this);
		this.vp = new ViewportProperties();
		
		this.CorrectPage();
				
		Event.observe(window, "resize", this.WindowResize);
	},
	
	OnWindowResize : function (evObj)
	{
		this.CorrectPage();
	},
	
	CorrectPage : function ()
	{
		this.vp.Calculate();
		
		var minHeight = $("Navigation").getHeight() + 3*50 + $("Content").getHeight();
		var minWidth = $("Content").getWidth() + 2*50;
		$("Page").setStyle({height: ((minHeight > this.vp.WindowHeight) ? minHeight : this.vp.WindowHeight) + "px", width: ((minWidth > this.vp.WindowWidth) ? minWidth : this.vp.WindowWidth) + "px"});
				
		var contentTop = Math.floor((this.vp.WindowHeight-$("Navigation").getHeight()-50-$("Content").getHeight())/2);
		$("Content").setStyle({top: ((minHeight > this.vp.WindowHeight) ? 50 : contentTop) + "px"});
		
		
		$("Content").setStyle({display: "block"});
		$("Navigation").setStyle({display: "block"});		
	}
}
/********** LayoutCorrection  **********/

/********** BackgroundExtender  **********/
var BackgroundExtender = Class.create();
BackgroundExtender.prototype =
{
	initialize : function (pics)
	{
		this.ImageLoaded = this.OnImageLoaded.bindAsEventListener(this);
		this.ImageChanged = this.OnImageChanged.bindAsEventListener(this);
		this.WindowResize = this.OnWindowResize.bindAsEventListener(this);
		
		this.Pics = pics;
		this.timeout = 0.1;
		this.usedPics = new Hash();
		this.NextImage = null;
		this.NextImageNum = "";
		
		this.ActiveBackground = $("Background1");
		this.NextBackground = $("Background2");
		
		this.bgWidth = null;
		this.VP = new ViewportProperties();
		this.CheckBackgroundWidth();
		Event.observe(window, "resize", this.WindowResize);

		Effect.Fade(this.NextBackground.down("img"), {duration: 0, afterFinish: this.ImageChanged});
		
	},
	
	CheckBackgroundWidth : function (evObj)
	{
			this.VP.Calculate();
		
     	this.bgWidth = "1000";
   		var screenWidth = this.VP.WindowWidth;
   		
    	
    	if ( screenWidth >= 1000 )
    		this.bgWidth = "1250";
    	if ( screenWidth >= 1250 )
    		this.bgWidth = "1500";
    	if ( screenWidth >= 1500 )
    		this.bgWidth = "1750";
    	if ( screenWidth >= 1750 )
    		this.bgWidth = "2000";
	},
	
	RandomImage : function (evObj)
	{
		this.NextImageNum = Math.ceil(Math.random()*this.Pics);
		
		while ( this.usedPics.get(this.NextImageNum) == "used" )
			this.NextImageNum = Math.ceil(Math.random()*this.Pics);
		
		return "images/bg-" + this.bgWidth + "/" + this.NextImageNum + ".jpg";
	},
	
	OnImageLoaded : function (evObj)
	{
		if (this.NextImage.complete) 
		{
			evObj.stop();
			
			this.NextBackground.down("img").src = this.NextImage.src;
			Effect.Fade(this.ActiveBackground.down("img"), {duration: 1});
			Effect.Appear(this.NextBackground.down("img"), {duration: 1, afterFinish: this.ImageChanged});
			
			var bg = this.ActiveBackground;
			this.ActiveBackground = this.NextBackground;
			this.NextBackground = bg;
			
			if ( this.usedPics.keys().length >= (this.Pics-1) )
				this.usedPics = new Hash();
			
			this.usedPics.set(this.NextImageNum, "used");
		}
	},

	OnImageChanged : function (evObj)
	{
		this.NextImage = new Image();
		this.NextImage.src = this.RandomImage();

		new PeriodicalExecuter(this.ImageLoaded, this.timeout);
		this.timeout = 5;
	},
	
	OnWindowResize : function(evObj)
	{
		this.CheckBackgroundWidth();
		this.NextImage = new Image();
		this.NextImage.src = this.RandomImage();
	}

}
/********** BackgroundExtender  **********/

/********** LinkExtender  **********/
var LinkExtender = Class.create();
LinkExtender.prototype = 
{
    initialize : function ( )
    {
        this.ItemOver = this.OnItemOver.bindAsEventListener(this);
        this.ItemOut = this.OnItemOut.bindAsEventListener(this);
        
        var listItemsImages = $("Page").select("a img");
        listItemsImages.each(
        	function(el) 
        	{ 
						if ( el.src.indexOf("-p.") != -1 )
						{
							Event.observe(el, "mouseover", this.ItemOver); 
							Event.observe(el, "mouseout", this.ItemOut);
						}
					}, this);
    },
    
    OnItemOver : function ( evObj )
    {
    	Event.element(evObj).src = Event.element(evObj).src.replace("-p.", "-a.");
    },
    
    OnItemOut : function ( evObj )
    {
    	
    	Event.element(evObj).src = Event.element(evObj).src.replace("-a.", "-p.");
    }
}
/********** LinkExtender  **********/

var VideoLoader = Class.create();
VideoLoader.prototype =
{
	initialize : function ()
	{
		this.VideoCodeLoaded = this.OnVideoCodeLoaded.bindAsEventListener(this);
		this.CloseClicked = this.OnCloseClicked.bindAsEventListener(this);
		
		if ( $("VideoHolder") )
		{
			Event.observe($("VideoHolder"), "click", this.CloseClicked);
		}
	},
	
	LoadVideo : function()
	{
		$("Video").innerHTML = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="480" height="425" id="video" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="allowFullScreen" value="false" /><param name="movie" value="video.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#333333" />	<embed src="video.swf" quality="high" bgcolor="#333333" width="480" height="425" name="video" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
		$("VideoHolder").setStyle({display: "block"});
		/*new Ajax.Request("video.html", { method: 'get', onSuccess: this.VideoCodeLoaded });*/
	},
	
	OnVideoCodeLoaded : function (evObj)
	{
		
		$("Video").innerHTML = evObj.responseText;
		$("VideoHolder").setStyle({display: "block"});
	},
	
	OnCloseClicked : function (evObj)
	{
		$("Video").innerHTML = " ";
		$("VideoHolder").setStyle({display: "none"});
	}
}

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;
			}
	}
};