var Storygraph = Class.create();
Storygraph.prototype = {
	initialize: function(storygraph,options) {
		var foo = storygraph.pentads;
		var bar = storygraph.sources;
		this.sources = Array();
		for (var index = 0; index < bar.length; ++index) {
			this.sources[index] = new Source(index,bar[index],{});
		}; 
		var props = {createdby:storygraph.createdby,title:storygraph.storygramtitle,primarysourceid:getSourceListBySourceIDs(storygraph.primarysourceid,this.sources),storygramid:storygraph.storygramid,editable:(storygraph.iseditable == 'true'?true:false)};
	  	this.properties = new Properties(props);
		this.permissions = new Permissions(storygraph.grouppermissions,storygraph.userpermissions);
		this.tags = new Tags(storygraph.tags);
	  	this.pentads = Array();  	

		for (var index = 0; index < foo.length; index++) {
			var options = {top:foo[index].xcoordinate,left:foo[index].ycoordinate}
			
			this.pentads[index] = new Pentad(index
									,(foo[index].scenes.length?foo[index].scenes[0]:{})
									,(foo[index].agents.length?foo[index].agents[0]:{})
									,(foo[index].tools.length?foo[index].tools[0]:{})
									,(foo[index].actions.length?foo[index].actions[0]:{})
									,(foo[index].goals.length?foo[index].goals[0]:{})
									,options);

			this.pentads[index].getScene().setSources(getSourceListBySourceIDs(this.pentads[index].getScene().getSources(),this.sources));
		  	this.pentads[index].getAgent().setSources(getSourceListBySourceIDs(this.pentads[index].getAgent().getSources(),this.sources));
		  	this.pentads[index].getTool().setSources(getSourceListBySourceIDs(this.pentads[index].getTool().getSources(),this.sources));
		  	this.pentads[index].getAction().setSources(getSourceListBySourceIDs(this.pentads[index].getAction().getSources(),this.sources));
		  	this.pentads[index].getGoal().setSources(getSourceListBySourceIDs(this.pentads[index].getGoal().getSources(),this.sources));
									
		};
		
		
	   this.options = Object.extend({
	  	
	    }, options || {});
	},
	getPermissions:function(){
		return this.permissions;
	},
	getSources:function(){
		return this.sources;
	},
	getProperties:function(){
		return this.properties;
	},
	setProperties:function(props){
		this.properties = props;
	},
	addSource:function(source){
		var index = this.sources.size();
		this.sources[index] = source;
		return index; 		
	},
	deleteSource:function(index){
		delete this.sources[index];	
	
	},
  	getTags: function(){
		return this.tags;
	},
	setTags: function(tags){
		this.tags.setTags(tags);
	},
	getPentads: function(){
		return this.pentads;
	},
	getPentad: function(index){
		return this.pentads[index];
	},
	addPentad: function(pentad){
		this.pentads[this.pentads.length] = new Pentad(this.pentads.length,pentad.scene,pentad.agent,pentad.tool,pentad.action,pentad.goal,{});
		return this.pentads.length - 1; 
	},
	deletePentad: function(index){
		//alert('before = ' + this.pentads.length);
		delete this.pentads[index];
		//alert('after = ' + this.pentads.length);
	},

	display: function(){
		var displaycorrect = true;
		this.properties.display(this.sources);
		this.tags.display();
		for (var index = 0; index < this.pentads.length; index++) {
			try{
				this.getPentad(index).display();			
			}catch(e){
				displaycorrect = false;
			}
			
		}
		if (!displaycorrect){
			Dialog.alert("There was a problem displaying all the pentads on your screen size try making your browswer window bigger and try reloading the page.",{
	 			className:"alphacube"
	 			,width:300,
				onOk:function(win){
					window.location = 'index.cfm?event=storygram.list';
				}
	 		});	
		}

	},
	serialize: function(){

		var s = '';
		for(var x = 0; x < this.sources.length;x++){
			if(this.sources[x])
				if (s.length)
					s = s + ',' +this.sources[x].serialize();
				else
					s = this.sources[x].serialize();
		}
		var p = '';

		
		for (var x = 0; x < this.pentads.length; x++){
			if (this.pentads[x])
				if(p.length)
					p = p + ',' + this.pentads[x].serialize();
				else	
					p = this.pentads[x].serialize();
		}
		return '{"storygram":{"permissions": ' + this.permissions.serialize() + ',"tags":[' + this.tags.serialize() + '],"properties":' + this.properties.serialize() + ',"sources":[' + s + '],"pentads":[' + p + ']}}';
		
	}
}



var Permissions = Class.create();
Permissions.prototype = {
	initialize: function(grouppermissions,userpermissions,options) {
		var groups = grouppermissions;
		var users = userpermissions;
		this.editor = new Array();
		this.viewer = new Array();
		
		for(var x = 0; x < users.length;x++)
			if (users[x].permissiontype == 'viewer'){
				var z = {"id": users[x].userid, "type": "user"}
				this.viewer.push(z);
			}
				
		for(var x = 0; x < groups.length;x++)
			if (groups[x].permissiontype == 'viewer'){
				var z = {"id": groups[x].groupid, "type": "group"}
				this.viewer.push(z);				
			}
			
		for(var x = 0; x < users.length;x++)
			if (users[x].permissiontype == 'editor'){
				var z = {"id": users[x].userid, "type": "user"}
				this.editor.push(z);
			}
				
		for(var x = 0; x < groups.length;x++)
			if (groups[x].permissiontype == 'editor'){
				var z = {"id": groups[x].groupid, "type": "group"}
				this.editor.push(z);				
			}
			
			


	   	this.options = Object.extend({
	    }, options || {});
	},

	getViewers: function(){
		return this.viewer;
	},
	setViewers: function(viewer){
		this.viewer = viewer;
	},
	getEditors: function(){
		return this.editor;
	},
	setEditors: function(editor){
		this.editor = editor;
	},
	serialize: function(editor){
		var viewers = Object.toJSON(this.viewer);
		var editors = Object.toJSON(this.editor);
		//var permissions = '{"viewers":' + viewers + ',"editors":' + editors '}';
		return '{"viewers":' + viewers + ',"editors":' + editors + '}';
	}
}


var Source = Class.create();
Source.prototype = {
	initialize: function(index,source,options) {

		this.id = index;
		this.title = source.title;
		this.sourceid = source.sourceid;
		this.publicationdate = source.publicationdate;
		this.authors = source.authors;
		this.publisher = source.publisher;
		this.publisherlocation = source.publisherlocation;
		this.pages = source.pages;
		this.url = source.url;
	   	this.options = Object.extend({
	    }, options || {});
	},
	display: function(){
		
	},
	serialize: function(){

		var foo = '{"id":"' + this.id + '","title":"' + this.title + '","publicationdate":"' + this.publicationdate + '","authors":"' + this.authors + '","publisher":"' + this.publisher + '","publisherlocation":"' + this.publisherlocation + '","pages":"' + this.pages+ '","url":"' + this.url + '"}';
		return foo
	}
	
}	
var Tags = Class.create();
Tags.prototype = {
	initialize: function(tags,options) {
		this.tags = '';
		if (tags.length)
			for(var x = 0; x < tags.length; x++)
				if (this.tags.length)
					this.tags = this.tags + ',' + tags[x].tag;
				else
					this.tags = tags[x].tag;

	   	this.options = Object.extend({
	    }, options || {});
	},
	getTags: function(){
		return this.tags;
	},
	setTags: function(tags){
		this.tags = tags;
	},
	display: function(){
		$('a_storygraph_tags').innerHTML = this.tags;
	},
	serialize: function(){
		var foo = this.tags.split(',');
		var bar = '';
		for (var x = 0; x < foo.length;x++){
			if (bar.length)
				bar = bar + ',"' + foo[x] + '"';
			else	
				bar = '"' + foo[x] + '"'; 
		}
		return (bar);
	}
}	




var Properties = Class.create();
Properties.prototype = {
	initialize: function(properties,options) {
		this.title = properties.title;
		this.primarysourceid = properties.primarysourceid;
		this.createdby = properties.createdby;	
		this.storygramid = properties.storygramid;
		this.ispublic = properties.ispublic;
		this.iseditable = properties.editable;
	   	this.options = Object.extend({
	    }, options || {});
	},
	isEditable:function(){
		return this.iseditable;
	},
	display: function(sources){
		$('a_storygraph_title').innerHTML = this.title;
		$('a_storygraph_primarysource').innerHTML = getTitleOfSourceByID(this.primarysourceid,sources)			
		$('a_storygraph_createdby').innerHTML = this.createdby;
	},
	serialize:function(){
		var foo = '{"title":"' + this.title + '","primarysourceid":"' + this.primarysourceid + '","storygramid":"' + this.storygramid + '","ispublic":"' + this.ispublic + '"}';
		
		return (foo);
	}
}	

var num_pentads = 0;

var Pentad = Class.create();
Pentad.prototype = {
  initialize: function(id,scene,agent,tool,action,goal,options) {
  	this.id = id;
  	this.scene = new Scene(scene);
  	this.agent = new Agent(agent);
  	this.tool = new Tool(tool);
	this.action = new Action(action);
	this.goal = new Goal(goal);

 	num_pentads++;
  
    this.options = Object.extend({
      top		:			125 + 20 * num_pentads,
      left		:			75 + 20 * num_pentads, 
      zIndex	:			50 + 20 * num_pentads
    }, options || {});
  }, 
  getScene: function(){
  	return this.scene;
  },
  getAgent: function(){
  	return this.agent;
  },
  getTool: function(){
  	return this.tool;
  },
  getAction: function(){
  	return this.action;
  },
  getGoal: function(){
  	return this.goal;
  },
  getTop: function(){
  	return this.options.top;
  },
  setTop: function(top){
  	this.options.top = top;
  },
  getLeft: function(){
  	return this.options.left;
  },
  setLeft: function(left){
  	this.options.left = left;
  },
  
  refresh: function(){
  	$('pentad_'+this.id+'_scene_text').innerHTML = this.scene.getLabel();
 	$('pentad_'+this.id+'_agent_text').innerHTML = this.agent.getLabel();
 	$('pentad_'+this.id+'_tool_text').innerHTML = this.tool.getLabel();
 	$('pentad_'+this.id+'_action_text').innerHTML = this.action.getLabel();
 	$('pentad_'+this.id+'_goal_text').innerHTML = this.goal.getLabel();
  },
  
  display: function(){
		var food = 'pentad_' + this.id;

			var win = new Window({
				id : food,
				className: "darkX", 
				width:430, 
				height:172, 
				zIndex: 50 + 20 * num_pentads,
				minimizable:false,
				maximizable:false, 
				closable:storygraph.getProperties().isEditable(), 
				resizable: false,  
				showEffect:Effect.Appear, 
				hideEffect: Effect.Fade, 
				draggable:storygraph.getProperties().isEditable(), 
				wiredDrag: false,
				destroyOnClose:true,
				onDestroy: function(win) {
					deleteid = this.id.split('_');
					storygraph.deletePentad(deleteid[1]);
				},
				onEndMove: function(win){
					deleteid = this.id.split('_');
					l = win.getLocation()
					storygraph.getPentad(deleteid[1]).setTop(l.top.replace(/px/g,""));
					storygraph.getPentad(deleteid[1]).setLeft(l.left.replace(/px/g,""));	

				}
			});
	
			

		var pentaddiv = '<div id="pentad_' + this.id + '_scene_box"><div class="wherewhen"  onclick="openScene(' + this.id + ')" title="the scene there the action took place" id="pentad_' + this.id + '_scene_text">' + this.getScene().getLabel() + '</div></div>'
	
			+	'<table border="0" style="margin:auto;padding-top:5px;text-align:center;padding-left:0px;padding-right:0px;margin-left:0px;" cellpadding="1" cellspacing="0">'
			+	'<tr>'
			+	'<td class="who" id="pentad_' + this.id + '_agent_box" onclick="openAgent(' + this.id + ')" title="the agent(s) who carried out the action">'
			+	'<div class="whotext" id="pentad_' + this.id + '_agent_text">' + this.getAgent().getLabel() + '</div>'
			+	'</td>'
			+	'<td class="how" id="pentad_' + this.id + '_tool_box" onclick="openTool(' + this.id + ')" title="the tool(s) used to carry out the action">'
			+	'<div class="howtext" id="pentad_' + this.id + '_tool_text">' + this.getTool().getLabel() + '</div>'
			+	'</td>'
			+	'<td class="what" id="pentad_' + this.id + '_action_box" onclick="openAction(' + this.id + ')" title="description of the action">'
			+	'<div class="whattext" id="pentad_' + this.id + '_action_text">' + this.getAction().getLabel() + '</div>'
			+	'</td>'
			+	'<td class="why" id="pentad_' + this.id + '_goal_box" onclick="openGoal(' + this.id + ')" title="the goal(s) that motivated the action" >'
			+	'<div class="whytext" id="pentad_' + this.id + '_goal_text">' + this.getGoal().getLabel() + '</div>'
			+	'</td>'
			+	'</tr>'
			+	'</table>'


			
		/*
		var pentaddiv = '<div id="pentad_' + this.id + '_scene_box"><div class="wherewhen"  title="the scene there the action took place" id="pentad_' + this.id + '_scene_text">' + this.getScene().getLabel() + '</div></div>'
	
			+	'<table border="0" style="margin:auto;padding-top:5px;text-align:center;padding-left:0px;padding-right:0px;margin-left:0px;" cellpadding="1" cellspacing="0">'
			+	'<tr>'
			+	'<td class="who" id="pentad_' + this.id + '_agent_box" title="the agent(s) who carried out the action">'
			+	'<div class="whotext" id="pentad_' + this.id + '_agent_text">' + this.getAgent().getLabel() + '</div>'
			+	'</td>'
			+	'<td class="how" id="pentad_' + this.id + '_tool_box" title="the tool(s) used to carry out the action">'
			+	'<div class="howtext" id="pentad_' + this.id + '_tool_text">' + this.getTool().getLabel() + '</div>'
			+	'</td>'
			+	'<td class="what" id="pentad_' + this.id + '_action_box" title="description of the action">'
			+	'<div class="whattext" id="pentad_' + this.id + '_action_text">' + this.getAction().getLabel() + '</div>'
			+	'</td>'
			+	'<td class="why" id="pentad_' + this.id + '_goal_box" title="the goal(s) that motivated the action" >'
			+	'<div class="whytext" id="pentad_' + this.id + '_goal_text">' + this.getGoal().getLabel() + '</div>'
			+	'</td>'
			+	'</tr>'
			+	'</table>'
			
			*/
		
		//Event.observe('pentad_' + this.id + '_content', "click", function(e){ dump(e.srcElement) });

		win.getContent().innerHTML= pentaddiv;
		win.setLocation(this.options.top,this.options.left);
		win.setDestroyOnClose();
		
		showEvidenceStrengthPentad('scene',this.id,this.scene.getSources().length);
		showEvidenceStrengthPentad('agent',this.id,this.agent.getSources().length);
		showEvidenceStrengthPentad('tool',this.id,this.tool.getSources().length);
		showEvidenceStrengthPentad('action',this.id,this.action.getSources().length);
		showEvidenceStrengthPentad('goal',this.id,this.goal.getSources().length);
		win.show();				
  },
  serialize: function(){
  	var scene = this.scene.serialize();
	var agent = this.agent.serialize();
	var tool = this.tool.serialize();
	var action = this.action.serialize();
	var goal = this.goal.serialize();
		
	return '{"xcoordinate":"' + this.options.top +'","ycoordinate":"' + this.options.left + '","scene":' + scene + ',"agent":' + agent + ',"tool":' + tool + ',"action":' + action + ',"goal":' + goal + '}';
  }
}


var Scene = Class.create();
Scene.prototype = {
  initialize: function(options) {
  
     this.options = Object.extend({
      label			:		"Where",
      time			:		"", 
      location		:		"",
	  culturalnorm	:		"",	
      notes			: 		"",
	  sources		: 		""
    }, options || {});
  },
  getLabel: function(){
  	return this.options.label;
  },
  getTime: function(){
  	return this.options.time;
  },
  getLocation: function(){
  	return this.options.location;
  },
  getCulturalNorm: function(){
  	return this.options.culturalnorm;
  },
  getNotes: function(){
  	return this.options.notes;
  },
  getSources: function(){
  	return this.options.sources;
  },
  setLabel: function(label){
  	this.options.label = label;
  },
  setTime: function(time){
  	this.options.time = time;
  },
  setLocation: function(location){
  	this.options.location = location;
  },
  setCulturalNorm: function(culturalnorm){
  	this.options.culturalnorm = culturalnorm;
  },
  setNotes: function(notes){
  	this.options.notes = notes;
  },
  setSources: function(sources){
  	this.options.sources = sources;
  },
  serialize:function(){
  	return '{"label":"' + this.options.label + '","time":"' + this.options.time + '","location":"' + this.options.location + '","culturalnorm":"' + this.options.culturalnorm + '","notes":"' + this.options.notes + '","sources":"' + this.options.sources + '"}';
  }
};



var Agent = Class.create();
Agent.prototype = {
  initialize: function(options) {
    this.options = Object.extend({
      label			:		"Who",
      role			:		"", 
      background	:		"",
      notes			:		"",
      sources		:		""
    }, options || {});
  },

  getLabel: function(){
  	return this.options.label;
  },
  setLabel: function(label){
  	this.options.label = label;
  },
  getRole: function(){
  	return this.options.role;
  },
  setRole: function(role){
  	this.options.role = role;
  },
  getBackground: function(){
  	return this.options.background;
  },
  setBackground: function(background){
  	this.options.background = background;
  },
  getNotes: function(){
  	return this.options.notes;
  },
  setNotes: function(notes){
  	this.options.notes = notes; 
  },
  getSources: function(){
  	return this.options.sources;
  },
  setSources: function(sources){
  	this.options.sources = sources;
  },
  serialize:function(){
  	return '{"label":"' + this.options.label + '","role":"' + this.options.role + '","background":"' + this.options.background + '","notes":"' + this.options.notes +  '","sources":"' + this.options.sources + '"}';
	}
};

var Tool = Class.create();
Tool.prototype = {
  initialize: function(options) {
    this.options = Object.extend({
      label			:		"How",
      tooltype		:		"", 
      notes			:		"",
      sources		:		""
    }, options || {});
  },
  getLabel: function(){
  	return this.options.label;
  },
  setLabel: function(label){
  	this.options.label = label;
  },
  getTooltype: function(){
  	return this.options.tooltype;
  },
  setTooltype: function(tooltype){
  	this.options.tooltype = tooltype;
  },
  getNotes: function(){
  	return this.options.notes;
  },
  setNotes: function(notes){
  	this.options.notes = notes;
  },
  getSources: function(){
  	return this.options.sources;
  },
  setSources: function(sources){
  	this.options.sources = sources;
  },
  serialize:function(){
  	return '{"label":"' + this.options.label + '","tooltype":"' + this.options.tooltype + '","notes":"' + this.options.notes +  '","sources":"' + this.options.sources + '"}';
	}

};

var Action = Class.create();
Action.prototype = {
  initialize: function(options) {
    this.options = Object.extend({
		label		:		"What",
		notes		:		"",
		sources		:		""
    }, options || {});
  },
  getLabel: function(){
  	return this.options.label;
  },
  setLabel: function(label){
  	this.options.label = label;
  },
  getNotes: function(){
  	return this.options.notes;
  },
  setNotes: function(notes){
  	this.options.notes = notes;
  },
  getSources: function(){
  	return this.options.sources;
  },
  setSources: function(sources){
  	this.options.sources = sources;

  },
  serialize:function(){
  	return '{"label":"' + this.options.label +  '","notes":"' + this.options.notes +  '","sources":"' + this.options.sources + '"}';
	}

}


var Goal = Class.create();
Goal.prototype = {
	initialize: function(options) {
    	this.options = Object.extend({
			label		:		"Why",
			notes		:		"",
			sources		:		""
		}, options || {});
	},
  getLabel: function(){
  	return this.options.label;
  },
  setLabel: function(label){
  	this.options.label = label;
  },
  getNotes: function(){
  	return this.options.notes;
  },
  setNotes: function(notes){
  	this.options.notes = notes;
  },
  getSources: function(){
  	return this.options.sources;
  },
  setSources: function(sources){
  	this.options.sources = sources;
  },
  serialize:function(){
  	return '{"label":"' + this.options.label +  '","notes":"' + this.options.notes +  '","sources":"' + this.options.sources + '"}';
	}

}




function toggleGroup(id){
	ulElement = $('ul_' + id);
	imgElement = $('img_' + id);
	if (ulElement)
		if (ulElement.className == 'closed'){        		
			ulElement.className = "opened";
			imgElement.src = "assets/images/arrowdown.gif";
		}
		else{
			ulElement.className = "closed";
			imgElement.src = "assets/images/arrowright.gif";
		}
}


	

function getTitleOfSourceByID(id,sources){
	for (var x = 0; x < sources.length; x++)
		if(sources[x])
			if (sources[x].id == id )
				return sources[x].title;
	return 'My telling of the story';

}


function enableEvidenceProperties(){
	
	var all_sources = storygraph.getSources();
	while($('properties_source_list').firstChild) 
		$('properties_source_list').removeChild($('properties_source_list').firstChild);
	if (all_sources.length)
		for (var x = 0; x < all_sources.length; x++){
			if(all_sources[x]){
				element = Builder.node('li',{className:'radio'},[ Builder.node('input',{type:'radio',name:'property_radio_author',value:all_sources[x].id,id:'property_radio_author_' + all_sources[x].id}),Builder.node('span',all_sources[x].authors + ' ('+ all_sources[x].publicationdate +') ' + all_sources[x].title)]);
				$('properties_source_list').appendChild(element);
			}
		}
	else
		$('properties_source_list').appendChild(Builder.node('li','No Sources'));
	
}



	function enableEvidence(type,element,index,isEditable){
		var all_sources = storygraph.getSources();
		var elementsource = element.getSources().split(',');
		// first remove all of the children of the evidence boxes
		while($(type +'_source').firstChild) 
			$(type +'_source').removeChild($(type +'_source').firstChild);
		while($(type +'_source_selected').firstChild) 
			$(type +'_source_selected').removeChild($(type +'_source_selected').firstChild);
		if(!elementsource[0].length)
			elementsource = new Array();
				
		// loop over all sources and check to see if this element has that source
		for (var x = 0; x < all_sources.length; x++){
			if(all_sources[x]){
				var test = false;
				for (var y = 0; y < elementsource.length; y++){
					if (all_sources[x].id == elementsource[y]){
						
						test = true;
					}
				}
				if (test){
					$(type +'_source_selected').appendChild(createSelectedEvidenceItem('li_' + all_sources[x].id,all_sources[x].authors + ' ('+ all_sources[x].publicationdate +') ' + all_sources[x].title,type));					
				}else{
					$(type +'_source').appendChild(Builder.node('li',{id:'li_' + all_sources[x].id,className:'sourcebar'},all_sources[x].authors + ' ('+ all_sources[x].publicationdate +') ' + all_sources[x].title));					
				}
			}
		}		

		if (isEditable) {
			// now make all the sources draggable		
			var lilist = $(type +'_source').immediateDescendants();
			for (var x = 0 ; x < lilist.length; x++){
				new Draggable(lilist[x].id,{revert:true});
			}
	
			// display the correct strength of the evidence
			showEvidenceStrength(type,index);
			
			//lastly enable the dropzone
			Droppables.add(type + '_evidence_source_box_selected', {
				accept: 'sourcebar',
				onDrop: function(element) {
					var li = Element.remove(element.id);
					$(type +'_source_selected').appendChild(createSelectedEvidenceItem(li.id,li.innerHTML,type));
	
					showEvidenceStrength(type,index);
					
				}
			});
		}
		
	}


	function createSelectedEvidenceItem(id,text,type){

		table = Builder.node('table', {className:'source',width:'100%'});
		tbody = Builder.node('tbody');
		tr = Builder.node('tr');
		
		td = Builder.node('td',{id:'evidencetext_' + id},text);			
		tr.appendChild(td);
		
		td = Builder.node('td',{width:'10%'},[ Builder.node('img',{src:'assets/images/x2.gif',onclick:'removeEvidence(\'' + id + '\',\'' + type + '\')'})]);			
		tr.appendChild(td);
		
		tbody.appendChild(tr);
		table.appendChild(tbody);
		newli = Builder.node('li',{id: id,className:'sourcebar'});
		newli.appendChild(table);
		return newli;
	}

	function showEvidenceStrength(type,index){
		var evidencestrength = $(type +'_source_selected').immediateDescendants();

		$(type + '_evidence_none').removeClassName('evidence-selected');
		$(type + '_evidence_one').removeClassName('evidence-selected');
		$(type + '_evidence_multiple').removeClassName('evidence-selected');

		if (evidencestrength.length == 0){
			$(type + '_evidence_none').addClassName('evidence-selected');
		}else if (evidencestrength.length == 1){
			$(type + '_evidence_one').addClassName('evidence-selected');
		}else{
			$(type + '_evidence_multiple').addClassName('evidence-selected');
		}

		showEvidenceStrengthPentad(type,index,evidencestrength.length);

	}


	function showEvidenceStrengthPentad(type,index,strength){
		if(index || index == 0){		
			$('pentad_' + index + '_' + type + '_box').removeClassName('evidence-strength-none');
			$('pentad_' + index + '_' + type + '_box').removeClassName('evidence-strength-one');
			$('pentad_' + index + '_' + type + '_box').removeClassName('evidence-strength-multiple');
			if (strength == 0){
				$('pentad_' + index + '_' + type + '_box').addClassName('evidence-strength-none');
			}else if (strength == 1){
				$('pentad_' + index + '_' + type + '_box').addClassName('evidence-strength-one');
			}else{
				$('pentad_' + index + '_' + type + '_box').addClassName('evidence-strength-multiple');
			}
		}
		
	}


	function getEvidenceList(type){
		var ullist = $(type +'_source_selected').immediateDescendants();
		var returnlist = '';
		for (var x = 0; x < ullist.length;x++){	
			var foo = ullist[x].id.split('_');
			if (returnlist.length){
				returnlist = returnlist + ',' + foo[1];
			}else{
				returnlist = foo[1];
			}	
		}
		return returnlist;
	}
	
	function removeEvidence(liid,type){
		
		
		var newli = Builder.node('li',{id:liid,className:'sourcebar'},$('evidencetext_' + liid).innerHTML);
		Element.remove(liid);
		$(type +'_source').appendChild(newli);
		new Draggable(newli.id,{revert:true});
		
		showEvidenceStrength(type);

	}

	
	
	
	
	
	
	
	
	
	
	
	
function lightup(imageobject, opacity){
 if (navigator.appName.indexOf("Netscape")!=-1
  &&parseInt(navigator.appVersion)>=5)
    imageobject.style.MozOpacity=opacity/100
 else if (navigator.appName.indexOf("Microsoft")!= -1 
  &&parseInt(navigator.appVersion)>=4)
    imageobject.filters.alpha.opacity=opacity
}





function dump(obj){
	var keys = '';
	for(key in obj)
			keys = keys + key + ' = ' + obj[key] + '<br><br><br><br><br><br><br><br>';

	
	
	
var win = new Window({className: "dialog", width:350, 
height:400, zIndex: 100, resizable: true, title: "Sample window", 
showEffect:Effect.BlindDown, hideEffect: Effect.SwitchOff, 
draggable:true, wiredDrag: true}) 
win.getContent().innerHTML=  keys;
win.setStatusBar("Status bar info"); 
win.showCenter();	
	
	
}



function getSourceListBySourceIDs(sourceids,sources){
	
	var list = '';
	var s = sourceids.split(',');

	for (var z = 0; z < s.length; z++)
		for (var x = 0; x < sources.length; x++)
			if (sources[x].sourceid == s[z])
				list = list + (list.length?',':'') + x;
	
	return list
}



function openElementDialog(foo){
	
	var foo = foo.split('_');
	var type = foo[2];
	var id = foo[1];
	switch (type){
		case "scene": openScene(id);break;
		case "agent": openAgent(id);break;
		case "tool" : openTool(id);break;
		case "action" : openAction(id);break;
		case "goal" : openGoal(id);break;
	}

}









/*
#
# 11111111
var f00 = {"storygram":{
	"permissions": {
		"viewers":[{"id":"56AE990B-EB87-7BF9-BD7924E9D0643E8A","type":"user"},{"id":"A3C28379-EE97-D20B-D36BB9E91F9A9D29","type":"user"}],
		"editors":[{"id":"56AE98F9-AB9F-BD9A-016DAA88A14E440C","type":"user"},{"id":"A3AFDA9D-9058-AC99-F287186A10E1B44D","type":"user"}]},
	"tags":["test","bananas"],
	"properties":{"title":"My Second Storygram","primarysourceid":"","storygramid":"946D8635-0B93-6547-32ED5C1132AC3F72"},
	"sources":[
		{"title":"Test Source","publicationdate":"234234","authors":"asdfasdf","publisher":"","publisherlocation":"","pages":"","url":""},
		{"title":"asdfsdf","publicationdate":"undefined","authors":"a","publisher":"dsfasdf","publisherlocation":"undefined","pages":"asdf","url":"asdf"}
	],
	"pentads":[
		{"xcoordinate":" 471.2928550125809",
		"ycoordinate":"159.48987691425882",
		"scene":{"label":"undefined","time":"undefined","location":"undefined","culturalnorm":"undefined","notes":"undefined","sources":"undefined"},
		"agent":{"label":"undefined","role":"undefined","background":"undefined","notes":"undefined","sources":"undefined"},
		"tool":{"label":"undefined","tooltype":"undefined","notes":"undefined","sources":"undefined"},
		"action":{"label":"undefined","notes":"undefined","sources":"undefined"},
		"goal":{"label":"undefined","notes":"undefined","sources":"undefined"}
		},
		{"xcoordinate":"30",
		"ycoordinate":"30",
		"scene":{"label":"undefined","time":"undefined","location":"undefined","culturalnorm":"undefined","notes":"undefined","sources":"undefined"},
		"agent":{"label":"undefined","role":"undefined","background":"undefined","notes":"undefined","sources":"undefined"},
		"tool":{"label":"undefined","tooltype":"undefined","notes":"undefined","sources":"undefined"},
		"action":{"label":"undefined","notes":"undefined","sources":"undefined"},
		"goal":{"label":"undefined","notes":"undefined","sources":"undefined"}
		}
	]
}} 

*/