function renderAudio(file){
	var reader = new FileReader();
	reader.onload = function(event){
    var audioFile = {};
		var url = event.target.result;
    document.getElementById('capture-preview-audio').src = url;
    audioFile.name = file.name;
    audioFile.fullPath = url;
    audioFile.type = file.type;
    audioFile.lastModifiedDate = file.lastModifiedDate;
    audioFile.size = file.size;
    getService('Capture').audioFile = audioFile;
	}
  //when the file is read it triggers the onload event above.
	reader.readAsDataURL(file);
}
function renderVideo(file){
	var reader = new FileReader();
	reader.onload = function(event){
    var videoFile = {};
		var url = event.target.result;
    document.getElementById('capture-preview-video').src = url;
		url = event.target.result;
    videoFile.name = file.name;
    videoFile.fullPath = url;
    videoFile.type = file.type;
    videoFile.lastModifiedDate = file.lastModifiedDate;
    videoFile.size = file.size;
    getService('Capture').videoFile = videoFile;
	}
  //when the file is read it triggers the onload event above.
	reader.readAsDataURL(file);
}
require(["dojo/dom",
	"dojo/has",
	"dojo/dom-construct",
	"dijit/form/Select"], function(dom, has, domConstruct, Select){
	addService("Capture", function(){
		this.captureDir = undefined;
    this.videoFile = null;
    this.audioFile = null;

		// Public
		// Handle requests
		this.exec = function(action, args, callbackId, uuid){
			_consoleLog("Capture." + action + "()");

      var generateError = document.getElementById('capture_gen_error').checked;
      if (generateError) {
        var errorSelectValue = document.getElementById('capture_error_type').value;
        var errorCode = null;
        // CAPTURE_INTERNAL_ERR = 0
        // CAPTURE_APPLICATION_BUSY = 1
        // CAPTURE_INVALID_ARGUMENT = 2
        // CAPTURE_NO_MEDIA_FILES = 3
        // CAPTURE_NOT_SUPPORTED = 20
        if (errorSelectValue.toString().indexOf("CAPTURE_INTERNAL_ERR") != -1) {
          errorCode = 0;
        } else if (errorSelectValue.toString().indexOf("CAPTURE_APPLICATION_BUSY") != -1) {
          errorCode = 1;
        } else if (errorSelectValue.toString().indexOf("CAPTURE_INVALID_ARGUMENT") != -1) {
          errorCode = 2;
        } else if (errorSelectValue.toString().indexOf("CAPTURE_NO_MEDIA_FILES") != -1) {
          errorCode = 3;
        } else if (errorSelectValue.toString().indexOf("CAPTURE_NOT_SUPPORTED") != -1) {
          errorCode = 20;
        }
        
        return new PluginResult(callbackId,
                      PluginResultStatus.ERROR,
                      errorCode, false);
      }
			
			var limit = 1;
			var platformID = getSimByUUID(uuid).device.platformID;
			var isiOS = platformID.toString().indexOf(
					".ios.") != -1;
			if ((isiOS == false) && (typeof args !== "undefined")) {
			    for (var i = 0; i < args.length; i++) {
			        if ((typeof args[i].limit !== "undefined")
			            && (args[i].limit !== null)) {
			            limit = args[i].limit;
			            break;
			        }
			    }
			}

			switch (action) {
				case "captureAudio" :
					var r = this.getAudioFiles(limit);
					return new PluginResult(callbackId, PluginResultStatus.OK, r, false);
				case "captureVideo" :
					r = this.getVideoFiles(limit);
					return new PluginResult(callbackId, PluginResultStatus.OK, r, false);
				default :
					alert('Capture.' + action + "() not implemented");
					return new PluginResult(callbackId, PluginResultStatus.OK, "NOT IMPLEMENTED", false);
			}
		};

		//this.playVideo = function(f){
			//f = this.captureDir + f;
			//var hasFF = has("ff") || (typeof window.navigator.originalUserAgent !== "undefined");
			//if (has("chrome") || hasFF)
				//f = f + ".ogg";
			//else
				//f = f + ".mp4";
			//if (!this.video) {
				//var captureVideoContainer = dom.byId("sim_capture_videoContainer");
				//this.video = dom.create("video", {
						//autoplay : true,
						//style : "width : 150px; "
				//}, captureVideoContainer);
			//}
			//var v = this.video;
			//v.src = f;
		//};

		this.newMediaFile = function() {
			var f = window.frames;
			if(f) {
			    for ( var i = 0; i < f.length; i++) {
			    	if (typeof f[i].MediaFile !== "undefined")
			    		return new f[i].MediaFile();
				}
			}
			return {};
		};

		this.getAudioFiles = function(limit){
      var ret = [], i; 
      if (!this.audioFile)
        return ret;
      for (i = 0; i < limit; i++) {
        var mediaFile = this.newMediaFile(); 
        mediaFile.name = this.audioFile.name;
        mediaFile.fullPath = this.audioFile.fullPath;
        mediaFile.type = this.audioFile.type;
        mediaFile.lastModifiedDate = this.audioFile.lastModifiedDate;
        mediaFile.size = this.audioFile.size;
        ret.push(mediaFile);
      }
      return ret;
      
		};

		this.getVideoFiles = function(limit){
      var ret = [], i;
      if (!this.videoFile)
        return ret;
      for (i = 0; i < limit; i++) {
        var mediaFile = this.newMediaFile(); 
        mediaFile.name = this.videoFile.name;
        mediaFile.fullPath = this.videoFile.fullPath;
        mediaFile.type = this.videoFile.type;
        mediaFile.lastModifiedDate = this.videoFile.lastModifiedDate;
        mediaFile.size = this.videoFile.size;
        ret.push(mediaFile);
      }
      return ret;
      
		};

		// Initialization
		{
			//var n = _pg_sim_nls;
			//var hasFF = has("ff") || (typeof window.navigator.originalUserAgent !== "undefined");
			//if (!(has("chrome") || hasFF || has("ie") || has("safari"))) {
				//var parentNode = dom.byId("capture");
				//domConstruct.empty(parentNode);
				//parentNode.innerHTML = n.sim_capture_browserSupport;
			//} else {
				
				//var errorSelect = dijit.byId('sim_capture_generate_error');
				//var listOfErrors = [
							//{ value: n.sim_compass_error_random, label: n.sim_compass_error_random },
							//{ value: "CAPTURE_INTERNAL_ERR", label: n.sim_compass_generate + " CAPTURE_INTERNAL_ERR" },
							//{ value: "CAPTURE_APPLICATION_BUSY", label: n.sim_compass_generate + " CAPTURE_APPLICATION_BUSY" },
							//{ value: "CAPTURE_INVALID_ARGUMENT", label: n.sim_compass_generate + " CAPTURE_INVALID_ARGUMENT" },
							//{ value: "CAPTURE_NO_MEDIA_FILES", label: n.sim_compass_generate + " CAPTURE_NO_MEDIA_FILES" },
							//{ value: "CAPTURE_NOT_SUPPORTED", label: n.sim_compass_generate + " CAPTURE_NOT_SUPPORTED" }
					//];
				//errorSelect.addOption(listOfErrors);
           //errorSelect.set('value', n.sim_compass_error_random);
           
				//this.captureDir = getScriptBase("capture.js") + "capture/";
			//}
		}
	});
});
