/* START: /wp-srv/js/utilsStatic.js */
function showContentDown(pid,cid) {
	if ( document.getElementById(pid) && document.getElementById(cid) ) {
		var parent = document.getElementById(pid);
		var child = document.getElementById(cid);
		child.style.position = 'absolute';
		child.className = 'show';
		child.style.top = parent.offsetTop+parent.offsetHeight;
		child.style.left = parent.offsetLeft;
		child.style.zIndex = '10000';
	}
}
function showContentUp(pid,cid) {
	if ( document.getElementById(pid) && document.getElementById(cid) ) {
		var parent = document.getElementById(pid);
		var child = document.getElementById(cid);
		child.style.position = 'absolute';
		child.className = 'show';
		child.style.top = parent.offsetTop-child.offsetHeight;
		child.style.left = parent.offsetLeft;
		child.style.zIndex = '10000';
	}
}
function showContentLeft(pid,cid) {
	if ( document.getElementById(pid) && document.getElementById(cid) ) {
		var parent = document.getElementById(pid);
		var child = document.getElementById(cid);
		child.style.position = 'absolute';
		child.className = 'show';
		child.style.top = parent.offsetTop;
		child.style.left = parent.offsetLeft+parent.offsetWidth;
		child.style.zIndex = '10000';
	}
}
function showContentRight(pid,cid) {
	if ( document.getElementById(pid) && document.getElementById(cid) ) {
		var parent = document.getElementById(pid);
		var child = document.getElementById(cid);
		child.style.position = 'absolute';
		child.className = 'show';
		child.style.top = parent.offsetTop;
		child.style.left = parent.offsetLeft-child.offsetWidth;
		child.style.zIndex = '10000';
	}
}
function showContent(id) {
	if ( document.getElementById(id) ) {
		var element = document.getElementById(id);
		element.className = 'show';
	}
}
function getValue(name) {
	var re = new RegExp("[&\?]"+name+"=([^&]+)&?","g") ;
	if (location.search.match(re)) return RegExp.$1 ;
	else return "" ;
}
function replaceTheLastCharacter(id,character) {
	if ( document.getElementById(id) ) {
		var e = document.getElementById(id) ;
		e.innerHTML = e.innerHTML.replace(/.$/,character)
	}
}
function replaceTheFirstCharacter(id,character) {
	if ( document.getElementById(id) ) {
		var e = document.getElementById(id) ;
		e.innerHTML = e.innerHTML.replace(/^./,character)
	}
}
function replaceLastInstanceOf(id,search,replace) {
	if ( document.getElementById(id) ) {
		var e = document.getElementById(id) ;
		var temp = "" + e.innerHTML;
		if (temp.lastIndexOf(search)>-1) {
			var pos = temp.indexOf(search) ;
			temp = "" + (temp.substring(0, pos) + replace +
			temp.substring((pos + search.length), temp.length)) ;
		}
		e.innerHTML = temp ;
	}
}
function findPosition(id) {
	if ( document.getElementById(id) ) {
		var e = document.getElementById(id) ;
		var left = 0 ;
		var top = 0 ;
		if (e.offsetParent) {
			left = e.offsetLeft ;
			top = e.offsetTop ;
			while ( e = e.offsetParent ) {
				left += e.offsetLeft ;
				top += e.offsetTop ;
			}
		}
	}
	return {x:left,y:top} ;
}
function moveContentBelow(pid,cid) {
	if ( document.getElementById(pid) && document.getElementById(cid) ) {
		var parent = document.getElementById(pid);
		var child = document.getElementById(cid);
		child.style.position = 'absolute';
		child.style.top = findPosition(pid).y+parent.offsetHeight;
		child.style.left = findPosition(pid).x;
		child.style.zIndex = '10000';
	}
}
function moveContentAbove(pid,cid) {
	if ( document.getElementById(pid) && document.getElementById(cid) ) {
		var parent = document.getElementById(pid);
		var child = document.getElementById(cid);
		child.style.position = 'absolute';
		child.style.top = findPosition(pid).y-child.offsetHeight;
		child.style.left = findPosition(pid).x;
		child.style.zIndex = '10000';
	}
}
function moveContentToTheRight(pid,cid) {
	if ( document.getElementById(pid) && document.getElementById(cid) ) {
		var parent = document.getElementById(pid);
		var child = document.getElementById(cid);
		child.style.position = 'absolute';
		child.style.top = findPosition(pid).y;
		child.style.left = findPosition(pid).x+parent.offsetWidth;
		child.style.zIndex = '10000';
	}
}
function moveContentToTheLeft(pid,cid) {
	if ( document.getElementById(pid) && document.getElementById(cid) ) {
		var parent = document.getElementById(pid);
		var child = document.getElementById(cid);
		child.style.position = 'absolute';
		child.style.top = findPosition(pid).y;
		child.style.left = findPosition(pid).x-child.offsetWidth;
		child.style.zIndex = '10000';
	}
}
function moveContentOnTopOf(pid,cid) {
	if ( document.getElementById(pid) && document.getElementById(cid) ) {
		var parent = document.getElementById(pid);
		var child = document.getElementById(cid);
		child.style.position = 'absolute';
		child.style.top = findPosition(pid).y;
		child.style.left = findPosition(pid).x;
		child.style.zIndex = '10000';
	}
}
function shiftDown(id,tweak) {
	if ( document.getElementById(id) ) {
		var e = document.getElementById(id) ;
		var top = e.style.top.replace('px','')/1 ;
		e.style.top = top + tweak ;
	}
}
function shiftUp(id,tweak) {
	shiftDown(id,-1*tweak) ;
}
function shiftLeft(id,tweak) {
	if ( document.getElementById(id) ) {
		var e = document.getElementById(id) ;
		var left = e.style.left.replace('px','')/1 ;
		e.style.left = left - tweak ;
	}
}
function shiftRight(id,tweak) {
	shiftLeft(id,-1*tweak) ;
}
function on(e) {
	e.className = 'on' ;
}
function off(e) {
	e.className = 'off' ;
}
function showContent(id) {
	if ( document.getElementById(id) ) {
		var element = document.getElementById(id);
		replaceClassName(id,'hide','show');
	}
}
function hideContent(id) {
	if ( document.getElementById(id) ) {
		var element = document.getElementById(id);
		replaceClassName(id,'show','hide');
	}
}
function setClassName(id,name) {
	if ( document.getElementById(id) ) {
		var e = document.getElementById(id) ;
		e.className = name;
	}
}
var tags_visibility = new Array(); // global because showTags needs it, too
function hideTags(tag,parent_id) {
	tags_visibility = new Array();
	var parent = document ;
	if ( parent_id && document.getElementById( parent_id ) ) {
		parent = document.getElementById( parent_id ) ;
	}

	var tags = parent.getElementsByTagName(tag) ;
	for (var i=0; i<tags.length; i++) {
		var tag = tags[i];
		tags_visibility[i] = tag.style.visibility ;
		tag.style.visibility = 'hidden' ;
	}
}
function showTags(tag,parent_id) {

/*
	var parent = document ;
	if ( parent_id && document.getElementById( parent_id ) ) {
		parent = document.getElementById( parent_id ) ;
	}

	var tags = parent.getElementsByTagName(tag) ;
	for (var i=0; i<tags.length; i++) {
		var tag = tags[i];
		tag.style.visibility = tags_visibility[i] ;
	}
	
*/
}
function replaceClassName(id,search,replace) {
	if ( document.getElementById(id) ) {
		var e = document.getElementById(id) ;
		var classes = e.className.split(/\s+/);
		var new_classes = '';
		var found_it = false ;
		for (var i=0; i<classes.length; i++) {
			if ( classes[i] == search ) {
				classes[i] = replace ;
				found_it = true ;
			}
			new_classes += classes[i] ;
			if ( i != classes.length -1 ) new_classes += ' ' ;
		}
		if ( !found_it ) {
			if ( new_classes )
				new_classes += ' ' ;
			new_classes += replace ;
		}
		e.className = new_classes ;
	}
}
function setHref(id,href) {
	if ( document.getElementById(id) ) {
		var e = document.getElementById(id) ;
		e.href = href ;
	}
}
function getUrlFromId(id,page) {
	var url = '' ;
	page = ( page ) ? '_'+page : '' ;

	if ( id.match(/^[A-Z]{2,2}\d{4,4}\d{2,2}\d{2,2}\d+$/) ) {
		var m = id.match(/^([A-Z]{2,2})(\d{4,4})(\d{2,2})(\d{2,2})\d+$/) ;
		var type = getTypeFromStub(m[1]).toLowerCase();
		url = '/wp-dyn/content/'+type+'/'+m[2]+'/'+m[3]+'/'+m[4]+'/'+id+page+'.html' ;
	}
	return url ;
}
function getTypeFromStub(stub) {
	switch ( stub ) {
		case 'AR': return 'Article' ;
		case 'BL': return 'Blog' ;
		case 'DI': return 'Discussion' ;
		case 'AU': return 'Audio' ;
		case 'GA': return 'Gallery' ;
		case 'GR': return 'Graphic' ;
		case 'PA': return 'Panorama' ;
		case 'PH': return 'Photo' ;
		case 'VI': return 'Video' ;
	}
}
// start: document enhancement
document.getElementsByClassName = function(target_class) {
	var target_elements = new Array();
	var elements = document.getElementsByTagName("*");
	for (var i = 0;i < elements.length;i++) {
		if (elements[i].className.indexOf(" ") >= 0) {
			var classes = elements[i].className.split(" ");
			for (var j = 0;j < classes.length;j++) {
				if (classes[j] == target_class) {
					target_elements.push(elements[i]);
				}
			}
		} else if (elements[i].className == target_class) {
			target_elements.push(elements[i]);
		}
	}
	return target_elements;
}
// end: document enhancement
// function layoutPage(root_element,page) {
// then use its position to set height and width
function layoutPage(page) {
	var topPagePadding = 2 ;
	var leftPagePadding = 2 ;

	var height = 0 ;
	if ( page.top_to_bottom ) {
		var left_to_right_ids = new Array();
		for (var i=0;i<page.top_to_bottom.length;i++) {
			var top_to_bottom = page.top_to_bottom[i] ;
			var id = top_to_bottom.id ;
			if ( top_to_bottom.left_to_right ) {
				width = 0 ;
				for (var j=0;j<top_to_bottom.left_to_right.length;j++) {
					var left_to_right = top_to_bottom.left_to_right[j] ;
					var id2 = left_to_right.id ;

					if ( document.getElementById(id2) ) {
						element = document.getElementById(id2) ;
						element.style.position = "absolute" ;
						element.style.top = 0 ;
						element.style.left = width ;
					
						width += document.getElementById(id2).offsetWidth ;

						left_to_right_ids[left_to_right_ids.length] = id2 ;

						/* Activate when ready to make it recursive, probably more than this needs to change
						   Will need to take into acount the position of the parent element
						*/
						if ( left_to_right.top_to_bottom ) {
							// layoutPage(left_to_right);
						}
					}
				}
			}

			if ( document.getElementById(id) ) {
				element = document.getElementById(id) ;
				element.style.position = "absolute" ;
				element.style.top = height+topPagePadding ;
				element.style.left = 0 +leftPagePadding ;

				height += (left_to_right_ids.length) ? getTallest(left_to_right_ids) : document.getElementById(id).offsetHeight ;
			}

		}
	}
}
function getTallest(ids) {
	var tallest = 0 ;
	for ( var i=0; i < ids.length; i++ ) {
		var id = ids[i] ;
		var h = document.getElementById(id).offsetHeight ;
		if ( h > tallest ) tallest = h ;
	}
	return tallest ;
}
/* END: /wp-srv/js/utilsStatic.js */

