// HELPERS

	// No cache for AJAX
    var noCache = function(uri) {
    	return uri.concat(/\?/.test(uri) ? "&" : "?", "noCache=", (new Date).getTime(), ".", Math.random() * 1234567);
    };

    // Create XML request
    var createXMLHttpRequest = function() {
        var ua;
        if(window.XMLHttpRequest) {
        	try { ua = new XMLHttpRequest(); } catch(e) { ua = false; }
        } else if(window.ActiveXObject) {
        	try { ua = new ActiveXObject('Microsoft.XMLHTTP'); } catch(e) { ua = false; }
        }
        return ua;
    };

// EXIF
	
	// Close EXIF
	var closeEXIF = function() {
		exifDataContainer.style.display = 'none';
		exifDataElement.innerHTML = '';
	};
	
	// Load EXIF data
	var loadEXIF = function(filePath) {
		closeEXIF();
		
		var exif = createXMLHttpRequest();
    	exif.open('get', noCache(exifPlugin + '?image=' + filePath), true);
    
	    exif.onreadystatechange = function() {
	    	if(exif.readyState == 4 && exif.status == 200) {
	    		parseEXIF(exif.responseXML);
	    	}
	    };
    
    	exif.send(null);
	};
	
	// Parse EXIF data
   	var parseEXIF = function(data) {
   		var exifData = {};
  		if(data) var exifNodes = data.getElementsByTagName('exif')[0];
   		
   		if(exifNodes) {
 	  		for(var i = 0; i < exifNodes.childNodes.length; i++) {
 	  			if(exifNodes.childNodes[i].childNodes[0]) {
 	  				exifData[exifNodes.childNodes[i].nodeName] = exifNodes.childNodes[i].childNodes[0].nodeValue;
 	  			}
 	  		}
   		}
   		
   		showEXIF(exifData);
   	};

	// Show EXIF data (template)
	var showEXIF = function(EXIFarray) {
		var lines = [];
		
		for(var entry in EXIFarray) {
			if(typeof exifNames[entry] != 'undefined')
				lines.push('<tr><td class="caption">' + exifNames[entry] + '</td><td class="value">' + EXIFarray[entry] + '</td></tr>');
		}
		
		if(lines.length > 0) {
			exifDataContainer.style.display = 'block';
			exifDataElement.innerHTML = '<table>' + lines.join('') + '</table>';
		}
	};
	
// START
	
	closeEXIF();
	if(typeof imagePath != 'undefined') loadEXIF(imagePath);