// JavaScript Document

// Load any AJAX content whenever document loads or hash tag changes
window.onhashchange = loadContent;

$(document).ready(function() {
	loadContent();
});

var loc;

// Load content through calling AJAX functions
function loadContent() {
		var currentHash = window.location.hash;
		loc = window.location+"";
		
		// blank hash cases
		if(currentHash==""){
			// intro pages
			if(loc.indexOf("news")>0){
				loadPage("getNews.php?limit=3", "news");
				loadCountdown();
			} else if(loc.indexOf("member")>0){
				loadPage("member_comps.php", "contentpanel");
				document.getElementById('comps').className = "chosen";
			} else if(loc.indexOf("comps")>0){
				loadPage("comps_intro.php", "content");
			} else if(loc.indexOf("projs")>0){
				loadPage("projs_intro.php", "content");
			} else if(loc.indexOf("conf")>0){
				loadPage("conf_intro.php", "content");
			} else if(loc.indexOf("about")>0){
				loadPage("about_intro.php", "content");
			} else if(loc.indexOf("media")>0){
				loadPage("media_intro.php", "content");
			}
			
			return;
		}
		
		currentHash = currentHash.substr(1); // eliminate the first "#" sign
		
		loadPage(currentHash+"", "content"); // load page into '#content' div
	}
	
var target;
var request;
	
function loadPage(file, element)	{
	document.body.style.cursor = "wait";
	target = document.getElementById(element);
	request = createXMLHttpObject();
	request.onreadystatechange = loadFile;
	request.open("GET",file+"",true);
	request.send(null);
}

function createXMLHttpObject()	{
	var xmlhttp;
	if (window.XMLHttpRequest)
		xmlhttp = new XMLHttpRequest();
	else
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	return xmlhttp;
}

function loadFile()	{
	if (request.readyState==4 && request.status==200)	{
		target.innerHTML = request.responseText;
		
		//load jQuery plugins
		if(loc.indexOf("comps")>0){
			loadFancyBox();
		}
		if(loc.indexOf("media")>0){
			loadMediaGallery();
		}
		if(loc.indexOf("officers")>0){
			loadAnythingSlider();
		}
		if(loc.indexOf("history")>0){
			compsHistory();
		}
		document.body.style.cursor = "auto";
	}
}


var date, date2;
var date1True, difftemp;

function loadCountdown()	{
	if(date1True == 1) {
			date = new Date();
			// month here is 0-11
			date.setFullYear(2012, 1, 25);
			date.setHours(13 , 0, 0, 0);
	}
	
	else  {
		date2 = new Date();
		//month here is 0-11
		date2.setFullYear(2012, 1, 23);
		date2.setHours(13, 00, 0, 0);
	}
	var t = setInterval("countDown()", 1000);
}


function countDown() {
	var text = ""; 
	var current = new Date();
	var difference, closerDate;
	if(date1True == 1) {
		difference = date - current;
		//difftemp = 0;
		//loadCountdown();
		//closerDate = date2 - current;
	}
	else {
		difference = date2 - current;
		//difftemp = 1;
		//loadCountdown();
		//closerDate = date - current;
	}	
	
	difference = Math.floor(difference / 1000);
	//closerDate = Math.floor(closerDate / 1000);
	
	/*if(closerDate < differenece) {
		if(date1True == 1) {
			date1True = 0;
		}
		else {
			date1True = 1;
		}
	}
	else {
		if(date1True == 1) {
			date1True = 0;
		}
		else {
			date1True = 1;
		}
	}*/
	
	if(difference <= 0) {
		if(date1True == 1) {
			date1True = 0;
		}
		else {
			date1True = 1;
		}
		loadCountdown();
		//text += "Right now is the"
	}
	else {
		var days = Math.floor(difference / (60 * 60 * 24));
		difference -= days * 24 * 60 * 60;
		var hours = Math.floor(difference / (60 * 60));
		difference -= hours * 60 * 60;
		var minutes = Math.floor(difference / 60);
		difference -= minutes * 60;
		var seconds = difference;
		
		
		text += "" + getText(Math.abs(days), "day");
		text += ", " + getText(Math.abs(hours), "hour");
		text += ", " + getText(Math.abs(minutes), "minute");
		text += ", " + getText(Math.abs(seconds), "second") + " until ";
	}
	loadEvent();
	document.getElementById('count').innerHTML = text;
}

function getText(time, text) {
	if (time == 1) {
		return time + " " + text;
	} else {
		return time + " " + text + "s";
	}
}
function loadEvent() {
	var eventText="";
	if(date1True == 1) {
		eventText = " Study Session";
	}
	else {
		eventText = " Study Session";
	}
	document.getElementById('event').innerHTML = eventText;
}
function loadFancyBox() {
	$("a.fancybox").fancybox({
		'type'				: 'inline',
		'autoDimensions': false,
		'titlePosition'		: 'inside',
		'transitionIn'		: 'none',
		'transitionOut'	: 'none'
	});
}

function loadMediaGallery() {
	$("a[rel=example_group]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
		    return '<span id="fancybox-title-over">Image ' +  (currentIndex + 1) + ' / ' + currentArray.length + ' ' + title + '</span>';
		}
	});
}

function loadAnythingSlider() {
	$('#officerinfo').anythingSlider({
		width               : 650,       // if resizeContent is false, this is the default width if panel size is not defined
		height              : 500,       // if resizeContent is false, this is the default height if panel size is not defined
		resizeContents      : false,     // If true, solitary images/objects in the panel will expand to fit the viewport
		autoPlay            : false,     // This turns off the entire slideshow FUNCTIONALY, not just if it starts running or not
		navigationFormatter : false, // Format navigation labels with text
		buildNavigation     : false,      // If true, builds a list of anchor links to link to each panel
		buildStartStop      : false //if true, builds the start - stop button
	})
	$("#laura_yang").click(function(){
		$('#officerinfo').anythingSlider(1);
	});
	$("#ryan_chui").click(function(){
		$('#officerinfo').anythingSlider(2);
	});
	$("#mallika_kapur").click(function(){
		$('#officerinfo').anythingSlider(3);
	});
	$("#emily_wong").click(function(){
		$('#officerinfo').anythingSlider(4);
	});
	$("#larry_xu").click(function(){
		$('#officerinfo').anythingSlider(5);
	});
	$("#laura_liu").click(function(){
		$('#officerinfo').anythingSlider(6);
	});
	$("#keshav_santhanam").click(function(){
		$('#officerinfo').anythingSlider(7);
	});
	$("#nikhil_mallipeddi").click(function(){
		$('#officerinfo').anythingSlider(8);
	});
	$("#yanbing_zhu").click(function(){
		$('#officerinfo').anythingSlider(9);
	});
	$("#barak_gila").click(function(){
		$('#officerinfo').anythingSlider(10);
	});
	$("#srisruthi_ramesh").click(function(){
		$('#officerinfo').anythingSlider(11);
	});
	$("#srinivasan_srikumar").click(function(){
		$('#officerinfo').anythingSlider(12);
	});
	$("#patrick_xie").click(function(){
		$('#officerinfo').anythingSlider(13);
	});
	$("#nikitas_kanellakopoulos").click(function(){
		$('#officerinfo').anythingSlider(14);
	});
	$("#minolee_vora").click(function(){
		$('#officerinfo').anythingSlider(15);
	});
	$("#sihua_xu").click(function(){
		$('#officerinfo').anythingSlider(16);
	});
}

function compsHistory()	{
	document.body.style.cursor = "wait";
	var params = "";
	
	var conferenceElement = document.getElementById("conference");
	var yearElement = document.getElementById("year");
	var placeElement = document.getElementById("place");
	
	var conference = conferenceElement.options[conferenceElement.selectedIndex].value;
	var year = yearElement.options[yearElement.selectedIndex].value;
	var place = placeElement.options[placeElement.selectedIndex].value;
	
	var evt = document.getElementById("event").value;
	var name = document.getElementById("name").value;
	
	params = "conference="+conference+"&year="+year+"&event="+evt+"&place="+place+"&name="+name;
	
	request = createXMLHttpObject();
	request.open("GET", "comps_history_submit.php?"+params, true);
	request.onreadystatechange = compsHistoryResponse;
	request.send(null);
}

function compsHistoryResponse(){
	if (request.readyState==4 && request.status==200){
		var response = request.responseText;
		document.getElementById("historyTable").innerHTML = response;
		document.body.style.cursor = "auto";
	}
}

/*function loginCheck(){
	var pass = true;
	var wrong = "";
	
	var x=document.getElementById("user").value;
	var atpos=x.indexOf("@");
	var dotpos=x.lastIndexOf(".");
	if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length){
		pass = false;
		wrong += "Invalid username";
	}
	
	var y=document.getElementById("pass").value;
	if(y.length < 6 || y.length > 20) {
		pass = false;
	}
	//alert(y);
	if(!pass){
		document.getElementById("error").innerHTML="Some of your entries did not pass validation. Please double-check all the fields to make sure you did not make a mistake and try again.";
		return;
	}
	
	document.getElementById("loginform").submit();
	return;
}*/

function loadPanel(panel) {
	var file = "member_" + panel + ".php";
	loadPage(file, "contentpanel");
	var nav = document.getElementById(panel);
	clearBackgrounds();
	nav.className = "chosen";
}

function clearBackgrounds()	{
	document.getElementById('comps').className = "";
	document.getElementById('events').className = "";
	/*document.getElementById('points').className = "";
	document.getElementById('projects').className = "";*/
	document.getElementById('awards').className = "";
	document.getElementById('profile').className = "";
}

function focusLink()	{
	var searchbar = document.getElementById("link");
	if(searchbar.value == "http://")	{
		searchbar.value = "";
		searchbar.style.color = "#000000";
	}
}

function blurLink()	{
	var searchbar = document.getElementById("link");
	if(searchbar.value == "")	{
		searchbar.value = "http://";
		searchbar.style.color = "#999999";
	}
}

function searchComps()	{
	var searchbar = document.getElementById("compslist").value;
	if(searchbar != "")	{
		document.body.style.cursor = "wait";
		var params = "";
		
		params = "comp="+searchbar;
		
		request = createXMLHttpObject();
		request.open("GET", "comps_resources.php?"+params, true);
		request.onreadystatechange = compsResourcesResponse;
		request.send(null);
	}
}

function compsResourcesResponse(){
	if (request.readyState==4 && request.status==200){
		var response = request.responseText;
		document.getElementById("resourcespanel").innerHTML = response;
		document.body.style.cursor = "auto";
	}
}

function submitResource()	{
	var comp = document.getElementById("insertlist").value;
	var name = document.getElementById("description").value;
	var url = document.getElementById("link").value;
	if(comp != "" && name != "" && url != "" && url != "http://" )	{
		document.body.style.cursor = "wait";
		var params = "";
		
		params = "comp="+comp+"&name="+name+"&url="+url;
		
		request = createXMLHttpObject();
		request.open("GET", "comps_resources_submit.php?"+params, true);
		request.onreadystatechange = compsResourcesSubmitResponse;
		request.send(null);
	}
	else
		document.getElementById("message").innerHTML = "<font color='red'>Your resource was not submitted correctly.</font>";
}

function compsResourcesSubmitResponse(){
	if (request.readyState==4 && request.status==200){
		var response = request.responseText;
		document.getElementById("message").innerHTML = response;
		document.body.style.cursor = "auto";
	}
}

function changeProfile()	{
	var address = document.getElementById("address").value;
	var cellphone = document.getElementById("cell").value;
	var homephone = document.getElementById("home").value;
	var third = document.getElementById("third").value;
	var business = document.getElementById("business").value;
	if(address != "" && cellphone != "" && homephone != "" && third != "" && business != "")	{
		document.body.style.cursor = "wait";
		var params = "";
		
		params = "address="+address+"&cell="+cellphone+"&home="+homephone+"&third="+third+"&business="+business;
		
		request = createXMLHttpObject();
		request.open("GET", "profile_change.php?"+params, true);
		request.onreadystatechange = profileChangeResponse;
		request.send(null);
	}
	else
		document.getElementById("message").innerHTML = "<font color='red'>Please fill out all of the fields.</font>";
}

function profileChangeResponse(){
	if (request.readyState==4 && request.status==200){
		var response = request.responseText;
		document.getElementById("message").innerHTML = response;
		document.body.style.cursor = "auto";
	}
}

function changePass()	{
	var oldpass = document.getElementById("oldpass").value;
	var newpass = document.getElementById("newpass").value;
	var checkpass = document.getElementById("checkpass").value;
	if(oldpass != "" && newpass != "" && checkpass != "" && newpass == checkpass)	{
		document.body.style.cursor = "wait";
		var params = "";
		
		params = "oldpass="+oldpass+"&newpass="+newpass+"&checkpass="+checkpass;
		
		request = createXMLHttpObject();
		request.open("GET", "pass_change.php?"+params, true);
		request.onreadystatechange = passChangeResponse;
		request.send(null);
	}
	else
		document.getElementById("passmessage").innerHTML = "<font color='red'>Please fill out all of the fields correctly.</font>";
}

function passChangeResponse(){
	if (request.readyState==4 && request.status==200){
		var response = request.responseText;
		document.getElementById("passmessage").innerHTML = response;
		document.body.style.cursor = "auto";
	}
}
