/**
 * there is one limit for this version methods will be uniq for all tabs of page
 */

RTAB=function(){ 
	  	
	  constructor=function(){
	  	
	  }
	  
	  return constructor;  
}
RTAB.selectTabClass='active';
RTAB.loadStatusText="<img style='margin: 100px auto; display: block;' src='http://media.intellipoker.com/images/global/ajaxload1.gif' border='0'>"
RTAB.loadStatusTextTargetID ='';
RTAB.ordinarTabClass='';
RTAB.targetId='IPContent';
RTAB.isDebugAllow=true;	     

RTAB.debug=function($data){
	  	if(RTAB.isDebugAllow) alert($data);
}

RTAB.elements=[];

RTAB.cache={};
RTAB.gets={};
RTAB.o={};
RTAB.mainTab='';
RTAB.s={};

RTAB.setContent=function(content,e){
	//document.getElementById(e.id).innerHTML = content;
	$("#"+e.id).html( content ); 
	
	$("#"+e.id).triggerHandler("contentChanged",[{id:e.id,method:RTAB.o[e.method].name}]);
    if (!RTAB.o[method]['denycache']){
	    RTAB.cache[e.method]= content;
    }
	RTAB.initContent(e.id);
}

RTAB.getMethod=function(method, fake){
		
	var i= method.indexOf( '#' ); 
	if(i<0 && !fake){
		RTAB.debug('Wrong url. Should begin from #');
		return false;
	}
    method=method.substring(i+1);

    var denycache = false;
    var i= method.indexOf( '!' );
    if(i>=0){
        method = method.substring(i+1);
        denycache = true;
    }


	var gets=method.split('?');
	
	
	if(gets.length>1){
		method=method.substring(0,method.indexOf('?'));
		var oname={name:""+method}
		gets=gets[1].split('&');
		var mg={};
		for(var i=0;i<gets.length;i++){
			var pair=gets[i].split('=');
			
			mg[ pair[0] ] = pair[1];
			method+=""+pair[0]+pair[1];
		}
		
		RTAB.gets[method]=mg;
		RTAB.o[method]=oname;
		
	}else{
		RTAB.o[method]={name:""+method}
	};
    RTAB.o[method]['denycache'] = denycache;
	
	return method;
}
    
RTAB.getTabContent=function(la){
				    /*if(la && la.currentTarget){
					// jQuery event	
						la=la.currentTarget;
					}*/
					
					if(la && la.target){
					// jQuery event	
						la=la.target;
						while(!$(la).is('a')){
							la=$(la).parent().get(0);
						}
					}

					if (la.getAttribute('fake')) {
						//document.location.href = la.target;
						return false;
					}					
					
				    // remove attribute from other tabs
					targetID=la.rel;
					for(var i=0;i<RTAB.s[targetID].elements.length;i++){
					    RTAB.s[targetID].elements[i].setAttribute('class', RTAB.ordinarTabClass);
					    RTAB.s[targetID].elements[i].setAttribute('className', RTAB.ordinarTabClass);
					}
					la.setAttribute('class', RTAB.selectTabClass);
					la.setAttribute('className', RTAB.selectTabClass);
					
					method=RTAB.getMethod(la.href, la.getAttribute('fake'));
					if (method == '') {
						if (RTAB.s[targetID]['flashIE']) {
							document.location.href = RTAB.s[targetID]['flashIE'];
							return;
						}

						//document.getElementById(targetID).innerHTML = RTAB.s[targetID].mainTab;
						$("#"+targetID).html( RTAB.s[targetID].mainTab );
						
						$("#"+targetID).triggerHandler("contentChanged",[{method:''}]);
						
						return false;
					}
					if(RTAB.cache[method]){
						
						//document.getElementById(targetID).innerHTML = RTAB.cache[method];
						$("#"+targetID).html( RTAB.cache[method] );
						RTAB.initContent(targetID);
						$("#"+targetID).triggerHandler("contentChanged",[{method:RTAB.o[method]['name']}]);
					}else{
						if(RTAB.gets[method]) {
							for (var name in RTAB.gets[method]) {
								setGet(name,RTAB.gets[method][name]);
							}
						}	
						getRbnsContent('show',RTAB.o[method]['name'],RTAB.setContent,0,{id:targetID, method:method});
						RTAB.showStatus(targetID);
						
					}
					
					
					return false;
				
}


RTAB.getContent=function(la){
    if(la && la.target){
        // jQuery event
        la=la.target;
    }

    targetID=la.rel;

	RTAB.getContentByLink(la.href);
}

RTAB.getContentByLink=function(href){
    method=RTAB.getMethod(href);

    if(method==''){
        if (RTAB.s[targetID]['flashIE']) {
            document.location.href = RTAB.s[targetID]['flashIE'];
            return;
        }
        document.getElementById(targetID).innerHTML = RTAB.s[targetID].mainTab;
        return false;
    }

    if(RTAB.gets[method]) {
        for (var name in RTAB.gets[method]) {
            setGet(name,RTAB.gets[method][name]);
        }
    }
    getRbnsContent('show',RTAB.o[method]['name'],RTAB.setContent,0,{id:targetID, method:method});

    RTAB.showStatus(targetID);



    return false;
}

RTAB.showStatus=function(targetID){
	if(RTAB.loadStatusTextTargetID){targetID=RTAB.loadStatusTextTargetID}
	if(RTAB.loadStatusText&&targetID){
			document.getElementById(targetID).innerHTML = RTAB.loadStatusText;
	}
}


RTAB.init=function($wrapId,targetID){
	RTAB.targetId=targetID;
	RTAB.s[targetID]={};
	RTAB.s[targetID]['elements']=[];
	$('#'+$wrapId+' li a').each(function(el){

		RTAB.s[targetID].elements.push(this);
		if(RTAB.getMethod(this.href, this.getAttribute('fake'))==''){
			
			RTAB.s[targetID]['mainTab'] =document.getElementById(targetID).innerHTML;
			if ($.browser.msie && $("#" + targetID + ' object').length) {
				
				RTAB.s[targetID]['flashIE']=document.location.href;
			}			
					
							
		}
	}).attr('rel',targetID);
	//$('#'+$wrapId+' li a').bind('click',this,RTAB.getTabContent);
	$('#'+$wrapId+' li a').bind('click',RTAB.getTabContent);
 
	
}
/**
 * Bind a with rev='this' to  RTAB.getTabContent
 * 
 * @param {string} id
 */
RTAB.initContent=function(id){
	$('#'+id+' a[rev="this"]').attr('rel',id).bind('click',this,RTAB.getContent);
}

