var myJax;

var AjaxLinks = new Class({
	Implements: [Events, Options],
	options: {
		loading: '',
		cache: true,
		controller: false,
		ajaxOptions: {
			method: 'get',
			evalScripts: false //true
		}
	},
	
	initialize: function(container,options) {
		this.setOptions(options);
		this.container = $(container);
		//this.loading = $(this.options.loading);
		//this.loadingFx = new Fx.Tween(this.loading, {property: "opacity", duration: 1100});
		this.loadingFx = new Fx.Tween(this.container, {property: "opacity", duration: 1100});
	},
	
	getContent: function(item) {
		//alert(this.isCached(item));
		this.container.empty().setStyle('display','none');
		this.container.setStyle('opacity','0');
		
		var request = item.retrieve('ajaxlink:request');
		if (!request) {
			//var url = item.get('href').split('/');
			var url = item.get('href');
			
			if (this.options.controller) {
				var page = this.options.ajaxOptions.url;
				var params = url[url.length - 2] + '=' + url[url.length - 1];
			} else {
				var page = item.get('href');
				var params;
			}
			var request = new Request.HTML($merge(this.options.ajaxOptions, {
				data: params,
				url: page
				//onComplete: function(){alert("complete")}
			})).addEvents({
				onRequest: function(instance) {
					//alert ("on request: show loading");
					//this.loading.setStyle('display', 'block');//show loader
				}.bind(this),
				onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
					//if (this.options.cache) this.container.store(url[url.length - 1]+':loaded', true);
					//this.container.store(url[url.length - 1]+':html', responseHTML);
					if (this.options.cache) this.container.store(url+':loaded', true);
					this.container.store(url+':html', responseHTML);
					this.pageLoaded(item, true);
				}.bind(this),
				onFailure: function(instance) {
					//if (this.failed < 10) {
					//	this.failed++;
					//	this.getContent(item);
					//} else {
						this.pageFailed(item);
					//}
				}.bind(this)
			});
			item.store('ajaxlink:request', request);
		}
		request.send();
	},
	
	pageLoaded: function(item) {//,flag	
		var url = item.get('href');
		this.container.set('html', this.container.retrieve(url+':html'));
		Mediabox.scanPage();
		this.container.setStyle('display','block');
		//if (flag){
			var myGroup = new Group(this.container.getElements('img'));
			myGroup.addEvent('load', function(){
				this.loadingFx.start(1);
			}.bind(this));
		//}else{
		//	this.container.setStyle('opacity','1');
		//}
		attachLinks();
		this.fireEvent('pageLoaded', this.container);
	},
	
	removeLoader: function(){
		//this.container.setStyle('display','block');
		this.loadingFx.start(1);
		/* if(this.loading.getStyle('opacity').toInt()==1){
			this.loadingFx.start(0).chain(function(){
				this.loading.setStyle('display','none')}.bind(this));
		}; */
	},
	
 	pageFailed: function(item) {
		//alert("AjaxLinks: loading "+item.get('href')+" failed!");
		//this.loading.setStyle('display', 'none');
		var pageError = new Element('p');
		pageError.set('text', 'The Server is not responding. Please check your connection and try again!');
		pageError.inject(this.container, 'top');
		this.loadingFx.start(1);
		//this.removeLoader();
	},
	
	cacheThis: function(item){
		if (this.isCached(item)){
			this.pageLoaded(item);
		} else {
			this.failed = 0;
			this.getContent(item);
		}
	},
	
	isCached: function(item){
		var url = item.get('href');
		if (this.options.cache && this.container.retrieve(url+':loaded')){return true}else{return false}
	}

});


window.addEvent("domready", function(){
	myJax = new AjaxLinks('ajax_content', {  loading:'ajaxLoading'  });
});

//(function(){
//})(); 