/* Rounded corners on 'round' images and 'riw' divs */
(function(container) {
	var wrapperClass = 'riw';
	var addCorners = function(wrapper,tl,tr,br,bl) {
		/* Create the four other inner nodes and give them classnames */
		tl = document.createElement('span');
		tl.className = 'tl';
		br = document.createElement('span');
		br.className = 'br';
		tr = document.createElement('span');
		tr.className = 'tr';
		bl = document.createElement('span');
		bl.className = 'bl';
		/* Glue the nodes back inside the wrapper */
		wrapper.appendChild(tl);
		wrapper.appendChild(tr);
		wrapper.appendChild(bl);
		wrapper.appendChild(br);
	}
	if(!(container = document.getElementById(container))) return;
	var imgs, img, wrapper, original, i, align, title, divs;
	imgs = container.getElementsByTagName('img');
	for (i = 0; i < imgs.length; i++) {         // start loop 
		original = img = imgs[i];
		if(original.className.indexOf('round')!=-1) {
			wrapper = document.createElement('span');  // Create the outer-most div (wrapper)
			align = /(left|right|center)/.exec(original.className);
			wrapper.className = wrapperClass+(align ? ' '+align[1] : '');    // Give it a classname - riw left/right/center
			wrapper.style.width = original.width+'px';     // give wrapper the same width as the current img
			/* Swap out the original img with our wrapper div (we'll put it back later) */
			if(img.parentNode.tagName.toUpperCase()=='A') original = img.parentNode; // if you link the image this will help the script find the right parent wrapper
			original.parentNode.replaceChild(wrapper, original);
	
			addCorners(wrapper);

			/* And glue the img back in after the DIVs */
			wrapper.appendChild(original);
			
			if(title = img.title) {
				tspan = document.createElement('span');
				tspan.className = 'title';
				tspan.innerHTML = title;
				wrapper.appendChild(tspan);
			}
		}
	}
	divs = container.getElementsByTagName('div');
	for(i=divs.length-1; i>=0; i--) {
		wrapper = divs[i];
		if(wrapper.className.indexOf(wrapperClass)!=-1) addCorners(wrapper);
	}
	divs = container.getElementsByTagName('li');
	for(i=divs.length-1; i>=0; i--) {
		wrapper = divs[i];
		if(wrapper.className.indexOf(wrapperClass)!=-1) addCorners(wrapper);
	}
})('inner-content');

(function(){
	var a;
	if(document.body.className.indexOf('program')==-1 && document.body.className.indexOf('headings')==-1) return;
	var ss = document.getElementsByTagName('span');
	for(var i=ss.length; i-->0;) {
		if(ss[i].className.indexOf('riw')!=-1) {
			if((a = ss[i].getElementsByTagName('a')) && a.length) {
				ss[i].className += ' albumlink';
				if(ss[i].lastChild) ss[i].lastChild.innerHTML += ' [<a href="'+a[0].href+'">see all photos</a>]';
			}
			else ss[i].className += ' flush';
		}
	}
})();

/* zebra striping on 'fancy' tables */
(function(container) { if(container = document.getElementById(container)) {
	var tbls, i, j, rows, t, r;
	tbls = container.getElementsByTagName('tbody');
	for(i=tbls.length-1; i>=0; i--) { t = tbls[i]; if(t.parentNode.className.indexOf('fancy'!=-1)) {
		rows = t.getElementsByTagName('tr');
		for(j=rows.length; j--;) {
			rows[j].className += j%2 ? ' even' : ' odd';
		}
	} }
} })('inner-content');

/* pop links */
(function(container){ if(container = container ? document.getElementById(container) : document) {
	var i, links, dim;
	links = container.getElementsByTagName('a');
	for(i=links.length; i--;) {
		if(dim = /pop([0-9]+)x([0-9]+)/.exec(links[i].rel))
			links[i].onclick = function(w,h) { return function(){ window.open(this.href,(new Date()).getTime(),"resizeable,scrollbars,width="+w+",height="+h); return false; }; }(dim[1],dim[2]);
	}
}})('inner-content');

/* rounded h2's in program pages */
(function(){
	if(document.body.className.indexOf('program')==-1 && document.body.className.indexOf('headings')==-1) return;
	var hs = document.getElementById('inner-content').getElementsByTagName('h2');
	for(var i=hs.length; i-->0;) {
		var br = document.createElement('span');
		br.className = 'br';
		var tr = document.createElement('span');
		tr.className = 'tr';
		hs[i].appendChild(tr);
		hs[i].appendChild(br);
		hs[i].className += ' riw';
		hs[i].style.position = 'relative';
	}
})();

/* fading images */
(function(container,showFor,fadeLength,fadeSteps){
	if(!(container = document.getElementById(container))) return;
	showFor = showFor ? showFor*1000 : 5000;
	fadeLength = fadeLength ? fadeLength*1000 : 1000;
	fadeSteps || (fadeSteps = 10);
	var cur = 0, nex, i, nodes = [], doFade, setOp, doShow, step = 1;
	for(i=0; i<container.childNodes.length; i++) {
		if(container.childNodes[i].nodeType==1) {
			nodes[nodes.length] = container.childNodes[i];
			container.childNodes[i].style.visibility = 'hidden';
			container.childNodes[i].style.zoom = 1;
		}
	}
	nodes[cur] && (nodes[cur].style.visibility = 'visible'); // make first child visible regardless
	if(nodes.length<2) return; // bail if one or zero children
	setOp = function(el,op){
		el.style.opacity = op/100;
		el.style.filter = 'alpha(opacity='+Math.round(op)+')';
		op && (el.style.visibility = 'visible');
	};
	doFade = function(){
		setOp(nodes[nex],100/fadeSteps*step);
		if(step++ < fadeSteps) setTimeout(doFade,fadeLength/fadeSteps);
		else {
			step = 1;
			setOp(nodes[cur],0);
			cur = nex;
			setTimeout(doShow,showFor);
		}
	};
	doShow = function(){
		nex = (cur+1)%nodes.length;
		nodes[nex].style.zIndex = 100;
		nodes[cur].style.zIndex = 10;
		doFade();
	};
	setTimeout(doShow,showFor);
})('frontcharts',6.5,0.7,10);