// DRAG & DROP
// evt: EVENT (The javascript event.)
// stack: TRUE OR FALSE (Brings selected element to the front of a stack.  Default is false.)

function drag(element,evt,stack) {
	var parentWidth,parentHeight,elementWidth,
	elementHeight,mouseStartX,mouseStartY,
	mouseX,mouseY,velX,velY,elementX,
	elementY,newX,newY,children,siblings,
	sortByZIndex,high,low,increment,zIndex,zKey;
	element.parentNode.style.position = 'relative';
	element.style.position = 'absolute';
	if(stack) {
		children = element.parentNode.childNodes;
		siblings = new Array();
		for (i=0; i<children.length; i++) {
			if(children[i].style) {
				siblings.push(children[i]);
				if(!children[i].style.zIndex) {children[i].style.zIndex=0;}
			}
		}
		sortByZIndex = function(a,b) {return a.style.zIndex - b.style.zIndex;}
		siblings.sort(sortByZIndex);
		high = siblings[siblings.length-1].style.zIndex;
		low = siblings[0].style.zIndex;
		increment = (high-low)/(siblings.length-1);
		var check = new Array();
		for(i=0; i<siblings.length; i++) {siblings[i].style.zIndex = (increment*i)+eval(low); check.push(siblings[i].style.zIndex);}
		//alert(check);
		var lower = false;
		for(i=0; i<siblings.length; i++) {
			if(lower==true) {siblings[i].style.zIndex = siblings[i].style.zIndex-increment;}
			if(siblings[i]==element) {
				lower = true;
				siblings[i].style.zIndex = increment*(siblings.length-1);
			}
		}
	}			
	document.onselectstart = function() {return false;}
	document.ondragstart = function() {return false;}
	parentWidth = element.parentNode.clientWidth;
	parentHeight = element.parentNode.clientHeight;
	element.style.left = element.offsetLeft+'px';
	element.style.top = element.offsetTop+'px';
	elementWidth = element.offsetWidth;
	elementHeight = element.offsetHeight;
	mouseStartX = evt.clientX;
	mouseStartY = evt.clientY;
	document.onmousemove = function(evt) {
		document.body.style.MozUserSelect = 'none';
		if(window.event) {evt = window.event;}
		document.onmouseup = function() {endDrag();}
		mouseX = evt.clientX;
		mouseY = evt.clientY;
		velX = mouseX - mouseStartX;
		velY = mouseY - mouseStartY;
		mouseStartX = mouseX;
		mouseStartY = mouseY;
		elementX = element.offsetLeft;
		elementY = element.offsetTop;
		if(velX<0 && elementX>0) {
			if((elementX+velX)<0) {newX=0;}
			else {newX = elementX+velX;}
			element.style.left = newX+'px';
		}
		if(velX>0 && (elementX+elementWidth)<parentWidth) {
			if((elementX+elementWidth+velX)>parentWidth) {newX = parentWidth-elementWidth;}
			else {newX = elementX+velX;}
			element.style.left = newX+'px';
		}
		if(velY<0 && elementY>0) {
			if((elementY+velY)<0) {newY=0;}
			else {newY = elementY+velY;}
			element.style.top = newY+'px';
		}
		if(velY>0 && (elementY+elementHeight)<parentHeight) {
			if((elementY+elementHeight+velY)>parentHeight) {newY = parentHeight-elementHeight;}
			else {newY = elementY+velY;}
			element.style.top = newY+'px';
		}
	}
	endDrag = function() {
		document.onmousemove = '';
		document.onselectstart = '';
		document.ondragstart = '';
		document.body.style.MozUserSelect = '';
	}
}




// FADE IN - OUT ELEMENT
// direction: 1 OR -1  ("1" for fade in; "-1" for fade out. If no value is specified, or if "0" is entered, element will alternate between in and out.)
// speed: 1->100  ("1" is the slowest speed; "100" is the fastest.  Default is "5".  Entering "0" sets to default.)
// hide: TRUE OR FALSE  (When fading out, if set to "TRUE", hides the element after it has completely faded out. Defatul value is "FALSE".)
// show: ANY ELEMENT DISPLAY VALUE  (When fading in, sets the display value of the element if it was previously "none".  Default is "block".)
// callback: ANY JAVASCRIPT CODE  (runs a script after completion.)

function fade(element,direction,speed,hide,show,callback) {
	opacity = element.style.opacity;
	if(opacity=='' | opacity==undefined && opacity!='0') {opacity=1;}	
	if(opacity>1) {opacity=1;}
	if(opacity<0) {opacity=0;}
	if(element.style.display=='none') {opacity=0;}
	element.style.opacity = opacity;
	element.style.filter = 'alpha(opacity='+opacity*100+')';
	if(!direction | direction==0) {
		if(opacity==0) {direction=1;}
		else {direction=-1;}
	}
	if(!show | show==0) {show='block';}
	if(element.style.display=='none' && direction==1) {element.style.display=show;}
	if(!speed | speed==0) {speed=5;}
	if(!hide | hide==0) {hide='false';}
	increment = .01*speed*direction;
	animateFade = function() {
		opacity = eval(opacity+'+'+increment);
		if(opacity<=0) {opacity=0;}
		if(opacity>=1) {opacity=1;}
		element.style.opacity = opacity;
		element.style.filter = 'alpha(opacity='+opacity*100+')';
		if(opacity==0 | opacity==1) {
			if(opacity==0 && hide==true) {element.style.display='none';}
			if(callback) {callback();}
			return false;
		}
		setTimeout(animateFade);
	}
	animateFade();
}




// SELECT ALL TEXT

function selectAllText(element) {
	start=0;
	end=element.innerHTML.length;
	if (document.selection) {
		var range = document.body.createTextRange();
		range.moveToElementText(element);
 	    range.collapse(true);
 	   	range.moveEnd('character',end);
 	    range.moveStart('character',start);
		range.select();
	}
	else if (window.getSelection) {
		var range = document.createRange();
		window.getSelection().removeAllRanges();
		range.selectNode(element);
		window.getSelection().addRange(range);
	}
}




// SLIDE OPEN - CLOSED
// max: ANY NUMBER 0 OR GREATER  (When direction is "1" for "open", maximum length element will expand to. Default value is "0".)
// dimension: 1-4  ("1" slides up, "2" slides right, "3" slides down, "4" slides left. Default value is "2".)
// direction: 1 OR -1  ("1" for slide open; "-1" for slide closed. If no value is specified, or if "0" is entered, element will alternate between open and closed.)
// min: ANY NUMBER 0 OR GREATER  (When direction is "-1" for "close", minimum length element will contract to. Default value is "0".)
// speed: 1->100  ("1" is the slowest speed; "100" is the fastest.  Default is "5".  Entering "0" sets to default.)
// hide: TRUE OR FALSE  (When direction is "-1" for "close", if set to "TRUE", hides the element after it has completely closed. Default value is "TRUE".)
// callback: ANY JAVASCRIPT CODE  (runs a script after completion.)

function slide(element,max,dimension,direction,min,speed,hide,callback) {
	element.style.position='relative';
	if(max==undefined) {max=0;}
	if(dimension==undefined) {dimension=2;}
	if(min==undefined) {min=0;}
	marginMin=0;
	marginMax = -1*(max-min);
	if(speed==undefined | speed==0) {speed=5;}
	if(hide==undefined) {hide=true;}
	if(dimension==1 | dimension==3) {
		if(element.style.height=='') {theLength=min;}
		else {theLength = parseInt(element.style.height);}
		element.style.height=theLength+'px';
	}
	else {
		if(element.style.width=='') {theLength=min;}
		else {theLength = parseInt(element.style.width);}
		element.style.width=theLength+'px';
	}
	if(dimension==1) {theMargin=parseInt(element.style.marginTop);}
	if(dimension==2) {theMargin=parseInt(element.style.marginRight);}
	if(dimension==3) {theMargin=parseInt(element.style.marginBottom);}
	if(dimension==4) {theMargin=parseInt(element.style.marginLeft);}
	if(!theMargin) {theMargin=marginMin;}
	if(direction==undefined | direction==0) {
		if(theLength==max) {direction=-1;}
		if(theLength==min) {direction=1;}
	}
	increment = .01*speed*direction*max;
	element.style.display='block';
	//alert(max+', '+dimension+', '+direction+', '+min+', '+speed+', '+hide+', '+callback);
	animateSlide = function() {
		theLength = eval(theLength+'+'+increment);
		theMargin = theMargin-increment;
		if(theLength>max) {theLength=max;}
		if(theLength<min) {theLength=min;}
		if(theMargin<marginMax) {theMargin=marginMax;}
		if(theMargin>marginMin) {theMargin=marginMin;}
		if(dimension==1 | dimension==3) {element.style.height=theLength+'px';}
		else {element.style.width=theLength+'px';}
		if(dimension==1) {element.style.marginTop = theMargin+'px';}
		if(dimension==2) {element.style.marginRight = theMargin+'px';}
		if(dimension==3) {element.style.marginBottom = theMargin+'px';}
		if(dimension==4) {element.style.marginLeft = theMargin+'px';}
		if(theLength==min | theLength==max) {
			if(direction==-1 && hide==true) {element.style.display='none';}
			if(callback) {callback();}
			return false;
		}
		setTimeout(animateSlide);
	}
	animateSlide();
}




function alternate_colors(the_colors,randomize) {
	var containers = getElementByClass('stratify');
	for (i=0; i<containers.length; i++) {
		var rows = containers[i].getElementsByTagName('li');
		var c = 0;
		for (j=0; j<rows.length; j++) {
			if (randomize==true) {
				num = Math.floor(Math.random()*the_colors.length)
				rows[j].style.backgroundColor = the_colors[num];
			}
			else {
				rows[j].style.backgroundColor = the_colors[c];
				if (c+1 == the_colors.length) {c = 0;}
				else {c++;}
			}
		}
	}
}

function addCorners(corners_path) {
		var corners = new Array('corner_top_left','corner_top_right','corner_bottom_right','corner_bottom_left');
		var positions = new Array('top:0; left:0;','top:0; right:0;','bottom:0; right:0;','bottom:0; left:0;');
		var elements = document.getElementsByTagName('*');
		for (i=0; i<elements.length; i++) {
			if (elements[i].getAttribute('corners')) {
				type = elements[i].getAttribute('type');
				path = corners_path+type+'/';
				elements[i].style.position = 'relative';
				for (j=0; j<corners.length; j++) {
					newCorner = '<img src="'+path+corners[j]+'.gif" style="z-index:100; position:absolute; '+positions[j]+'"/>';
					elements[i].innerHTML += newCorner;
				}
			}
		}
	}

function select_text(element,start,end) {
	element.focus();
	if(element.setSelectionRange) {element.setSelectionRange(start, end);}
	else {
   		var r = element.createTextRange();
   		r.collapse(true);
  		r.moveEnd('character', end);
   		r.moveStart('character', start);
  		r.select();   
	}
}

function getElementByClass(theClass) {
	var allTags = new Array();
	var theTags = new Array();
	var allTags = document.getElementsByTagName("*");
	for (i=0; i<allTags.length; i++) {
		if (allTags[i].className==theClass) {
			theTags.push(allTags[i]);
		}
	}
	return theTags;
}

function fadeInElement(element,i,o,s,a) {
	i=i+s;
	s=s+a;
	element.style.opacity=i;
	element.style.filter = 'alpha(opacity='+(i*100)+')';
	var func = function() {fadeInElement(element,i,o,s,a);}
	if (i<o) {setTimeout(func,10);}
}

function fadeOutElement(element,i,o,v,a,d) {
	i=i-v;
	v=v+a;
	element.style.opacity=i;
	element.style.filter = 'alpha(opacity='+i*100+')';
	var func = function() {fadeOutElement(element,i,o,v,a,d);}
	if (i>o) {setTimeout(func,10);}
	else {
		if (d) {element.style.display='none';}
	}
}

function shrinkElement(element,i,o,v,a,d) {
	i=i-v
	v=v+a;
	element.style.height=i+'px';
	var func = function() {shrinkElement(element,i,o,v,a,d);}
	if (i>o) {setTimeout(func,10);}
	else {
		if (d) {element.style.display='none';}
	}
}

function growElement(element,i,o,v,a) {
	i=i+v
	v=v+a;
	element.style.height=i+'px';
	var func = function() {growElement(element,i,o,v,a);}
	if (i<o) {setTimeout(func,10);}
}


function ExtractNumber(value) {
	var n = parseInt(value);
	return n;
}

function $(element) {return document.getElementById(element);}