/**
 * Coop Program JS utilities
 *
 * @author Tim Neill
 */

// Redraw function by Thomas Fuchs
Element.addMethods({
	redraw: function(element){
		element = $(element);

		var n = document.createTextNode(' ');
		element.appendChild(n);

		(function(){ n.parentNode.removeChild(n) }).defer();

		return element;
	}
});

// Applies specific CSS fixes
function insertFix(browserName, osName, cssPath) {
	browserName = (browserName || '');
	osName 		= (osName || '');
	cssPath		= (cssPath || '');
	
	browserName = browserName.toLowerCase();
	osName 		= osName.toLowerCase();
	
	var UA = navigator.userAgent.toLowerCase();
	
	var isBrowser	= UA.indexOf(browserName) > -1;
	var isOS		= UA.indexOf(osName) > -1;
	
	if (isBrowser && isOS) {
		var cssSrc = cssPath + browserName + '-' + osName + '.css';
		
		var linkNode = document.createElement('link');
		
		linkNode.charset = 'utf-8';
		linkNode.media	 = 'screen';
		linkNode.type 	 = 'text/css';
		linkNode.href 	 = cssSrc;
		linkNode.rel 	 = 'stylesheet';
		
		var headTag = document.getElementsByTagName('head')[0];
		
		if (headTag) {
			headTag.appendChild(linkNode);
		}
	}
}

/**
 * Prototype-specific JS
 *
 * @author Tim Neill
 */

// Controls tab windows
var coTabset = Class.create({
	initialize: function(navElement, tabContainer)
	{
		if (!$(navElement) || !$(tabContainer)) {
			return;
		}
		
		this.nav  = navElement.select('li a');
		this.tabs = tabContainer.select('div.tab');
		
		this.currentTab = 1;
		
		for(var i = 0; i < this.nav.length; ++i) {
			Event.observe(this.nav[i], 'click', this.changeTab.bind(this, i));
		}
		
		this.changeTab();
	},
	
	changeTab: function(index)
	{
		if (!index && index !== 0) {
			index = 0;
		}
		
		// Add the on state to the list item
		$A(this.nav).each(function(elm) {
			$(elm.parentNode).removeClassName('on');
		});
		
		$(this.nav[index].parentNode).addClassName('on');
		
		// Show the relevant tab
		$A(this.tabs).invoke('hide');
		$(this.tabs[index]).show();
	}
});

var coTabProfiles = Class.create({
	initialize: function(navElement, profileWrapper)
	{
		if (!$(profileWrapper) || !$(navElement)) {
			return;
		}
		
		// Get profile wrappers
		this.profiles = $(profileWrapper).select('div.tabBody');
		
		// Assign the nav elements
		this.nav = $(navElement);
		
		Event.observe($(this.nav).down('span.left'),  'click', this.navigate.bind(this, -1));
		Event.observe($(this.nav).down('span.right'), 'click', this.navigate.bind(this,  1));
		
		if (this.profiles[0]) {
			// Set the default profile
			this.currentProfile = 0;
			
			// Fake a nav switch call
			$(this.nav).down('span.left').hide();
			
			// Show the default profile
			this.switchProfile(0);
		}
	},
	
	switchProfile: function(index)
	{
		if (index > (this.profiles.length - 1) || index < 0) {
			return;
		}
		
		if (index != this.currentProfile) {
			$A(this.profiles).each(function(elm) {
				elm.hide();
			});
			
			$(this.profiles[index]).show();
			
			if (index === 0) {
				$(this.nav).down('span.left').hide();
			} else {
				$(this.nav).down('span.left').show();
			}
			
			if (index == (this.profiles.length - 1)) {
				$(this.nav).down('span.right').hide();
			} else {
				$(this.nav).down('span.right').show();
			}
			
			this.currentProfile = index;
		}
	},
	
	navigate: function(direction) // -1, 1
	{
		// Don't recover from a function call error, just fail
		if (!direction || Math.abs(parseInt(direction)) > 1) {
			return;
		}
		
		this.switchProfile(this.currentProfile + direction);
	}
});