// JAVASCRIPT DOM LIBRARY
// SHAPESHIFT INTERACTIVE 
// CREATED: 19.03.2009
// VER: 1.0 
//----------------------------------------------------------------------------------------------------------

//----------------------------------------------------------------------------------------------------------
// CREATE AND DESTROY
//----------------------------------------------------------------------------------------------------------

function hideElement(obj) {
	if (obj)
		obj.style.display	= 'none';
}

function freeElement(obj) {
	if (obj)
		obj.parentNode.removeChild(obj);
}

freeDiv	= freeElement

function createDiv(div_id, class_name) {
	if (div_id) {
		var newDiv = document.createElement('div');
		newDiv.setAttribute('id', div_id);
		
		if (class_name)
			newDiv.className = class_name;
		
		//document.body.appendChild(newDiv);
		
		return newDiv;
	} else
		return false;
}

//----------------------------------------------------------------------------------------------------------
// POPULATE
//----------------------------------------------------------------------------------------------------------

function setCookie(c_name, value, expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function hideObj(obj) {
	//obj.style.visibility = 'hidden';
	obj.style.display = 'none';
}

function unhideObj(obj) {
	//obj.style.visibility = 'visible';
	obj.style.display = 'block';
}

function drawDiv(div_id, div_class, div_type, div_innerHTML, originator_obj) {
	if (div_id) {
		var container_div		= document.getElementById(div_id);
		
		if (!container_div) {
			container_div		= createDiv(div_id, div_class);
			document.body.appendChild(container_div);
			//document.body.insertBefore(container_div, document.body.childNodes[0])
		}
		
		if (div_type)
			container_div.setAttribute('type', div_type);
		
		var new_div			= createDiv(div_id, div_class);
		
		hideStubborns(document);
		
		new_div.innerHTML	= div_innerHTML;
		
		container_div		= processContainer(container_div, new_div, originator_obj);
	}
}

function setOuterHTML(oldObj, newObj) {
	if (oldObj && newObj) {
		if (isIE())
			oldObj.outerHTML = newObj.outerHTML;
		else
			oldObj.parentNode.replaceChild(newObj, oldObj);
	}
}

function swapInnerHTML(sourceObj, destinationObj) {
	if (sourceObj && destinationObj) {
		var contents				= destinationObj.innerHTML;
		destinationObj.innerHTML	= sourceObj.innerHTML;
		sourceObj.innerHTML			= contents;
	}
}

function mergeEvents(targetObj, sourceObj) {
	
}

function mergeAttributes(targetObj, sourceObj) {
	if (targetObj && sourceObj) {
		if (isIE())
			targetObj.mergeAttributes(sourceObj);
		else {
			for (i=0; i<sourceObj.attributes.length; i++)
				targetObj.setAttribute(sourceObj.attributes[i].nodeName, sourceObj.attributes[i].nodeValue);
		}
	}
	
	return targetObj;
}

function inheritAll(targetObj, sourceObj) {
	if (targetObj && sourceObj) {
		mergeEvents(targetObj, sourceObj)
		
		mergeAttributes(targetObj, sourceObj);
		
		targetObj.innerHTML	= sourceObj.innerHTML;
	}
}

function swapNode(firstObj, secondObj) {
	var bubble_div	= createDiv('bubble_' + Math.floor(Math.random() * 999999), '');
	
	firstObj.parentNode.appendChild(bubble_div);
	
	firstObj.parentNode.replaceChild(bubble_div, firstObj);
	
	secondObj.parentNode.replaceChild(firstObj, secondObj);
	
	bubble_div.parentNode.replaceChild(secondObj, bubble_div);
}

function swapAttribute(firstObj, secondObj, attribute) {
	var bubble_div			= createDiv('bubble_' + Math.floor(Math.random() * 999999), '');
	
	bubble_div.setAttribute(attribute, firstObj.getAttribute(attribute));
	
	firstObj.setAttribute(attribute, secondObj.getAttribute(attribute));
	
	secondObj.setAttribute(attribute, bubble_div.getAttribute(attribute));
}

function swapInnerHTML(firstObj, secondObj) {
	var bubble_div			= createDiv('bubble_' + Math.floor(Math.random() * 999999), '');
	
	bubble_div.innerHTML	= firstObj.innerHTML;
	
	firstObj.innerHTML		= secondObj.innerHTML;
	
	secondObj.innerHTML		= bubble_div.innerHTML;
}

function inheritAttribute(sourceObj, destinationObj, attribute) {
	destinationObj.setAttribute(attribute, sourceObj.getAttribute(attribute))
}

//----------------------------------------------------------------------------------------------------------
// INFORMATION
//----------------------------------------------------------------------------------------------------------

function isIE() {
	var str		= "MSIE";
	var index	= navigator.appVersion.indexOf(str)
	
	return (index > -1)?parseInt(navigator.appVersion.substr(index + str.length + 1, 2)):0;
}

function getElementsByClass(node, searchClass, tag) {
	var classElements = new Array();
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)" + searchClass + "(\\s|$)");
//	var pattern = new RegExp("\b"+searchClass+"\b");
	for (i = 0, j = 0; i < elsLen; i++) {
    	if ( pattern.test(els[i].className) ) {
		    classElements[j] = els[i];
		    j++;
	    }
	}
	return classElements;
}

function divChildIndex(divObj, exclude_empty) {
	var count	= 0;
	
	for (var i=0; i<divObj.parentNode.childNodes.length; i++) {
		if (divObj.parentNode.childNodes[i].nodeName.toLowerCase() == 'div') {
			if (divObj.parentNode.childNodes[i].id == divObj.id)
				return count;
			
			if (exclude_empty && (divObj.parentNode.childNodes[i].innerHTML != ''))
				count++;
		}
	}
	
	return false;
}

getDivIndex	= divChildIndex;

function getPosition(e){ 
	var left = 0;
	var top  = 0;
	
	while (e.offsetParent){
		left	+= e.offsetLeft; 
		top		+= e.offsetTop; 
		e		= e.offsetParent; 
	}
	
	left	+= e.offsetLeft;
	top		+= e.offsetTop;
	
	return {x:left, y:top};
}

function findPosX(obj) {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}

function clientWidth() {
	if (window.innerWidth)
		return window.innerWidth;
	else if (document.documentElement && document.documentElement.clientWidth)
		return document.documentElement.clientWidth;
	else if (document.body)
		return document.body.clientWidth;
	else
		return 0;
}

function clientHeight() {
	if (window.innerHeight)
		return window.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight)
		return document.documentElement.clientHeight;
	else if (document.body)
		return document.body.clientHeight;
	else
		return 0;
}

function documentWidth() {
	return document.body.clientWidtht;
}

function documentHeight() {
	if (document.body && document.body.clientHeight)
		return document.body.clientHeight;
	else if (window.innerHeight)
		return window.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight)
		return document.documentElement.clientHeight;
	else 
		return 0;
}

function getBounds(obj) {
	return {
		x1: findPosX(obj),
		x2: findPosX(obj) + obj.offsetWidth,
		y1: findPosY(obj),
		y2: findPosY(obj) + obj.offsetHeight
	}
}

function hitTest(x, y, obj) {
	var bounds	= getBounds(obj);
	
	return ((x >= bounds.x1) && (x <= bounds.x2) && (y >= bounds.y1) && (y <= bounds.y2));
}

function defaultAttribute(obj, attribute, default_value) {
	return (obj.getAttribute(attribute))?obj.getAttribute(attribute):default_value;
}

//----------------------------------------------------------------------------------------------------------
// POSITIONING
//----------------------------------------------------------------------------------------------------------

function positionChildNodes(parentNode) {
	if (parentNode) {
		var div	= parentNode.getElementsByTagName('div');
		
		for (i=0; i<div.length; i++) {
			if (div[i].getAttribute('position') || div[i].getAttribute('width') || div[i].getAttribute('height')) {
				if (document.documentElement.scrollTop == 0)
					var scrollTop = document.documentElement.scrollTop + document.body.scrollTop;
				else
					var scrollTop = document.documentElement.scrollTop;
				
				//if (isIE())
				//	scrollTop	= 0;
				
				var posX	= 0;
			    var posY	= 0;
			    
			   switch(div[i].getAttribute('width')) {
			    	case 'max':
			    		div[i].style.width	= '100%';
			    		break;
			    	
			    	case 'client':
			    		div[i].style.width	= (clientWidth() - 100) + 'px';
			    		break;
			    	
			    	/*
			    	default:
			    		if (div[i].getAttribute('width') > 0)
				    		div[i].style.width	= div[i].getAttribute('width') + 'px';
				    	else
				    		div[i].style.width	= (clientWidth() - Math.abs(div[i].getAttribute('width'))) + 'px';
			    		break;
			    	*/
			    }
			    
				switch(div[i].getAttribute('height')) {
			    	case 'max':
			    		div[i].style.height	= '100%';
			    		break;
			    	
			    	case 'client':
			    		div[i].style.height	= (clientHeight() - 100) + 'px';
			    		break;
			    	
			    	/*
			    	default:
			    		if (div[i].getAttribute('height') > 0)
				    		div[i].style.height	= div[i].getAttribute('height') + 'px';
				    	else
				    		div[i].style.height	= (clientHeight() - Math.abs(div[i].getAttribute('height'))) + 'px';
			    		break;
			    	*/
			    }
			    
			    var divWidth	= div[i].offsetWidth;
			    var divHeight	= div[i].offsetHeight;
			    
				switch(div[i].getAttribute('position')) {
					case 'center':
						scrollTop	= 0;
					case 'center_page':
						posX		= (clientWidth() - div[i].offsetWidth) / 2;
						
						if (divHeight > clientHeight())
							posY	= scrollTop;
						else
							posY	= ((scrollTop + (clientHeight() / 2)) - (div[i].offsetHeight / 2));
						break;
					
					case 'center_page_x':
						if (divWidth > clientWidth())
							posX	= 0;
						else
							posX	= (clientWidth() - div[i].offsetWidth) / 2;
						break;
					
					case 'center_page_y':
						if (divHeight > clientHeight())
							posY	= scrollTop;
						else
							posY	= ((scrollTop + (clientHeight() / 2)) - (div[i].offsetHeight / 2));
						break;
					
					case 'top':
						scrollTop	= 0;
					case 'client_top':
						posX		= (clientWidth() - div[i].offsetWidth) / 2;
						posY		= scrollTop;
						break;
					
					case 'mouse':
						posX		= mouseX;
						posY		= mouseY - scrollTop;
						break;
				}
				
				if (div[i].nodeName.toLowerCase() == 'div') {
					switch(div[i].getAttribute('height')) {
						case "max":
							div[i].style.height	= Math.max(documentHeight(), clientHeight()) + 'px';
							break;
						
						case "max-viewport":
							div[i].style.height	= clientHeight() + 'px';
							break;
						
					}
				}
				
				div[i].style.left	= posX + 'px';
				div[i].style.top	= posY + 'px';
			}
		}
	}
}

//----------------------------------------------------------------------------------------------------------
// RESIZING
//----------------------------------------------------------------------------------------------------------

function divResize(div, top, padding) {
	if (top > -1)
		parent.scrollTo(0, top);

	document.getElementById(div).style.height = 0 + 'px';

	if (document.documentElement && !document.documentElement.clientHeight == 0)
		scrollHeight = document.documentElement.scrollHeight;
	else if (document.body)
		scrollHeight = document.body.scrollHeight + document.body.clientHeight;

	document.getElementById(div).style.height = padding + 'px';
	document.getElementById(div).style.height = scrollHeight + padding + 'px';
	document.getElementById(div).style.width = window.document.body.scrollWidth + 'px';
}

function verticalFill(divObj) {
	var containerObj	= document.getElementById(divObj.getAttribute('container'));
	var indexObj		= document.getElementById(divObj.getAttribute('index'));
	var total_height	= 0;
	
	for (var i=0; i<containerObj.childNodes.length; i++) {
		if (containerObj.childNodes[i].nodeName.toLowerCase() == 'div') {
			total_height	+= containerObj.childNodes[i].offsetHeight;
		}
	}
	
	//alert(indexObj.offsetHeight + ' ' + total_height + ' ' + (divObj.offsetHeight + (indexObj.offsetHeight - total_height)))
	//alert(indexObj.clientHeight)
	
	if (indexObj.offsetHeight > total_height)
		divObj.style.height		= (divObj.offsetHeight + (indexObj.offsetHeight - total_height) - 24) + 'px';
	else {
		//alert(indexObj.offsetHeight + ' ' + total_height + ' ' + (indexObj.style.paddingTop + indexObj.style.paddingBottom + indexObj.style.marginTop + indexObj.style.marginBottom))
		indexObj.style.height	= (total_height - 20) + 'px';
	}
}

//----------------------------------------------------------------------------------------------------------
// PARAM ELEMENT
//----------------------------------------------------------------------------------------------------------

function appendParam(parentNode, name, value, defaultVal) {
	var param = document.createElement('param');
		
	param.setAttribute('name', name);
	
	param.setAttribute('value', value?value:defaultVal);
	
	parentNode.appendChild(param);
}

function appendParamBefore(paramObj, name, value, defaultVal) {
	var param = document.createElement('param');
		
	param.setAttribute('name', name);
	
	param.setAttribute('value', value?value:defaultVal);
	
	paramObj.parentNode.insertBefore(param, paramObj);
}

//----------------------------------------------------------------------------------------------------------
// INPUT ELEMENT
//----------------------------------------------------------------------------------------------------------

function toggleCheckboxes() {
	var inputs	= document.getElementsByTagName('input');
	
	for (i=0; i<inputs.length; i++) {
		if ((inputs[i].name == this.name) && (inputs[i].id != this.id))
			inputs[i].checked = this.checked;
	}
}

function toggleDefault() {
	toggleValue(this);
}

function clearValue(object, val) {
	if (object.value == val)
		object.value = "";
}

function restoreValue(object, val) {
	if (object.value == "")
		object.value = val;
}

function setValue(object, val) {
	object.value = val;
}

function setDefaultValue(object, val) {
	object.defaultValue = val;
	object.value = val;
}

function toggleValue(field){
	if (field.defaultValue == field.value)
		field.value = '';
	else if (field.value == '')
		field.value = field.defaultValue;
}

function selectText() {
	this.select();
}

clearText	= toggleValue

//----------------------------------------------------------------------------------------------------------
// CHANGE
//----------------------------------------------------------------------------------------------------------

function addClass(obj, className) {
	if (obj) {
		var newClass	= new Array();
		newClass.push(className);
		
		var classes		= obj.className.split(' ');
		classes			= classes.concat(newClass);
		
		obj.className	= '';
		
		obj.className	= classes.join(' ');
	}
}

function removeClass(obj, className) {
	if (obj) {
		var classes		= obj.className.split(' ');
		obj.className	= '';
		
		for (var i=0; i<classes.length; i++) {
			if (classes[i] != className)
				obj.className += classes[i] + ' ';
		}
	}
}

function setClass(obj, className) {
	if (obj)
		obj.className = className;
}














