// only show ".filter" elements with the given search term inside them

function filter(search_term) {
	els = $$('.filter');
	for(i=0; i<els.length; i++)
	{
		m = els[i].innerHTML.match(search_term);
		if(m == null)
			els[i].hide();	
		else
			els[i].show();
	}
}

/* Switches visibility based upon state paramater */
function switchVisibility(idOrClassName, state)
{
	// id
	
	// class
	if(state===true || state==1)
	{
		if($(idOrClassName) != null) $(idOrClassName).show();
		$$('.'+idOrClassName).each(function(el){el.show()});
	}
	else
	{
		if($(idOrClassName) != null) $(idOrClassName).hide();
		$$('.'+idOrClassName).each(function(el){el.hide()});
	}
}

/* Rotating LI Elements */
function rotateLI()
{
	wrappers = $$('ul.rotating')
	// each rotation container
	wrappers.each(function(ul)
	{
		var els = ul.childElements();
		// each li...
		ul.childElements().each(function(li,i)
		{
			// am i visible?
			if(li.visible())
			{
				// who is after me?
				next_el_i = i + 1;
				if(els[next_el_i] != undefined)
					next_el = els[next_el_i];
				else
					next_el = els[0];

				// hide me
				new Effect.Fade(li.identify());
			}
		});

		// show next
		new Effect.Appear(next_el.identify());
		
	});
	
	if(wrappers.length > 0) setTimeout("rotateLI()", 5000);
}

// make ".cta-hover" elements visible if they are inside a ".hoverable"
function initHoverable()
{
	$$('.hoverable').each(function(el, i){
		Event.observe(el.identify(), "mouseover", function(){
			el.addClassName('hover')
		});
		Event.observe(el.identify(), "mouseout", function(){
			el.removeClassName('hover')
		});
	});
}
	

/* Popup */
function popup(content,opts)
{
	var dimensions = document.viewport.getDimensions();
	
	if(opts == undefined)
		opts = {};
		
	// remove any open elements if a form is already open
	if($('pop_overlay') != undefined)
	{
		destroyPopup();	
	}
	
	// SOURCE...
	if(content===false)
		var pop_content = '';
	
	//
	else
		var pop_content = $(content).innerHTML;
	
		
		
	
	/* Create Elements */
	$$('body')[0].insert({
		'top': new Element('div',{'id':'pop_overlay'}).setOpacity(0).setStyle({
			height:(dimensions.height+500)+'px'
		})
	});
	
	$('pop_overlay').insert({
		'before': new Element('div', { 'id': 'pop_container'}).insert({
			'top': new Element('div',{'id':'pop_box'}).insert({
				'top': new Element('div', {'id':'pop_box_frame'}).insert({
					'top': pop_content
				})
			})
		}).setOpacity(0)
	});
	
	new Effect.Opacity('pop_overlay', {from:0, to: overlay_opacity, duration:0.5});
	new Effect.Opacity('pop_container', {from:0, to: 1, duration:0.6});
	new Effect.Morph('pop_container', {from:0, to: 1, duration:0.6});
	
	/* Title and Description */
	if(opts.description)
		$('pop_box').insert({'top': new Element('p').insert({'top':opts.description}) });
	
	if(opts.title)
		$('pop_box').insert({'top': new Element('h2').insert({'top':opts.title}) });
	
	Event.observe($('pop_overlay'), "click", function(){
		destroyPopup();
	});
}
function destroyPopup()
{
	$('pop_overlay').remove(); 
	$('pop_container').remove();		
}


// when called, binds elements to given position
// prepare sticky elements


function positionStickyElements(e)
{
	$$('.sticky').each(function(el){
		offset = el.viewportOffset();
		doffset = document.viewport.getScrollOffsets();
		
		if(el.hasClassName('sticky-top')){
			
			if(el.retrieve('default_offset_top') < doffset.top )
				el.setStyle({position:'fixed', top:'0px'});
			else
				el.setStyle({position:el.retrieve('default_position')});
			
		}
		
	});
	
}
/**
* Returns the value of the selected radio button in the radio group, null if
* none are selected, and false if the button group doesn't exist
*	RE: http://xavisys.com/using-prototype-javascript-to-get-the-value-of-a-radio-group/

* @param {radio Object} or {radio id} el
* OR
* @param {form Object} or {form id} el
* @param {radio group name} radioGroup
*/

function $RF(groupName) {
	
	var output = null;
	$$('input[type="radio"][name="'+groupName+'"]').each(function(el){
		
		if(el.checked == true)
			output = $F(el);

	});
	return output;
}


Event.observe(window,"load", function(){
	initHoverable();	
	setTimeout("rotateLI()", 5000);
	
	// stick top images listener
	$$('.sticky').each(function(el){
		el.store('default_position', el.getAttribute('position')||'static');
		el.store('default_offset_top', el.viewportOffset().top );
	});
	
	Event.observe(window, 'scroll', positionStickyElements.bindAsEventListener(this), false);
	
	if (Prototype.Browser.IE) {
		Event.observe(document, 'mousewheel', positionStickyElements.bindAsEventListener(this), false);
	} else {
		Event.observe(window, 'DOMMouseScroll', positionStickyElements.bindAsEventListener(this), false);
	}	
});



