﻿if(typeof rcp == "undefined") var rcp = new Object();

rcp.SWFInterface = function(){
};

rcp.SWFInterface.prototype = {
	
	// Initalization
	init: function (s) {
		this.title = '';
		this.noCallback = false;
		this.swfTargetName = s;
		this.swfTarget = this.getSWF(s);
		// Backup the original hash
		this.startupHash = location.hash.substring(1);
	},
	
	// Return the original backuped hash
	getStartupHash: function () {
		return this.startupHash;
	},
	
	//Return a reference to a SWF
	getSWF: function (movieName) {
		var flashMovie;
		if (document.getElementById) {
			flashMovie = document.getElementById(movieName);
		}
		return flashMovie;
	},
	
	// put a link to the history then call doLink - no callback
	updateLink: function (param) {
		this.pushLink(param, true);
	},
	
	// push a link to the flash then call doLink - no callback
	flashLink: function (param) {
		this.swfTarget.updateLink(param);
		this.pushLink(param, true);
	},
	
	// put a link to the history then call doLink - flash will herit callback
	historyLink: function (param) {
		this.pushLink(param, false);
	},
	
	// push the link to unFocus.History
	pushLink: function ($hash, $noCallback) {
		if($noCallback != true) $noCallback = false;
		this.noCallback = $noCallback;
		$hash = $hash.split('?').join('_');
		unFocus.History.addHistory($hash);
		document.title = this.title + this.extractTitleFromParam($hash);
	},
	
	// unFocus.History callback
	onHistoryChange: function (param) {
		if (this.noCallback != true){
			this.swfTarget.updateLink(param);
			document.title = this.title + this.extractTitleFromParam(param);
		}
		this.noCallback = false;
	},
	
	//
	extractTitleFromParam: function (param) {
		var p = param.split('-');
		p.pop();
		var s = p.join('-');
		return s;
	}
}

SWFInterface = new rcp.SWFInterface();
unFocus.History.addEventListener("historyChange", function(param){SWFInterface.onHistoryChange(param)});