/* ==========================================================
     Design for Mambo javascript common display engine v1.0

     Author  : Nguyen Manh Cuong
     Email   : cuongnm@designformambo.com
     Homepage: http://designformambo.com
========================================================== */

if (typeof d4m_display_engine_included == 'undefined') {
	// browser window`s width and height
	var window_width;
	var window_height;
	if( typeof( window.innerWidth ) == 'number' ) {
	  window_width = window.innerWidth;
	  window_height = window.innerHeight;
	} else if( document.documentElement &&
	    ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	  window_width = document.documentElement.clientWidth;
	  window_height = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	  window_width = document.body.clientWidth;
	  window_height = document.body.clientHeight;
	}
	
	// is Netscape?
	var ns = (navigator.appName.indexOf("Netscape") != -1);
	// is IE?
	var ie = (navigator.appVersion.indexOf("MSIE") != -1);
	
	function oldValueContainer() {
		// Pointer to hidden div containing old data/value
		if (!document.getElementById('old_value')) {
			var oldValue = document.createElement('div');
			oldValue.setAttribute('id', 'old_value');
			oldValue.style.display = 'none';
			document.getElementsByTagName('body').item(0).appendChild(oldValue);
		} else {
			var oldValue = document.getElementById('old_value');
		}
		return oldValue;
	}
	
	// Function to clear the data of an element
	function clearElement(node) {
		clearChildNodes(node);
		node.innerHTML = '';
	}
	
	// Switch tab function
	function switchActiveTab(obj, activeTabId) {
		var old = document.getElementById(activeTabId);
		old.removeAttribute('id');
		obj.setAttribute('id', activeTabId);
	}
	
	// Swap element data/value function
	function swapElementStatus(obj, isImg, newValue, stringOrImgPath) {
		if (typeof obj != 'object' && !document.getElementById(obj)) {
			return;
		} else {
			obj = typeof obj == 'object' ? obj : document.getElementById(obj);
			oldValue = oldValueContainer();
			if (isImg) {
				if (obj.src == stringOrImgPath || obj.src == mosConfig_live_site+'/'+stringOrImgPath) {
					if (newValue != '') {
						obj.src = newValue;
					} else {
						obj.src = oldValue.innerHTML;
						clearElement(oldValue);
					}
				} else {
					oldValue.innerHTML = obj.src;
					obj.src = stringOrImgPath;
				}
			} else {
				if (obj.innerHTML == stringOrImgPath || obj.innerHTML == '<a>'+stringOrImgPath+'</a>') {
					if (newValue != '') {
						obj.innerHTML = newValue;
					} else {
						obj.innerHTML = oldValue.innerHTML;
						clearElement(oldValue);
					}
				} else {
					oldValue.innerHTML = obj.innerHTML;
					obj.innerHTML = stringOrImgPath;
				}
			}
		}
	}
	
	// show/hide loading text
	function swapLoading(loading_text) {
		if (!document.getElementById('loading_text')) {
			var loadingText = document.createElement('div');
			loadingText.setAttribute('id', 'loading_text');
			var att = document.createAttribute('class');
			att.value = 'loading_text';
			loadingText.setAttributeNode(att);
			loadingText.style.position = 'absolute';
			loadingText.style.display = 'none';
			loadingText.style.textAlign = 'center';
			loadingText.style.width = String(loading_text.length * 7) + 'px';
			loadingText.style.lineHeight = '20px';
			loadingText.style.left = String(window_width - (loading_text.length * 7) - 35) + 'px';
			document.getElementsByTagName('body').item(0).appendChild(loadingText);
			loadingText.innerHTML = '<b>' + loading_text + '</b>';
		} else {
			var loadingText = document.getElementById('loading_text');
		}
		var pY = ns ? pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
		var top = (pY > 15) ? (pY + 15) : 15;
		loadingText.style.top = top+'px';
		if (arguments.length > 1) {
			if (arguments[1] == true) {
				loadingText.style.display = 'block';
			} else {
				loadingText.style.display = 'none';
			}
		} else {
			if (loadingText.style.display == 'none') {
				loadingText.style.display = 'block';
			} else {
				loadingText.style.display = 'none';
			}
		}
	}
	
	// Set position for Popup Div
	function PopupPosition(obj, width, height) {
		obj.style.position = 'absolute';
		obj.style.width = width + 'px';
		obj.style.height = '100%';
		if (arguments.length > 3 && arguments[3] != null) {
			if (arguments.length > 4 && arguments[4] != null)
				var padding = arguments[4];
			else
				var padding = 0;
			if (arguments[3] == 'left') {
				obj.style.left = String(1 + padding) + 'px';
			} else if (arguments[3] == 'right') {
				if (ie) {
					obj.style.left = String(Math.round(window_width - width - padding - 3)) + 'px';
				} else {
					obj.style.left = String(Math.round(window_width - width - padding - 20)) + 'px';
				}
			}
			if (ie) {
				obj.style.marginTop = '20px';
			}
		} else {
			var left = Math.round((window_width - width) / 2);
			var startY = Math.round((window_height - height) / 2);
			var pY = ns ? pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
			var top = (pY > startY) ? (pY + startY) : startY;
			obj.style.top = top + 'px';
			obj.style.left = left + 'px';
		}
		obj.style.display = 'block';
	}
	
	// Set content for Popup Div
	function PopupContent(action, id, title, toolbar, content, width, height, pathToCloseImg) {
		real_id = action + (id != '' ? '_'+id : '');
		oldValue = oldValueContainer();
		var current_form = document.getElementById(real_id+'_form');
		if (!document.getElementById(real_id+'_list')) {
			// Create new elements
			var hiddenDivContent = document.createElement('div'); //div
			var att1 = document.createAttribute('class');
			att1.value = 'hiddenDivContent';
			hiddenDivContent.setAttributeNode(att1);
			
			var hiddenDivTable = document.createElement('table'); //table
			var att2 = document.createAttribute('class');
			att2.value = 'hiddenDivTable';
			hiddenDivTable.setAttributeNode(att2);
			hiddenDivTable.setAttribute('border','0');
			hiddenDivTable.setAttribute('cellspacing','0');
			hiddenDivTable.setAttribute('cellpadding','0');
			hiddenDivTable.style.width = '100%';
			
			var hiddenDivTableTR = document.createElement('tr'); //title row
			
			var hiddenDivTableTD1 = document.createElement('td'); //title col in title row
			var att3 = document.createAttribute('class');
			att3.value = 'hiddenDivTableHeader';
			hiddenDivTableTD1.setAttributeNode(att3);
			hiddenDivTableTD1.setAttribute('id',real_id+'_title');
			
			var hiddenDivTableTD2 = document.createElement('td'); //close button col in title row
			var att4 = document.createAttribute('class');
			att4.value = 'hiddenDivTableHeader';
			hiddenDivTableTD2.setAttributeNode(att4);
			hiddenDivTableTD2.style.textAlign = 'right';
			
			if (toolbar != '') {
				var hiddenDivTableToolbarTR = document.createElement('tr'); //toolbar row
				var hiddenDivTableToolbarTD = document.createElement('td'); //toolbar col
				var att5 = document.createAttribute('class');
				att5.value = 'hiddenDivTableToolbar';
				hiddenDivTableToolbarTD.setAttributeNode(att5);
				hiddenDivTableToolbarTD.setAttribute('id',real_id+'_toolbar');
				hiddenDivTableToolbarTD.setAttribute('colspan','2');
			}
			
			var hiddenDivTableBodyTR = document.createElement('tr'); //content row
			
			var hiddenDivTableBodyTD = document.createElement('td'); //content col
			hiddenDivTableBodyTD.setAttribute('colspan','2');
			
			var hiddenDivTableBody = document.createElement('div'); // body div
			var att6 = document.createAttribute('class');
			att6.value = 'hiddenDivTableBody';
			hiddenDivTableBody.setAttributeNode(att6);
			hiddenDivTableBody.setAttribute('id',real_id+'_list');
			hiddenDivTableBody.style.height = height+'px';
			
			// Append new elements
			clearElement(oldValue);
			oldValue.appendChild(hiddenDivContent);
			hiddenDivContent.appendChild(hiddenDivTable);
			
			hiddenDivTable.appendChild(hiddenDivTableTR);
			hiddenDivTableTR.appendChild(hiddenDivTableTD1);
			hiddenDivTableTR.appendChild(hiddenDivTableTD2);
			
			if (toolbar != '') {
				hiddenDivTable.appendChild(hiddenDivTableToolbarTR);
				hiddenDivTableToolbarTR.appendChild(hiddenDivTableToolbarTD);
			}
			
			hiddenDivTable.appendChild(hiddenDivTableBodyTR);
			hiddenDivTableBodyTR.appendChild(hiddenDivTableBodyTD);
			hiddenDivTableBodyTD.appendChild(hiddenDivTableBody);
			
			// Input content for new elements
			document.getElementById(real_id+'_title').innerHTML = title;
			
			var close_link = document.createElement('a');
			close_link.setAttribute('href','javascript: void(0)');
			close_link.setAttribute('onClick','document.getElementById(\''+real_id+'_form\').style.display = \'none\'');
			close_link.setAttribute('title','Close');
			
			var close_img = document.createElement('img');
			close_img.setAttribute('id',real_id+'_close');
			close_img.setAttribute('src',pathToCloseImg);
			close_img.setAttribute('border','0');
			close_img.style.margin = '2px';
			
			hiddenDivTableTD2.appendChild(close_link);
			close_link.appendChild(close_img);
			
			if (toolbar != '') {
				document.getElementById(real_id+'_toolbar').innerHTML = toolbar;
			}
			
			if (arguments.length > 8 && arguments[8] == true) { // keep innerHTML of current form
				document.getElementById(real_id+'_list').innerHTML = current_form.innerHTML + content;
				clearElement(current_form);
			} else {
				document.getElementById(real_id+'_list').innerHTML = content;
			}
			
			current_form.innerHTML = oldValue.innerHTML;
			clearElement(oldValue);
		} else if (content != null && content != '') {
			document.getElementById(real_id+'_list').innerHTML = content;
		}
		if (arguments.length > 9 && arguments[9] != null) { // popup div alignment is set to left or right
			if (arguments.length > 10 && arguments[10] != null) // padding to edge is set
				PopupPosition(current_form, width, height, arguments[9], arguments[10]);
			else
				PopupPosition(current_form, width, height, arguments[9]);
		} else {
			PopupPosition(current_form, width, height);
		}
	}
	
	// Popup thumbnail preview on mouseover
	function showImage(name, url) {
		var html=name;
		if (url != null && url != '') {
			html = '<img border="0" src="'+url+'" name="imagelib" alt="No image selected" />';
		} else {
			html = 'No image selected';
		}
		return overlib(html, CAPTION, '&nbsp;'+name);
	}
	
	// Scroll to top
	function scrollToTop() {
		window.scrollBy(0,-9999); // horizontal and vertical scroll increments
	}
	var d4m_display_engine_included = 1; // Has been loaded
}