
bbcjs.css = new Object();
bbcjs.css.prototype = new bbcjs.Module("css", 1, "$Revision: 1.8 $", "$Date: 2005/05/06 14:44:15 $");
bbcjs.css.targets =
{
win : 		1,
mac : 		2,
linux : 	4,
ie  : 		8,
nav : 		16,
opera :		32,
ie5up :		64,
nav6up :	128,
ie6up : 	256,
safari :	512,
gecko :		1024
}
bbcjs.css.detect = {};
bbcjs.css.detect.init = function()
{
// AGENT STRING
this.agt			=	navigator.userAgent.toLowerCase();
// VERSION NUMBER
this.is_major		=	parseInt(navigator.appVersion);
this.is_minor		=	parseFloat(navigator.appVersion);
// PLATFORMS
this.is_win			=	((this.agt.indexOf("win")!=-1) || (this.agt.indexOf("16bit")!=-1) );
this.is_mac			=	(this.agt.indexOf("mac")!=-1);
this.is_linux		=	(this.agt.indexOf("inux")!=-1);
// BROWSERS
// navigator
this.is_nav			=	((this.agt.indexOf('mozilla')!=-1) && (this.agt.indexOf('spoofer')==-1) && (this.agt.indexOf('compatible') == -1) && (this.agt.indexOf('opera')==-1) && (this.agt.indexOf('webtv')==-1) && (this.agt.indexOf('hotjava')==-1));
this.is_nav2		=	(this.is_nav && (this.is_major == 2));
this.is_nav3		=	(this.is_nav && (this.is_major == 3));
this.is_nav4		=	(this.is_nav && (this.is_major == 4));
this.is_nav4up		=	(this.is_nav && (this.is_major >= 4));
this.is_navOnly		=	(this.is_nav && ((this.agt.indexOf(";nav") != -1) || (this.agt.indexOf("; nav") != -1)) );
this.is_nav6		=	(this.is_nav && (this.is_major == 5));
this.is_nav6up		=	(this.is_nav && (this.is_major >= 5));
// safari
this.is_safari 		= 	((this.agt.indexOf("safari") != -1));
// gecko
this.is_gecko		=	(this.agt.indexOf('gecko') != -1);
// ie
this.is_ie			=	((this.agt.indexOf("msie") != -1) && (this.agt.indexOf("opera") == -1));
this.is_ie3			=	(this.is_ie && (this.is_major < 4));
this.is_ie4			=	(this.is_ie && (this.is_major == 4) && (this.agt.indexOf("msie 4")!=-1) );
this.is_ie4up		=	(this.is_ie && (this.is_major >= 4));
this.is_ie5			=	(this.is_ie && (this.is_major == 4) && (this.agt.indexOf("msie 5.0")!=-1) );
this.is_ie5_5		=	(this.is_ie && (this.is_major == 4) && (this.agt.indexOf("msie 5.5") !=-1));
this.is_ie5up		=	(this.is_ie && !this.is_ie3 && !this.is_ie4);
this.is_ie5_5up		=	(this.is_ie && !this.is_ie3 && !this.is_ie4 && !this.is_ie5);
this.is_ie6			=	(this.is_ie && (this.is_major == 4) && (this.agt.indexOf("msie 6.")!=-1) );
this.is_ie6up		=	(this.is_ie && !this.is_ie3 && !this.is_ie4 && !this.is_ie5 && !this.is_ie5_5);
// opera
this.is_opera		=	(this.agt.indexOf("opera") != -1);
this.is_opera2		=	(this.agt.indexOf("opera 2") != -1 || this.agt.indexOf("opera/2") != -1);
this.is_opera3		=	(this.agt.indexOf("opera 3") != -1 || this.agt.indexOf("opera/3") != -1);
this.is_opera4		=	(this.agt.indexOf("opera 4") != -1 || this.agt.indexOf("opera/4") != -1);
this.is_opera5		=	(this.agt.indexOf("opera 5") != -1 || this.agt.indexOf("opera/5") != -1);
this.is_opera5up	=	(this.is_opera && !this.is_opera2 && !this.is_opera3 && !this.is_opera4);
}
bbcjs.css.detect.init();
bbcjs.trace('<b><font color="green">jst_css.js</font> was included.</b>',2);
bbcjs.css.Swap = function (url, profiles)
{
bbcjs.trace('new bbcjs.css.Swap()');
this.url = url || '';
this.profiles = profiles || [];
}
bbcjs.css.swap = new bbcjs.css.Swap();
bbcjs.css.Swap.prototype.load = function ()
{
bbcjs.trace('<hr><b>bbcjs.css.swap.load()</b>...',2);
if (this.url)
{
this.write(this.url);
if (this.profiles)
{
var profile = this._parseProfiles(this.profiles);
if (profile.length > 0)
{
var s = '';
for (var i in profile) s += '.' + profile[i];
var profileFile = this.url.replace(/\.css/, s + '.css');
this.write(profileFile);
return true;
}
}
return true;
}
return false;
}
bbcjs.css.Swap.prototype._parseProfiles = function (profiles)
{
bbcjs.trace('<hr><b>bbcjs.css.swap._parseProfiles(' + profiles + ')</b>...',2);
var profile = [];
var isMatch = true;
for (var a=0; a<profiles.length; a++)
{
// Convert profile instance to binary string (e.g 10010)
var operand = parseInt(profiles[a]).toString(2);
bbcjs.trace('<br><b>Profile ' + profiles[a] +' = ' + operand + '</b>',3);
// Loop through the binary string
for (var i=operand.length, j=1; i>0; i--, j++){
// Look for 'bits' ('1's)
if (operand.charAt(i-1)==1)
{
bbcjs.trace('<i>Bit found at position ' + (j) + '</i>',4);
// Now loop through the jst css targets associative array
for (var t in bbcjs.css.targets)
{
bbcjs.trace('&nbsp;&nbsp;trying target: ' + t + ' = ' + bbcjs.css.targets[t].toString(2) + ' = ' + (bbcjs.css.targets[t].toString(2).length) + ')',4);
// If targets binary string is same length as position of the 'bit' in profile binary string then the target is a match
if (bbcjs.css.targets[t].toString(2).length==j)
{
bbcjs.trace('<u>Profile ' + profiles[a] + ' target = "' + t + '"</u>',3);
// Update the isMatch var by and-ing the targets jst css detect value
isMatch = isMatch && eval("bbcjs.css.detect.is_" + t);
// Add the target to the profile array and break
profile[profile.length] = t;
break;
}
}
}
}
// If isMatch is false then one or more targets in the profile did not evaluate to true
if (!isMatch)
{
bbcjs.trace('<font color="red">Profile ' + profiles[a] + ' (' + profile + ') did not match this environment</font>',3);
profile = [];
isMatch = true;
}
// If isMatch is true then all targets in this profile matched this environment, stop trying other profiles
else
{
bbcjs.trace('<font color="green">Profile ' + profiles[a] + ' targets (' + profile + ') matched this environment</font>',3);
break;
}
}
// profile will contain target names if there was a match, otherwise it will be an empty array
return profile;
}
bbcjs.css.Swap.prototype.write = function (href)
{
bbcjs.trace('<hr><b>bbcjs.css.swap.write("' + href + '")</b>...',2);
var le = '<link rel="stylesheet" type="text/css" href="' + href + '">';
document.write(le);
}

