// JavaScript Document

//Execute code as soon as the document is loaded
$(document).ready(function(){
	
	
	//Flickr Feed
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=41612457@N08&lang=en-us&format=json&jsoncallback=?", function(data){
		$.each(data.items, function(i,item){
    		$("<img/>").attr("src", item.media.m).appendTo("#flickr")
    			.wrap("<a href='" + item.link + "'></a>");
  		});
		//$("#title").html(data.title);
  		//$("#description").html(data.description);
  		$("#link").html("<a href='"+data.link+"' target=\"_blank\">Click here to get to the Flickr group</a>");
	});
	
	//Breadcrumbs
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	/*****
	Dynamic Javascript Breadcrumb Navigation by Adam DuVander
	http://duvinci.com/projects/javascript/crumbs/
	
	Released under Creative Commons License:
	http://creativecommons.org/licenses/by/2.5/
	*****/
	
	var crumbsep = " > ";
	var precrumb = "<span class=\"crumb\">";
	var postcrumb = "</span>";
	var sectionsep = "/";
	var rootpath = "/"; // Use "/" for root of domain.
	var rootname = "Home";
	
	var ucfirst = 1; // if set to 1, makes "directory" default to "Directory"
	
	var objurl = new Object;
	objurl['topics'] = 'All Topics';
	
	// Grab the page's url and break it up into directory pieces
	var pageurl = (new String(document.location));
	var protocol = pageurl.substring(0, pageurl.indexOf("//") + 2);
	pageurl = pageurl.replace(protocol, ""); // remove protocol from pageurl
	var rooturl = pageurl.substring(0, pageurl.indexOf(rootpath) + rootpath.length); // find rooturl
	if (rooturl.charAt(rooturl.length - 1) == "/") //remove trailing slash
	{
	  rooturl = rooturl.substring(0, rooturl.length - 1);
	}
	pageurl = pageurl.replace(rooturl, ""); // remove rooturl fro pageurl
	if (pageurl.charAt(0) == '/') // remove beginning slash
	{
	  pageurl = pageurl.substring(1, pageurl.length);
	}
	
	var page_ar = pageurl.split(sectionsep);
	var currenturl = protocol + rooturl;
	var allbread = precrumb + "<a href=\"" + currenturl + "\">" + rootname + "</a>" + postcrumb; // start with root
	
	for (i=0; i < page_ar.length-1; i++)
	{
	  var displayname = "";
	  currenturl += "/" + page_ar[i];
	  if (objurl[page_ar[i]])
	  {
		displayname = objurl[page_ar[i]];
	  }
	  else
	  {
		if (ucfirst == 1)
		{
		  displayname = page_ar[i].charAt(0).toUpperCase() + page_ar[i].substring(1);
		}
		else
		{
		  displayname = page_ar[i];
		}
	  }
	  allbread += crumbsep + precrumb + "<a href=\"" + currenturl + "\">" + displayname + "</a>" + postcrumb;
	}
	$("#crumbs").hide();
	$("#crumbs").html(allbread);
	$("#crumbs").slideDown("slow");
	
	
	//TFC GALILEO
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	$("#department").change(function() {
		
		//Submit the form to be processed
		$.post("../../php_forms/tfcGalileo.php", $("#tfcGalileo").serialize(), function(data){
		$("#response").html(data);
		 
		
		//Show the response with a cool effect
      	$("#response").show("slow");
		});
	});
	
	$("#galileoUpdater").click(function() {
		
		//Submit the form to be processed
		$.post("../../php_forms/tfcGalileoLogin.php", $("#tfcGalileoLogin").serialize(), function(data){
		$("#response").html(data);
		 
		 if ( data != 'Please user the correct username and password'){
			 $("#LoginForm").hide("fast");
		 }
		
		//Show the response with a cool effect
      	$("#response").show("slow");
		});
	});
	
	$("#updateTFCGalileo").click(function() {
		
		//Submit the form to be processed
		$.post("../../php_forms/tfcGalileoUpdateScript.php", $("#tfcGalileoUpdate").serialize(), function(data){
		$("#updateRespose").html(data);
		 
		
		//Show the response with a cool effect
      	$("#updateRespose").show("slow");
		$("#tfcGalileoUpdate")[0].reset();
		});
	});
	
	/*$(function(){
		$('#contact').contactable({
			name: 'Name',
			email: 'email',
			message: 'message',
	 		recipient: 'webmaster@tfc.edu',
	 		subject: 'Request for Information',
			recievedMsg : 'Thank you for requesting information. You will be contacted soon.',
 			notRecievedMsg : 'Sorry, your message could not be sent, try again later'
	 	});
	});*/
	
	$("#admin").click(function() {
		
		//Submit the form to be processed
		$.post("../../php_forms/admin_dir.php", function(data){
		$("#dirResponse").html(data);
		 
		
		//Show the response with a cool effect
      	$("#dirResponse").show("slow");
		});
	});
	
	$("#dept").click(function() {
		
		//Submit the form to be processed
		$.post("../../php_forms/dept_dir.php", function(data){
		$("#dirResponse").html(data);
		 
		
		//Show the response with a cool effect
      	$("#dirResponse").show("slow");
		});
	});
	
	$("#fac").click(function() {
		
		//Submit the form to be processed
		$.post("../../php_forms/faculty_dir.php", function(data){
		$("#dirResponse").html(data);
		 
		
		//Show the response with a cool effect
      	$("#dirResponse").show("slow");
		});
	});
	
	$("#staff").click(function() {
		
		//Submit the form to be processed
		$.post("../../php_forms/staff_dir.php", function(data){
		$("#dirResponse").html(data);
		 
		
		//Show the response with a cool effect
      	$("#dirResponse").show("slow");
		});
	});
		
});

	//Contact Form Submit
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	$('#onlineForm').submit(function() {

     // stupid IE returns an object for the forms action
    var url = this.attributes['action'] && 
	this.attributes['action'].nodeValue || this.getAttribute('action');
	
	$.post(url, $("#onlineForm").serialize(), function(data){
		$("#response").html(data);
		$("#form").hide("slow");
		$("#response").show("slow");
	});

    return false;

	});
	
	//Library Account
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	$(function() {	
		$("#myAccount").dialog({
				bgiframe: true,
				autoOpen: false,
				height: 260,
				modal: true,
				buttons: {
					'Submit': function() {
							var id = document.getElementById("studentID").value;
							var name = document.getElementById("lastName").value;
							
							theURL = "http://www.tfc.edu/athcgi/athpatinfo.pl?a=i&patid="+ id + "&patid2=" + name;
						
							var newinfowin = null;
							newinfowin = window.open(theURL,"newinfowin","toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,width=500,height=650");
							
							$(this).dialog('close');
					},
					Cancel: function() {
						$(this).dialog('close');
					}
				}
			});
		
		//Test
		$('#accountPopup').click(function() {
				$('#myAccount').load('http://www.tfc.edu/library/myaccount.html').dialog('open');
		})
	});
	
	
//	var patidarray = new Array ("Surname")
//	$("#myAccount").click(function(){
//		if (document.patidform.patid.value == '') { 
//		alert('Please enter your Patron ID.')
//		return
//	}
//	if (document.patidform.patid2.value == '') { 
//		alert('Value of ' + patidarray[0] + ' is required.')
//		return
//	}
//	theURL = "/athcgi/athpatinfo.pl?a=i" +
//			 
//	"&patid=" + escape(document.patidform.patid.value) +
//	"&patid2=" + escape(document.patidform.patid2.value)
//
//	var newinfowin = null
//	newinfowin = window.open(theURL,"newinfowin","toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,width=500,height=650");
//	});

