/////////////////////////////////////////////////////
// 		Author:     cristian.deschamps[at]nurun.com
// 		Updated:    25 Mars 2007
//		Copyright Nurun inc.
/////////////////////////////////////////////////////
var NURUN = window.NURUN || {}
NURUN.Flash = NURUN.Flash || {};

// ------[ Master Onload for all page. ]------------------------------------------------- //	
NURUN.Flash.Repository = {

	// Config Parameters
	ShowFrame : false, // Show or Hide the Iframe Content in the page.
	IndexPages : ['/default.aspx'], // First item: Root, Second item: root+default page, other item: all variation of the index page. All LowerCase.
	FlashVersion : 9, // Minimum Flash Version
	Html : false, // Enable/Disable the Frame Manager 

	// Private Properties
	ContentID : null, // HTML object to be replaced by the Flash.
	Language : "en",
	ContextPath : "/",
	FlashUrl : null, // Flash File
	iFrame : null,
	IsFramed : (top != self),
	CurrentFrame : null,
	Browser : null,
	SafariForm : null,
	DetectFlashContainer : null,
	
	Variables : {
		Value : [],
		
		Add : function(name,value) {
			if(name != '') {
			this.Value.push(name);
			this.Value.push(value);
			} else {
				alert("The name of the variable to be passed to Flash can't be null")
			}						
		},
		
		Get : function(obj) {
			for(x=0;x<this.Value.length; x++){
			obj.addVariable(this.Value[x], this.Value[x+1]);			
			x++;			
			}
		}				
	},
	
	/* Dyanmic Config */
	LoadConfig : function() {		
		var debug = (NURUN.util.GetRequestParameter("debug") == 'true') ? true : false;
		var html = (NURUN.util.GetRequestParameter("html") == 'true') ? true : false;	
		
		// Check if debug is enabled from the URL and save it for the session
		if(NURUN.util.GetRequestParameter("debug") != '') {
			NURUN.util.Cookie.Save("debug", debug, true);
			this.ShowFrame = debug;					
		} 
		else {
			this.ShowFrame = (NURUN.util.Cookie.Load("debug") == 'true') ? true : false;			
		}
		
		// Check if Flash is enabled from the URL and save it for the session
		if(NURUN.util.GetRequestParameter("html") != '') {				
			NURUN.util.Cookie.Save("html", html, true);
			this.Html = html;
		}	
		else {
			this.Html = (NURUN.util.Cookie.Load("html") == 'true') ? true : false;					
		}	
		
		this.Browser = NURUN.util.BrowserDetection();
				
		if(!this.Html) {
			this.DetectFlashContainer = document.createElement('div');
			this.DetectFlashContainer.style.position = "absolute";
			this.DetectFlashContainer.style.width = '919';
			this.DetectFlashContainer.style.height = '525';
			this.DetectFlashContainer.style.left = 0;
			this.DetectFlashContainer.style.top = 0;
			this.DetectFlashContainer.style.backgroundcolor = "#ccc";
			this.DetectFlashContainer = document.body.insertBefore(this.DetectFlashContainer, document.body.firstChild);
			
			var detectFlashVersion = new SWFObject('spc.swf','detectflashversion','919','525',8);
			detectFlashVersion.addParam("wmode", "transparent");
			detectFlashVersion.useExpressInstall("expressInstall.swf");
			detectFlashVersion.write(this.DetectFlashContainer);
			
			this.Html = (detectFlashVersion.installedVer.major < 8) ? true : false;	
			
			if(!this.Html) {
				this.ExpressInstallCancelled();
				}
			
			if((detectFlashVersion.installedVer.major == 6) && (detectFlashVersion.installedVer.rev < 65)) {
				this.ExpressInstallCancelled();
				}
			
		}
		NURUN.Flash.Repository.log('Config Debug: ' + this.ShowFrame);
		NURUN.Flash.Repository.log('Config Html: ' + this.Html);
			
	},
	
	ExpressInstallCancelled : function() {
		this.DetectFlashContainer.innerHTML = "";
		this.DetectFlashContainer.style.display = "none";
		NURUN.Flash.Repository.log('clean express install');
	},
	
	// Create the Master Flash
	Create : function() {	
		this.LoadConfig();
		
		if(!this.ShowFrame) {
			var width = '919';
			var height = '525';
		} else {
			var width = '919';
			var height = '525';			
		}
		
		if((!this.IsFramed) && (!this.Html)) {	
			//this.SetPageToIndex(); // Check if the current page is the Index (Nicer URL)
			
			var so = new SWFObject(this.FlashUrl, 'masterFlash', width, height, this.FlashVersion, "#0d0705", true);
			so.addParam("allowScriptAccess", "always");
			so.addParam("wmode", "opaque");
			this.Variables.Get(so);
			so.write(this.ContentID);
			NURUN.Flash.Repository.log('Flash Rendered...');	
		} 
	},
	
	// Initialize Repository after the page load.
	Init : function() {
				
		// Page Initialize
		if((this.IsFramed == false) && (this.Html == false)) {
						
			if(this.Browser == 'safari') {
				this.SafariForm = document.createElement('form');
				this.SafariForm.name = 'safariHash';
				this.SafariForm.method = 'get';
				this.SafariForm.action = '';
				this.SafariForm = document.body.appendChild(this.SafariForm);
			}
		
			this.iFrame = document.createElement('iframe');
			this.iFrame.name = 'repository' // Use name only, get bug with ID in safari :-S
			this.iFrame.className = 'repository';
			this.iFrame.scrolling = 'auto';
			this.iFrame.setAttribute('src','javascript:document.open();document.write("");document.close();');
			this.iFrame.style.display = 'none';											
			this.iFrame = document.body.appendChild(this.iFrame);		

			this.CurrentFrame = this.GetHash(); // Get the Frame from the hash param.

			// Register Custom Event for the Hash Manual Update/Back Button
			this.HashWatcher();				
			
			if(this.CurrentFrame == ''){				
				thisCurrentFrame = this.GetHash(document.location.pathname.toLowerCase());
				this.SetHash(document.location.pathname.toLowerCase());				
			}				
			
			YAHOO.util.Dom.setStyle(this.iFrame, 'display', 'block');														
			if(this.ShowFrame == false) {
				YAHOO.util.Dom.setStyle(this.iFrame, 'width', '1px');
				YAHOO.util.Dom.setStyle(this.iFrame, 'height', '1px');
				YAHOO.util.Dom.setStyle(this.iFrame, 'position', 'absolute');
				YAHOO.util.Dom.setStyle(this.iFrame, 'left', '-2000px');
				YAHOO.util.Dom.setStyle(this.iFrame, 'top', '-2000px');
				YAHOO.util.Dom.setStyle(this.iFrame, 'visibility', 'hidden');
			} else {
				YAHOO.util.Dom.setStyle(this.iFrame, 'width', '968px');
				YAHOO.util.Dom.setStyle(this.iFrame, 'height', '574px');				
			}															
			
			// Load Iframe with delay, because Safari need some time to render the Iframe in the DOM.
			this.LoadIFrame();								
			
		// FrameInitialize		
		} else if(this.Html == false) {								
			parent.NURUN.Flash.Repository.Update(document.title, document.location.pathname, NURUN.Flash.Repository.Screen, NURUN.Flash.Repository.XML);	
		}

		NURUN.Flash.Repository.log('Init');
	},
	
	LoadIFrame : function() {
		if((frames['repository']) || (this.Browser == 'msie')) {									
			NURUN.Flash.Repository.GoTo(NURUN.Flash.Repository.CurrentFrame);	
			NURUN.Flash.Repository.log('LoadIFrame');				
		} else {
			setTimeout('NURUN.Flash.Repository.LoadIFrame()', 100 );
		}
	},
	
	Listener : false,
	HashWatcher : function() {
		loop = 0;
		this.Listener = setInterval( function () {
			var value = NURUN.Flash.Repository.GetHash();
			if(NURUN.Flash.Repository.CurrentFrame.toLowerCase() != value.toLowerCase()) {
				NURUN.Flash.Repository.CurrentFrame = value;
				NURUN.Flash.Repository.GoTo(value);	
				NURUN.Flash.Repository.log('HashWatcher: ' + value);		
			}			
		}, 500 );		
	},
		
	SetHash : function(value) {
		if(value) {
			if(this.Browser == 'safari') {				
				document.forms.safariHash.action = '#' + value;
				document.forms.safariHash.submit();				
			} else {
				document.location.hash = value;
			}
		
		NURUN.Flash.Repository.log('SetHash: ' + value);
		}
	},

	GetHash : function(value) {
		if(!value) value = document.location.hash.toLowerCase();

		if(value.indexOf('#') != -1) {
			return value.substring(1,value.length);
		} else {						
			return value;
		}					
	},

	// Change IFrame Location (from the Flash)
	GoTo : function(page) {				
		if(page == '') page = document.location.pathname.toLowerCase();
							
		if(this.Browser == 'msie') {				
			frames[0].location.href = page;
		} else {
			frames['repository'].location.replace(page);
			
			this.CurrentFrame = this.GetHash(page.toLowerCase());
			this.SetHash(page.toLowerCase());
		}	
		NURUN.Flash.Repository.log('GoTo: ' + page);	
	},
	
	// Update Page Parent (Iframe onload)
	Update : function(title,location,screen,xml) {					
		// Call the Action Script function in the Flash 'Master'.
		document.getElementById('masterFlash').section(screen,xml); 		

		if(this.Browser == 'msie') {
			this.CurrentFrame = this.GetHash(location.toLowerCase());
			this.SetHash(location.toLowerCase());
		}

		document.title = title;
		NURUN.Flash.Repository.log('Update screen: ' + screen);	
		NURUN.Flash.Repository.log('Update xml: ' + xml);
	},
		
	// Reset the Page URL to the Index page, Only in Framed Mode.
	/*
	SetPageToIndex : function() {
		var loc = document.location;		
		var isIndex = false;
		for(x=0; x<this.IndexPages.length; x++) {
			if(loc.pathname.indexOf(this.IndexPages[x]) != -1) isIndex = true;
		}
			
		if(!isIndex) {
			document.location.href = this.GetIndexPage() + '#' + loc.pathname; 		
		}
	},

	GetIndexPage : function() {
		var url = this.IndexPages[0];		
		return url;
	},
	*/
	
	// Log Event to the console (firefox)
	log : function(string) {
		if ('object' == typeof console) {
			var from = (top != self) ? "[Frame] " : "[Page ] ";
			console.log(from + string);
		}
	}
	
};

YAHOO.util.Event.addListener(window, 'load', NURUN.Flash.Repository.Init, NURUN.Flash.Repository, true);
window.onunload = function() { };