//-------------------------------------------------
//	ADD/CHANGE STYLESHEET 
//-------------------------------------------------
var styles = addStyle('');

// Add a stylesheet to the document and return a reference to it 
function addStyle(css) { 
   var style = document.createElement( 'style' ); 
   style.type = 'text/css'; 
   var head = document.getElementsByTagName('head')[0]; 
   head.appendChild( style ); 
   if( style.styleSheet )  // IE 
      style.styleSheet.cssText = css; 
   else  // other browsers 
      style.appendChild( document.createTextNode(css) ); 
   return style; 
} 

// Replace the entire contents of a stylesheet 
function changeStyle(style, css ) { 
   if( style.styleSheet )  // IE 
      style.styleSheet.cssText = css; 
   else  // other browsers 
      style.replaceChild( document.createTextNode(css), style.firstChild ); 
   return style; 
}


//-------------------------------------------------
//	SHADED BORDER FUNCTION
//-------------------------------------------------
var path = '../../_images/general/';
var box = 'box.png';
var border = 'border.png';
var boxGIF = 'box.gif';
var borderGIF = 'borders.gif';
var radius = 10;
var shadowtop = 3;
var shadowright = 6;
var shadowbottom = 7;
var shadowleft = 4;
var secRun = false;
var shdwImgClass = "";

function createElement(element) {
	if (typeof document.createElementNS != 'undefined') {
		return document.createElementNS('http://www.w3.org/1999/xhtml', element);
	}
	if (typeof document.createElement != 'undefined') {
		return document.createElement(element);
	}
	return false;
}

function initBorders(reRun) {
	if (reRun=='overview'){
		path = '../../_images/general/';
	}
	if(reRun!=true){
		
		// Find all div elements
		var divs = document.getElementsByTagName('div');
		var shdwImgDivs = [];
		// var ccsDivs = [];
		for (var i = 0; i < divs.length; i++) {
			
			// ccm CLASSNAMES -----------------
			if (/\bshade\b/.test(divs[i].className)) {
				shdwImgDivs[shdwImgDivs.length] = divs[i];
				//shdwImgClass = divs[i].className.split(' ')[1];
				shdwImgClass = "";
			}
		}
		setBorders(shdwImgDivs);

	}

	function setBorders(divArray) {

		// Loop through the found div elements
		var container, topcorner, i1, i2, kind1, kind2, kind3;
		for (var i = 0; i < divArray.length; i++) {
		
			container = divArray[i];
			var height = container.offsetHeight;
			container.className = container.className.split(' ')[1];
			
			outer = container.firstChild;
		
			topcorner = createElement('div');
			topcorner.className = 'topcorner';
			container.insertBefore(topcorner,outer);
		
			topbar = createElement('div');
			topbar.className = 'topbar';
			container.insertBefore(topbar,outer);
		
			left = createElement('div');
			left.className = 'left';
			container.insertBefore(left,outer);
		
			right = createElement('div');
			right.className = 'right';
			container.insertBefore(right,outer);
		
			bottomcorner = createElement('div');
			bottomcorner.className = 'bottomcorner';
			container.insertBefore(bottomcorner,outer);
		
			bottombar = createElement('div');
			bottombar.className = 'bottombar';
			container.insertBefore(bottombar,outer);
		}
		if(reRun!=true){
			// DEFINE CSS -----------------
			var css = '.topcorner{position:relative;height:'+radius+'px;width:'+radius+'px;top:'+0+'px;margin-left:'+0+'px;}'+ 
					'.topbar{ position:absolute;height:'+radius+'px;top:'+0+'px;left:'+radius+'px;right:'+0+'px;}'+ 
					'.left{ position:absolute;top:'+radius+'px;bottom:'+radius+'px;left:'+0+'px;width:50%;}'+ 
					'.right{ position:absolute;top:'+radius+'px;bottom:'+radius+'px;left:50%;right:'+0+'px;}'+ 
					'.bottomcorner{ position:absolute;height:'+radius+'px;width:'+radius+'px;bottom:'+0+'px;left:'+0+'px;}'+ 
					'.bottombar{ position:absolute;height:'+radius+'px;bottom:'+0+'px;left:'+radius+'px;right:'+0+'px;}'+
					
					'.topcorner{ background:url('+path+shdwImgClass+box+') no-repeat 0 0;}'+
					'.topbar{ background:url('+path+shdwImgClass+box+') no-repeat 100% 0;}'+
					'.left{ background:url('+path+shdwImgClass+border+') repeat-y 0 0 ;}'+
					'.right{ background:url('+path+shdwImgClass+border+') repeat-y 100% 0 ;}'+
					'.bottomcorner{ background:url('+path+shdwImgClass+box+') no-repeat 0 100%;}'+
					'.bottombar{ background:url('+path+shdwImgClass+box+') no-repeat 100% 100%;}';
					
					// '.'+ccsClass+' .topcorner{ background:url('+path+ccsClass+box+') no-repeat 0 0;}'+
					// 				'.'+ccsClass+' .topbar{ background:url('+path+ccsClass+box+') no-repeat 100% 0;}'+
					// 				'.'+ccsClass+' .left{ background:url('+path+ccsClass+border+') repeat-y 0 0;}'+
					// 				'.'+ccsClass+' .right{ background:url('+path+ccsClass+border+') repeat-y 100% 0;}'+
					// 				'.'+ccsClass+' .bottomcorner{ background:url('+path+ccsClass+box+') no-repeat 0 100%;}'+
					// 				'.'+ccsClass+' .bottombar{ background:url('+path+ccsClass+box+') no-repeat 100% 100%;}';

			var cssie6 ='.topcorner{ position:absolute;height:7%;width:64%;top:0;left:0;}'+ 
					'.topbar{ position:absolute;height:7%;top:0;left:50%;right:0;width:50%;}'+ 
					'.left{ position:absolute;top:5%;bottom:5%;left:0;right:50%;width:65%;height:90%;}'+ 
					'.right{ position:absolute;top:5%;bottom:5%;left:50%;right:0;width:50%;height:90%;}'+ 
					'.bottomcorner{ position:absolute;height:7%;width:64%;bottom:-1px;left:0;}'+ 
					'.bottombar{ position:absolute;height:7%;bottom:-1px;width:50%;left:50%;right:0;}'+
					
					'.'+shdwImgClass+' .topcorner{ background:url('+path+shdwImgClass+boxGIF+') no-repeat 0 0;}'+
					'.'+shdwImgClass+' .topbar{ background:url('+path+shdwImgClass+boxGIF+') no-repeat 100% 0;}'+
					'.'+shdwImgClass+' .left{ background:url('+path+shdwImgClass+borderGIF+') repeat-y 0 0;}'+
					'.'+shdwImgClass+' .right{ background:url('+path+shdwImgClass+borderGIF+') repeat-y 100% 0;}'+
					'.'+shdwImgClass+' .bottomcorner{ background:url('+path+shdwImgClass+boxGIF+') no-repeat 0 100%;}'+
					'.'+shdwImgClass+' .bottombar{ background:url('+path+shdwImgClass+boxGIF+') no-repeat 100% 100%;}'+
					'.'+shdwImgClass+' .bottombar{ background:url('+path+shdwImgClass+boxGIF+') no-repeat 100% 100%;}';
					
					// '.'+ccsClass+' .topcorner{ background:url('+path+ccsClass+boxGIF+') no-repeat 0 0;}'+
					// '.'+ccsClass+' .topbar{ background:url('+path+ccsClass+boxGIF+') no-repeat 100% 0;}'+
					// '.'+ccsClass+' .left{ background:url('+path+ccsClass+borderGIF+') repeat-y 0 0;}'+
					// '.'+ccsClass+' .right{ background:url('+path+ccsClass+borderGIF+') repeat-y 100% 0;}'+
					// '.'+ccsClass+' .bottomcorner{ background:url('+path+ccsClass+boxGIF+') no-repeat 0 100%;}'+
					// '.'+ccsClass+' .bottombar{ background:url('+path+ccsClass+boxGIF+') no-repeat 100% 100%;}';
	
			//var css = (IE6)?cssie6:css;
			changeStyle(styles, css);
		}
		
		if(reRun==true&&secRun==false){
			// DEFINE CSS -----------------
			var css = '.'+ccwClass+' .topcorner{ background-image:none;}'+'.'+ccwClass+':hover .topcorner{ background:url('+path+ccwClass+box+') no-repeat 0 0;}'+
					'.'+ccwClass+' .topbar{ background-image:none;}'+'.'+ccwClass+':hover .topbar{ background:url('+path+ccwClass+box+') no-repeat 100% 0;}'+
					'.'+ccwClass+' .left{ background-image:none;}'+'.'+ccwClass+':hover .left{ background:url('+path+ccwClass+border+') repeat-y 0 0;}'+
					'.'+ccwClass+' .right{ background-image:none;}'+'.'+ccwClass+':hover .right{ background:url('+path+ccwClass+border+') repeat-y 100% 0;}'+
					'.'+ccwClass+' .bottomcorner{ background-image:none;}'+'.'+ccwClass+':hover .bottomcorner{ background:url('+path+ccwClass+box+') no-repeat 0 100%;}'+
					'.'+ccwClass+' .bottombar{ background-image:none;}'+'.'+ccwClass+':hover .bottombar{ background:url('+path+ccwClass+box+') no-repeat 100% 100%;}';

			var cssie6 = '.'+ccwClass+':hover .topcorner{ background:url('+path+ccwClass+boxGIF+') no-repeat 0 0;}'+
					'.'+ccwClass+':hover .topbar{ background:url('+path+ccwClass+boxGIF+') no-repeat 100% 0;}'+
					'.'+ccwClass+':hover .left{ background:url('+path+ccwClass+borderGIF+') yellow repeat-y 0 0;}'+
					'.'+ccwClass+':hover .right{ background:url('+path+ccwClass+borderGIF+') repeat-y 100% 0;}'+
					'.'+ccwClass+':hover .bottomcorner{ background:url('+path+ccwClass+boxGIF+') no-repeat 0 100%;}'+
					'.'+ccwClass+':hover .bottombar{ background:url('+path+ccwClass+boxGIF+') no-repeat 100% 100%;}';
					
			var oldcss = styles.innerHTML;
			//var css = (IE6)? oldcss+cssie6: oldcss+css;
			changeStyle(styles, css);
			secRun = true;
		}	
	}
}
