/* image preloader */
var Asset = {
	// for one image
	image: function(source, properties){
		properties = $merge({
			onload: $empty,
			onabort: $empty,
			onerror: $empty
		}, properties);
		var image = new Image();
		var element = document.id(image) || new Element('img');
		['load', 'abort', 'error'].each(function(name){
			var type = 'on' + name;
			var event = properties[type];
			delete properties[type];
			image[type] = function(){
				if (!image) return;
				if (!element.parentNode){
					element.width = image.width;
					element.height = image.height;
				}
				image = image.onload = image.onabort = image.onerror = null;
				event.delay(1, element, element);
				element.fireEvent(name, element, 1);
			};
		});
		image.src = element.src = source;
		if (image && image.complete) image.onload.delay(1);
		return element.set(properties);
	},
	// for multiple images
	images: function(sources, options){
		options = $merge({
			onComplete: $empty,
			onProgress: $empty,
			onError: $empty,
			properties: {}
		}, options);
		sources = $splat(sources);
		var images = [];
		var counter = 0;
		return new Elements(sources.map(function(source){
			return Asset.image(source, $extend(options.properties, {
				onload: function(){
					options.onProgress.call(this, counter, sources.indexOf(source));
					counter++;
					if (counter == sources.length) options.onComplete();
				},
				onerror: function(){
					options.onError.call(this, counter, sources.indexOf(source));
					counter++;
					if (counter == sources.length) options.onComplete();
				}
			}));
		}));
	}

};


Element.implement({
	// method works with inputs default VALUE
	resetValue: function(){
		this.addEvents({
			'focus': function(){
				if( this.get('value') === this.retrieve('defaultValue'))
					this.set('value', '');
				return false;
			},
			'blur': function(){
				if( this.get('value') === '')
					this.set('value', this.retrieve('defaultValue'));
				return false;
			}
		});
		return this;
	}
});




window.addEvent('domready', function(){

	// relese inputs
	$$('form input[type=text]').each(function(o){
		o.store('defaultValue', o.get('value'));
	});
	$$('form input[type=text]').resetValue();

	// some fixes for 6 IE :(
	if ( Browser.Engine.trident && Browser.Engine.version <= 4 ) {
		// dublicate 'type' value as class name
		$each( $$('input'), function(obj){
			obj.addClass( obj.get('type') );
		});
	}

	// release semi-link togglers
	$$('.toggler').addEvent('click', function(){
		if (this.get('rel')) {
			$(this.get('rel')).toggleClass('hidden');
		}
		return false;
	});

	// flash for main page
	if ( $('flash') ) {
		new Swiff('/flash/main.swf', {
			container: $('flash'),
			width: '100%',
			height: 480,
			params: {
				wMode: 'transparent',
				allowfullscreen: 'always'
			}
		});
	}





});
