﻿/* File: main.js, Author: Gareth Wood, September 2007

 *

 * JS functions for Comp112 project2

 *

 * Notes:  Any logic based on browser type assumes firefox

 *         is the default browser so may only preform

 *         actions if browser is not firefox. As the site

 *         has been primarly coded for firefox.

*/



/* On page load (if JS enabled), setup styleshees then smooth boxes.

 *

 * Params: none

 *

 * Return: none

 */

function loadPage(){

	alterStyleSheets();

	smoothBox(document.getElementById("contentContainer"));

//	smoothBox(document.getElementById("header"));

	smoothBottom(document.getElementById("mainContent"));

	smoothTop(document.getElementById("navigation"));

}



/* Alterstylesheets - detect browser type and 

 * add secondary style sheet as appropriate.

 *

 * Params: none

 *

 * Return: none

 */

function alterStyleSheets(){

	if(getAgent() == "safari")

		addStyleSheet("safari.css");

	

	else if(getAgent() == "opera")

			addStyleSheet("opera.css");

			

	else if(getAgent() == "ie")

			addStyleSheet("ie.css");

	

	else if(getAgent() == "chrome")

		addStyleSheet("chrome.css");

}



/* Addstylesheet - add the given style sheet

 * to the document.

 *

 * Params: cssname - the name of the sheet to add.

 *

 * Return: none

 */

function addStyleSheet(cssname){

	var headID = document.getElementsByTagName("head")[0];

	var cssNode = document.createElement("link");

	cssNode.type = "text/css";

	cssNode.rel = "stylesheet";

	cssNode.href = "css/" + cssname;

	headID.appendChild(cssNode);

}



/* Smoothbox - calls smooth top then smooth bottom

 *

 * Params: obj - the object to smooth

 *

 * Return: none

 */

function smoothBox(obj){

	smoothTop(obj);

	smoothBottom(obj);			

}



/* Smoothtop - smooths the top of a div element

 * by adding 4 div elements above it.

 * Sets background colours via setBk function

 * Uses the 4 cornerPart* classes in main.css

 *

 * Params: obj - the object to smooth

 *

 * Return: none

 */

function smoothTop(obj){

	var top = window.document.createElement("div");

	var tempTop;

	top.className = "boxTop";

	setBkColour(top, obj.parentNode);

	for(var i = 1; i < 5; i++){

		tempTop = document.createElement("div");

    	tempTop.className = "cornerPart" + i;

	    setBkColour(tempTop, obj);

	    top.appendChild(tempTop);		    

	}

	obj.insertBefore(top, obj.firstChild);

}



/* Smoothbottom - smooths the bottom of a div element

 * by adding 4 div elements below it.

 * Sets background colours via setBk function

 * Uses the 4 cornerPart* classes in main.css

 *

 * Params: obj - the object to smooth

 *

 * Return: none

 */

function smoothBottom(obj){

	var bottom = window.document.createElement("div");

	var tempBot;

	bottom.className = "boxBottom";	

	setBkColour(bottom, obj.parentNode);

	for(var i = 1; i < 5; i++){

		tempBot = document.createElement("div");

    	tempBot.className = "cornerPart" + (5 - i);

		setBkColour(tempBot, obj);

	    bottom.appendChild(tempBot);

	}

	obj.appendChild(bottom);		

}



/* SetBk colour - sets the bk of obj1 to the

 * background of obj2.

 *

 * Params: obj1 - the object to set bk on.

 *         obj2 - the object to get the bk from.

 *

 * Return: none

 *

 * Notes:  Bk image doesn't work on safari.

 */

function setBkColour(obj1, obj2){

   	if(getStyle(obj2, "background-color", "backgroundColor") == "transparent")

   		obj1.style.backgroundImage = getStyle(obj2, "background-image", "backgroundImage");

	else

	   	obj1.style.backgroundColor = getStyle(obj2, "background-color", "backgroundColor");

}



/* getStyle - finds the value associated with

 * the given cssname for any given element.

 *

 * Params: obj - the object to inspect

 *

 * Return: value of cssname.

 */

function getStyle(obj, cssname, jsname){

	if(getAgent() == "ie")

		return eval("obj.currentStyle." + jsname);

	return document.defaultView.getComputedStyle(obj, "").getPropertyValue(cssname);

}



/* getAgent - attempts to determine useragent

 * via searching in DOM elements... nothing

 * is full proof though.

 *

 * Params: None

 *

 * Return: detected user agent

 *

 * Notes:  Useful for determining whether a dom

 *         element will be scriptable or not.

 *         Only implemented safari, firefox, opera and ie.

 *         (Netscape works similar to firefox.)

 */

function getAgent(){

	if(navigator.vendor == "Apple Computer, Inc.")

		return "safari";

	

	else if(window.opera != null){

		if(window.opera == "[object Opera]")

			return "opera";

	}

	else if(navigator.userAgent != null){

		if(navigator.userAgent.indexOf("MSIE") != -1)	

			return "ie";

		else if(navigator.userAgent.indexOf("Firefox") != -1){

			return "firefox";

		}

		else if(navigator.userAgent.indexOf("Chrome") != -1){

			return "chrome";

		}



	}

	return "unknown";

}



/* getOS - attempts to determine operating system type

 * via searching in DOM elements... nothing

 * is full proof though.

 *

 * Params: None

 *

 * Return: detected operating system.

 *

 */

function getOS(){

	if(navigator.platform != null){

		if(navigator.platform.indexOf("Win") != -1)

			return "windows";

		else if(navigator.platform.indexOf("Mac") != -1)

			return "mac";

		else if(navigator.platform.indexOf("Linux") != -1)

			return "linux";

	}

	return "unknown";

}


