var classRef;

jQuery.fn.tab = function(options) {
  
	settings = jQuery.extend({
		active: null,
		current_hash: document.location.hash.toString()

	}, options);
	
	classRef = this;
	
	settings.current_hash = document.location.hash.toString().toLowerCase();
	settings.current_hash = settings.current_hash.replace("#","");
	
	if(settings.current_hash != ""){
		settings.active = settings.current_hash;
	}
	
	var active_obj = jQuery('#tab_'+settings.active).attr("rel");
	active_obj = eval('('+active_obj+')');
	
	var active_id = active_obj.id;
	
	if(!active_id){
		var active_obj = jQuery('#tab_nav > ul > li > a:first').attr("rel");
		active_obj = eval('('+active_obj+')');
		active_id = active_obj.id;
		settings.active = active_id;
	}
	
	// Hide All Content Divs
	
	jQuery('#tab_content > div').each(function(i){
		jQuery(this).css("display","none");
	});
	
	// Setup click handlers
	
	jQuery('.tab').each(function(i){
		$(this).bind('click',function(e){
			var obj = $(this).attr("rel");
			obj = eval('('+obj+')');
			classRef.updateContent(obj);
		});
	});

	// reset
	
	this.reset = function(){
		this.updateContent(active_obj,false);
		this.checkLocation();
	}
	
	this.updateContent = function(obj, fromHash){
		
		if(obj == '') return;
		
		// Get the id from the hash
		
		if(fromHash && obj != ''){
			new_obj = $('#tab_nav').find("#tab_"+obj).attr("rel");
			obj = eval('('+new_obj+')');
		}
		
		jQuery('#tab_'+active_obj.id).removeClass("active"); // tab
		jQuery('#pane_'+active_obj.id).css("display","none"); // pane
		jQuery('#'+active_obj.rhu).css("display","none"); // rhu
		
		$('.indicator').remove(); // indicator
		
		jQuery('#tab_'+obj.id).attr("className","active"); // tab
		jQuery('#tab_'+obj.id).before('<span class="indicator"></span>'); // indicator
		
		switch(obj.id){
	
			case "about":
				jQuery('#pane_'+obj.id).css("display","block"); // pane
				jQuery('#'+obj.rhu).css("display","block"); // rhu
			break;

                        case "news":
				jQuery('#pane_'+obj.id).css("display","block"); // pane
				jQuery('#'+obj.rhu).css("display","block"); // rhu
			break;

                        case "press":
				jQuery('#pane_'+obj.id).css("display","block"); // pane
				jQuery('#'+obj.rhu).css("display","block"); // rhu
			break;

                        case "contacts":
				jQuery('#pane_'+obj.id).css("display","block"); // pane
				jQuery('#'+obj.rhu).css("display","block"); // rhu
			break;

//			case "own":
//				jQuery('#pane_'+obj.id).css("display","block"); // pane
//				jQuery('#'+obj.rhu).css("display","block"); // rhu
//				var so = new SWFObject(CI.flash_path + "rhu/franchise_rhu_v1.swf" , "rhu_viewer", "290", "355", "8.0.0", "#FFFFFF");
//				so.addVariable("xml_path", CI.base_url + "xml/get_franchise_rhu");
//				so.addParam("wmode", "transparent");
//				so.write(obj.rhu);
//			break;
			
			case "local":
				jQuery('#pane_'+obj.id).css("display","block"); // pane
				jQuery('#'+obj.rhu).css("display","block"); // rhu
			break;
			
		}
		
		active_obj = obj;
		
	}
	
	this.checkLocation = function(){
		
		var new_hash = document.location.hash.toString();
		new_hash = new_hash.replace("#","");
		
		if(settings.current_hash != new_hash){
			settings.current_hash = new_hash;
			classRef.updateContent(settings.current_hash,true);
		}
		
		window.setTimeout(classRef.checkLocation, 300);
		
	}
	
	this.reset()
	
}

