// JavaScript Document
function tabControl(tabSelector, tabContentSelector, requestSendAnimation, responseReceivedAnimation){
	this.responseReceivedAnimation=responseReceivedAnimation;
	this.requestSendAnimation=requestSendAnimation;
	this.contentInstance=jQuery(tabContentSelector);
	
	if (typeof(requestSendAnimation) == 'function')
		jQuery(tabSelector).bind('onResponseReceived', responseReceivedAnimation);
	if (typeof(responseReceivedAnimation) == 'function')
		jQuery(tabSelector).bind('onRequestSend',requestSendAnimation);
	
	//this.responseReceivedAnimation;
//	this.requestSendAnimation;

	this.requestUpdatedContent=function(e){
		id=e.data;
		var ev=jQuery.Event('onRequestSend');
		clickedLink=e.currentTarget;
		ev.clickedLink=e.currentTarget;
		ev.tabControlInstance=this.tabControlInstance;
		ev.activeId=id;
		try{
			jQuery(tabSelector).trigger(ev);
		}
		catch(er)
		{}
		
		jQuery.post('index-ajax.php',{q:'assets/snippets/tabControl/ajax.php',activeId:id},updateTabContent);
		return false;
		
		
	}
	var o=this;
	jQuery(tabSelector+' a').each(
		function(){
			var href=String(jQuery(this).attr('href'));
			var idPattern=/activeId=(\d*)/;
			var id=href.match(idPattern)[1];
			jQuery(this).bind('click',id,requestUpdatedContent);
			this.tabControlInstance=o;
		}
	);
	


	function updateTabContent(content){
		var ev=jQuery.Event('onResponseReceived');
		ev.clickedLink=clickedLink;
		ev.tabControlInstance=o;
		try{
			jQuery(tabSelector).trigger(ev);
		}
		catch(er)
		{}
		jQuery(tabContentSelector).html(content);
	}

	return this;
}
