
// from the cnet library
// http://clientside.cnet.com/libraries/cnets-global-framework/

Element.extend({
/*	Property: visible
		Returns a boolean; true = visible, false = not visible.
		
		Example:
		>$(id).visible()
		> > true | false	*/
	visible: function() {
		return this.getStyle('display') != 'none';
	},
/*	Property: toggle
		Toggles the state of an element from hidden (display = none) to 
		visible (display = what it was previously or else display = block)
		
		Example:
		> $(id).toggle()
	*/
	toggle: function() {
		return this[this.visible() ? 'hide' : 'show']();
	},
/*	Property: hide
		Hides an element (display = none)
		
		Example:
		> $(id).hide()
		*/
	hide: function() {
		this.originalDisplay = this.getStyle('display'); 
		this.setStyle('display','none');
		return this;
	},
/*	Property: show
		Shows an element (display = what it was previously or else display = block)
		
		Example:
		>$(id).show() */
	show: function(display) {
		this.setStyle('display',(display || this.originalDisplay || 'block'));
		return this;
	}
});



// Bronwen - extend mootools
tabSwapper = Fx.Base.extend({

	getOptions: function(){
		return {
			startTab: 0,	
			tabClass: 'tabs',
			onActive: Class.empty,
			onBackground: Class.empty,
			cookieName:''
		};
	},

	initialize: function(container, links, blocks, options){
		this.setOptions(this.getOptions(), options);

		this.tabs = [] ;
		this.links = $$(links);
		this.blocks = $$(blocks);
		this.now = -1 ;

		// add bulleted list to container
		this.tabContainerList = new Element('ul').addClass(this.options.tabClass) ;
		this.tabContainerList.injectBefore( $(container).getChildren()[0] ) ;
		// getFirst() isn't working!
		// $(container).getFirst() returns false, so $x.getChildren() instead
		
		this.links.each(function(link, i){
			tab = new Element('li')
			new Element('span').appendText(link.innerHTML).injectInside(tab);
			tab.injectInside(this.tabContainerList);
			tab.addEvent('click', this.display.bind(this, i));
			this.tabs.push(tab);
			link.setStyle('display','none');
		}, this);

 		if(this.options.cookieName && this.getCookie()) 
 			this.display(this.getCookie().toInt());
 		else 
 			this.display(this.options.startTab);
	},

	saveCookie: function(index){
		Cookie.set(this.options.cookieName, index, 3);
	},
	getCookie: function(){
		return $pick(Cookie.get(this.options.cookieName), false);
	},


	display: function(tabNum){
		if (tabNum === this.now) return this;
		if (tabNum >= this.tabs.length) tabNum = 0;
		this.now = tabNum;
		this.blocks.each(function(el, i){
			if (i != tabNum){
				this.fireEvent('onBackground', [this.tabs[i], el]);
				el.setStyle('display', 'none');
			} else {
				this.fireEvent('onActive', [this.tabs[i], el]);
				el.setStyle('display', 'block');
			}
		}, this);
		if(this.options.cookieName) this.saveCookie(tabNum);
	}

});


/*	Class: window.popup
This class opens a popup window with the passed in values.
		
Arguments
	url - the destination for the popup
	options - an object containing key/value options
	
Options:
	width - (integer) the width of the window; defaults to 500
	height - (integer) the height of the window; defaults to 300
	x - (integer) the offest from the left of the screen; defaults to 50
	y - (integer) the offset from the top of the screen; defaults to 50
	toolbar - (integer) show the browser toolbar in the window; 
			0 (zero) does not show it, 1 (one) does; defaults to 0 (zero)
	location - (integer) show the location in the browser;
			0 does not show it; defautls to 0
	directories - (integer) show the directories in the browser;
			0 does not show it; defautls to 0
	status - (integer) show the status bar in teh browser;
			0 does not show it; defautls to 0
	scrollbars - (string) 'auto' shows the scroll bars if they are required,
			'no' shows none, 'yes' shows them all the time
	resizeable - (integer) lets the user resize the window;
			1 allows resizeing; defaults to 1
	name - (string) the name of the popup; defaults to "popup"
	
	Examples:
	(start code)
var myPopup = new window.popup('http://www.example.com'); //opens with default parameters

var myPopup = new window.popup('http://www.example.com', {
	width: 300,
	height: 800,
	x: 500,
	toolbar: 1
}); //launch a window with custom properties
	(end)

	Property: window
	The window object itself (the popup). The class window.popup opens a new browser window. The pointer to this
	window can be reached like so:
	(start code)
	var myPopup = new window.popup('http://www.example.com');
	myPopup.window // this is the reference to the popup itself.
	(end)
	
	Note that if you call this class with the same name (the default name is 'popup') as an already open window
	you won't open a new popup window, but instead will send your url to the existing window. You should probably
	give it something unique so you can have more than one if you need. 

	Example:
	(start code)
	var myPopup = new window.popup('http://www.example.com'); //default name for the popup is "popup"
	var anotherPopup = new window.popup('http://www.example2.com'); //you just refreshed the "popup" window with this new url
	(end)
	
	This actually represents a way to keep refering to the same window that's already open. So long as the window
	calling it is the same window that opened the popup to begin with (even if the user goes to another page), the
	above code will always re-acquire the already open popup.
	
	Example:
	(start code)
	//page loads
	var myPopup = new window.popup('http://www.example.com'); //default name for the popup is "popup"
	
	//user goes to another page, and, when that page loads, this happens again
	var myPopup = new window.popup('http://www.example.com'); //default name for the popup is "popup"
	(end)
	
	The result is you just refreshed the already open window with the same url. There are ways to do this
	without refreshing, but not with this class (yet).
	*/
window.popup = new Class({
	setOptions: function(options) {
		this.options = Object.extend({
			width: 500,
			height: 300,
			x: 50,
			y: 50,
			toolbar: 0,
			location: 0,
			directories: 0,
			status: 0,
			scrollbars: 'auto',
			resizeable: 1,
			name: 'popup'
		}, options || {});
	},
	initialize: function(url, options){
		this.url = url || false;
		this.setOptions(options);
		if(this.url) this.openWin();
		return this;
	},
	openWin: function(url){
		url = url || this.url;
		this.window = window.open(url,
			this.options.name,
			'toolbar='+this.options.toolbar+
			',location='+this.options.location+
			',directories='+this.options.directories+
			',status='+this.options.status+
			',scrollbars='+this.options.scrollbars+
			',resizable='+this.options.resizeable+
			',width='+this.options.width+
			',height='+this.options.height+
			',top='+this.options.y+
			',left='+this.options.x);
		this.focus();
		return this.window;
	},
/*	Property: focus
		Focus the window related to the window.popup object.
		
		Example:
		(start code)
var myPopup = new window.popup('http://www.example.com'); //opens with default parameters
myPopup.focus(); //bring it to the front
		(end)
		
		Note:
		When you create a new popup it calls .focus() on itself immediately by default.
	*/
	focus: function(){
		this.window.focus();
		return this;
	},
/*	Property: close
		Closes the popup window related to the window.popup object.

		Example:
		(start code)
var myPopup = new window.popup('http://www.example.com'); //opens with default parameters
myPopup.close(); //close the window
		(end)

	*/
	close: function(){
		this.window.close();
	}
});



//--- Image swapping class
var imageSwap = Fx.Base.extend({

	getOptions: function(){
		return {
			action: "click" ,
			linkOnClass: "linkOn",
			linkOffClass: "linkOff"
		};
	},

	initialize: function(blocks, links, options){
		this.setOptions(this.getOptions(), options);
		this.currentBlock = -1 ;
		this.blocks = $$(blocks);
		this.links = $$(links);

		// entirely possible for blocks and activating links not to match up!
		if (this.blocks.length != this.links.length) return ;

		this.links.each(function(link, i){
			link.addEvent(this.options.action, this.display.bind(this, i));
		}, this);

		this.effects = new Fx.Elements(this.blocks, {duration: 2000});
		this.blocks.each(function(el){
			el.setStyle('opacity',0);
		});
		this.display(0);
	},

	display: function(iToShow){
		action = {} ;
		this.blocks.each(function(el, idx){
			if (idx == iToShow){	// show
				$(this.links[idx]).removeClass(this.options.linkOffClass).addClass(this.options.linkOnClass);
				action[idx.toString()] = {'opacity': [1]};
			} else {
				$(this.links[idx]).removeClass(this.options.linkOnClass).addClass(this.options.linkOffClass);
				action[idx.toString()] = {'opacity': [0]};
			}
		}, this);
		this.currentBlock = iToShow;
		this.effects.start(action);
	}

});



