/**
 * PathwaySite  the main class for handling a Pathway framework flash site
 * 
 * depends on
 * - jQuery 1.3.2  http://www.jquery.com
 * - jQuery history plugin  http://www.mikage.to/jquery/jquery_history.html
 * - SWFObject v2.2 ttp://code.google.com/p/swfobject/
 */

var PathwaySite = function() {
	//constants
	var PATHWAY_SITE_ID = "pathway_main_site"
	
	//public
	var _swfSource = "";
	var _contentID = "";
	var _width = "100%";
	var _height = "100%";
	var _version = "10.0.0";
	var _params = null;
	var _flashvars = null;
	
	//private
	var path = "";
	var cacheHash = "";
	
	var intervalID = -1;
	var queryString;
	
	/**
	 *  initialize the pathway site
	 *  @param String the location of the swf source file
	 *  @param String the ID of the HTML element into which to place the flash embed code
	 *  @param String the width of the swf
	 *  @param String the height of the swf
	 *  @param String the Flash version to check for
	 *  @param Object flashvars to pass to the swf
	 */
	function init(swfSource, contentID, width, height, version, flashvars, params) {
		_swfSource = swfSource;
		_contentID = contentID;
		_width = width;
		_height = height;
		_version = version;
		_flashvars = flashvars || {};
		_params = params || {};
		
		if(location.hash.length > 1 && location.hash != 'loading') {
			path = location.hash.substr(1);
		}else{
			path = "";
		}
		
		$(document).ready(onLoadDOM);
		
		location.hash = "loading";
		//setTimeout(500, function(){try {window.title='Think before you Post!';} catch(e){}});
		return false;
	}
	
	/**
	 * adds the flash when dom is ready loading, this makes sure the flash load bug is circumvented  
	 */
	function onLoadDOM() {
		_flashvars.path = path;
		swfobject.embedSWF(_swfSource, _contentID, _width, _height, _version, "expressInstall.swf", _flashvars, _params);
	}
	
	function enableHistorySupport() {
		if(intervalID == -1){
			intervalID = setInterval(PathwaySite.pathCheck, 200);
		}
	}
	
	function getPath() {
		var hash = "";
		if(location.hash.length > 1) {
			hash = location.hash.substr(1);
		}
		return hash;
	}
	
	function setPath(hash) {
		if(hash == "") {
			hash = "/";
		}
		queryString = $('#qs').val();
		anatlytics = queryString.length > 1 ? queryString + hash : hash;
		pageTracker._trackPageview(anatlytics);
		
		path = hash;
		location.hash = hash;
		//setTimeout(500, function(){try {window.title='Think before you Post!';} catch(e){}});
		return false;
	}
	
	function setStat(hash) {
		queryString = $('#qs').val();
		anatlytics = queryString.length > 1 ? queryString + hash : hash;
		pageTracker._trackPageview(anatlytics);
		//log(anatlytics);
	}

	function pathCheck() {
		var tmp = getPath();
		if(path != tmp) {
			path = tmp;
			flashMovieReference(_contentID).pathChanged(path);	
		}
	}
	
	function openPopup(url, w, h) {
		var scroll = 1;
		win = window.open(url , "window", "menubar=0,scrollbars=" + scroll + ",toolbar=0,location=0,directories=0,status=0,resizable=1,width=" + w + ",height=" + h);
		win.resizeTo(w, h);
	}
	
	
	function flashMovieReference(movieName) {
		if (document.embeds && document.embeds[movieName]){
			return document.embeds[movieName]; 
		} else if (window.document[movieName]) {
			return window.document[movieName];
		} else {
			return document.getElementById(movieName);
		}
	}

	return {
		init 					: init,
		getPath 				: getPath,
		enableHistorySupport	: enableHistorySupport,
		setPath					: setPath,
		setStat					: setStat,
		pathCheck				: pathCheck,
		openPopup				: openPopup
	}
}();



