/*
	(C) Ilkka Huotari
	All Rights Reserved
*/

var PhotoAlbum = {
	image : null,
	currentSizes : [],
	nextSizes : {},
	winW : null,
	winH : null,

	init : function(currentSizes, nextSizes) 
	{
		var newPhoto;
		this.image = document.getElementById('nd_photo_img');
		this.controls = document.getElementById('photo_controls');
		this.currentSizes = currentSizes;
		this.nextSizes = nextSizes;

		this.checkWindowSize();
		newPhoto = this.setPhotoSize();

		if (!newPhoto)
		{
			this.preloadNext();
		}

		this.addEvent(window, "resize", this.setPhotoSize.bind(this));
		this.addEvent(window, "mouseout", this.onBlur.bind(this));
		this.addEvent(window, "mouseover", this.onFocus.bind(this));
		this.addEvent(document, "keydown", this.onKeydown.bind(this));

		function bind(object) {
			var __method = this;
			return function() {
				return __method.apply(object, arguments);
			}
		}
	},

	addEvent : function(obj, name, handler) {
		if (obj.attachEvent)
			obj.attachEvent("on" + name, handler);
		else
			obj.addEventListener(name, handler, false);
	},

	onKeydown : function(e)
	{
//		if (!e) e = window.event;
		var o, key = (e.keyCode || e.which);

		if (key == 37)
		{
			o = document.getElementById('NDPhotoAlbumPrevButton');
		}
		if (key == 38)
		{
			o = document.getElementById('NDPhotoAlbumUpButton');
		}
		else if (key == 39)
		{
			o = document.getElementById('NDPhotoAlbumNextButton');
		}

		if (o)
		{
			window.location.href = o.href;
		}
	},

	onBlur : function()
	{
		this.controls.style.display = 'none';
	},

	onFocus : function()
	{
		this.controls.style.display = 'block';
	},
	
	onLoad : function()
	{
	},

	onResize : function()
	{
	},

	checkWindowSize : function()
	{
		var w, h;

		if (self.innerWidth)
		{
			w = self.innerWidth;
			h = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientWidth)
		{
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		}
		else if (document.body)
		{
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}

		this.winW = w;
		this.winH = h;
	},

	setPhotoSize : function()
	{
		this.checkWindowSize();

		for (i = 0; i < this.currentSizes.length; i++)
		{
			var force = false;

			if (this.image.width > this.winW ||
				this.image.height + 40 > this.winH)
			{
				force = true;
			}

			if (this.image.width && 
				this.image.height && 
				(force ||
				(this.image.width < this.currentSizes[i][0] &&
				this.image.height < this.currentSizes[i][1])) &&
				this.currentSizes[i][0] < this.winW &&
				this.currentSizes[i][1] + 40 < this.winH)
			{
				this.image.onload = this.preloadNext.bind(this);
				this.image.width = this.currentSizes[i][0];
				this.image.height = this.currentSizes[i][1];
				this.image.src = this.currentSizes[i][2];
				this.setCookie();
				return true;
			}
		}

		function bind(object) {
			var __method = this;
			return function() {
				return __method.apply(object, arguments);
			}
		}

	},

	preloadNext : function()
	{
		var ImagePreload = new Image();

		for (var i in this.nextSizes)
		{
			if (i < this.winW)
			{
//				alert(i+" .. "+this.nextSizes[i]);
				ImagePreload.src = this.nextSizes[i];
				return;
			}
		}
	},

	setCookie : function(w, h) {
		var date = new Date();
		date.setTime(date.getTime()+(24*60*60*1000));
		var expires = "; expires="+date.toGMTString();

		document.cookie = "netdoc[photoalbum][w]="+this.winW+expires+"; path=/";
		document.cookie = "netdoc[photoalbum][h]="+this.winH+expires+"; path=/";
	}
}

