// JavaScript Document
// Utility Functions

// compares the height of two elements and sets them both to the highest value
function matchHeight(element1, element2){
	if ($('#'+element1).hasClass('maxHeight')){
		$('#'+element1).removeClass('maxHeight').removeAttr('style');
	}
	if ($('#'+element2).hasClass('maxHeight')){
		$('#'+element2).removeClass('maxHeight').removeAttr('style');
	}
	
	var max_height = $('#'+element1).height();
	if ($('#'+element2).height() > max_height) { max_height = $('#'+element2).height(); }
	
	$('#'+element1).height(max_height);
	$('#'+element2).height(max_height);	
	$('#leftedge').height(max_height);
	$('#rightedge').height(max_height);
	$('#leftedge').addClass('maxHeight');
	$('#rightedge').addClass('maxHeight');
	$('#'+element1).addClass('maxHeight');
	$('#'+element2).addClass('maxHeight');
}

function updateResults(resultStr){	
	
										
		displayResults(resultStr);
}

function displayResults(resultStr){
	$('#results').html(resultStr);
	matchHeight('main', 'sidebar');
}

function AddDb(action){
	if(action == "test"){
		var foo = AddDb.arguments[1];
		var bar = AddDb.arguments[2];
		var bazz = AddDb.arguments[3];
		
		PostTest(foo, bar, bazz);
	}
	
	if(action == "article"){
		var type = AddDb.arguments[1];
		var title = AddDb.arguments[2];
		var content = AddDb.arguments[3];
		PostArticle(type, title, content);		
	}
}

function EditDb(action, row){
	if(action == "test"){
		var foo = EditDb.arguments[2];
		var bar = EditDb.arguments[3];
		var bazz = EditDb.arguments[4];
		
		EditTest(row, foo, bar, bazz);
	}
	
	if(action == "article"){
		var type = EditDb.arguments[2];
		var title = EditDb.arguments[3];
		var content = EditDb.arguments[4];
		
		EditArticle(row, type, title, content);
	}
}

function DeleteDb(action, row){
		DeleteData(action, row);
}

function LoadModules(result){
	var content = "";
	var links = "";
	
	for(i=0;i<result.length;i++){
		content = content + createModule (i, result) + "<br />";
		links = links + '<li><a href="#mod_' + i + '">' + result[i].title + '</a></li>';
	}
	
	$('#content').html(content);
	$('#latest').html(links);
	
	matchHeight('main', 'sidebar');
}

function createModule(id, result){
	
	var title = result[i].title;
	var content = result[i].content;
	var date = result[i].posted_date;
	
	var module = ' \
	<div id="mod_' + id + '" class="mod"> \
  	<div class="mod_header"> \
    	<div> \
      	<div class="infoLeft">' + title + '</div> \
    	  <div class="infoRight">' + date + '</div>&nbsp; \
  	  </div> \
	  </div> \
		<br /> \
		<div class="content">' + content + '</div> \
		<br /> \
		<br /> \
	</div>';
	
	return module;
}
