
var element;

function showDesc(response){
	output = response.split("$|$");
	if(output.length>1){
		var title = output[0];
		var preco = output[1];
		response = output[2];
		var div = document.createElement('div');
		div.style.display = 'none';
		div.className = 'desc';
		div.innerHTML = "<h3>"+title+"</h3>"+"<p class='pre'>"+preco+"</p><p>"+response+"</p><p><a class=\"hide\" onclick='hideDesc(this)'>Hide Description</a></p>";
	// in unable to find course description
	} else {
		var div = document.createElement('div');
		div.style.display = 'none';
		div.className = 'desc';
		div.innerHTML = "<p><strong>"+response+"</strong></p><p><a class=\"hide\" onclick='hideDesc(this)'>Hide Description</a></p>";
	}

	$(element).parent().get(0).appendChild(div);
	$('.desc').show("slow");
}
function getDesc(course){
	x_getDesc(course,showDesc);	//initiate sajax (php function located in ajax.php
}
function hideDesc(element){
	var p = $(element).parent().get(0);
	var div = $(p).parent().get(0)
	$(div).hide("slow");
}

function initialize() {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map_canvas"));
		map.setCenter(new GLatLng(35.785671,-78.667978),15);
			
		var point = new GLatLng(35.785671,-78.667978);
		var marker = new GMarker(point);
		var html = '<div style="height: 120px;"><h4 style="margin: 0px; padding: 0px;">IMSE Institute</h4><div style="font-size: .9em;">311 Daniels Hall<br />111 Lampe Drive<br />North Carolina State University<br />Raleigh, NC 27698</div></div>';
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(html);
		});

		map.addOverlay(marker);
		marker.openInfoWindowHtml(html);
		map.setUIToDefault();
		
	}
}

function activateform(){		
	var code = document.getElementById("captcha_response");
	var form = document.getElementById("contactform");
	var name = document.getElementById("name");
	var email = document.getElementById("email");
	var message = document.getElementById("message");
	var error = false;
			
	if(name){
		name.onfocus = function(){
			if(name.value == "Name"){
				name.value = "";
			}
				
			name.style.fontStyle = "normal";
			name.style.color = "#000000";
			name.style.backgroundColor = "#ffffcc";	
					
		};
			
		name.onblur = function(){
			name.style.fontStyle = "italic";
			name.style.color = "#666666";
			name.style.backgroundColor = "#eeeeee";	
					
			if(name.value == ""){
				name.value = "Name";						
			}					
					
		};
	}
			
			
	if(email){
		email.onfocus = function(){
			if(email.value == "Email"){
				email.value = "";
			}
					
			email.style.fontStyle = "normal";
			email.style.color = "#000000";
			email.style.backgroundColor = "#ffffcc";				
		};
				
		email.onblur = function(){					
			email.style.fontStyle = "italic";
			email.style.color = "#666666";
			email.style.backgroundColor = "#eeeeee";
					
			if(email.value == ""){
				email.value = "Email";						
			}						
		};
	}
			
			
	if(message){
		message.onfocus = function(){
			if(message.value == "Please type your message here ..."){
				message.value = "";
			}
					
			message.style.fontStyle = "normal";
			message.style.color = "#000000";
			message.style.backgroundColor = "#ffffcc";	
		};
				
		message.onblur = function(){					
			message.style.fontStyle = "italic";
			message.style.color = "#666666";
			message.style.backgroundColor = "#eeeeee";	
					
			if(message.value == ""){
				message.value = "Please type your message here ...";						
			}						
		};
	}
		
	if(code){
		code.onfocus = function(){					
			code.style.fontStyle = "normal";
			code.style.color = "#000000";
			code.style.backgroundColor = "#ffffcc";						
		};
				
		code.onblur = function(){
			code.style.fontStyle = "italic";
			code.style.color = "#666666";
			code.style.backgroundColor = "#eeeeee";	
		};
	}
	
	form.onsubmit = function(){
		var finished = false;
		var url = 'code=' + code.value;
		url += '&name=' + name.value;
		url += '&email=' + email.value;	
		
		//check if captcha is correct
		$.ajax({
			type:	"POST",
			url:   	"securimage/check_captcha.php?" + url,
			async: 	false,
			success: function(transport){
				var resp = transport;
				if(resp != "true"){								
					var errordiv = document.getElementById("error");
					var errmsg 	 = "The following items require attention before submission:<br />";
					errmsg 		+= resp;
					errordiv.innerHTML = errmsg;
				} else {
					finished = true;
				}
			}// end success
		});
				
		return finished;
	}// end function()	
}// end activateform

function startup(){						 
	$(".coreLink").click(function () {
	   var li = $(this).parent().get(0);
		var elmClass = $(li).attr("class");
		if(elmClass == "class"){
			li = $(li).parent().get(0);	
			element = $(this).parent().get(0);
		} else {
			element = $(this);	
		}
		var class2 = $(li).attr("class");
		var children = $(li).children();
		if(children.length<3){
			clasz = $(this).attr("class");
			clasz = clasz.split(" ");
			var classArr = clasz[1].split("-");
			var course = classArr[0].toUpperCase()+" "+classArr[1];			
			getDesc(course);
		} else {
			$(children[2]).hide("slow",function(){$(children[2]).remove();});		
		}
	});
	
}

function getMap(){ 
	if($("#map_canvas").css("display") == "block"){
		$("#map_canvas").slideUp("meduim");	
		$("#hideMap").slideUp("medium");
	} else {
		$("#map_canvas").slideDown("medium",function(){ $("#map_canvas").css("background","url(../img/loading.gif) no-repeat center"); setTimeout("initialize();",1000); });
		$("#hideMap").slideDown("meduim");
	}
}

function nav(){
	$("#navigation ul li").mouseover(function(){$(this).addClass("hover");});
	$("#navigation ul li").mouseout(function(){$(this).removeClass("hover");});
	$("#navigation li.drop").mouseover(function(){$("#navigation ul").css("display","block");});
	$("#navigation li.drop").mouseout(function(){$("#navigation ul").css("display","none");});	
}


window.onload=function(){
	startup();
	var form = document.getElementById("contactform");
	if(form)	activateform();
	var tables = document.getElementsByTagName("table");
	if(tables.length > 0){
		$(".directory").tablesorter({widgets:['zebra'],sortList: [[0,0]]});
		$(".directory tbody tr").mouseover(function(){$(this).addClass("over");});
		$(".directory tbody tr").mouseout(function(){$(this).removeClass("over");});	
	}
	
	nav();
	if($("table.students").length > 0){
		var rows = $("table tbody tr");
		var length = rows.length;
		var cities = $("table.students td:last-child");
		for(var i = 0; i < length; i++){
			var city = $(cities[i]).html();
			var url = "<a target=\"_blank\" href=\"http://maps.google.com/?q=" + $(cities[i]).html() + "\">" + city + "</a>";	
			$(cities[i]).html(url);
		}
	}
	
};




    



