/*
	TOC
	1. Round corners
*/



/*
-----------------  1. Round corners ------------------------------------------------------*/	

// Get links that are buttons, place these links within a <span> and <div> then append back in page
// Also change the name of the parent contaier div to allow for styling of button without JavaScript
		
var roundCorners = {

	/** Buttons **/
	buttons:function() {
		var links = g.tn("a");
		for (var i = 0; i < links.length; i++) {
			if (links[i].className && (' ' + links[i].className + ' ').indexOf(' round ') != -1) {
				// Create Button and surrounding elements
				var emptyDiv = g.ceCn("div", "emptybut");
				var button = g.ceCn("div", "button");
				var span = g.ce("span");
				// Assign current link to a variable and give its parent an id, replace the divs className
				var parentTest = links[i];
				var buttonContainer = roundCorners.getButtonContainer(parentTest);
				buttonContainer.setAttribute("id", "container" + i);
				var getCurrentClass = buttonContainer.className;
				var startPos = getCurrentClass.indexOf(getCurrentClass);
				var restOfString = getCurrentClass.substr(startPos +17);			
				var containerDiv = g.id("container" + i);			
				containerDiv.className = "js-button-container " + restOfString;
				// Apend elements to their parents
				span.appendChild(links[i]);
				button.appendChild(span);
				containerDiv.appendChild(emptyDiv);
				containerDiv.appendChild(button);
			}
		}				
	},
	
	/** Boxes **/
	boxes:function() {
		var div = g.tn("div");
		for (var i = 0; i < div.length; i++) {
			// Find the Divs witht the className corners
			if (div[i].className && (' ' + div[i].className + ' ').indexOf(' corners ') != -1) {
				// Create elements to be appended to page
				var emptyDiv = g.ceCn("div", "empty");
				var inner = g.ceCn("div", "inner");
				inner.setAttribute("id", "inner" + [i])
				
				// Get the element from the document.  (Corresponds to the className)
				// Get all of the child elements and store in an array called "boxContentArray"
				if (!g.id(div[i].id)) return false;
				var container = g.id(div[i].id);
				container.style.border = "0";
				
				var boxContentArray = g.elementWrap(container);
				
				// Append Elements back on page and Loop through "boxContentArray" array appending a new child on each loop
				var boxContent = g.ceCn("div", "box-content clearing");
				for (var k = 0; k < boxContentArray.length; k++) {
				boxContent.appendChild(boxContentArray[k])
				}
				inner.appendChild(boxContent);
				container.appendChild(inner);
				
				// Insert an empty div before the newly created inner Div element			
				var currentInner = g.id("inner" + [i])
				var parentDiv = currentInner.parentNode;				
				parentDiv.insertBefore(emptyDiv, currentInner);
			}
		}
	},	
	
	// Get parent node until DIV with className button-container
	getButtonContainer:function(node) {
		if ( node.nodeName == "DIV" && node.className.indexOf("button-container") != -1 ) {
		return node;
		}
		if ( node.className.indexOf("button-container") == -1  ) { 
		return roundCorners.getButtonContainer(node.parentNode);
		}
		return null;
	}
}
		


/*
-----------------  7. Man in Blue Resolution Dependent Layout Script & ecmascript.stchur.com IE resize script -----------------------------------*/	
	

var resDepLayout = {
	
	/** Check initial size and provide suitable CSS **/
	checkBrowserWidth:function() {
		var theWidth = resDepLayout.getBrowserWidth();
			
		if (theWidth == 0)	{
			var resolutionCookie = document.cookie.match(/(^|;)tmib_res_layout[^;]*(;|$)/);
			if (resolutionCookie != null) {
				setStylesheet(unescape(resolutionCookie[0].split("=")[1]));
			}
			g.addLoadEvent(resDepLayout.checkBrowserWidth);
			return false;
		}
			
		if (theWidth > 994)	{
			resDepLayout.setStylesheet("1024");
			
			document.cookie = "tmib_res_layout=";
			document.cookie = "tmib_res_layout=" + escape("1024");
			
		} else {
			resDepLayout.setStylesheet("");
			
		} return true;
	},
	
	
	getBrowserWidth:function() {
	
		if (window.innerWidth)	{ //FF
			return window.innerWidth;
		} else if (document.documentElement && document.documentElement.clientWidth) { 	
			return document.documentElement.clientWidth;
		} else if (document.body) {
			return document.body.clientWidth;
		}	
		return 0;
	},
	
	setStylesheet:function (styleTitle) {
		var currTag;
		
		if (document.getElementsByTagName) {
		
		
			for (var i = 0; (currTag = document.getElementsByTagName("link")[i]); i++) {
			
				if (currTag.getAttribute("rel").indexOf("style") != -1 && currTag.getAttribute("title")) {
					
					currTag.disabled = true;
					if(currTag.getAttribute("title") == styleTitle) {
						currTag.disabled = false;
						
					}
				}
			}
		}
		return true;
	},		
	
	xBrowserResize:function () {
		window.g_prevSize = resDepLayout.getViewportSize();
		setInterval(resDepLayout.resize, 10);
	},
	
	
	resize:function () {
		var currentSize = resDepLayout.getViewportSize();
		
		if (currentSize[0] != g_prevSize[0] || currentSize[1] != g_prevSize[1]) {
		g_prevSize = currentSize;
		
		var theWidth = currentSize[0];
		
		if (theWidth > 994) {
			resDepLayout.setStylesheet("1024");
			document.cookie = "tmib_res_layout=" + escape("1024");
		} else {
			resDepLayout.setStylesheet("");
			document.cookie = "tmib_res_layout=";
		}
		}
	},

	getViewportSize: function() {
		var size = [0, 0];
		if (typeof window.innerWidth != 'undefined') {
			size = [ window.innerWidth, window.innerHeight ];
		} else if (typeof	document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
			size = [ document.documentElement.clientWidth, document.documentElement.clientHeight ];
		} else {
			size = [ document.getElementsByTagName('body')[0].clientWidth, document.getElementsByTagName('body')[0].clientHeight ];
		}
		return size;				
	}

}



/*
-----------------  8. Window Popups ------------------------------------------------------*/	

/** Open in new window **/
	
var popups = {

	links:g.tn("a"),
	
	/** Standard popup window **/
	standard:function(){
		if(!document.getElementsByTagName) return false
			for(var i=0; i<popups.links.length; i++){
				if (popups.links[i].className && (" "+popups.links[i].className+" ").indexOf(" popup ")!=-1 ){
					popups.links[i].onclick=function(){
						window.open(this.href, ""); 
						return false;
				}
			}
		}
	},

	/** Metafaq popup window **/
	metafaq:function(){
		if(!document.getElementsByTagName) return false;
			for(var i=0;i<popups.links.length;i++){
				if(popups.links[i].className&&(" "+popups.links[i].className+" ").indexOf(" metafaq ")!=-1){
					popups.links[i].onclick=function(){
						window.open(this.href,'mypop',"height=650, width=780, status, resizable, scrollbars, toolbar"); 
						return false;
				}
			}
		}
	}
	
}
	
	

/*
----------------- 1. Load events on page load -----------------------------*/
       
    if (is_mac && is_ie) {
		g.addEvent(window, 'load', popups.standard, false);
		g.addEvent(window, 'load', popups.metafaq, false);
	} else {
		g.addEvent(window, 'load', popups.standard, false);
		g.addEvent(window, 'load', popups.metafaq, false);
		resDepLayout.checkBrowserWidth();
		g.addEvent(window, 'load', resDepLayout.xBrowserResize, false);
	}

