//js for pageEditor pages		

function changeSequence(event){
	if (event.target.id.substr(0,2) == "go"){
		var tableId = event.target.id.substr(2);
		var seqInput = "sequenceInput"+articleId; 
		var newValue = $('seqForm'+tableId).sequenceInput.value;
		var articleId = $('seqForm'+tableId).sequenceInput.getAttribute('articleId');
		//off to Ajax we go
		//scrape user info from the hidden divs
		var loginId = $('loginId').innerHTML;
		var loginKey = $('loginKey').innerHTML;
		var url = "../php/updateArticleSequence.php";
		new Ajax.Request (url,{
			method: 'post',
			parameters: {
				tableId: tableId,
				loginId: loginId,
				loginKey: loginKey,
				articleId: articleId,
				newValue: newValue
			},
			onSuccess: function(transport){
				//change value in the modalbox
				$('seqDisp'+tableId).update(newValue);
				//clear the inputbox
				$('seqForm'+tableId).sequenceInput.clear();
			},
			onFailure: function(transport){
			}
		});
	}
}

function closeNewArticle(){
	//hide the new article div
	Effect.Fade('newArticleDiv');
	//show the articles div
	Effect.Appear('articles',{queue: 'end'});
}	

function deleteArticle(id){
	Modalbox.hide();
	//scrape user info from the hidden divs
	var loginId = $('loginId').innerHTML;
	var loginKey = $('loginKey').innerHTML;
	var url = "../php/deleteArticle.php";
	new Ajax.Request(url,{
		method: 'post',
		parameters: {
			loginId: loginId ,
			loginKey: loginKey,
			newsId: id
		},
		onSuccess: function(transport){
			if(transport.responseText == 1){
				//reload page
				window.location="news.php";
			}
		},
		onFailure: function(transport){
		}
	});
}

function displayAddArticle(){
	//hide the other articles
	$('articles').setStyle('display:none;');
	
	var addEditor = new FCKeditor('newArticle');
	addEditor.BasePath = "../../fckeditor/";
	addEditor.ToolbarSet = "designGarage";
	addEditor.Height = 400;
	addEditor.Value = "enter new article";
	//place it
	$('newArticleForm').innerHTML = addEditor.CreateHtml();
	
	//build a spacer
	var addArticleSpacer = new Element('div').addClassName('addArticleSpacer');
	$('newArticleForm').appendChild(addArticleSpacer);
	
	//build the title label
	var titleLabel = new Element ('label',{
		'id' : 'newTitleLabel',
		'for': 'newTitleInput'
	});
	$('newArticleForm').appendChild(titleLabel);
	$('newTitleLabel').innerHTML = 'Title: ';
	
	//add input for the title
	var titleInput = new Element ('input',{
		'id':'newTitleInput',
		'name':'newTitleInput',
		'size': '27',
		'type':'text'
	});
	$('newArticleForm').appendChild(titleInput);
	
	//build the name label
	var nameLabel = new Element ('label',{
		'id' : 'newNameLabel',
		'for': 'newNameInput'
	}).addClassName('articleEditLabel');
	$('newArticleForm').appendChild(nameLabel);
	$('newNameLabel').innerHTML = 'Name: ';
	
	//add input for the name
	var nameInput = new Element ('input',{
		'id':'newNameInput',
		'name':'newNameInput',
		size: 25,
		'type':'text'
	});
	$('newArticleForm').appendChild(nameInput);
	
	//build the sequence label for input
	var sequenceLabel = new Element ('label',{
		'id' : 'newSequenceLabel',
		'for': 'newSequenceInput'
	}).addClassName('articleEditLabel');
	$('newArticleForm').appendChild(sequenceLabel);
	$('newSequenceLabel').innerHTML = 'Sequence: ';
	
	//add input for the sequence
	var sequenceInput = new Element ('input',{
		'id':'newSequenceInput',
		'name':'newSequenceInput',
		'type':'text',
		'size': '2'
	});
	$('newArticleForm').appendChild(sequenceInput);
	
	//build a spacer
	var addArticleSpacer = new Element('div').addClassName('addArticleSpacer');
	$('newArticleForm').appendChild(addArticleSpacer);
	
	//show it
	Effect.Appear('newArticleDiv');
}

function fireEditor(id){
	var newsId = id;

	//hide all the other articles	
	var articleArr = $$('div.article');
	
	for(var i = 0; i < articleArr.length; i++){
		var articleId =  articleArr[i].id.substr(7);
		if(articleId != newsId){
		articleArr[i].setStyle("display: none");
		}
	}
	
	
	//instantiate an FCK editor
	var oFCKeditor = new FCKeditor( 'editor'+newsId ) ;
	oFCKeditor.BasePath = "../../fckeditor/";
	oFCKeditor.ToolbarSet = "designGarage";
	oFCKeditor.Height = 400;
	//load it with the contents of the presentation div
	oFCKeditor.Value = $('articleHtml'+newsId).innerHTML;
	//place it
	$('articleForm' + newsId).innerHTML = oFCKeditor.CreateHtml();
	//build the hidden input with newsId
	var hidden = new Element ('input',{
		'type':'hidden',
		'name':'editId',
		'value': newsId				
	});
	//append it to the form
	$('articleForm' + newsId).appendChild(hidden);
	
	//build a spacer
	var addArticleSpacer = new Element('div').addClassName('addArticleSpacer');
	$('articleForm' + newsId).appendChild(addArticleSpacer);
	
	//add the label and input for the title
	var titleLabel = new Element ('label',{
		'id' : 'titleLabel',
		'for': 'titleInput'
	});
	$('articleForm' + newsId).appendChild(titleLabel);
	$('titleLabel').innerHTML = 'Title: '
	
	//add the label and input for the title
	var articleTitle = $('articleTitle'+newsId).innerHTML;
	var titleInput = new Element ('input',{
		'id':'titleInput',
		'name':'titleInput',
		size: 27,
		'type':'text',
		'value': articleTitle
	});
	$('articleForm' + newsId).appendChild(titleInput);
	
	//build the name label
	var nameLabel = new Element ('label',{
		'id' : 'nameLabel',
		'for': 'nameInput'
	}).addClassName('articleEditLabel');
	$('articleForm' + newsId).appendChild(nameLabel);
	$('nameLabel').innerHTML = 'Name: ';
	
	//add input for the name
	var articleName = $('articleName'+newsId).innerHTML;
	var nameInput = new Element ('input',{
		'id':'nameInput',
		'name':'nameInput',
		size: 25,
		'value':articleName,
		'type':'text'
	});
	$('articleForm' + newsId).appendChild(nameInput);
	
	//build the sequence input label
	var sequenceLabel = new Element ('label',{
		'id' : 'sequenceLabel',
		'for': 'sequenceInput'
	}).addClassName('articleEditLabel');
	$('articleForm' + newsId).appendChild(sequenceLabel);
	$('sequenceLabel').innerHTML = 'Sequence: ';
	
	//add input for the sequence
	var sequence = $('sequence'+newsId).innerHTML
	var sequenceInput = new Element ('input',{
		'id':'sequenceInput',
		'name':'sequenceInput',
		value: sequence,
		'type':'text',
		'size': '2'
	});
	$('articleForm' + newsId).appendChild(sequenceInput);
	
	//build a spacer
	var addArticleSpacer = new Element('div').addClassName('addArticleSpacer');
	$('articleForm' + newsId).appendChild(addArticleSpacer);
	
	//present the editor form, hide the presentation div
	Effect.Fade('articleDisplay'+newsId);
	Effect.Appear('articleEdit'+newsId , { queue: 'end' });
}

function editorCancel(id){
	var newsId = id;
	//hide the new editor div
	$('articleEdit'+newsId).setStyle('display: none;');
	Effect.Appear('articleDisplay'+newsId);
	
	//show all article divs
	//hide all the other articles	
	var articleArr = $$('div.article');
	for(var i = 0; i < articleArr.length; i++){
		articleArr[i].setStyle("display: block");
	}
}

function generateSequencer(pageName){
	var page = pageName;
	var url = "../php/getArticleSequences.php";
	//http request to get json of titles and sequences
	
	new Ajax.Request(url, {
		method: 'post',
		
		parameters: {
			page: page
		},
		onSuccess: function(transport) {
			var arr = transport.responseText;
			var json = eval(arr);
			//build the table
			var node = new Element('div',{id: 'sequenceContainer'})
			.insert(
			new Element('table',{id: 'sequenceTable'}).addClassName('sequenceTable')
			).insert(
			new Element('tr')
			).insert(
			new Element('th').addClassName('sequenceHeader').update("Article Name")
			).insert(
			new Element('th').addClassName('sequenceHeader').update("Sequence")
			).insert(
			new Element('th').addClassName('sequenceHeader').update("New Value")
			).insert(
			new Element('th').addClassName('sequenceHeader').update("")
			);
			for(var i = 0 ; i < json.length ; i++){
				//determine whether it's an odd or even row for alternate styling
				var j = i/2;
				if(isInteger(j) == true){
					var tdClass = "td1";
				}else{
					var tdClass = "td2";
				}
			
				//build a table row
				var row = new Element('tr')
				var articleName = new Element('td').addClassName(tdClass).update(json[i].name);
				var articleSequence = new Element('td',{id: 'seqDisp'+i}).addClassName(tdClass).update(json[i].sequence);
				var seqForm = new Element('td').addClassName(tdClass).insert(new Element('form',{id:'seqForm'+i}).insert(new Element('input',{type: 'text', size: '3',articleId: json[i].articleId, name:'sequenceInput'})));
				//var sequenceGo = new Element('input',{id : 'go'+i, type:'button',value:'Go'});
				var sequenceGo = new Element('td').addClassName(tdClass).insert(new Element('input',{id: 'go'+i, type:'button',value:'Go'}));
				row.insert(articleName).insert(articleSequence).insert(seqForm).insert(sequenceGo);
				
				node.appendChild(row);
			}
			//add a spacer
			var spacer = new Element('div').addClassName('sequenceSpacer');
			node.appendChild(spacer);
			
			//add the refresh option
			var refresh = new Element('input',{
				type: 'button',
				value: 'Refresh Page',
				onClick: 'refresh()'
			});
			node.appendChild(refresh);
			Modalbox.show(node,{title: 'Change Article Order', width: 400, afterLoad: setObserver, onHide: removeObserver});return false;
		},
		onFailure: function(transport) {
		}
	});
}

function hideEditOptions(){
	//hide edit divs on the articles
	var arr = $$('div.articleOptions');
		for(var i = 0; i < arr.length; i++){
			arr[i].setStyle("display: none");
		}
	
	//show the show button
	$('showEditOptions').setStyle('display: inline;');
	//hide the hide button
	$('hideEditOptions').setStyle('display: none;');
		
}

function showEditOptions() {
	//hide edit divs on the articles
	var arr = $$('div.articleOptions');
		for(var i = 0; i < arr.length; i++){
			arr[i].setStyle("display: block");
	}
	
	//show the hide button
	$('hideEditOptions').setStyle('display: inline;');
	
	//hide the show button
	$('showEditOptions').setStyle('display: none;');
}


function isInteger(i){
	var a = parseInt(i);
	var b = parseFloat(i);
	return (a == b);
}
function refresh(){
	window.location.reload(true);
}

function removeObserver(){
	$('sequenceContainer').stopObserving('click',changeSequence);
}

function setObserver(){								
	$('sequenceContainer').observe('click',changeSequence);
}	



