/*
---
description: This class gives you a method to upload files 'the ajax way'

license: MIT-style

authors:
- Arian Stolwijk

requires:
requires: 
  core/1.2.4: 
  - Class.Extras
  - Element.Event
  - Element.Style

provides: [Element.iFrameFormRequest, iFrameFormRequest]

...
*/

/**
 * @author Arian Stolwijk
 * Idea taken from http://www.webtoolkit.info/ajax-file-upload.html
 */

var iFrameFormRequest = new Class({
	
	Implements: [Options, Events],
	
	options: { /*
		onRequest: function(){},
		onComplete: function(data){},
		onFailure: function(){}, */
		eventName: 'submit'
	},
	
	initialize: function(form, options){
		this.setOptions(options);
		var frameId = this.frameId = 'f' + Math.floor(Math.random() * 99999);
		var loading = false;

		this.form = document.id(form);
		
		this.formEvent = function(){
			if(this.options.validator){
				if(this.options.validator.validate() !== true){
					return false;
				}
			}
			loading = true;
			this.fireEvent('request');
		}.bind(this);

		this.iframe = new IFrame({
			name: frameId,
			styles: {
				display: 'none'
			},
			src: 'about:blank',
			events: {
				load: function(){
					if (loading) {
						var doc = this.iframe.contentWindow.document;
						if (doc) {
							if (doc.location.href == 'about:blank') this.fireEvent('failure');
							else this.fireEvent('complete', doc.body.innerHTML);
						} else {
							this.fireEvent('failure');
						}
						loading = false;
					}
				}.bind(this)
			}
		}).inject(document.id(document.body));
		
		this.attach();
	},
	
	send: function(){
		this.form.submit();
	},
	
	attach: function(){
		this.form
			.store('iFrameFormRequest:formTarget', this.form.get('target'))
			.set('target', this.frameId)
			.addEvent(this.options.eventName, this.formEvent);
	},
	
	detach: function(){
		this.form
			.store('iFrameFormRequest:formTarget', this.form.get('target'))
			.set('target', this.form.retrieve('iFrameFormRequest:formTarget'))
			.removeEvent(this.options.eventName, this.formEvent);
	},

	toElement: function(){
		return this.iframe;
	}
	
});

Element.implement('iFrameFormRequest', function(options){
	this.store('iFrameFormRequest', new iFrameFormRequest(this,options));
	return this;
});

var BlinkText = new Class({
 
  //implements
  Implements: [Options,Events],
 
  //options
  options: {
    from: '#000000',
    to: '#f00000',
    duration: 500,
    times: 3
  },
 
  //initialization
  initialize: function(el,options) {
    //set options
    this.setOptions(options);
    this.element = $(el);
    this.times = 0;
  },
 
  //starts the blink effect
  start: function(times) {
    this.element.setStyle('color',this.options.from);
 
    if(!times) times = this.options.times * 2;
    this.running = 1;
    // Fire start event
    this.fireEvent('start').run(times -1);
  },
 
  //stops the blink effect
  stop: function() {
    this.running = 0;
    // Fire stop event
    this.fireEvent('stop');
  },
  run: function(times) {
    //make it happen
    var self = this;
    var new_to = self.element.getStyle('color') == self.options.from ? self.options.to : self.options.from;
 
    self.fx = new Fx.Tween(self.element,{
      duration: self.options.duration / 2,
      onComplete: function() {
        // Fire tick event
        self.fireEvent('tick');
        if(self.running && times)
        {
          self.run(times-1);
        }
        else 
        {
          if(self.element.getStyle('color') != self.options.from) self.run(0);
          self.element.setStyle('color', self.options.to)
		  self.fireEvent('complete');
        }
      }
    }).start('color',new_to);
  }
});
