/**
 * @author paweł smoleński <pawel@smolenski.pl>
 * @copyright paweł smoleński <pawel@smolenski.pl>
 */

var pl =
{
	smolenski:
	{
		Form: new Class
		({
			__submitButton: null,
			__fields: null,

			initialize: function()
			{
			},
			
			clear: function(fieldName)
			{
				var field = this.__fields[fieldName];
				
				if (!field)
					return false;
				
				switch (field.get('tag'))
				{
					case 'select':
						field.options.length = 0;
						break;
				}
			},
			
			addOption: function(fieldName, label, value)
			{
				var field = this.__fields[fieldName];
				var option = new Option(label, value);
				field.options[field.options.length] = option;
			}
		}),
		
		Gallery: new Class
		({
			__container: null,
			__background: null,
			__frame: null,
			__photos: null,
			__current: null,
			__animationDuration: 500,
			__ie6: null,
			
			initialize: function()
			{
				this.__ie6 = new pl.smolenski.IE6();
			},
			
			setGallery: function(data)
			{
				this.__photos = data;
			},
			
			show: function(i)
			{
				i = Math.min(Math.max(0, i), this.__photos.length - 1);
				
				this.__current = i;
				
				this.__container.setStyle('display', 'block');
				
				var framePadding = 
				{
					'left': this.__frame.getStyle('padding-left').toInt(),
					'top': this.__frame.getStyle('padding-top').toInt()
				};
				
				var photo = this.__frame.getElement('.photo');
				
				photo.setStyles.delay
				(
						200, photo,
						[{
					'background-image': 'url(' + this.__photos[i].filename + ')',
					'background-position': 'center -2000px'
				}]);
				
				photo.setStyle.delay
				(
					this.__animationDuration,
					photo,
					['background-position', 'center center']
				);
				
				new Fx.Morph
				(
					photo,
					{
						'duration': this.__animationDuration,
						'transition': Fx.Transitions.Quad.easeOut
					}
				)
				.start
				({
					'height': this.__photos[i].height,
					'width': this.__photos[i].width
				});
				
				new Fx.Morph
				(
					this.__frame,
					{
						'duration': this.__animationDuration,
						'transition': Fx.Transitions.Quad.easeOut
					}
				)
				.start
				({
					'margin-left': (-this.__photos[i].width / 2 -framePadding.left),
					'margin-top': (-this.__photos[i].height / 2 -framePadding.top)
				});

				
				this.__frame.getElement('.label')
					.set('text', this.__photos[i].label)
					.setStyle('display', this.__photos[i].label ? 'block' : 'none');
				
				this.__frame.getElement('.navigation .counter')
					.set('text', (i + 1) + '/' + this.__photos.length);
				
				this.__frame.getElement('.navigation')
					.setStyle('display', this.__photos.length > 1 ? 'block' : 'none');
				
				this.__resize();
				
				this.__ie6.hideSelects();
			},
			
			showNext: function()
			{
				this.show(this.__current + 1);
			},
			
			showPrev: function()
			{
				this.show(this.__current - 1);
			},
			
			close: function()
			{
				this.__container.setStyle('display', 'none');
				this.__ie6.showSelects();
			},
			
			__generate: function()
			{
				if (!this.__container)
					return false;
				
				this.__background = this.__container.getElement('.background');
				this.__frame = this.__container.getElement('.frame');
				
				this.__resize();
				
				window.addEvent('resize', this.__resize.bind(this));
				this.__background.addEvent('click', this.close.bind(this));
				this.__frame.getElement('.photo').addEvent('click', this.close.bind(this));
				this.__frame.getElement('.navigation .prev').addEvent('click', this.showPrev.bind(this));
				this.__frame.getElement('.navigation .next').addEvent('click', this.showNext.bind(this));
			},
			
			__resize: function()
			{
				this.__background.setStyles
				({
					'width': 0,
					'height': 0
				});
				
				var docSize = document.getScrollSize();

				this.__background.setStyles
				({
					'width': docSize.x,
					'height': docSize.y
				});
				
				if (this.__ie6.is())
					this.__frame.setStyles
					({
						'top': window.getScroll().y + window.getSize().y / 2
					})
			}
		}),
		
		Interface: new Class
		({
			addFlash: function(targetContainer, flashData)
			{
				defaultData = 
				{
					'wmode': 'transparent',
					'vars': '',
					'backgroundColor': 'transparent'
				}
				
				flashData = $merge(defaultData, flashData);
				
				$(targetContainer).innerHTML = '<object id="' + flashData.id + '" width="' + flashData.width + 'px" height="' + flashData.height + 'px" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0">'
				+ ' <param name="allowScriptAccess" value = "sameDomain" />'
				+ ' <param name="movie" value="' + flashData.url + '" />'
				+ ' <param name="loop" value="true" />'
				+ ' <param name="menu" value="false" />'
				+ ' <param name="quality" value="high" />'
				+ ' <param name="bgcolor" value="' + flashData.backgroundColor + '" />'
				+ ' <param name="flashvars" value="' + flashData.vars + '" />'
				+ ' <param name="wmode" value="' + flashData.wmode + '" />'
				+ ' <embed name="' + flashData.id + '" bgcolor="' + flashData.backgroundColor + '" src="' + flashData.url + '" swLiveConnect="true" loop="true" play="true" menu="false" quality="best" allowScriptAccess="sameDomain" width="' + flashData.width + 'px" height="' + flashData.height + 'px" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="' + flashData.vars + '" wmode="' + flashData.wmode + '"></embed>'
				+ ' </object>';
			},
			
			getFlash: function(id)
			{
				return (navigator.appName.indexOf("Microsoft") != -1) ? window[id] : document[id];
			},
			
			scrollToAnchor: function(anchorName)
			{
				var anchorElement = $E('a[name=' + anchorName + ']');
				
				if (!anchorElement)
					return false;
					
				new Fx.Scroll(window).scrollTo(0, anchorElement.getTop());
			}		
		}),
		
		IE6: new Class
		({
			__selects: [],
			__enabled: false,
			
			initialize: function()
			{
				this.__enabled = (Browser.Engine.trident && Browser.Engine.version == 4);
			},
			
			is: function()
			{
				return this.__enabled;
			},
			
			hideSelects: function()
			{
				if (!this.__enabled)
					return false;
				
				this.__selects = document.getElements('select');
				
				for (var i = 0; i < this.__selects.length; i++)
					this.__selects[i].setStyle('visibility', 'hidden');
			},
			
			showSelects: function()
			{
				if (!this.__enabled)
					return false;
				
				for (var i = 0; i < this.__selects.length; i++)
					this.__selects[i].setStyle('visibility', 'visible');
			},
			
			getWindowCenter: function()
			{
				if (!this.__enabled)
					return false;
			}
				
		})
	}
};
