// CSU Library Website
// Delicious Links JavaScript file
// Joshua Gross, August 2009

// This function sets up the heading(s) and adds the linkroll(s) to the page.
function createList() {

	// These two variables specify the wording of the headings for the Subject Support index pages and dictionaries pages
	var indexHeading = "The_Latest";
	var dictionariesHeading = "Dictionaries_&_Encyclopaedias";

	
	// These two variables specify the maximum number of links to appear in the regular link rolls and the Subject Support index link rolls
	var maxLinks = 50;
	var indexMaxLinks = 5;
	
	// This function checks the Heading of the page to see if it has any Subheadings
	function getSubTag(Heading){
		var pageURL = "none";
		var subHeading = "none";
		switch(Heading){
			
			// This lists each of the Online Reference subjects and all the subheadings (if any) for that subject
			case "Abbreviations,_Acronyms_&_Quotations":
				pageURL = "links/abbreviations";
				subHeading = new Array("Abbreviations_&_Acronyms", "Quotations");
				break;
			case "Atlases_&_Maps":
				pageURL = "links/atlases";
				subHeading = "none";
				break;
			case "Australian_Information":
				pageURL = "links/australian";
				subHeading = "none";
				break;
			case "Awards_&_Prizes":
				pageURL = "links/awards";
				subHeading = new Array("Art_Awards", "Literary_Awards", "Other_Awards");
				break;
			case "Biographies":
				pageURL = "links/biographies";
				subHeading = new Array("Australian_Biographies", "International_Biographies");
				break;
			case "Book_Publishers,_Bookshops_&_reviews":
				pageURL = "links/books";
				subHeading = new Array("Australian_Bookshops", "International_Bookshops", "Book_Reviews", "Book_Publishers");
				break;
			case "Business_&_Finance":
				pageURL = "links/business";
				subHeading = new Array("General_Interest_Business_Resources", "Australian_Business_Resources", "Other_Country_Specific_Business_Resources");
				break;
			case "Calculators_&_Measurement":
				pageURL = "links/calculators";
				subHeading = "none";
				break;
			case "Consumer_Advice":
				pageURL = "links/consumer";
				subHeading = new Array("Australian_ACCC_and_Office_of_Fair_Trading_websites", "Other_Australian_government_websites", "Other_Consumer_Advice_Organisations");
				break;
			case "Countries_&_Travel":
				pageURL = "links/countries";
				subHeading = new Array("Country_Information", "Travel_-_Australia", "Travel_-_International");
				break;
			case "Dictionaries_&_Thesauruses":
				pageURL = "links/dictionaries";
				subHeading = new Array("Subscription_Resources", "Combination_Dictionary_and_Thesaurus", "General_Dictionaries", "Specialised_Dictionaries");
				break;
			case "Employment":
				pageURL = "links/employment";
				subHeading = new Array("Australian_Employment", "Canadian_Employment", "International_Employment");
				break;
			case "Encyclopaedias":
				pageURL = "links/encyclopaedias";
				subHeading = "none";
				break;
			case "Government_Information":
				pageURL = "links/government";
				subHeading = new Array("Australian_Government_Information", "Canadian_Government_Information", "International_Government_Information", "Elections_&_Referendums");
				break;
			case "Image_Collections":
				pageURL = "links/images";
				subHeading = new Array("Free_Collections", "Fee-based_collections_(includes_Royalty_Free)");
				break;
			case "Institutional_Repositories":
				pageURL = "links/institutional";
				subHeading = "none";
				break;
			case "Law_&_Legislation":
				pageURL = "links/law";
				subHeading = new Array("Australian_Legislation_(Acts_&_Regulations)", "Australian_Cases", "Other_Law_Resources", "International_Treaties_&_Conventions");
				break;
			case "Libraries,_Archives,_Galleries_&_Museums":
				pageURL = "links/libraries";
				subHeading = "none";
				break;
			case "News_&_Media":
				pageURL = "links/news";
				subHeading = new Array("Australian_Newspapers", "Canadian_Newspapers", "International_Newspapers", "Australian_Television", "Canadian_Television", "International_Television", "Australian_Radio", "Canadian_Radio", "International_Radio");
				break;
			case "Standards,_Patents_&_Trademarks":
				pageURL = "links/standards";
				subHeading = new Array("Australian_Standards", "International_Standards", "Patents_and_Trademarks");
				break;
			case "Statistics":
				pageURL = "links/statistics";
				subHeading = new Array("Australian_Statistics", "International_Statistics");
				break;
			case "Telephone_Directories_&_Postcodes":
				pageURL = "links/telephone";
				subHeading = new Array("Australian_Directories_&_Postcodes", "Canadian_Directories_&_Postcodes", "International_Directories_&_Postcodes");
				break;
			case "Tertiary_Study":
				pageURL = "links/tertiary";
				subHeading = new Array("Universities", "Student_Assistance");
				break;
			case "Time_&_Time_Zones":
				pageURL = "links/tertiary";
				subHeading = new Array("Universities", "Student_Assistance");
				break;
			case "Weather_&_Climate":
				pageURL = "links/weather";
				subHeading = new Array("Australian_Weather", "International_Weather");
				break;
			case "Web_Applications":
				pageURL = "links/web";
				subHeading = new Array("Email_clients", "Web_Browsers", "Online_Applications", "Free_Downloadable_Applications", "Search_Engines_&_Subject_Directories");
				break;
			case "Writing_&_Style_Guides":
				pageURL = "links/writing";
				subHeading = new Array("Writing_Guides", "Style_Guides");
				break;
				
							
			// This lists each of the Subject Support subjects and all the subheadings (if any) for that subject
			case "Agriculture_&_Horticulture":
				pageURL = "subject/agriculture/websites";
				subHeading = "none";
				break;
			case "Allied_Health":
				pageURL = "subject/allied/websites";
				subHeading = new Array("Occupational_Therapy", "Physiotherapy", "Podiatry", "Speech_Pathology", "Anatomy_&_Physiology");
				break;
			case "Animal_&_Vet_Sciences":
				pageURL = "subject/animal/websites";
				subHeading = "none";
				break;
			case "Biomedical_Sciences":
				pageURL = "subject/biomedical/websites";
				subHeading = new Array("Prehospital_Care", "Emergency_Management", "Biotechnology", "Complementary_&_Alternative_Medicine", "Health_Services_Management");
				break;
			case "Business":
				pageURL = "subject/business/websites";
				subHeading = new Array( "Accounting", "Management", "Human_Resource_Management", "Marketing", "Tourism", "Economics,_Finance_&_Banking", "Project_Management", "International_Business");
				break;
			case "Communication":
				pageURL = "subject/communication/websites";
				subHeading = "none";
				break;
			case "Creative_&_Visual_Media":
				pageURL = "subject/design/websites";
				subHeading = "none";
				break;
			case "Dentistry_&_Oral_Health":
				pageURL = "subject/dentistry/websites";
				subHeading = "none";
				break;
			case "Education_Studies":
				pageURL = "subject/education/websites";
				subHeading = new Array("Early_Childhood", "Higher_Education", "Middle_Schooling", "Canadian_Educational_Resources", "VET_Resources", "Curriculum_Studies", "Professional_Studies");
				break;
			case "English_Literature":
				pageURL = "subject/english/websites";
				subHeading = "none";
				break;
			case "Environmental_Studies":
				pageURL = "subject/environment/websites";
				subHeading = new Array("Environmental_Management", "Ecology", "Ecotourism", "Park_Management", "Spatial_Science", "GIS_&_Remote_Sensing", "Water_Sustainability", "Heritage_Studies", "Ornithology", "Interpretive_Planning", "Outdoor_Recreation_Management", "Soil_Resource_Management");
				break;
			case "Ethics_&_Philosophy":
				pageURL = "subject/ethics/websites";
				subHeading = "none";
				break;
			case "Exercise_Science":
				pageURL = "subject/exercise/websites";
				subHeading = "none";
				break;
			case "History_&_Politics":
				pageURL = "subject/history/websites";
				subHeading = new Array("History", "Politics");
				break;
			case "Indigenous_Studies":
				pageURL = "subject/indigenous/websites";
				subHeading = "none";
				break;
			case "Information_Technology":
				pageURL = "subject/information/websites";
				subHeading = new Array("Programming", "General_IT", "Games_Tech");
				break;
			case "Justice_Studies_&_Policing":
				pageURL = "subject/law/websites";
				subHeading = "none";
				break;
			case "Legal_Studies":
				pageURL = "subject/legal/websites";
				subHeading = "none";
				break;
			case "Library_&_Information_Science":
				pageURL = "subject/library/websites";
				subHeading = "none";
				break;
			case "Medical_Radiation_Science":
				pageURL = "subject/medical/websites";
				subHeading = "none";
				break;
			case "Nursing":
				pageURL = "subject/nursing/websites";
				subHeading = "none";
				break;
			case "Psychology":
				pageURL = "subject/psychology/websites";
				subHeading = "none";
				break;
			case "Science":
				pageURL = "subject/science/websites";
				subHeading = new Array("Maths_&_Statistics_Resources", "Other_Science_Resources");
				break;
			case "Social_Sciences":
				pageURL = "subject/sociology/websites";
				subHeading = new Array("Aboriginal_&_Torres_Strait_Islanders", "Aged_&_Elderly", "Disability", "Drugs", "Family", "Gambling", "Gender_Studies", "Juvenile_Justice", "Research", "Rural", "Welfare_Agencies_&_Societies", "Youth_&_Children");
				break;
			case "Theology":
				pageURL = "subject/theology/websites";
				subHeading = "none";
				break;
			case "Viticulture_&_Wine_Science":
				pageURL = "subject/viticulture/websites";
				subHeading = "none";
				break;
	
			default:
				pageURL = "invalid";
				subHeading = "none";
				break;
		}
		var tempArray = new Array(pageURL, subHeading);
		return tempArray;
	}
	
	function splitTags(mainTag,subTag,type){
			
		function createLinkRoll(listTags){
			if (typeof window.Delicious == 'undefined')
			window.Delicious = {};
			Delicious.Linkrolls_CB_85238 = function(posts) {
				
			var thisSort = "alpha";
			if (listTags.split(" ")[1] == indexHeading){
				var thisSort = "date";
			}
			
			Delicious.Linkrolls.writeln({
			"user":"csulibraryOnlineReference","usertags":mainTag,"version":"2","count":maxLinks,"sort":thisSort,"title":"CSU Library's Online Reference Bookmarks","tags":true,"extended":true,
			"BASE_URL":"http:\/\/delicious.com","STATIC_URL":"http:\/\/l.yimg.com\/hr\/10334"}, posts);
			};
			
			Delicious.Linkrolls = function() {
				return {
			
					writeln: function(o, posts) {
						document.write(this.render(o, posts));
					},
					
					render: function(o, posts) {
			
						var out = [];
						w = function (s) { out.push(s); }
			
						// Do some HTML escaping to avoid formatting probs and XSS
						o.title_h    = this.htmlEscape(o.title);
						o.user_h     = this.htmlEscape(o.user);
						o.user_q     = encodeURIComponent(o.user);
						o.usertags_q = encodeURIComponent(o.usertags);
			
						if (o.icon === true) o.icon = 'm';
			
						// Apply default options to incoming options.
						for (k in this.defaults)
							if (typeof o[k] == 'undefined')
								o[k] = this.defaults[k];
			
						// Sort the posts alphabetically if necessary.
						if (o.sort == 'alpha') {
							posts.sort(function(a,b) {
								var ad = (''+a.d).toLowerCase();
								var bd = (''+b.d).toLowerCase();
								return ( (ad > bd) ? 1 : ( (ad < bd) ? -1 : 0) );
							});
						} else {
							posts.sort(function(b,a) {
								return ( (a.dt > b.dt) ? 1 : (a.dt < b.dt) ? -1 : 0 );
							});
						}
			
						w('<div class="delicious-posts" id="delicious-posts-'+o.user+'">');
			
						if(subTag != "none" && subTag != dictionariesHeading){
							var subHeading = subTag[headingCounter].replace(/_/g," ");
							if(subHeading == "The Latest"){
								w('<h'+'4>' + subHeading + '</h'+'4>');
							} else {
								w('<h'+'3>' + subHeading + '</h'+'3>');
							}
						}
			
						
						
						w('<table class="online-reference">');
			
						// Iterate through all the posts and build the linkroll main body.
			
						for(var i=0,p;( i<o.count ) && ( p=posts[i] );i++){
							var thisLink = "valid";
							if(subTag == "none"){
								for(yyy = 0; yyy < p.t.length; yyy++){
									var tagtest = p.t[yyy].toLowerCase();
									if(tagtest == dictionariesHeading.toLowerCase() || tagtest == indexHeading.toLowerCase()){
										thisLink = "invalid";
									}
								}
							}
							
							if(thisLink != "invalid"){
								w('<tr><th>');
									if (o.bullet) { w(o.bullet+'&#160;'); }
									w('<a class="delicious-link"'+(p.n?(' title="'+this.htmlEscape(p.n)+'"'):'')+' href="'+p.u+'" target="_blank">'+this.htmlEscape(p.d)+'</a></th>');
									if(o.extended) {
										if(p.n) w('<td class="delicious-extended">'+this.htmlEscape(p.n));
									}
									if (o.tags && type == "links") {
										if (p.t) {
											var tagCounter = 0;
											for(var j=0;j<p.t.length;j++) {
												if(p.t[j] != mainTag){
													var pSub = getSubTag(p.t[j])[0]
													if(pSub == "invalid"){
														p.t[j] = "invalid";
													}
													if(p.t[j] != "invalid") {
														var otherTag = getSubTag(p.t[j])[0].replace(/_/g," ")
														if(tagCounter==0){
															w('<br /><span class="delicious-tag">See also: </span>');
															tagCounter++;
														}
														w('<a class="delicious-tag" href="/division/library/find-info/' + otherTag + '.html">'+(this.htmlEscape(p.t[j])).replace(/_/g," ")+'</a> ')
													}
												}
											}
										}
									}
									w('</td></tr>');
							}
						}
			
						w('</table>');
						
						if((headingCounter + 1) == tags.length){
							w('<p>For more useful links, see: <a href="http://delicious.com/csulibraryOnlineReference/'+mainTag+'" target="_blank">CSU Library\'s Online Reference Bookmarks</a> on Delicious <img src="/division/library/images/delicious-logo.gif" alt="Delicious" title="Delicious" />.</p>');
						}
						
						headingCounter++;
			
			
						
						w('</div>');
						
						return out.join('');
					},
			
					htmlEscape: function(s) {
						return (''+s).replace(/&/g,'&amp;').replace(/"/g,'&quot;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
					},
			
					EOF: null
				};
			}();
			var thisCount = maxLinks;
			if (listTags.split(" ")[1] == indexHeading){
				var thisCount = indexMaxLinks;
			}
			document.writeln('<scr'+'ipt type="text/javascript" src="http://feeds.delicious.com/v2/json/csulibraryOnlineReference/' + listTags + '?count=' + thisCount + '&callback=Delicious.Linkrolls_CB_85238"></scr'+'ipt>');
		}
		
		var pageTags = new Array;
		pageTags[0] = mainTag;
		
		var tags = new Array;
		tags[0] = mainTag;
		
		if(subTag != "none"){
			for(x = 0; x < subTag.length; x++){
				pageTags[x+1] = subTag[x]
				tags[x] = mainTag + " " + subTag[x];
			}
		}
				
		var headingCounter = 0;
		
		for(zzz = 0; zzz < tags.length; zzz++){
			createLinkRoll(tags[zzz]);
		}
	}

	// Check the page for an element with the ID "tag"
	if(document.getElementById("tag") != undefined){
			
		// Assign the contents of the "tag" HTML element to a JavaScript variable called "tag"
		var tag = document.getElementById("tag").innerHTML;
		
		// Remove ampersand coding from the tag and replace spaces with underscores
		var mainTag = (tag.replace(/&amp;/g,"&")).replace(/ /g,"_");
		
		// Check to see whether this is a Subject Support or Online Reference page
		var type = document.location.pathname.split("/")[4];
		switch(type){
			
			// If this is a subject support page, check to see if it is a dicitionaries or index page
			case "subject":
				var pageType = (document.location.pathname.split("/")[6]).split(".html")[0];
				switch(pageType){
					
					// If it is a dictionaries page, assign the dictionaries subheading
					case "dictionaries":
						subTag = new Array(dictionariesHeading);
						break;
						
					// If it is an index page, assign the "latest" subheading
					case "index":
						subTag = new Array(indexHeading);
						break;
						
					// Otherwise, see the "getSubTag" function to see if this subject has any subheadings
					default:
						var subTag = getSubTag(mainTag)[1];
						break;
				}
				break;
			
			// If this is an online reference page, see the "getSubTag" function to see if this subject has any subheadings
			case "links":
				var subTag = getSubTag(mainTag)[1];
				break;
			default:
				break;
		}
		
		// Remove the content of the "online-reference-links" div (by default, this contains a link to the delicious page)
		document.getElementById("online-reference-links").innerHTML = "";
		
		splitTags(mainTag,subTag,type)
	}
}
