/*	
	Updated @ 2009-05-20
	License: GNU General Public License v3
	Developer: Ehsun Behravesh
	email: ehsun7b@gmail.com
	Tested on Firefox 3.0.3, Opera 9.52, Safari 3.1.2, Internet Explorer 7
*/

var AjaxContent = Class.create({
	initialize: function(container, url, link, loader, options) {
		try {
			this.container          = container;
			this.url                = url;
	    	this.loader             = loader;
            this.link               = link;
            this.loaderContainer    = (options != undefined) && (options.loaderContainer != undefined) ? options.loaderContainer : this.container;
			this.eval               = (options != undefined) && (options.eval != undefined) ? options.eval : false;
            this.onSend             = (options != undefined) && (options.onSend != undefined) ? options.onSend : null;
            this.onReceive          = (options != undefined) && (options.onReceive != undefined) ? options.onReceive : null;
			
			//	---------------------------------
			
			if (this.loader.src == null) {			
				var path = this.loader;
				this.loader = new Image();
				this.loader.src = path;
			}
			
			//	---------------------------------
			
			if (this.container.id == null) {
				this.container = $(this.container);
			}

			//	---------------------------------
			
			if (this.link.id == null) {
				this.link = $(this.link);
			}

			//	---------------------------------

			if (this.loaderContainer.id == null) {
				this.loaderContainer = $(this.loaderContainer);
			}

			//	---------------------------------
			
			this.link.onclick = this.click.bind(this);
			
		} catch (e) {
			alert("error: " + e.name + " - " + e.message);
		}
	},
	
	click: function() {
        if (this.onSend)
            this.onSend();

		this.container.update();
		this.loaderContainer.appendChild(this.loader);
		
		var hC = this.loaderContainer.getHeight();
		var hL = this.loader.height;

		var marginStr = (hC - hL) / 2 + "px auto";
			
		if (Prototype.Browser.IE) {
			this.loader.style.display = "block";
			this.loader.style.margin = marginStr;
		} else {
			this.loader.setStyle({display:  "block", margin: marginStr});
		}
		
		var request = new Ajax.Request(this.url);
		request.options.onSuccess = this.fetch.bind(this, request);
		request.options.onFailure = this.fetchFailure.bind(this, request);
		request.options.onException = this.fetchException.bind(this, request);

		if(this.link.tagName == "A")
			return false;			
	},
	
	fetch: function(req) {
		try {
            if (this.onReceive)
                this.onReceive();

            this.loaderContainer.update();
            if (Prototype.Browser.IE) {
                this.loader.style.margin = '0px';
            } else {
                this.loader.setStyle({'margin': '0px'})
            }
           
			this.container.update(req.transport.responseText.stripScripts());
			if (this.eval == true) {
				var scripts = req.transport.responseText.extractScripts();
				for (i = 0; i < scripts.length; ++i) {
					var e = document.createElement("script");
					e.setAttribute('type','text/javascript');				
					if (Prototype.Browser.IE) {
						e.text = scripts[i];
					} else {
						e.appendChild(document.createTextNode(scripts[i]));
					}
					this.container.appendChild(e);
				}
			}
		} catch (e) {
			alert("error: " + e.name + " - " + e.message);
		}
	},
	
	fetchFailure: function(req) {
		try {
            this.loaderContainer.update();
            if (Prototype.Browser.IE) {
                this.loader.style.margin = '0px';
            } else {
                this.loader.setStyle({'margin': '0px'})
            }
            
			this.container.update(this.url + ' Not found!');
            this.loader.setStyle({'margin': '0px'})
		} catch (e) {
			alert("error: " + e.name + " - "+ e.message);
		}
	},
	
	fetchException: function(req) {
		try {
            this.loaderContainer.update();
            if (Prototype.Browser.IE) {
                this.loader.style.margin = '0px';
            } else {
                this.loader.setStyle({'margin': '0px'})
            }
            
			this.container.update('Error in fetching URL!');
            this.loader.setStyle({'margin': '0px'})
		} catch (e) {
			alert("error: " + e.name + " - " + e.message);
		}
	}		
	
}); 