/** StorageSupport functionalities */
function StorageSupport() {}

// Flash storage attributes
StorageSupport.flashFile = "/res/swf/storageSupport/storageSupport.swf";
StorageSupport.overrideDiv = "flashLocalStorageObject"; // Div is defined in the layout VM files
StorageSupport.minFlashVerRequired = "9";
StorageSupport.isFlashStorageLoaded = false;

// HTML5 storage attributes
StorageSupport.isHtml5StorageSupported = typeof window.localStorage != "undefined";

// Embed Flash LSO
$(document).ready(function() {
	if(swfobject.hasFlashPlayerVersion(StorageSupport.minFlashVerRequired)) {
		swfobject.embedSWF(StorageSupport.flashFile, StorageSupport.overrideDiv, "1", "1", StorageSupport.minFlashVerRequired,
			null, {}, {params:"allowScriptAccess"}, {}, (function(e){StorageSupport.isFlashStorageLoaded = e.success;}));
	}
});

// Set local storage
StorageSupport.setLocalStorage = function(varName, varValue, expiryDays) {
	// Set in Flash storage
	if(StorageSupport.isFlashStorageLoaded) {
		if(window[StorageSupport.overrideDiv]) {
			window[StorageSupport.overrideDiv].setFlashCookie(varName, varValue);
		} else {
			document[StorageSupport.overrideDiv].setFlashCookie(varName, varValue);
		}
	}

	// Set in HTML5 storage
	if(StorageSupport.isHtml5StorageSupported) {
		localStorage.setItem(varName, varValue);
	}

	// Set as browser cookie
	if(!expiryDays) {expiryDays = 30;} // If an expiry was not set, we set it to 30 days
	$.cookie(varName, varValue, {expires:expiryDays, path:'/'});
};

// Get local storage
// returns : If cookie not found an empty string is returned
StorageSupport.getLocalStorage = function(varName) {
	var retValue = "";
	
	// Get browser cookie
	retValue = $.cookie(varName);
	if (retValue) {
		return retValue;
	}
	
	// Get from Flash storage
	if(StorageSupport.isFlashStorageLoaded) {
		if(window[StorageSupport.overrideDiv]) {
			retValue = window[StorageSupport.overrideDiv].getFlashCookie(varName);
		} else {
			retValue = document[StorageSupport.overrideDiv].getFlashCookie(varName);
		}
		
		if (retValue != "") {
			return retValue;
		}
	}
	
	// Get from HTML5 Storage
	if (StorageSupport.isHtml5StorageSupported) {
		retValue = localStorage.getItem(varName);
		if(retValue) {
			return retValue;
		}
	}
	
	// If we couldn't retrieve the cookie we return an empty string
	return "";
};
