function WU3(){
	var WU_Interface = this;
	this.WU;
	var Busy = false;
	this.isBusy = function(){
		return Busy;
	}
	this.setWU = function(w){
		WU_Interface.WU = w;
	};
	this.version = 3;
	this.getVersion = function(bool){
		if(bool) return {version:that.version,revision:'1'};
		return that.version;
	}
	var services = [];
	
	var service = new Weblink_Service('detectDevices');
	services.push(service);
	var service = new Weblink_Service('flash');
	service.addValidator(Weblink_Validators.NO_STARTER_UPDATE);
	services.push(service);
	                
	this.getServices = function(){
		return services;
	}
	
	//return false;
	/**
	 * Detects and broadcasts 
	 * dispatches: 'deviceDetected(type,infos)', 'deviceDetectComplete'
	 * throws: DeviceNotDetectedException,CableNotDetectedException,DetectionErrorException, PluginTimeOutException
	 */
	this.detectDevices = function()
	{
		function getModule(){
			var Serial;
			var Boot;
			/**
			 * Get module bootloaderversion
			 */
			function detectModule()
			{
				function getModuleVersion(){
					var method = "getModuleVersion";
					var body = buildRequest(method,ns,"");
					var xhr = new FlashXMLHttpRequest();
					xhr.onload = function(res)
					{
						var name = method + "Result";
						var body = buildRequest(method,ns,"");
						var sOut = rinse(res,name);
						Boot = sOut;
						if(Boot==0)
						{
							WU_Interface.WU.$.triggerHandler('deviceDetectError',[new DetectionErrorException()]);
							return;
							}
						getSN();
						
						};
					xhr.onerror = function()
					{
						WU_Interface.WU.$.triggerHandler('deviceDetectError',[new PluginTimeOutException()]);
						return;
					};
					xhr.open("POST", url);
					xhr.setRequestHeader("Content-Type", "text/xml; charset=utf-8");   	
					xhr.send(body);
				}
			
			function getSN()
			{
				var method = "printSerial";
				var body = buildRequest(method,ns,"");
				var xhr = new FlashXMLHttpRequest();
				
				xhr.onload = function()
				{
					var name = method + "Result";
					var sOut = rinse(xhr.responseText,name);
					Serial = sOut;
					if(Serial!=undefined && Serial==0)
					{
						WU_Interface.WU.$.triggerHandler('deviceDetectError',[new DetectionErrorException()]);
						return;
					}
					var Type = Weblink_Types.getByBootLoader(Boot);
					WU_Interface.WU.$.triggerHandler('deviceDetected',[Type,{'Serial':Serial,'BootLoader':Boot}]);
				};
				xhr.onerror = function()
				{
					WU_Interface.WU.$.triggerHandler('deviceDetectError',[new PluginTimeOutException()]);
					return;
				};
			
				xhr.open("POST", url);
				xhr.setRequestHeader("Content-Type", "text/xml; charset=utf-8");   	
				xhr.send(body);
				}
				getModuleVersion();
			}
			detectModule();
		}
		
		WU_Interface.WU.$.one(
				'deviceDetected',
				function(){  WU_Interface.WU.$.triggerHandler('deviceDetectComplete');  }
			);
		getModule();
		
	};
	
	/**
	 * Writes to device memory
	 * dispatches 'writeProgress','writeComplete'
	 * throws WriteConfException, PluginTimeOutException
	 */
	this.writeConfiguration = function(device, conf){
		return false;
	};
	
	/**
	 * Reads from device memory
	 * 
	 * dispatches 'readProgress','readComplete(result)'
	 * throws ReadException, PluginTimeOutException
	 */
	this.readConfiguration = function(device,rules){
		return false;
	};
	
	
	/**
	 * Writes Firmware to device
	 * dispatches 'flashStart','flashDelay','flashError(error)','flashProgress(bytes,perc)','flashComplete'
	 * throws WriteException, PluginTimeOutException
	 */
	this.flash = function(device, firmware){
		if(!(device instanceof Weblink_Device)) throw new InvalidDeviceException();
		if(device.getType() == Weblink_Types.REMOTE_STARTER){
			 throw new UnsupportedDeviceException('Remote starter firmware update not supported by this version');
		}
		if(!(firmware instanceof Weblink_Firmware)) throw new InvalidFirmwareException();
		
		var zeros = 0;
		var blocksize = device.getBlocks();
		
		//Function to fetch progress
		function getStatus(){
				var method = "flashStatus";
				var body = buildRequest(method,ns,"");
					var xhr = new FlashXMLHttpRequest();
					xhr.onload = function(res) {
						try{
							var blocks = rinse(res,"flashStatusResult");
						} catch(e){
							var blocks = 0;
						}
						
						
						if(blocks == '0' && zeros++ > 8){
							setTimeout(getFlashStatus,2000);
							zeros=0;
							return;
						} else if(!isNaN(parseInt(blocks)) && blocks != '0'){
							zeros=0;
							percentage = 100-(Math.round(parseInt(blocks)/blocksize*100));
							WU_Interface.WU.$.triggerHandler('flashProgress',[blocks,percentage]);
							
						}
						
						setTimeout(getStatus,150);
							
						//alert(xhr.responseText);
					};
					xhr.open("POST", url+"?id=" + new Date());
					xhr.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
					xhr.send(body);
		}
		
		//Function to check errors and successes
		function getFlashStatus() {
			var method = "errorStatus";
			var body = buildRequest(method,ns,"");
				var xhr = new FlashXMLHttpRequest();
				xhr.onload = function(res) {
					sOut = rinse(res,"errorStatusResult");
					//document.getElementById('errortext').innerHTML = xhr.responseText;
					
					if (sOut=="Success.")
					{
						Busy = false;
						WU_Interface.WU.$.triggerHandler('flashComplete');
						return;
					}
					else if (sOut=="Module Detected.")
					{
						Busy = false;
						WU_Interface.WU.$.triggerHandler('flashComplete');
						return;
					}
					else if (sOut=="Flash starts.")
					{
						Busy = false;
						WU_Interface.WU.$.triggerHandler('flashDelay');
						setTimeout(getStatus,150);
						return;
					}
					/*else if(sOut=="Update Module features success.")
					{
						''
					}
					else if(sOut=="Update Module features failed.")
					{
						WU_Interface.WU.errorHandle(new WriteConfException(sOut));
					}
					else if(sOut=="Flash starter firmware starts.")
					{
						WU_Interface.WU.$.triggerHandler('flashDelay');
						setTimeout(getStatus,150);
						return;
					}
					else if(sOut=="Success update starter firmware")
					{
						WU_Interface.WU.$.triggerHandler('flashComplete');
						return;
					}*/
					else
					{
						WU_Interface.WU.errorHandle(new FlashException(sOut));	
					}		
				};
				
				xhr.open("POST", url);
				xhr.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
				xhr.send(body);
			}
		
		//Call to flash module
		var method = "flash_now";
		var content = "<software_id>" + firmware.id + "</software_id>";
		var body = buildRequest(method,ns,content);
				
		var xhr = new FlashXMLHttpRequest();
		xhr.onload = function() { };
		xhr.onerror = function(){WU_Interface.WU.$.triggerHandler('flashError',[new FlashException()]);};
		xhr.open("POST", url+"?date=" + new Date());
		xhr.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
		xhr.send(body);
		WU_Interface.WU.$.triggerHandler('flashStarts');
		setTimeout(getStatus,2000);
	};
	
}
WU3.prototype = new WU_Abstract();