/**
 * AVU3D is a Javascript Singleton used to manipulate the Avu3d flash object
 * 
 * @author		Maxime Gendreau
 * @company 	UrbanImmersive
 * @link 		http://www.urbanimmersive.com
 * @version 	0.08
 * 
 */
jQuery.noConflict();
var AVU3D = new Object(); // Create a namespace for Avu3d
AVU3D.Singleton = function() {

	var $ = jQuery; // Shortcut for the jQuery Object

	/**
	 * Public method to create the flash object with the good flash vars params
	 * @version 0.07
	 * 
	 * @param config: an object containing all the settings
	 */
	this.createProject = function(config) {
		if(!config)config = new Object();
		if(!config.id)config.id = "avu3d_as3_ProjetTest1";
		if(!config.swf)config.swf = "app.swf";
		if(!config.expressInstallSwf)config.expressInstallSwf = "libs/expressInstall.swf";

		var flash = $('#' + config.id); 
		var container = document.createElement('div');
		var containerId = config.id + '__container';
		container.id = containerId;
		container = $(container);
		
		container.css('width', '1px');
		container.css('height', '1px');
		container.insertBefore(flash);
		flash.detach();
		container.append(flash);
		var flashvars = {htmlFlashId: config.id};
		if(config.imagesPath)flashvars.imagesPath = config.imagesPath;
		if(config.projectName)flashvars.projectName = config.projectName;
		if(config.mainPath)flashvars.mainPath = config.mainPath;
		if(config.libsPath)flashvars.libsPath = config.libsPath;
		if(config.allowDomain)flashvars.allowDomain = config.allowDomain;
		if(config.policyFile)flashvars.policyFile = config.policyFile;
		if(config.debugMode)flashvars.debugMode = config.debugMode;

		
		var params = {
			allowFullScreen: "true",
			allowScriptAccess: "always",
			allownetworking: "all"
		};
		var attributes = {};
		swfobject.embedSWF(config.swf, config.id, "100%", "100%", "10.0.0", config.expressInstallSwf, flashvars, params, attributes);	
	
	};
	
	/**
	 * Public method to create the flash object with the good params and with a config txt file
	 * @version 0.06
	 * 
	 * @param id: 							the id of the embed flash object
	 * @param configPath:					the path of the application config file
	 * @param swfFilePath: 					the path of flash application swf file
	 * @param swfExpressInstallFilePath: 	the path of the Express Install swf file (for people who doesn't have the right version of flash)
	 */
	this.createProjectConfig = function(id, configPath, swfFilePath, swfExpressInstallFilePath) {
		var flash = $('#' + id); 
		var container = document.createElement('div');
		var containerId = id + '__container';
		container.id = containerId;
		container = $(container);
		
		//container.css('border', '1px solid red');
		//container.css('display', 'block');
		container.css('width', '1px');
		container.css('height', '1px');
		container.insertBefore(flash);
		flash.detach();
		container.append(flash);		
		var flashvars = {
			htmlFlashId: id,
			configPath: configPath
		};
		var params = {
			allowFullScreen: "true",
			allowScriptAccess: "always",
			allownetworking: "all"
		};
		var attributes = {};
		swfobject.embedSWF(swfFilePath, id, "100%", "100%", "10.0.0",swfExpressInstallFilePath, flashvars, params, attributes);	//, function(){alert('callback');}
	
	};
	
	/**
	 * Resize the container containing the flash, since the flash is 100% width and 100% height, it's like resizing the flash...
	 * @version 0.06
	 * 
	 * @param id: 		the id of the embed flash object
	 * @param width: 	the width to resize to
	 * @param height: 	the height to resize to
	 */
	this.resizeProject = function(id, width, height) {
		var container = $("#" + id + '__container');
		var flash = $('#' + id); 
		container.width(width + 'px');
		container.height(height + 'px');
	};

	/**
	 * Put the focus on the flash object to be sure it has the maximum control
	 * @version 0.06
	 * 
	 * @param id: 		the id of the embed flash object
	 */
	this.focusOnProject = function(id) {
		var flash = document.getElementById(id);
		flash.blur();
		document.body.focus();
		flash.tabIndex = 0; // It suppose to correct the focus bug on Chrome for Mac (still does'nt work in Chrome for Windows)
		flash.focus();
	};
	
	/**
	 * Call an alert
	 * @version 0.08
	 * 
	 * @param msg: The message to alert
	 */	
	this.logAlert = function(msg) {
		alert(msg);
	};
	
	
	/**
	 * Open a popup
	 * @version 0.08
	 * 
	 * @param url: Url of the popup
	 * @param w: Width of the popup
	 * @param h: Height of the popup
	 */	
	this.openPopup = function(url, w, h) {
		var popUpWindow = window.open(url,'PopUpWindow','height='+h+',width='+w+',toolbar=no,scrollbars=yes,resizable=yes');
		if (window.focus) {
			popUpWindow.focus()
		}
		return false;
	};

}

/**
 * AVU3D Class access OBJECT (SINGLETON)
 */
AVU3D.$ = function() {
	if (!AVU3D._instance) {
		AVU3D._instance = new AVU3D.Singleton();
	}
	return AVU3D._instance;
}

