
var dummyAccord = new Class({
	Implements: [Events, Options],
	initialize: function(contentEl,options){
		var self = this;
		this.setOptions(options);
		this.contentEl = $(contentEl);
	},
	show:function(){
		this.contentEl.setStyle('display','');
	},
	hide:function(){
		this.contentEl.setStyle('display','none');
	}
});


var ajaxizeTabs = new Class({
	Implements:[Options,Events],
	cookieLastTabName:'ajaxizeTabs_last',
	initialize: function(container){
		var self = this;
		this.initDone = false;
		this.container = container;
		this.tabs = new Array();
		$$('#ajaxizeTabsContainer ul.tabs li').each(function(item, index){
			self.tabs.push( {
				id: item.getProperty('rel'),
				index: index,
				//url: item.getElement('a').getProperty('href')
			});
		});
		
		this.currentTab = 0;
		
		//Cookie stored last tab ?
		var c = Cookie.read(this.cookieLastTabName);
		if(c){
			var t = this.getTab(c);
			if(t) this.currentTab = t.index;
		}
		
		//accordions
		this.tabs.each(function(tab,i){
			tab.contentLoaded = true;
			tab.ajaxTarget = $('ajaxTabs_'+tab.id).getElement('.acc-content');
			tab.accEl = $('ajaxTabs_'+tab.id).getElement('.acc-content');
			tab.togglerEl = $('ajaxTabs_'+tab.id+'-toggler');
			tab.accordion = new dummyAccord(tab.accEl);
			tab.togglerEl.addEvent('click',self.goTab.pass(i,self));
			if(i!=self.currentTab) tab.accordion.hide();
		});
		
		//load first tab
		this.goTab(this.currentTab);
		this.initDone = true;
	},
	getTab: function(index){ //here index can be tab.index or tab.id
		if(isNaN(index)){
			for(var i=0;i<this.tabs.length;i++){
				if(this.tabs[i].id==index) return this.tabs[i];
			}			
		}else{
			for(var i=0;i<this.tabs.length;i++){
				if(i==index) return this.tabs[i];
			}		
		}

	},
	showTab: function(index){
		for(var i=0;i<this.tabs.length;i++){
			if(i==index){
				this.tabs[i].accordion.show();
				this.tabs[i].togglerEl.addClass('active');
			}else{
				this.tabs[i].accordion.hide();
				this.tabs[i].togglerEl.removeClass('active');
			}
		}
	},
	goTab: function(index){	
		if(this.currentTab==index && this.initDone) return;
		var tab = this.getTab(index);
		this.showTab(index);
		if(!tab.contentLoaded){
			this.refreshTab(index);
		}
		this.currentTab = index;
		Cookie.write(this.cookieLastTabName,index);
	},
	refreshTab: function(index){
		var index = typeof(index)=='undefined' ? this.currentTab : index;
		var tab = this.getTab(index);
		tab.ajaxTarget.set('html','');
		new Request.HTML({
			url:tab.url,
			method:'get',
			update:tab.ajaxTarget,
			onComplete:function(){
				tab.contentLoaded=true;
				if(tab.ajaxize){
					setAjaxBehaviour(tab.ajaxTarget); //historical
				}
			}
		}).send();
	}
});

function setAjaxBehaviour(targetEl){
	
	targetEl.getElements('form').each(function(item){
		
		//set ajax behaviour
		item.set('send', {
			url: item.getProperty('action'), 
			method: item.getProperty('method'),
			onComplete: function(r){setAjaxBehaviour_callBack(r,targetEl);ui.scrollToTop();},
			evalScripts: true
		});
		
		//override submit event
		item.addEvent('submit', function(e) {
			new Event(e).stop();
			this.send();
			//protect submit button against multiple submit
			this.getElement('input[type=submit]').setProperty('disabled','disabled').setProperty('value','Loading...');
			//protect close btn too
			if(this.getElement('input[type=button]')){
				this.getElement('input[type=button]').setProperty('disabled','disabled');
			}
			
		});
		
	});
	
	targetEl.getElements('a').each(function(item){
		var aTarget = item.getProperty('target');
		if(item.href.substr(0,11)!='javascript:' && aTarget!='_top' && aTarget!='_blank'){
			item.addEvent('click', function(e) {
				var e = new Event(e).stop();
				var htmlRequest = new Request({
					url: item.getProperty('href'), 
					method: 'get',
					onComplete: function(r){setAjaxBehaviour_callBack(r,targetEl);ui.scrollToTop();}
				}).get({isAjax:1});
			});		
		}
	});
	
	
	//reset password link must open in popup, not in tab.
	var passLinks = targetEl.getElements('a.passwordLink');
	if(passLinks){
		passLinks.each(function(link){
			link.removeEvents('click');
			link.addEvent('click',function(e){
				e.stop();
				
				with(ui.popup){
					title = 'Password reset';
					content = '<div id="resetPass_ajaxTarget"></div>';
					loading = true;
				};
				ui.showPopup();
				
				new Request.HTML({
					url:this.href,
					method:'get',
					update:$('resetPass_ajaxTarget'),
					onComplete:ui.popup_hideLoad.bind(ui), 
					data:{isAjax:1}
				}).send();
			});
		});
	}
	
}

function setAjaxBehaviour_callBack(html,targetEl){
	targetEl.set('html',html);
	setAjaxBehaviour(targetEl);
}
