/* for layout */
function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}
		else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

function setFooter(layoutType, layoutStyle) {
	if (layoutType != 'fixed_size_center' && layoutType != 'fixed_size_top' && layoutType != 'auto') {
		var windowHeight = getWindowHeight();
		if (windowHeight > 0) {
			var contentHeight = document.getElementById('mainUpBox').offsetHeight;
			var footerElement = document.getElementById('mainDownBox');
			var footerHeight  = footerElement.offsetHeight;
			if (windowHeight - (contentHeight + footerHeight) >= 0) {
				footerElement.style.top = (windowHeight - (contentHeight + footerHeight)) + 'px';
			}
			else {
				footerElement.style.top = '0px';
			}
		}
	}
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function setHeaderImage(image) {
	if (trim(image) != '' ) {
		var headerElement = document.getElementById('head');
		headerElement.style.backgroundImage = 'url(../../cms_files/' + image + ')';
	}
}

function setLayout(layoutType, layoutStyle, layoutMainWidth, layoutHeaderHeight, layoutMenuItemHeight, layoutMenuItemWidth, layoutFooterHeight, layoutMenuHeight, layoutContentMarginTop, layoutContentMarginLeft, layoutContentMarginBottom, layoutContentMarginRight, layoutContentHeight, layoutMenuPaddingTop, layoutMenuPaddingLeft, layoutMenuPaddingBottom, layoutMenuPaddingRight) {
	
	var windowHeight = getWindowHeight();
	
	// calculate content height, width
	if (layoutType == 'fixed_auto') {
		if (layoutStyle == 'left') {
			var contentHeight = windowHeight - layoutHeaderHeight - layoutFooterHeight;
		}
		else {
			var contentHeight = windowHeight - layoutHeaderHeight - layoutFooterHeight - layoutMenuItemHeight;
		}
	}
	else if (layoutType == 'fixed_size_top' || layoutType == 'fixed_size_center') {
		var contentHeight = layoutContentHeight;
	}
	
	if (layoutStyle == 'left') {
		var contentWidth = layoutMainWidth - layoutMenuItemWidth;
	}
	else {
		var contentWidth = layoutMainWidth;
	}
	
	
	var contentElement = document.getElementById('contentBox');
	var sectionElement = document.getElementById('sectionBox');	
	
	
	if (layoutType == 'fixed_auto' || layoutType == 'fixed_size_center' || layoutType == 'fixed_size_top') {
		if (Math.ceil(layoutMenuHeight) > Math.ceil(contentHeight)) {
			contentHeight = layoutMenuHeight;
		}
		contentElement.style.height = contentHeight + 'px';
		contentElement.style.width = contentWidth + 'px';
		var sectionHeight = contentHeight - layoutContentMarginTop - layoutContentMarginBottom;
		var sectionWidth = contentWidth - layoutContentMarginLeft - layoutContentMarginRight;
		sectionElement.style.height = sectionHeight + 'px';	
		sectionElement.style.width = sectionWidth + 'px';	
		sectionElement.style.overflow = 'auto';
		sectionElement.style.overflowX = 'hidden';
	}


	//header
	var headerBoxElement = document.getElementById('headerBox');
	headerBoxElement.style.height = layoutHeaderHeight + 'px';

	//footer
	var footerBoxElement = document.getElementById('footerBox');
	footerBoxElement.style.height = layoutFooterHeight + 'px';

	var mainUpElement = document.getElementById('mainUpBox');
	mainUpElement.style.width = layoutMainWidth + 'px';
	var mainDownElement = document.getElementById('mainDownBox');
	mainDownElement.style.width = layoutMainWidth + 'px';

	if (layoutStyle == 'below' || layoutStyle == 'above') {
		var menuElement = document.getElementById('menuBox');
		menuElement.style.width = layoutMainWidth + 'px';
		menuElement.style.height = layoutMenuItemHeight + 'px';
		var menuListElement = document.getElementById('menuList');
		menuListElement.style.height = layoutMenuItemHeight + 'px';
		var listItems = $('menuList').getElementsByTagName('LI');
		for (var i = 0; i < listItems.length; i++){
			listItems[i].style.display = 'block';
			if(listItems[i].style.cssFloat == '') {
				listItems[i].style.cssFloat = 'left';
			}
			if(listItems[i].style.styleFloat == '') {
				listItems[i].style.styleFloat = 'left';
			}
			listItems[i].style.height = layoutMenuItemHeight + 'px';
			listItems[i].style.width = layoutMenuItemWidth + 'px';
			listItems[i].style.paddingTop = layoutMenuPaddingTop + 'px';
			listItems[i].style.paddingLeft = layoutMenuPaddingLeft + 'px';
			listItems[i].style.paddingBottom = layoutMenuPaddingBottom + 'px';
			listItems[i].style.paddingRight = layoutMenuPaddingRight + 'px';
		}
		var listLinks = $('menuList').getElementsByTagName('A');
		for (var i = 0; i < listLinks.length; i++){
			//listLinks[i].style.display = 'block';
			//listLinks[i].style.float = 'left';
			//listLinks[i].style.height = layoutMenuItemHeight + 'px';
			//listLinks[i].style.width = layoutMenuItemWidth + 'px';
		}
	}
	else {
		var menuElement = document.getElementById('menuBox');
		menuElement.style.width = layoutMenuItemWidth + 'px';
		var listItems = $('menuList').getElementsByTagName('LI');
		for (var i = 0; i < listItems.length; i++){
			listItems[i].style.height = layoutMenuItemHeight + 'px';
			listItems[i].style.display = 'block';			
			listItems[i].style.paddingTop = layoutMenuPaddingTop + 'px';
			listItems[i].style.paddingLeft = layoutMenuPaddingLeft + 'px';
			listItems[i].style.paddingBottom = layoutMenuPaddingBottom + 'px';
			listItems[i].style.paddingRight = layoutMenuPaddingRight + 'px';
		}
	}
	
	if (layoutType == 'fixed_size_center') {
		if (layoutStyle == 'left') {
			var topMargin = windowHeight - contentHeight - layoutHeaderHeight - layoutFooterHeight;
		}
		else {
			var topMargin = windowHeight - contentHeight - layoutHeaderHeight - layoutFooterHeight - layoutMenuItemHeight;
		}
		topMargin = Math.round(topMargin / 2);
		if (topMargin > 0) {
			mainUpElement.style.marginTop = topMargin + 'px';
		}
	}
	
}


/* roll over images */

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}



function popUp(url) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(url, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=0,width=610,height=501');");
}

function popUpWithParams(url, height, width, location, menubar, resizable, scrollbars, status, titlebar, toolbar) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(url, '" + id + "', 'height=" + height + ",width=" + width +  ",location=" + location  + ",menubar=" + menubar + ",resizable=" + resizable + ",scrollbars=" + scrollbars + ",status=" + status + ",titlebar=" + titlebar + ",toolbar=" + toolbar + "');");
}