// JavaScript Core Document

function external(){
	var l = $$('a');
	l.each(function(el){
		if (el.getProperty('rel')=='external'){
			el.setProperty('target', '_blank');
		}
	});
}

function forms(){
	var s = $$('form #sort');
	s.each(function(el){
		el.addEvent('change', function(e){
			if (el.value != 'Please select'){
				el.getParent().getParent().submit();
			}
		});
	});
}
function hide_notice(){
	if ($('hide_notice')){
		$('hide_notice').addEvent('click', function(e){
			var myFx = new Fx.Tween($('notice_holder'), {duration: 2000});
			myFx.start('opacity', '1', '0');
			var e= new Event(e);
			e.stop();
		});
	}
}
function sliders(){
	var sliders = $$('.bf_line');
	sliders.each(function(el){
		var steps = el.getProperty('rel');
		var target = el.getElement('input');
		var slider = new Slider(el, el.getElement('.bf_knob'), {
			steps: steps,
			wheel: true,
			range: [1],	// Minimum value is 8
			onChange: function(step){
				// Everytime the value changes, we change the font of an element
				el.getElement('.bf_tip').removeClass('hide');
				target.value = step;
				if ($(el.id + '_' + step) && !isNaN(step)){
					el.getElement('.bf_tip').set('html', $(el.id + '_' + step).innerHTML);
					// alert ($(el.id + '_' + step).innerHTML);
				}
			}
		});
		el.getElement('.bf_tip').addClass('hide');
		slider.set(1);
	});
	/*
	var tipz = new Tips('.bf_knob',{
		className: 'tipz',
		fixed: true,
		hideDelay: 250,
		showDelay: 50
	});
	*/
}

function bouncescroll(){
	var l;
	var bouncescrollers = $('tweets-here');
	var first = bouncescrollers.getFirst();
	var fx1 = new Fx.Tween(bouncescrollers,{transition: Fx.Transitions.Quad.easeOut, duration: 500});
	bouncescrollers.setStyle('margin-left','722px');	
	var pop = first.dispose();
	pop.inject(bouncescrollers);
	fx1.start('margin-left', '0px');
	/*
	first = bouncescrollers.getFirst().get('html');
	l = first.length;
	*/
}

function tweet(){
	if ($('tweets-here')){
		var req = new Request.HTML({url:'/feeds.php',
			onSuccess: function(html) {
				//Clear the text currently inside the results div.
				//Inject the new DOM elements into the results div.
				$('tweets-here').adopt(html);
				$('tweets-here').removeClass('loading');
				if (html != 'Oops, the Tweet feeder is empty!'){
					bouncer = bouncescroll.periodical(6000);
					var bouncer;
				}
			}
		}).send();
	}
}

function flash(){
	var flash = $$('.flash');
	flash.each(function(el){
		var fl = new Swiff(el.getProperty('rel'), {width: el.getStyle('width'), height: el.getStyle('height'), container: el});
	});
}

function extras(){
	if ($('wordleMap')){
		wordleMap();
	}
}
function wordleMap(){
	var alerts = $$('#wordleMap area');
	alerts.each(function(el){
		el.set('title', el.get('alt'));
		if (el.getProperty('rel') != null){
			el.addEvent('click', function(e){
				alert (el.getProperty('rel'));
				var e = new Event(e);
				e.stop();
			});
		};
	 });
}

window.addEvent('domready', function(){
	external();
	forms();
	tweet();
	flash();
	// sliders();
	hide_notice();
	extras();
/*
var csize = $('container_bottom').getSize();
var cheight = csize.y;
var remainder = cheight % 33;
var paddingtop = $('container_bottom').getStyle('padding-top').toInt();
var paddingbottom = $('container_bottom').getStyle('padding-bottom').toInt();
var newheight = cheight+remainder-paddingtop-paddingbottom;
$('container_bottom').setStyle('height',newheight);
//console.log(remainder);
//alert("oh: "+cheight+"\nrem: "+remainder+"\npt: "+paddingtop+"\npb: "+paddingbottom+"\nh: "+newheight);
*/


});