1 /*
  2   Copyright (c) 2008 Seneca College
  3   Licenced under the MIT License (http://www.c3dl.org/index.php/mit-license/)
  4 */
  5 
  6 
  7 /**
  8 	Check if the context, 'contextVersion' is supported.
  9 
 10 	@param {float} contextVersion must be c3dl.GLES_CONTEXT_20
 11 
 12 	@returns {boolean} True if the context 'contextVersion' is 
 13 	supported or false if it's not supported or the 'contextVersion' 
 14 	number was invalid.
 15 */
 16 c3dl.isContextSupported = function(contextVersion)
 17 {
 18 	var isSupported = true;
 19 	var dynamicCanvas;
 20 	var contextString;
 21 
 22 	if(contextVersion != c3dl.GLES_CONTEXT_20)
 23 	{
 24 		return false;
 25 	}
 26 
 27 	try{
 28 		// create a canvas element in the html
 29 		if(dynamicCanvas = document.createElement('canvas'))
 30 		{
 31 			// ignore the return value of getContext(), if this method does 
 32 			// not throw an exception, we're ok.
 33 			dynamicCanvas.getContext("moz-glweb20");
 34 		}
 35 	}
 36 	catch(err)
 37 	{
 38 		isSupported = false;
 39 	}
 40 
 41 	return isSupported;
 42 }
 43 
 44 
 45 /**
 46 	Create a copy of 'object' and return the copy.  This works with single
 47 	dimensional Arrays and Objects.
 48 
 49 	@param {Object|Array} object The object to copy.
 50 
 51 	@returns {Object|Array} A copy of 'object'.
 52 */
 53 c3dl.copyObj = function(object)
 54 {
 55 	if( object instanceof Array)
 56 	{
 57 		return object.slice();
 58 	}
 59 	else
 60 	{
 61 		var obj = new Object();
 62 		
 63 		for (i in object)
 64 		{
 65 			obj[i] = object[i];
 66 		}
 67 		
 68 		return obj;
 69 	}
 70 }
 71 
 72 
 73 /**
 74 	Is the given path absolute or relative?
 75 
 76 	@returns {boolean} true if the path is absolute false if relative
 77 */
 78 c3dl.isPathAbsolute = function(path)
 79 {
 80 	var isAbsolute = false;
 81 
 82 	for (var i = 0; i < path.length && i < 8; i++)
 83 	{
 84 		if(path.charAt(i) == ":")
 85 		{
 86 			isAbsolute = true;
 87 		}
 88 	}
 89 
 90 	return isAbsolute;
 91 }
 92 
 93 
 94 /**
 95 	Given a path to a file, this funciton will return the path without the
 96 	filename.  If the following path was given,
 97 	http://www.site.com/images/file.jpg
 98 	this would be returned
 99 	http://www.site.com/images/
100 
101 	If the path is simply a filename, null will be returned.
102 
103 	@returns {String} the path the user specified without the filename.
104 */
105 c3dl.getPathWithoutFilename = function(path)
106 {
107 	var pathWithoutFilename = "";
108 
109 	if( path != "")
110 	{
111 		// find the last slash either forward or backwards in the string.
112 		var lastForwardSlashPos = path.lastIndexOf('/');
113 		var lastBackSlashPos = path.lastIndexOf('\\');
114 		var lastSlashPos = lastForwardSlashPos > lastBackSlashPos ? lastForwardSlashPos: lastBackSlashPos;
115 
116 		// copy chars from 0 to lastSlashPos
117 		for (var i = 0; i < lastSlashPos+1; i++)
118 		{
119 			pathWithoutFilename += path[i];
120 		}
121 	}
122 	return pathWithoutFilename;
123 }
124 
125 /**
126 	Get the absolute position of an object in the DOM.
127 
128 	@returns {Array} an array with two elements, x and y.
129 */
130 c3dl.getObjectPosition = function(obj)
131 {
132 	var currleft = 0;
133 	var currtop = 0;
134 	
135 	if (obj.offsetParent)
136 	{
137 		do
138 		{
139 			currleft += obj.offsetLeft;
140 			currtop += obj.offsetTop;
141 		}while (obj = obj.offsetParent);
142 	return [currleft,currtop];
143 	}
144 }
145 
146 
147 /**
148 	@private
149 */
150 c3dl.isValidColor = function(color)
151 {
152 	// Check if the value being passed is an array
153 	if (color instanceof Array)
154 	{
155 		// 4 color components: red, green, blue, alpha
156 		if (color.length == 4)
157 		{
158 			for (var i = 0; i < 4; i++)
159 			{
160 				// Check for Bad values
161 				if (isNaN(color[i]))
162 					return false;
163 			}
164 			return true;
165 		}
166 	}
167 	else{
168 		return false;
169 	}
170 }
171