/*
Welcome to the WAVE javascript file. Please enjoy your time here.
WAVE uses the Mootools library (mootools.net) to enhance your experience.
In general, our approach has been to hijax already functional elements to make them better with
scripting. Everything SHOULD work with scripting disabled.
Authored by Jared Smith (WebAIM)
*/

window.addEvent('domready', function(){

$$('#js_url, #js_file, #js_code, #js_hurl, #js_hfile').setProperty('value', '2');

//store titles and text for tooltips
$$('.wave4tip').each(function(element,index) {
	img = "<img src='"+element.get('src')+"'/> ";
	
	element.store('tip:title', img+element.get('title'));
	element.store('tip:text',  element.get('description'));
});

// Initiate tips
var myTips = new Tips('.wave4tip', {className:'wave4tooltip'});


// Toggle file upload/enter web page
var uriform = $('wave4uriform');
var uploadform = $('wave4uploadform');
var fx = new Fx.Morph('wave4uriform', {duration: 500, transition: Fx.Transitions.Sine.easeOut});
var fx2 = new Fx.Morph('wave4uploadform', {duration: 500, transition: Fx.Transitions.Sine.easeOut});

$('wave4toggleupload').addEvent('click', function(e){
	e.stop();
	//e = new Event(e).stop();
		uploadform.setStyles({
			display:'block'
		});
		fx.start({top:-100}).chain(function(){
			fx2.start({top:0});
			$('wave4file').focus();
			uriform.setStyles({
				display:'none'
			});
		});
});

$('wave4toggleuri').addEvent('click', function(e){
		e.stop();
		//e = new Event(e).stop();
		uriform.setStyles({
			display:'block'
		});
		
		fx2.start({top:-100}).chain(function(){
			fx.start({top:0});
			$('wave4uri').focus();
			uploadform.setStyles({
				display:'none'
			});
		});
});


// Set the URL default value. On focus, clear value. On blur, set value again if unchanged.
var uriboxvalue = $('wave4uri').title+'...';
if($('wave4uri').value == "" || $('wave4uri').value == uriboxvalue) {
	$('wave4uri').value=uriboxvalue;
	$('wave4uri').setProperty('class','wave4default');
}

$('wave4uri').addEvent('focus', function(e){
	if($('wave4uri').value==uriboxvalue) {
		$('wave4uri').value="";
		$('wave4uri').setProperty('class','');
	}
});
$('wave4uri').addEvent('blur', function(e){
	if($('wave4uri').value=="") {
		$('wave4uri').value=uriboxvalue;
		$('wave4uri').setProperty('class','wave4default');
	}
});

$('wave4uributton').addEvent('click', function(e2){
	if($('wave4uri').value==uriboxvalue) {
		$('wave4uri').value="";
		$('wave4uri').setProperty('class','');
	}
});



// Error focusing and highlighting for feedback form
var fadefx = new Fx.Tween('wave4fade', {'duration': 2000});
if ($('wave4fade')) {
	fadefx.start('background-color', '#ffff33', '#fff');
	$('wave4error').setProperty('tabindex','-1');
	$('wave4error').focus();
	$('wave4fade').getElements('a').addEvent('click', function(evt) {
		// disable the link href
		(new Event(evt)).stop();
		// get the anchor it links to, and set focus to the relevant element
		thisanchor = this.getProperty('href');
		thisanchor = thisanchor.substring(thisanchor.indexOf('#')+1,thisanchor.length);
		if(thisanchor) $(thisanchor).focus();
	});
}


// Toggle styles in reports
if ($('wave4disablestyles')) {
	$('wave4disablestyles').addEvent('click', function(evt) {
		// disable the link href
		(new Event(evt)).stop();
		
		// disable external styles
		$$('.wave4externalstyle').each(function(item){
			item.setProperty('disabled','true');
		});
		// disable internal styles
		var a = $('wave4output').getElements('*').each(function(el){
			if (el.getProperty('style') && !el.getProperty('class').contains("wave4")) {
				el.setAttribute("wave4style", el.getProperty("style"));
				el.setAttribute("style", "");
			}
		});
		$('wave4reportarea').setProperty('tabindex','-1');
		$('wave4reportarea').focus();
		$('wave4output').setProperty('class','wave4linearize');
		$('wave4disablestyles').className = 'wave4hidden';
		$('wave4enablestyles').className  = '';
		
		
		// Toggle the display of the output to force a re-render in IE, otherwise some stupid bug causes
		// random page elements to disappear. Only do this in IE, cuz it suxors the mostest.
		if(Browser.Engine.trident)
		{
			$('wave4output').style.display = 'none';
			$('wave4output').style.visibility = 'hidden';
			var toggleDisplay = function()
			{
				$('wave4output').style.display = 'block';
				$('wave4output').style.visibility = 'visible';
			}
			toggleDisplay.delay(10);
		}
	});
}
if ($('wave4enablestyles')) {
	$('wave4enablestyles').addEvent('click', function(evt) {
		// disable the link href
		(new Event(evt)).stop();
		
		// re-enable external styles
		$$('.wave4externalstyle').each(function(item){
			item.setProperty('disabled','');
		});
		
		// re-enable internal styles
		var a = $('wave4output').getElements('*').each(function(el){
				if (el.getProperty('wave4style')) {
					el.setAttribute("style", el.getAttribute("wave4style"));
					el.setAttribute("wave4style", "");
				}
		});
		$('wave4reportarea').setProperty('tabindex','-1');
		$('wave4reportarea').focus();
		$('wave4output').setProperty('class','');
		$('wave4enablestyles').className   = 'wave4hidden';
		$('wave4disablestyles').className  = '';
		
		
		// Toggle the display of the output to force a re-render in IE, otherwise some stupid bug causes
		// random page elements to disappear. Only do this in IE, cuz it suxors the mostest.
		if(Browser.Engine.trident)
		{
			$('wave4output').style.display = 'none';
			$('wave4output').style.visibility = 'hidden';
			
			var toggleDisplay = function()
			{
				$('wave4output').style.display = 'block';
				$('wave4output').style.visibility = 'visible';
			};
			
			toggleDisplay.delay(10);
		}
	});
}

// Remove title attributes so IE won't display them. Silly IE!
$$('.wave4tip').each(function(element,index) {
	element.setAttribute('title','');
	// element.setAttribute('tabIndex','1');
});

// Set the tab-index for the focuser to -1. This is a silly hack to trigger hasLayout in IE (which I
// loath beyond all description).
$('wave4focuser').setProperty('tabindex','-1');

// If you're still reading this, you really should turn off your computer and go outside for a change.
});
