/* access.see.be - WAI-ARIA based Accessibility-Library for Prototype, www.access-see-be.net, Version 0.8 beta
 * Copyright (C) 2009  diamonddogs web consulting gesmbh, www.diamonddogs.cc
 *  
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *  
 * Component: aria.js
 * Loading mechanism based on script.aculo.us <http://script.aculo.us/>
 *
 *--------------------------------------------------------------------------*/

if (typeof Prototype == 'undefined') {
	throw("access.see.be requires the JavaScript-Framework Prototype");
}

var Aria = {
	VERSION:'0.8 beta',
	DELIMITER:'asb',
	CLASSNAME:'access-see-be',
	NS_STATE:'http://www.w3.org/2005/07/aaa',
	NS_MAPPING:'aaa:',
	WIDGETS:$H(),
  
	/*
	 * Method to initialize access.see.be. Checks for Prototype and embeds the necessary files
	 */
	initialize: function() {
		if(	(typeof Prototype=='undefined') || 
			(typeof Element == 'undefined') || 
			(typeof Element.Methods=='undefined') ||
			parseFloat(Prototype.Version.split(".").first() + "." + Prototype.Version.split(".")[1]) < 1.6) {
				throw("access.see.be requires the JavaScript-Framework Prototype >= 1.6.0");
		}
			
		$A($$("script")).findAll( function(s) {
			return (s.src && s.src.match(/aria\.js(\?.*)?$/))
		}).each( function(s) {
			ariaPath = s.src.substring(0,s.src.indexOf('aria.js'));		
			ariaImagePath = ariaPath + '../images/aria/';
			
			var includes = s.src.match(/\?.*load=([a-z,]*)/);			
			(includes ? includes[1] : 'ariaLibraryStyles,ariaMyStyles').split(',').each(function(include) {
				Aria.require(ariaPath + '../stylesheets/' + include + '.css', 'style');
			});
			
			if (Aria.supports.minimized) {
				(includes ? includes[1] : 'ariaMyFunctions,ariaMinimized').split(',').each(function(include) { 
					Aria.require(ariaPath + include + '.js', 'script');
				});
			} else {
				(includes ? includes[1] : 'ariaCookies,ariaFlashObjects,ariaWidgets,ariaNavigationWidgets,ariaFormWidgets,ariaMyFunctions,ariaSetup').split(',').each(function(include) { 
					Aria.require(ariaPath + include + '.js', 'script');
				});		
			}
		});
	},
	
  	/*
	 * Checks if the Browser-Requirements for access.see.be are achieved.
	 * Version 0.8 beta supports Firefox 2-3 and Internet Explorer 8 Beta 1-2
	 */
  	supported: function() {
		var supports = this.supports = new Object();		
			supports.ff = new Object();
				supports.ff[2] = navigator.userAgent.include('Firefox/2');
				supports.ff[3] = navigator.userAgent.include('Firefox/3');
			supports.ie = ( navigator.appName == 'Microsoft Internet Explorer' && navigator.appVersion.indexOf('8.0') > 0 ) ? true : false;
			supports.opera = ( navigator.appName == 'Opera' && navigator.appVersion.indexOf('9.5') == 0 ) ? true : false;
			//supports.safari = ( navigator.appName == 'Netscape' && navigator.appVersion.indexOf('5.0') == 0 ) ? true : false;
									
			supports.viewInAllBrowsers = false;
			supports.minimized = false;
						
			var cssStyleTester = new Element('div');
				// Necessary because of IE-Bug
				cssStyleTester.update('&nbsp;<style>head {color:red;}</style>');
				$$('head').first().insert({top: cssStyleTester.innerHTML})
				
			supports.css = $$('head').first().getStyle('color').include('rgb(255, 0, 0)') || $$('head').first().getStyle('color').include('red') || $$('head').first().getStyle('color').include('#ff000');
			$$('style').first().remove()
		
		supports.flash = false;
		for (n = 0; n < navigator.plugins.length; n ++) { 
			if (navigator.plugins[n].name.toLowerCase().indexOf('shockwave flash') != -1) {				
				supports.flash = true;
		  	}
		}

		if (window.ActiveXObject) {
			try {
				if (new ActiveXObject("ShockwaveFlash.ShockwaveFlash")) {
					supports.flash = true;
				}
			}
			catch(e) {}
		}
		
		if (typeof(window['ariaSettings']) == 'undefined' ?  false : true) {
			Object.keys(ariaSettings).each(function(item) {
				supports[item] = ariaSettings[item];
			}.bind(this));
		}
		
		return supports.viewInAllBrowsers ? true : ( supports.ff[2] || supports.ff[3] || supports.ie || supports.opera || supports.safari ) && supports.css;
	},
	
	/*
	 * Method to add JavaScript-Files and Stylesheets per runtime.
	 * based on <http://en.design-noir.de/webdev/JS/loadScript/>
	 *
	 * @param url - the path to the file 
	 * @param kind - distinction between JavaScript-Files and Stylesheets
	 */
	require: function(url, kind, callback) {	
		var f = arguments.callee;
		if (!("queue" in f))
			f.queue = {};
		var queue =  f.queue;
		if (url in queue) { 
			// script is already in the document
			if (callback) {
				if (queue[url]) 
					// still loading
					queue[url].push(callback);
				else 
					// loaded
					callback();
			}
			return;
		}
		queue[url] = callback ? [callback] : [];
		
		switch (kind) {
			case 'script':
				var script = Element.extend(new Element('script', { 'type':'text/javascript', 'src':url }));
					script.onload = script.onreadystatechange = function() {
						if (script.readyState && script.readyState != "loaded" && script.readyState != "complete")
							return;
						script.onreadystatechange = script.onload = null;
						while (queue[url].length)
							queue[url].shift()();
						queue[url] = null;
					};			
				break;
			case 'style':
				var style = Element.extend(new Element('link', { 'type':'text/css', 'rel':'stylesheet', 'href':url }));
					style.onload = style.onreadystatechange = function() {
						if (style.readyState && style.readyState != "loaded" && style.readyState != "complete")
							return;
						style.onreadystatechange = style.onload = null;
						while (queue[url].length)
							queue[url].shift()();
						queue[url] = null;
					};		
		}		
		
		$$("head").first().appendChild(Object.isUndefined(script) ? style : script);
	}
}

Aria.supported() ? Aria.initialize() : null;