/*#################################################

Tabs | v1.3 
Max Felker | max@bigroomstudios.com

Creates tabbed content

#################################################*/
 
// START Class
var Tabs = Class.create({

	initialize: function(config) {
	
		// containers 
		this.container = $(config.container);
		
		if(config.deep_linking) {
			this.deep_linking = true;
			this.hash_safe_id = this.container.id.gsub('-','_');
		}
		
		// tabs
		this.tab_elements = $$('#'+config.container+' .tab');
		this.tabs_count = this.tab_elements.length;
		this.tabs_current_index = 0;
		this.tabs = $A();
		
		this.counter = 0;
		
		this.tab_elements.each(function(tab) {

			var tab_id = tab.id.gsub('-tab','');
			tab.flash_id = 'flash-container-' + tab_id.gsub('portfolio_viewer_','');
			tab.youtube_id = 'youtube_' + tab_id.gsub('portfolio_viewer_','');
			tab.panel = $(tab_id+'-panel');
			tab.panel.hide();
			tab.index = this.counter;
			this.tabs[tab.index] = tab;
			this.counter++;
		
		}.bind(this));
	
		this.counter = 0;
		
		this.tabs_current_index = 0;
		
		if(this.deep_linking) {
			current_hash = _.get_hash('tab_'+this.hash_safe_id);
			if(current_hash) {
				this.tabs_current_index = current_hash;
			}
		}
		
		this.show_tab();
		
		this.tabs.each(function(tab) { 
		
			tab.observe('click', function() {
				portfolio_tab.pause_flash_player();
				this.tabs_current_index = tab.index;
				this.show_tab();
			
			}.bind(this));
		
		}.bind(this));
		
		
	
		if(this.deep_linking) {
			
			SWFAddress.setHistory(0);
			if(config.history) {
				SWFAddress.setHistory(1);
			}
				
			Event.observe(SWFAddress,'change', function() {
			
				var current = _.get_hash('tab_'+this.hash_safe_id);
				
				if(current!=this.tabs_current_index) {
					if(!current) {
						current = 0;	
					}
					this.tabs_current_index = current;
					this.show_tab();
				}
				
			}.bind(this));
		
		} // END if deep linking

		var portfolio_tab = this;
	
	}, // END init

	play_flash_player: function() {
		//get the flash player in this tab if there is one
		var flash = $(this.current_tab.flash_id);
		if(flash) {
			flash.play_content();
		}
		
		//get the youtube player in this tab
		var youtube = $(this.current_tab.youtube_id);
		if(youtube) {
			youtube.playVideo();
		}
	},
	
	pause_flash_player: function() {
		//get the flash player in this tab if there is one
		var flash = $(this.current_tab.flash_id);
		if(flash) {
			flash.pause_content();
		}

		//get the youtube player in this tab
		var youtube = $(this.current_tab.youtube_id);
		if(youtube) {
			youtube.pauseVideo();
		}
	},
	
	show_tab: function() {
	
		this.tabs.each(function(tab) {
		
			tab.panel.hide();
			tab.removeClassName('current');
		
		});
		
		this.current_tab = this.tabs[this.tabs_current_index];
		
		this.current_tab.panel.show();
		this.current_tab.addClassName('current');

		if(this.deep_linking) {
			_.set_hash('tab_'+this.hash_safe_id,this.tabs_current_index);
		}
	
	},
	
	next_tab: function() {
		this.pause_flash_player();
		this.tabs_current_index = (this.tabs_current_index+1 + this.tabs_count) % this.tabs_count;
		
		this.show_tab();
	
	},
	
	previous_tab: function() {
		this.pause_flash_player();
		this.tabs_current_index = (this.tabs_current_index-1 + this.tabs_count) % this.tabs_count;
		
		this.show_tab();
	
	}
});
// END CLass

