//Browser On Load Events

$(function ()

{
/**************************GLOBAL*******************************/

//hide course-search
$("#search-course").hide();


/*INITIALIZE TEXT_SIZE*/
// Build content tools
	contentTools = $("<ul></ul>").attr("id", "content-tools");
	// Initialise TextReisze plugin
	var opts = {
	       max:2,
               steps: 10
	}
	if (!$("body#home").length) {
 	    $("#content .middle").textResize(contentTools, opts);
	}
        else{
 	    $(".contentLayout").textResize(contentTools, opts);
        }

	// Insert content tools
	contentTools.appendTo($("#tools").eq(0));// JavaScript Document
	
	
	//manipulate
$("#text-resize a").each(function () {
$(this).wrap("<li></li>");

});

$("#text-decrease").addClass("decreaseSize");
$("#text-decrease").wrapInner("<span></span>");
$("#text-increase").addClass("increseSize");
$("#text-increase").wrapInner("<span></span>");

//$("#text-resize").wrapInner("<ul class='last'></ul>");

$("#text-resize li:last").addClass("last");
$("#text-reset:parent").css("display","none");
$("#text-resize").addClass("last");

/**
	 * Print Page
	 *
	 * Print this page stylesheet switcher
	 *
	 */
	
	// Insert print button
	contentTools.prepend('<li id="print-version"><a title="Print friendly version" href="#" class="print"><span>Print friendly version</span></a></li>');
	var printSheet = $("link[title='print preview']");
	var screenSheet = $("link[media='screen']");
	var printButton = $("a[title='Print friendly version']");
	
	// Print Preview
	printButton.click( function() {	
		// Switch to print
		$("body").fadeOut("fast", function() {
			screenSheet.each(function() {
				this.disabled = true;
			});
			printSheet.each(function() {
				this.disabled = true;
				this.disabled = false;
			});
			$(this).fadeIn("slow");
			
			$('html, body').animate({scrollTop:0}, 'fast');

			// Create Print Preview Heading
			$("body").prepend('<div id="preview-message"><h3>Print preview</h3><p><a href="#" id="preview-print">Print this page</a> | <a href="#" id="turnoff-print">Return to the normal view</a></p></div>');
			$("#preview-message").hide().slideDown("slow");
			
			// Switch Back
			$("a#turnoff-print").bind("click", function(){
				$("body").fadeOut("fast", function() {
					$("#preview-message").remove();
					// Switch to screen
					screenSheet.each(function() {
						this.disabled = false;
					});
					printSheet.each(function() {
						this.disabled = true;
					});
					$(this).fadeIn("slow");
				});
				return false;
			});
			
			// Print this page
			$("a#preview-print").bind("click", function(){
				window.print();
				return false;
			});
		});
		
		return false;
	});
	
	// Dont print preview message
	$("head").append(
		'<style type="text/css" media="print" title="Hide print preview message on print">' +
			'#preview-message {' +
					'display: none;' +
			'}' +
		'</style>'
	);



/*GLOBAL SEARCH BOX*/
//adds radio filter buttons for search

$("#search").html( 
$("#search").html()+"<div><label><input id='entireSite' type='radio' value='true' name='searchScope' checked='checked' tabindex='2'/>Entire site</label><label><input id='coursesOnly' type='radio'  name='searchScope' tabindex='3'/>Courses only</label></div>");

getId("entireSite").onclick = function()
{
   $("#search-site").show();
   $("#search-course").hide();
}

getId("coursesOnly").onclick = function()
{
   $("#search-site").hide();
   $("#search-course").show();
}

/*NAVIGATION
*****************/
//This ensures that the top links in the secondary nav are always blue
$("#leftNav li:first-child").addClass("blue");
$("#leftNav li:nth-child(2)").addClass("blue");
$("#leftNav li li:first-child").removeClass("blue");
$("#leftNav li li:nth-child(2)").removeClass("blue");

//This gives the primary naviation its curved edges by adding class names to first and last elements
var curves = getIdTag("nav", "a");

curves[0].className += " first";

curves[curves.length - 1].className += " last";   


//This inserts the arrows into the second level of the menu
var firstLevel = getIdTag("nav", "li");

var i =0;
var secondLevel;

for(i=0;i<firstLevel.length;i++){

    secondLevel = getTagFO(firstLevel[i], "li");
   
    var j= 0;

    for(j=0;j<secondLevel.length;j++){
        
          if(getTagFO(secondLevel[j],"ul").length >= 1)
          {
          var anchors = getTagFO(secondLevel[j],"a");
          anchors[0].className += "hasSubmenu";
          }
    }


}

/*************************COURSES-DETAILS SCREEN*******************/
//check if this is course details page


if(getId("courseDetails"))
{
//fix style for study modes
if(getId("study-modes")){
createStudyModeIcons(getId("study-modes"));
}
if(getId("location")){
//Fix styling for campus
var campus = getId("location").innerHTML;
campus = "<ul><li><a>" + campus.replace(/;/g, "</a></li><li><a>") + "</a></li></ul>";
getId("location").innerHTML = campus;
}
//adds links
var campus_links = getIdTag("location","a");
var c=0;
for(c=0;c<campus_links.length;c++)
{
addCampusLinks(campus_links[c]);	
}

}//END OF COURSE DETAILS

/************************EXPLORE STUDY AREAS***********************/
//check if this is study areas page
if(getId("studyAreas-nojs"))
{
//rename classes for use when js is enabled
getId("studyAreas-nojs").id = "studyAreas";


var SA_nojs = getIdTag("studyAreas", "div");


var h=0;
var b = SA_nojs.length;


for(h=0;h<b;h++)
{
	SA_nojs[h].className = "alphaStudy";
}

var x = getIdTag("studyAreas", "div");

x[0].className = "clear";

for(j=3;j<x.length;j=j+3)
{
	
    x[j-2].className = "alphaStudy_first";
    x[j-1].className = "alphaStudy";
    x[j].className = "alphaStudy_last";
	
	
	
	if(x.length - j == 3){
	 x[x.length-2].className = "alphaStudy_first";
    x[x.length-1].className = "alphaStudy";
	}

	if(x.length - j == 2){
    x[x.length-1].className = "alphaStudy_first";
	}
}



$(".alphaStudy_first").each(function(){
	$(this).add($(this).next()).add($(this).next().next()).wrapAll(document.createElement("div"));

	});
//fix for links

$(".mktree li span").not(".mktree li li span").each(function(){

   $(this).next().remove();
   
   $(this).html($(this).next().html()); 
   $(this).next().remove();
});
}


/***************************COURSE SEARCH PAGES****************/
//for making advance search close and expand
if(getId("advancedClosed")){
//starts
getIdTag("advancedClosed", "a")[0].onclick = function()
{
getId("advancedClosed").style.display = "none";
getId("advancedExpanded").style.display = "block";
};


getIdTag("advancedExpanded", "a")[0].onclick = function()
{
getId("advancedExpanded").style.display = "none";
getId("advancedClosed").style.display = "block";
};
}

/*COURSE SEARCH RESULTS PAGE****************/
if(getId("searchResults")){
//creates study icons
var sRstudymodes = getIdTag("searchResults","span");
var b =0;

for(b=0;b<sRstudymodes.length;b++)
{
    if(sRstudymodes[b].className == "sm")
    {
		createStudyModeIcons(sRstudymodes[b]);
	}
	
}

//fixes syles for campuses
var campusALL = getIdTag("searchResults","span")
var campuses;
var campuses_links;
var g=0;
for(g=0;g<campusALL.length;g++)
{
    if(campusALL[g].className == "campus" && campusALL[g].innerHTML != "")
    {
		
        campuses = campusALL[g];
        campuses.innerHTML = campuses.innerHTML.replace(/;/g, ",") ;
    }
}

/*****************************************/
}


//END WINDOW LOAD ***********************************************************************************
});