
// BROWSER SNIFFING FOR CSS USE ____________________________________________________
// Author: Rafael Lima (http://rafael.adm.br)

var css_browser_selector = function()
{
	var 
		ua=navigator.userAgent.toLowerCase(),
		is=function(t){ return ua.indexOf(t) != -1; },
		h=document.getElementsByTagName('html')[0],
		b=( !(/opera|webtv/i.test(ua)) && /msie (\d)/.test(ua) )? ('ie ie'+RegExp.$1):
			is('gecko/')? 'gecko':
				is('opera/9')? 'opera opera9':
					/opera (\d)/.test(ua)? 'opera opera'+RegExp.$1:
						is('konqueror')? 'konqueror':
							is('applewebkit/')? 'webkit safari':
								is('mozilla/')? 'gecko':'',
		os=( is('x11')||is('linux') )? ' linux':
			is('mac')? ' mac':
				is('win')? ' win':
					'';
	var c=b+os+' js';
	
	h.className += h.className?' '+c:c;
}();


pathToGFX = '/';

// PRELOAD ___________________________________________________________________

Preload = 
{
	// ADD GRAPHICS HERE
	images : [
		"gfx/c-l.png","gfx/c-r.png",
		"gfx/round_white.png","gfx/horiz_white.png","gfx/vert_white.png",
		"gfx/round_grey.png","gfx/horiz_grey.png","gfx/vert_grey.png"
	],
	
	run:function()
	{
		var p = document.createElement("DIV");
		p.style.display = "none";
		
		for(var i=0; i<this.images.length; i++)
		{
			var img = new Image();
			img.src = pathToGFX+this.images[i];
			p.appendChild(img);
		}
		
		document.body.appendChild(p);
	}
}




// STYLING ___________________________________________________________________

Style =
{
	apply:function()
	{
		// rounded buttons
		$$('a.button').each(function(e){
			var span = document.createElement("SPAN");
			span.appendChild(e.removeChild(e.firstChild));
			e.appendChild(span);
			e.insertBefore(Style.getImg(pathToGFX+"gfx/c-l.png"),e.firstChild);
			e.appendChild(Style.getImg(pathToGFX+"gfx/c-r.png"));
		});
		
		// prevent nav focus box
		$$('.menu a').each(function(e){
			e.onfocus = function(){this.blur(); return;}
		});
		
	},
	
	getImg:function(src)
	{
		var img = document.createElement("IMG");
		img.src = src;
		return img;
	}
};




// ROUNDED CORNERS ___________________________________________________________________

RoundCorners =
{
	default_colour : "white",
	valid_colours : ["white","grey"],
	
	apply : function()
	{
		var blocks = $$('div.rounded');
		
		for(var i=0; i<blocks.length; i++)
		{
			blocks[i].removeClassName("rounded");
			// create table with rounded cells (inserted before div to be replaced in DOM)
			var target = this.getTable(blocks[i]);
			
			// remove div to be replaced, insert into table center cell
			target.appendChild(blocks[i].parentNode.removeChild(blocks[i]));
		}
	},
	
	getTable : function(target)
	{
		var classes = $w(target.className);
		// check if first class is valid col, otherwise use default col
		var col = classes[0]; // get css colour class of content (!MUST BE FIRST CLASS SPECIFIED)
		if(this.valid_colours.indexOf(col)==-1) col = this.default_colour;
		
		var tableClass = "rounded "+col;
		if (classes.indexOf("tabbed")>-1) tableClass += " tabbed"; // tabbed div has different gfk
		
		var main = DOM.getElement("TD",{className:col}); // the cell in which target div will reside
		
		var table = DOM.getElement("TABLE",{className:tableClass,cellPadding:0,cellSpacing:0},
			["TBODY",null,
				["TR",null,
					["TD",{className:"tl"},"<!-- -->"],
					["TD",{className:"t"},"<!-- -->"],
					["TD",{className:"tr"},"<!-- -->"]
				],
				["TR",null,
					["TD",{className:"l"},"<!-- -->"],
					main,
					["TD",{className:"r"},"<!-- -->"]
				],
				["TR",null,
					["TD",{className:"bl"},"<!-- -->"],
					["TD",{className:"b"},"<!-- -->"],
					["TD",{className:"br"},"<!-- -->"]
				]
			]
		);
		
		target.parentNode.insertBefore(table,target);
		
		return main;
	}
};


// helper methods to create content easily ___________________________
DOM = 
{
	
	getElement:function (type,attributes)
	{
		var e = document.createElement(type);
		
		for(x in attributes)
		{
			switch(x)
			{
				case "style": //expand style
					for(y in attributes["style"]){
						e.style[y] = attributes["style"][y];
					}
					break;
				
				default:
					try{e[x] = attributes[x];}
					catch(error){log(x,attributes[x],error)}
					break;
			}
		}
		
		this.build.apply( this, $A(arguments).slice(2)._unshift(e) );
		
		return e;
	},
	
	build:function(target)
	{
		var content = $A(arguments).slice(1);
		var l = content.length;
		
		for(var i=0; i<l; i++)
		{
			var o = content[i];
			
			if(o === undefined || o === null) // undefined,null
				continue;
				
			if(Object.isArray(o)) // Array - recursive
				target.appendChild( this.getElement.apply(this,o) );
			
			else if(typeof o == "object") // DOM object
				target.appendChild(o);

			else // string,integer
				target.innerHTML += o; //CAUTION : can break prior elements, use a SPAN instead
				//target.appendChild(document.createTextNode(o)); // entities/tags wont be converted
		}
	}
}

// MISC _____________________________________________________
// small Array enhancement : unshifts in one line
Array.prototype._unshift = function(e)
{
	this.unshift(e);
	return this;
};