/**
 * @author jfrymann
 */
function writeLink(link, section){
    if (link.sublink) { // if the sublink property of link is defined
        var new_link = '<div class="sublink">'; // set the div id to sublink
    }
    else {
        var new_link = '<div class="link">';
    }
    
    new_link += '<a href="' + link.url + '" target="_blank">' + link.title + '</a>'; // always set a title and href value
    if (link.annotation) { // if the annotation property of link is defined
        new_link += '<span class="annotation">' + link.annotation + '</span>';
    }
    
    if (link.source) {
        new_link += '<span class="source">';
        for (z = 0; z < link.source.length; z++) { // there can be multiple sources so go through an array
            new_link += link.source[z] + "<br />";
        }
        new_link += '</span>';
    }
    new_link += '</div>';
    
    if (link.sublink) { // if the link is a sublink add it under the link that's value matches the sublink property
        $("a[href='" + link.sublink + "']").parent().append(new_link);
    }
	else if (link.reference) {
		$("#" + section + ' > *').before(new_link);
	}
    else {
        $("#" + section).append(new_link);
    }
}


function writeSection(projectSection, mainSection){
	$("#link_content").append("<div id='" + mainSection.stripSpaces() + "'><h2>" + mainSection + "</h2>");
	$("#section_nav").append("<li id='nav_" + mainSection.stripSpaces() + "'><a href='#" + mainSection.stripSpaces() + "'>" + mainSection + "</a><ul class='one'></ul></li>");
	
	var section_list = new Array();
	for (var a = 2; a < arguments.length; a++) {
		if (arguments[a].substring(4, 0) === 'sub_') {
            section_list[section_list.length-1].push(arguments[a].replace(/sub_/, ""));
        }
        else {
            section_list.push(new Array(arguments[a]));
        }
	}
	
	section_list.sort();
	for (y = 0; y < section_list.length; y++) {
        if (section_list[y][0] === 'general') {
            temp = new Array('general')
            section_list.splice(y, 1);
            section_list.splice(0, 0, temp); // general should always be first in the array
        }
    }
	
	var section_label = "";
	for (var x = 0; x < section_list.length; x++) {
		section_title = section_list[x][0];
		section_label = mainSection.stripSpaces() + "_" + section_list[x][0].stripSpaces();
		$("#nav_" + mainSection.stripSpaces() + " ul.one").append("<li id='nav_" + section_label + "'><a href='#" + section_label + "'>" + section_title + "</a></li>");
		$("#" + mainSection.stripSpaces()).append("<div id='" + section_label + "'><h3>" + section_title + "</h3>");
		if (section_list[x].length > 1) {
			var tempMain = section_list[x].shift();
			section_list[x].sort();
			section_list[x].unshift(tempMain);
			var sub_section_label = "";
			$("#nav_" + section_label).append("<ul></ul>");
			$("#" + section_label).append("<div id='uncategorized'></div>");
			for (var y = 1; y < section_list[x].length; y++) {
				sub_section_title = section_list[x][y];
				sub_section_label = section_label + "_" + section_list[x][y].stripSpaces();
				$("#nav_" + section_label + " ul").append("<li id='nav_" + sub_section_label + "'><a href='#" + sub_section_label + "'>" + sub_section_title + "</a></li>");
				$("#" + section_label).append("<div id='" + sub_section_label +"'><h4>" + sub_section_title + "</h4></div>");
			}
		}
		$("#link_content").append("</div>");
	}
	$("#link_content").append("</div>");
	
	
    $.post("http://earthguide.ucsd.edu/links/get_links.php", {
        attribute: 'section',
        value: projectSection,
        value2: mainSection
    }, function(data){
        var temp_category = "";
        for (i in data) { // each link
			for (var b = 0; b < section_list.length; b++) { // each section called in
				for (var c = 0; c < data[i].section.length; c++) { //each link section
					if (data[i].section[c] == section_list[b][0]) {
						temp_category = mainSection.stripSpaces() + "_" + section_list[b][0].stripSpaces();
						if (section_list[b].length == 1) {
							writeLink(data[i], temp_category);
						}
						else {
							var written = false;
							for (var d = 1; d < section_list[b].length; d++) {
								for (var e = 0; e < data[i].section.length; e++) {
									if (section_list[b][d] == data[i].section[e]) {
										written = true;
										writeLink(data[i], temp_category + "_" + section_list[b][d].stripSpaces());
									}
								}
							}
							if (written == false) {
								writeLink(data[i], temp_category + " #uncategorized");
							}
						}
					}
				}
			}
        }
    }, "json");
}


// Adds the ability to remove spaces from a string by calling string_name.stripSpaces()
String.prototype.stripSpaces = function(){
    return this.replace(/\s/g, "");
};
