if (!isset(translations)) { var translations=new Object(); }
translations['action']="ACTION";
translations['cancel']="CANCEL";
translations['delete']="DELETE";
translations['dialogue']="DIALOGUE";
translations['submit']="SUBMIT";
translations['slugline']="SLUGLINE";
translations['insert_element']="INSERT NEW ELEMENT";
translations['int']="INT.";
translations['ext']="EXT.";


var commentManager=new Object();
var mapManager=new Object();
var htmlStorage=new Object();
var editManager=new Object();
var actionManager=new Object();
var promptID=1;
var sceneID="2000-1";

function screenplay() {  }
screenplay.prototype=new penumbra();

g=new screenplay();

screenplay.prototype.cancelAction=function() {
	
	alert('Sorry! There was a problem with your request--we\'re not sure why! Please report what happened in the bug manager.\nIf your scene has not already automatically refreshed, please push the \"Refresh Scene\" button to the left of the screenplay.\n')
	this.refreshScene(null, actionManager) 		
}

screenplay.prototype.cleanUp=function(e) {
	
	for (element in editManager) {
		if (element !=e) {
			$(element + "_container").innerHTML=htmlStorage[element]
			delete htmlStorage[element]
			delete editManager[element]
		}
	}	
}

screenplay.prototype.mark=function(e) {
	
	if (e !=undefined) {
		if (htmlStorage !=undefined && editManager !=undefined && e !=undefined && $(e + "_container") !=undefined) {
			htmlStorage[e]=$(e + "_container").innerHTML
			editManager[e]=1;
		}
	}
}

screenplay.prototype.cancelEdit=function(e) { this.cleanUp(); }

screenplay.prototype.ee=function(e, autoEdit) {

	this.cleanUp(e)
	this.mark(e)
	
	strPieces=e.split('-')
	var eType=strPieces[0]
		
	var h="<div class='insertBox' style='width:360px; text-align:center;margin:0px 0px 0px 0px;'>";
		
	switch(eType){
		
		case '100':
		
			intext=$(e + "_intext").value
			loc=cleanStr($(e + "_loc").innerHTML)
			time=cleanStr($(e + "_time").innerHTML)
			h+=this.buildSlugline(e, intext, loc, time)			
			break;
			
		case '200':
			v=cleanStr($(e + "_content").innerHTML)

			h+=this.buildAction(e, v)			
			break;
			
		case '300':
			character=cleanStr($(e + "_character").innerHTML)
			dialogue=cleanStr($(e + "_dialogue").innerHTML)
			h+=this.buildDialogue(e, character, dialogue)			
			break;	
	}
	
	h+="<input type='hidden' id='" + e + "_insertType' value='0'>"
	h+="<input type='hidden' id='" + e + "_insertOrder' value=''>"
	
	var fn=e		
	h+=this.insertEditButtons(eType , e , 1, 1 , 0);
		
	h+="</div>";	
		
	$(e + "_container").innerHTML=h
	
	if (autoEdit !=1) {	urchinTracker('demo/edit_element');		}	
	if (eType==200) { pb.resize($(e)); } else if (eType==300) { pb.resize($(e + "_dialogue")); } 

	this.setFocus(e, eType, 1)
	
}

screenplay.prototype.chooseElement=function(eType , insertType , insertOrder) {
	
	var e="000-2000-1";
	var newElement=1;
	
	this.cleanUp()
	this.mark(e)
	
	var h="<div class='insertBox' style='text-align:center;margin-left:0px;width:370px;'>";
	
	switch(eType){		
		case '100':	h+=this.buildSlugline();	break;
		case '200':	h+=this.buildAction();	break;			
		case '300':	h+=this.buildDialogue(); break;	
	}
	
	showElementSwitch = (insertType==2) ? 1 : 0;

	h+=this.insertEditButtons(eType , e , 0, 0, showElementSwitch);
	h+="</div>";
	
	$("000-" + sceneID + "_container").innerHTML=h;	
	this.setFocus("000-" + sceneID, eType, 0);

}

screenplay.prototype.submitElement=function(e, eType) {	
	
	o=[]
	o.requestType='submitElement'
	o.e=this.edite=e
		
	elementPrefix=e.substr(0,3);
	
	o.newElement=(elementPrefix=='000' && o.insertType !=1) ? 1 : 0;	
	o.eType=lastEType=eType;
	
	switch(eType){
		
		case '100':		
			o.intext=$(e + "_intext").value
			o.content=$(e + "_loc").value
			o.time=$(e + "_time").value
			if (!isset(o.content)) { this.buildInput('100'); return;}
			break;
		case '200':				
			o.content=$(e).value; 	
			if (!isset(o.content)) { this.buildInput('200'); return;}
			break;
		case '300':
			o.character=$(e + "_character").value
			o.content=$(e + "_dialogue").value			
			if (!isset(o.character) || !isset(o.content)) { this.buildInput('300'); return;}			
			break;	
	}
			
	this.changeObj=o
	
	urchinTracker('demo/submit_element');	
	
	this.submitWait(e)			
	this.nextPrompt()
	
}

screenplay.prototype.nextPrompt=function() {

	++promptID

	if (isset($('prompt'))) {
		o={requestType:"getPrompt", pID:promptID};
		this.irequest(o,'g', 'promptReturn')
	}
}

screenplay.prototype.promptReturn=function(obj) {
	$('prompt').innerHTML=obj.responseText

}

screenplay.prototype.submitWait=function(e){
	$(e + "_container").innerHTML="<div class='box' style='width:300px;'>" + translations['saving'] + "</div>";
	this.submitReturn(e)
}


screenplay.prototype.submitReturn=function(e) {

	this.cancelEdit()	
	//if (promptID==1) { lastEType='200'; } //Overwrite for first submit
	
	if (e=="000-2000-1") {
		e=lastEType + "-" + (5 + promptID)
		h=''
		h+="<div class='cleaner'></div><div id='" + e + "_container'>"
		
		switch(lastEType){		
			case '100':	
				h+="<div class='slugline'><div class='content'><a href=\"javascript:g.ee('" + e + "')\"><input type='hidden' id='" + e + "_intext' value='" + o.intext + "'><span id='" + e + "_intext_text'>";
				h+=(o.intext==1) ?  translations['ext'] : translations['int'];
				h+="</span> <span id='" + e + "_loc'>" + o.content + "</span> ";
				h+=" 	&#8212; <span id='" + e + "_time'>" + o.time + "</span></a></div></div>";
				break;				
			case '200':
				h+="<div class='action'><a href=\"javascript:g.ee('" + e + "')\"><span id='" + e + "_content'></span></a></div>";
				break;			
			case '300': 
				h+="<div><div class='character'><a href=\"javascript:g.ee('" + e + "')\"><span id='" + e + "_character'></span></a></div><div class='dialogue'><a href=\"javascript:g.ee('" + e + "')\"><span id='" + e + "_dialogue'></span></a></div></div>";
				break;	
		}			
						
		h+="</div>";			
		$("screenplay_content").innerHTML+=h
		
	}
	
	this.rebuild(e);			
	this.buildInput(lastEType)
}

screenplay.prototype.buildInput=function(lastType) {
	switch(lastType){		
		case '100':	this.chooseElement('200' , 2 , 'null');	break;
		case '200':	this.chooseElement('200' , 2 , 'null');	break;			
		case '300': this.chooseElement('300' , 2 , 'null');	break;	
	}	
}


screenplay.prototype.rebuild=function(e) {
		
	if (o.eType==100) {
		
		if (o.intext==0) {
			$(e + "_intext").value="0";
			$(e + "_intext_text").innerHTML=translations['int'];
		} else {
			$(e + "_intext").value="1";
			$(e + "_intext_text").innerHTML=translations['ext'];
		}
		
		$(e + "_loc").innerHTML=o.content;
		$(e + "_time").innerHTML=o.time;
			
		o.intext=$(e + "_intext").value;

	} else if (o.eType==200) {
		
		$(e + "_content").innerHTML=o.content;
	
	} else if (o.eType==300) {
		
		$(e + "_character").innerHTML=o.character;
		$(e + "_dialogue").innerHTML=o.content;
	}
}


screenplay.prototype.buildSlugline=function(e, intext, loc, time) {
	
	if (!isset(e)) {
		var fn="000-" + sceneID
		var newElement=1;
	} else {
		var fn=e
	}
	
	if (!isset(htmlStorage[fn]) && $(fn + "_container") !=undefined){
		htmlStorage[fn]=$(fn + "_container").innerHTML;
	}
	
	if (!isset(intext)) {	intext='0'	}	
	if (!isset(loc)) { loc='' }	
	if (!isset(time)) { time=''	}
	
	if (intext==0) {
		selectInt='selected';		selectExt='';
	} else {
		selectInt='';	selectExt='selected';
	}
		
	var h="<select style=\"margin-top:2px;vertical-align:top;margin-right:10px;height:18px;\" name=\"" + fn + "_intext\" id=\"" + fn + "_intext\" tabindex='10'><option value=\"0\" " + selectInt + ">" + translations['int'] + "</option><option value=\"1\" " + selectExt + ">" + translations['ext'] + "</option></select>";	
	h+="<textArea name=\"" + fn + "_loc\" id=\"" + fn + "_loc\" tabindex='11' rows=\"1\" cols=\"25\" wrap=\"soft\" style=\"margin-top:0px;height:14px;\">" + loc + "</textArea> &#8212; ";
	h+="<textArea name=\"" + fn + "_time\" id=\"" + fn + "_time\" tabindex='12' rows=\"1\" cols=\"5\" wrap=\"soft\" style=\"margin-top:0px;height:14px;\">" + time + "</textArea>";

	return h;
	
}

screenplay.prototype.buildAction=function(e, v) {
	
	if (!isset(e) || !isset(v)) {
		var fn="000-" + sceneID
		var newElement=1;
	} else {
		var fn=e
	}
	
	if (!isset(v)) {	v=''; 	}
	
	var h="<textArea name=\"" + fn + "\" id=\""+ fn + "\" tabindex='10' rows=\"4\" cols=\"45\" wrap=\"soft\" style=\"margin-top:5px\" onkeyup=\"pb.resize(this)\">" + v + "</textArea>";
		
	return h;
	
}

screenplay.prototype.buildDialogue=function(e, character, dialogue) {
	
	if (e=='' || e==undefined) {
		var fn="000-" + sceneID
		var newElement=1;
	} else {
		var fn=e
	}
	
	if (!isset(character)) { character=''; 	}	
	if (!isset(dialogue)) {	dialogue='';	}
			
	var h="<textArea name=\"" + fn + "_character\" id=\"" + fn + "_character\"  tabindex='10' rows=\"1\" cols=\"20\" style='height:15px;' wrap=\"soft\">" + character + "</textArea>"
	
	h+="<div><textArea name=\"" + fn + "_dialogue\" id=\"" + fn + "_dialogue\"  tabindex='11' rows=\"2\" cols=\"40\" wrap=\"soft\" onkeyup=\"pb.resize(this)\">" + dialogue + "</textArea></div>"
		
	return h;
	
}

screenplay.prototype.setFocus=function(e, eType, editType) {

	switch(eType){
		case '100':	$(e + "_loc").focus();	break;
		case '200':	$(e).focus();	break;			
		case '300':	if (editType==1) {	$(e + "_dialogue").focus();		} else {	$(e + "_character").focus();	}		break;	
	}	
	
}

screenplay.prototype.insertInput=function(e, lastSceneOrder) {

	this.cleanUp();

	var nextSceneOrder=lastSceneOrder+1
	
	var insertOrder=null;
	var insertType=0;
	
	editManager[e]=1
	htmlStorage[e]=$(e + "_container").innerHTML
	
	var h="<div id='000-" + sceneID + "_container' style='margin-bottom:20px;'><div class='box' style='margin-left:20px;margin-right:20px;padding:10px;'><div>Select an element type.</div><div style='text-align:center;margin-top:5px;'><input onclick=\"g.chooseElement('" + mapID +  "', '" + sceneID +  "', '100' , '" + insertType + "' , '" + insertOrder + "')\" type='submit' value='SLUGLINE' name='slugline' class='submitButton'  style='width:150px;'/> <input onclick=\"g.chooseElement('" + mapID +  "', '" + sceneID +  "', '200' , '" + insertType + "' , '" + insertOrder + "')\" type='submit' value='ACTION' name='action' class='submitButton'  style='width:150px;'/> <input onclick=\"g.chooseElement('" + mapID +  "', '" + sceneID +  "', '300' , '" + insertType + "' , '" + insertOrder + "')\" type='submit' value='DIALOG' name='dialogue' class='submitButton'  style='width:150px;'/></div></div></div>"
	
	$(e + "_container").innerHTML=h
	
}

screenplay.prototype.insertEditButtons=function(eType , e , showDelete , showInsert , showSwitch) {
	
	switch(eType){
		
		case '100':	var tabNum=13; break;
		case '200':	var tabNum=12; break;	
		case '300':	var tabNum=12; break;	
	}	
	
	var h="<div style='margin-top:5px;'>"
	h+="<input onclick=\"g.submitElement('"  + e + "','" + eType + "')\" tabindex='" + tabNum++ + "' type='submit' value='" + translations['submit'] + "' name='action' class='submitButton'  style='width:150px;'/> "
	h+=" <input onclick=\"g.cancelEdit('" + e +  "')\" tabindex='" + tabNum++ + "' type='submit' value='" + translations['cancel'] + "' name='cancel' class='submitButton grey_button'  style='width:150px;'/>";
	h+="</div>";
		
	if (showSwitch==1) {
		
		insertType=2;
		insertOrder=null
		
		h+="<div style='margin-top:5px'>";
		
		if (eType !=100) {
			h+="<input onclick=\"g.chooseElement('100' , '" + insertType + "' , '" + insertOrder + "')\" tabindex='" + tabNum++ + "' type='submit' value='" + translations['slugline'] + "' name='slugline' class='submitButton green'  style='width:150px;'/> "
		}
		
		if (eType !=200) {			
			h+="<input onclick=\"g.chooseElement('200' , '" + insertType + "' , '" + insertOrder + "')\" tabindex='" + tabNum++ + "' type='submit' value='" + translations['action'] + "' name='action' class='submitButton green'  style='width:150px;'/> " 
		}
		
		if (eType !=300) {			
			h+="<input onclick=\"g.chooseElement('300' , '" + insertType + "' , '" + insertOrder + "')\" tabindex='" + tabNum++ + "' type='submit' value='" + translations['dialogue'] + "' name='dialogue' class='submitButton green'  style='width:150px;'/>"
		}
		h+="</div> "
	}	
	
	return h;
	
}

