/**
	@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
	@modified 2/2008 Danny Campbell to use urls of the form index.html#sectionID instead of passing in the page to load in the querystring.
*/

/**
	@param flashURL String; the url containing the main flash movie. Omit if it is the current document.
*/
RippleManager = function(flashURL){
// flashURL is the page where the main Flash lives
	if(arguments.length == 0 || flashURL == null){
		this.flashURL = location.href;
		this.atTheFlashPage = true;
	}else{
		this.flashURL = flashURL;
	}
	RippleManager.instance = this;
}
var hash = (location.hash.length>0)?location.hash.substr(1):"";
// hash or "" if none
var queryVars;
var dontRedirect = false;
var p = RippleManager.prototype;
p.flashURL = "";
p.sectionID = "";
p.view = "";
p.atTheFlashPage = false;
p.rippleCallback = "javascript:void(RippleManager.instance.pageChange($url$));";
p.ourBasePath = basePath(location.href)+"/pages/";

p.initPage = function(){
// runs in the head of a content page, passing index.html or in index.html with no value
// initially there will be no search and no hash unless redirect=false
	var flashURL = this.flashURL;
	queryVars = this.parseQueryString(location.search);
//	alert( "cookie dontRedirect is: "+getCookie("dontRedirect") );
	if(getCookie("dontRedirect")=="true") {
		dontRedirect=true;
	}
	if(queryVars.redirect == "false") {
		//alert("setting dontRedirect cookie");
		// don't redirect.
		// set a cookie so we can check this on all other pages too
		setCookie("dontRedirect","true",null,"/");
		dontRedirect=true;
		//return;
	}
	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 sectionID has been passed in the hash. Store it so it can be passed to the Flash movie
			this.sectionID=(hash=="")?"defaultState":hash;
			//alert(this.sectionID);
		}else{
			// not on the FlashPage - rebuild the URL
			// converting the path/page to a sectionID
			// in this case flashURL is always going to be passed from the calling page i.e. /pages/rewards.aspx
			// so there will be no query or hash on flashURL
			
			// get just the value of the page name from the url
			var sectionID = stripExt(pageName(location.href));
			if(sectionID=="rewards") {sectionID="rewards_0";}
			if(sectionID=="index") {sectionID="";}
			flashURL = flashURL + "#" + sectionID;
			//alert("flashURL:"+flashURL);
			// now go there
			if(dontRedirect!=true){
				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){
	// this happens on the flash page as the flash obj is being written to the page
	// basically sends all the necessary variables into Flash
//	if(flashObj.getVariable("sectionID") == null){
		flashObj.addVariable("sectionID", this.sectionID);
		//alert("initflash with ripplePage: "+this.page);
//	}
	// flash will call this when the page changes from within Flash (i.e. in response to clicking an href in some html text), passing the url of the page
    flashObj.addVariable("ourBasePath",this.ourBasePath);	
	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;