Loader = new Class({
	Implements: Options,
	
	options: {
		'height': false,
		'width': false,
		'text': false,
		'image': false,
		'logo': false,
		'color': false,
		'border':false,
		'onComplete': $empty
	},
	
	initialize: function(parent,options){
		this.setOptions(options);
		this.parent = parent;
		this.cancel = false;
		this.loading = false;
		//Generate HTML
		this.loader_container = new Element('div',{'class':'loader_container'})
		this.loader = new Element('div',{'class':'loader'});
		this.logo_container = new Element('div',{'class':'Obray Logo'});
		this.loader_animation_container = new Element('div',{'class':'loader_animation_container'});
		this.loader_animation = new Element('img');
		if(this.options.text != false){this.loader_animation.setProperty('alt',this.options.text);} else {this.loader_animation.setProperty('alt','Loading...');}
		this.loader_text = new Element('div',{'class':'loader_txt'});
		if(this.options.text != false){this.loader_text.set('html',this.options.text);}
		this.br = new Element('br',{'class':'clear'});
		
		//determine Loading Animation
		this.preLoadImages = []
		if(this.options.image == 'huge') {
			this.loader_animation.setProperty('alt','obray/images/loaders/loading_animation_100x100.gif');
			this.preLoadImages[0] = 'obray/images/loaders/loading_animation_100x100.gif';
			this.loader_text.setStyles({'font-size':'100px'});
		} else if(this.options.image == 'big') {
			this.loader_animation.setProperty('src','obray/images/loaders/loading_animation_50x50.gif');
			this.preLoadImages[0] = 'obray/images/loaders/loading_animation_50x50.gif';
			this.loader_text.setStyles({'font-size':'50px'});
		} else if(this.options.image == 'medium') {
			this.loader_animation.setProperty('src','obray/images/loaders/loading_animation_25x25.gif');
			this.preLoadImages[0] = 'obray/images/loaders/loading_animation_25x25.gif';
			this.loader_text.setStyles({'font-size':'20px'});
		} else if(this.options.image == 'small') {
			this.loader_animation.setProperty('src','obray/images/loaders/loading_animation_20x20.gif');
			this.preLoadImages[0] = 'obray/images/loaders/loading_animation_20x20.gif';
			this.loader_text.setStyles({'font-size':'15px'});
		} else if(this.options.image == 'tiny') {
			this.loader_animation.setProperty('src','obray/images/loaders/loading_animation_15x15.gif');
			this.preLoadImages[0] = 'obray/images/loaders/loading_animation_15x15.gif';
			this.loader_text.setStyles({'font-size':'12px'});
		} else if(this.options.image != false){
			this.loader_animation.setProperty('src',this.options.image);
			this.preLoadImages[0] = this.options.image;
		}else {
			this.loader_animation.setProperty('src','obray/images/loaders/loading_animation_25x25.gif');
			this.preLoadImages[0] = 'obray/images/loaders/loading_animation_25x25.gif';
			this.loader_text.setStyles({'font-size':'20px'});
		}
				
		//determine logo
		if(this.options.logo == 'huge') {
			this.logo_image.setProperty('src','obray/images/loaders/obray_txt_228x100.gif');
			this.preLoadImages[1] = 'obray/images/loaders/obray_txt_228x100.gif';
		} else if(this.options.logo == 'big') {
			this.logo_image.setProperty('src','obray/images/loaders/obray_txt_114x50.gif');
			this.preLoadImages[1] = 'obray/images/loaders/obray_txt_114x50.gif';
		} else if(this.options.logo == 'medium') {
			this.logo_image.setProperty('src','obray/images/loaders/obray_txt_57x25.gif');
			this.preLoadImages[1] = 'obray/images/loaders/obray_txt_57x25.gif';
		} else if(this.options.logo == 'small') {
			this.logo_image.setProperty('src','obray/images/loaders/obray_txt_46x20.gif');
			this.preLoadImages[1] = 'obray/images/loaders/obray_txt_46x20.gif';
		} else if(this.options.logo == 'tiny') {
			this.logo_image.setProperty('src','obray/images/loaders/obray_txt_35x15.gif');
			this.preLoadImages[1] = 'obray/images/loaders/obray_txt_35x15.gif';
		} else if(this.options.logo != false){
			this.logo_image.setProperty('src',this.options.logo);
			this.preLoadImages[1] = this.options.logo;
		}

		//set styles
		if(this.options.width != false && this.options.height != false){
			this.loader_container.setStyles({'position':'relative','width':this.options.width,'height':this.options.height});
		} else {
			this.loader_container.setStyles({'position':'relative'});
		}
		this.loader_animation_container.setStyles({'float':'left'});
		this.loader_text.setStyles({'float':'left','font-family':'"Century Gothic","Trebuchet MS",Arial,Helvetica,sans-serif','padding-left':'15px','padding-top':'0px'})
		if(this.options.color != false){
			this.loader_text.setStyles({'color':this.options.color});
		}
		if(this.options.border != false){
			this.loader_animation_container.setStyles({'background-color':'#ffffff','padding':'3px','border':'1px solid #777777'})
			this.loader_text.setStyles({'padding':'3px'});
		}
		
		//preload images
		this.loading = true;
		var loaderImage = new Asset.images(this.preLoadImages,{onComplete:function(){	
			this.loading = false;
			if(this.cancel == false){
				//Construct loader
				if(this.options.width != false && this.options.height != false){
					this.loader.inject(this.loader_container);
				}
				if(this.options.logo != false){
					this.logo_image.inject(this.loader);
				}
				this.loader_animation_container.inject(this.loader);
				this.loader_animation.inject(this.loader_animation_container);
				this.loader_text.inject(this.loader);
				this.br.inject(this.loader);
				if(this.options.width != false && this.options.height != false){
					this.loader_container.inject(this.parent);
				} else {
					this.loader.inject(this.parent);
				}
				
				//center loader
				if(this.options.width != false){
					var loader_text_width = this.loader_text.getSize().x + 10;
					var loader_animation_width = this.loader_animation_container.getSize().x;
					this.loader.setStyles({'width':loader_text_width + loader_animation_width,'position':'absolute','left':'50%','margin-left':-(loader_text_width+loader_animation_width)/2});
					
					var loader_animation_height = this.loader_animation_container.getSize().y;
					this.loader.setStyles({'height':loader_animation_height,'position':'absolute','top':'50%','margin-top':-(loader_animation_height)/2});
				}
				this.options.onComplete();
			} else {
				this.cancel = false;
			}
			
		}.bind(this)});
	},
	
	destroy: function(){
		if(this.loading == true){this.cancel = true;}
		if(this.options.width != false && this.options.height != false){
			this.loader_container.destroy();
		} else {
			this.loader.destroy();
		}
	},
	
	get: function(){
		if(this.options.width != false && this.options.height != false){
			return this.loader_container;
		} else {
			return this.loader;
		}	
	}
});