/**
	@class RippleManager
	@author Peter Hall
	@email info@peterjoel.co.uk
	@web http://www.peterjoel.com
	@description Determines whether the user should be given the Flash version or html. Redirects and manages links accordingly
	@version 0.2.1
*/

/**
	@param flashURL String; the url containing the main flash movie. Omit if it is the current document.
*/
RippleManager = function(flashURL){
	if(arguments.length == 0 || flashURL == null){
		this.flashURL = location.href;
		this.atTheFlashPage = true;
	}else{
		this.flashURL = flashURL;
	}
	RippleManager.instance = this;
}

var p = RippleManager.prototype;
p.flashURL = "";
p.page = "";
p.view = "";
p.atTheFlashPage = false;
p.rippleCallback = "javascript:void(RippleManager.instance.pageChange($url$));";

p.initPage = function(){
	var flashURL = this.flashURL;
	var queryVars = this.parseQueryString(location.search);
	if(queryVars.rippleView != null){
		this.view = queryVars.rippleView;
	}
	if(queryVars.rippleView == "html"){
		
		// update all the links in the document, but wait until the document is fully loaded
		// this chunk of code is just coverng all bases for how browsers deal with events.
		// The listener model is preferred
		var self = this;
		function _initRipple(){
			// a little scope hack
			self.updateLinks();
		}
		if(window.attachEvent){
			window.attachEvent("onload", _initRipple);
		} else if(document.addEventListener){ 
			document.addEventListener("load", _initRipple, false);	
		}else if(window.addEventListener){
			window.addEventListener("load", _initRipple, false);	
		} else {
			// listeners are not supported. So just define onload. If onload is already set by another script then
			// make sure to call that too. We can't do anything about other scripts overriding this one though.
			// (**TO DO: check if can do this with watch()**)
			if(typeof window.onload == "function"){
				var oldOnLoad = window.onload;
				window.onload = function(){ oldOnLoad(); _initRipple(); };
			} else {
				window.onload = _initRipple;
			}
		}
		
		
	}else{
		if(this.atTheFlashPage){
			// then we are already on the flashURL, so check if
			// a page url has been passed. Store it so it can be passed to the Flash movie
			if(queryVars.ripplePage != null){
				this.page = queryVars.ripplePage;
			}

		}else{
			// rebuild the URL, adding the ripplePage variable
			var searchInd = flashURL.indexOf("?");
			var hashInd = flashURL.indexOf("#");
			var hash = "";
			var searchObj;
			if(hashInd > -1){
				// if there is a # in the URL remove the hash id, and remember it
				hash = flashURL.substr(hashInd);
				flashURL = flashURL.substring(0,hashInd);
			}
			if(searchInd > -1){
				// if there is a ? in the URL remove the query and create an object from it
				searchObj = this.parseQueryString(mainPage.substr(searchInd));
				flashURL = flashURL.substring(0,searchInd);
			}else{
				searchObj = new Object();
			}
			// add in the ripplePage variable to the search string and rebuild the url
			// this will be passed on to the 
			searchObj.ripplePage = location.href;
			flashURL = flashURL + this.createQueryString(searchObj) + hash;
			
			// now go there
			location.replace(flashURL);
		}
	}
}

// default handler. Override to add functionality. For example, to load the content into a hidden frame
// to get browser back button functionality
p.pageChange = function(url){
	alert(url);
}

p.initFlash = function(flashObj){
	if(flashObj.getVariable("ripplePage") == null){
		flashObj.addVariable("ripplePage", this.page);
	}
	// flash will call this when the page changes from within Flash, passing the url of the page
	flashObj.addVariable("rippleCallback", this.rippleCallback);
	flashObj.addVariable("rippleWindow", window.name);
}

// this is a fairly indiscriminate blanketing of all links.
// ideally, we should pick out the ones that are internal to this site
// but adding a variable to the query string *shouldn't* break anything...
p.updateLinks = function(){
	var n = document.links.length;
	var searchObj;
	if(this.view.length){
		for(var i=0; i<n; i++){
			searchObj = this.parseQueryString(document.links[i].search);
			searchObj.rippleView = this.view;
			document.links[i].search = this.createQueryString(searchObj);
		}
	}
}


p.createQueryString = function(obj){
	var q = "?";
	var gotVars = false;
	for(var p in obj){
		gotVars = true;
		q += p + "=" + escape(obj[p]) + "&";
	}
	if(gotVars){
		return q;
	}
	return "";
}

p.parseQueryString = function(str){
	var obj = new Object();
	if(str.charAt(0) == "?"){
		str = str.substr(1);
	}
	var vars = str.split("&");
	var n = vars.length;
	var varParts;
	for(var i=0; i<n; i++){
		varParts = vars[i].split("=");
		if(varParts.length > 1){
			obj[varParts[0]] = unescape(varParts[1]);
		}else if(varParts[0].length && varParts[0]!="?"){
			obj[varParts[0]] = "";
		}
	}
	return obj;
}

p.isIE = function(){
	return navigator.appName.indexOf("Microsoft") > -1;
}


delete p;
