/* ===================================================
        Serialize a topic map to the javascript
        necessary to construct it.

		For long result strings, building up strings by
		concatenation is slow.  So we append text fragments
		to an array, then join them at the end.
   ===================================================*/
function saveMap(map){
	var win, src,doc
	var genmsg,topics,Topic,id,names,Name,scopes,Scope
	var occurs,Occur,resourceRef,resourceData,Type
	var t,o,s
	var identity,href
	var str
	var result=new Array()

	// Function definitions
	genmsg="//<pre>\n\
// This Topic Map was automatically generated\n\
// using Javascript.\n\
\n\
var top1,oc,sc,a1,mem,subject,uri,locator\n\
var map=new topicMap('')\n\n"

	result.push(genmsg)

	// Write topics
	result.push("//================= Topics ============\n")
	topics=map.getTopics()
	for (t in topics){
		Topic=topics[t]
		if (!Topic){continue}
		names=Topic.getNamesFiltered()
		Name=''
		if (names&&names.length){Name=names[0].name}
		Name=fixData(Name)// Fix quotes
		
		id=Topic.id
		if (!(id&&Name)){continue}
		Type=Topic.instanceOf ||''
		result.push('add_topic(map,"'+id+'","'+Name+'","'+Type+'")\n')

		// Subject Identity
		identity=Topic.getIdentity()
		if (identity){		
			uri=identity.getResourceRef()
			if (uri){
				result.push('add_subject_resource_ref(map,"' + id + '","' + uri + '")\n')
			}
			
			var indicators = identity.getSubjectIndicators()
			if (indicators){
				for (var i in indicators){
					uri=indicators[i]
					result.push('add_subject_indicator(map,"'+ id + '","' + uri + '")\n')
				}
			}

		}

	}
	result[result.length]='\n'

	// Write occurrences
	result[result.length]="//================= Occurrences ============\n"
	for (t in topics){
		Topic=topics[t]
		if (!Topic){continue}
		id=Topic.id
		occurs=Topic.getOccursFiltered()
		if (!(occurs&&occurs.length)){continue}
		for (o in occurs){
			Occur=occurs[o]
			resourceRef=Occur.getResource()
			resourceData=fixData(Occur.getData())

			resourceData='"'+resourceData+'"'
			Type=Occur.instanceOf

			// Get scope if any
			scopes=Occur.getScopes()
			if (!scopes) {continue}
			Scope=''
			if (scopes.length){
				Scope=scopes[0]
				Scope=Scope.topic
			}

			result[result.length]="add_occur(map,'"+id+"','"+Type+"',"
			result[result.length]="'"+Scope+"','"+resourceRef+"',"+resourceData+")\n"
		}
		
	}

	result[result.length]='\n'

	// Add scopes of topic names
	result[result.length]="//================ Scopes ===================\n"

	for (t in topics){
		Topic=topics[t]
		if (!Topic){continue}
		names=Topic.getNamesFiltered()
		if (!(names&&names.length)){continue}
		Name=names[0]
		scopes=Name.getScopes()
		if (!scopes.length){continue}
		for (s in scopes){
			Scope=scopes[s].topic
			if (Scope=='st-unconstrained'){continue}
			result[result.length]="top1=map.getTopicById('"+Topic.id+"')\n"
			result[result.length]='top1.nameList[0].addScope(new scope("'
			result[result.length]=Scope+'"))\n\n'		
		}
	}
	
	result[result.length]='\n'
    result[result.length]="//================ Associations ================\n"
    var a,assoc,associations
    var m,members,member,role
    var p,players,player

    associations=map.getAssociations()
    for (a in associations){
        assoc=associations[a]
		if (!assoc){continue}
		
        result[result.length]="a1=new association('"+assoc.id+"')\n"
        result[result.length]="a1.instanceOf='"+assoc.instanceOf+"'\n"
        
        members=assoc.getMembers()
        for (m in members){
            member=members[m]
            role=member.roleSpec
            result[result.length]="mem=new member('"+role+"')\n"
            players=member.getPlayers()
            for (p in players){
                player=players[p].id
				if (!player){continue}
                result[result.length]="mem.addPlayer("
                result[result.length]="map.getTopicById('"+player+"'))\n"
            }
            result[result.length]="a1.addMember(mem)\n\n"
        } 
        
        scopes=assoc.getScopes()
        for (s in scopes){
            Scope=scopes[s]
            result[result.length]="a1.addScope(new scope('"+Scope.topic+"'))\n"
        }
        
        result[result.length]="\nmap.addAssoc(a1)\n\n"
    }
	
	result.push('//</pre>')

	src=result.join('')
    
	win=window.open('','_new')
	doc=win.document
	doc.open()
    doc.write(src)
	doc.close()
	win.focus()
}



