
sjs_stack = [];

function sjs() {

	sjs_stack.push(this);
	this_pointer = sjs_stack[(sjs_stack.length-1)];
	timer_stack = [];

	Array.prototype.remove = function(from, to) {
	
		var rest = this.slice((to || from) + 1 || this.length);
		this.length = from < 0 ? this.length + from : from;
		return this.push.apply(this, rest);
	};

	this.get = function(ident,type) {

		switch (type)
		{
		case "class":
			var all_elems = document.body.getElementsByTagName("*");
			var found_elems = [];
			for (i=0; i<all_elems.length; i++) {
				if (all_elems[i].className == ident) {
					found_elems.push(all_elems[i]);
				}
			}
			return found_elems;
			break;
		case "tag":
			return document.getElementsByTagName(ident);
			break;
		default:
			return document.getElementById(ident);
			break;
		}
	}

	this.write = function(obj,content,bool_add) {

		obj.innerHTML = (bool_add) ? obj.innerHTML + content : content;
	}

	this.swap = function(a,b) {
	
		var temp = [a.innerHTML,b.innerHTML];
		this.write(a,temp[1],false);
		this.write(b,temp[0],false);
		temp = null;

	}

	this.show = function(obj,a) {
		switch (a) 
		{
		case "array":
			for (i=0; i<obj.length; i++) {
				obj[i].style.display = "block";
				obj[i].style.zoom = 1;
			}
			break;
		default:
			obj.style.display = "block";
			obj.style.zoom = 1;
			break;
		}
	}
	
	this.hide = function(obj,a) {
		switch (a) 
		{
		case "array":
			for (i=0; i<obj.length; i++) {
				obj[i].style.display = "none";
			}
			break;
		default:
			obj.style.display = "none";
			break;
		}
	}

	this.chop = function() {

		this.string = function(str,min,max,lc) {
			var chopped = str.substring(min,max);
			if (lc==true) {
				chopped = chopped.toLowerCase();
			}
			return chopped;
		}
		
		this.array = function(obj,min,max,lc) {
			var chopped = [];
			for (i=0; i<obj.length; i++) {
				chopped[i] = obj[i].substring(min,max);
				if (lc==true) { 
					chopped[i] = chopped[i].toLowerCase();
				}
			}
			return chopped;
		}		

		return this;
	}

	this.sum = function(arr,wipe) {
		var _sum = 0;
		for (i=0; i<arr.length; i++) {
			if(arr[i]!=null) {
			_sum += arr[i];
				if (wipe) arr[i] = 0;
			}
		}
		return _sum;
	}

	this.px_split = function(input) {
		var split = input.split('p');
		return split[0];
	}

	this.validate = function(elem,type,response) {
		switch (type) 
		{
			case "email":
				email_regex = emailpat = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9-])+(\.[a-zA-Z0-9_-]+)+$/;
				if (!email_regex.test(elem.value)) {
					if (response=="alert") {
						alert("Please enter a valid email address.");
					} else {
						elem.style.backgroundColor = "red";
						elem.onkeyup = function() { elem.style.backgroundColor = "white"; };
					}
					return false;
				} else {
					return true;
				}
				break;
			case "input":
				if (!(elem.value.length>0&&elem.value!=" ")) {
					if (response=="alert") {
						alert("You have not filled in all required fields.");
					} else {
						elem.style.backgroundColor = "red";
						elem.onkeyup = function() { elem.style.backgroundColor = "white"; };
					}
					return false;
				} else {
					return true;
				}
				break;
			default:
				break;
		}
	}

		

	this.fade = function(obj,fps,inc,type) {

		this.obj = obj;
		this.fps = fps;
		this.inc = inc;
		this.count = (type=="o") ? 100 : 0;
		this.type = type;
		this.goprep = this_pointer.fade_internal(this,timer_stack.length,type);
		this.go = function() {			
			this.obj.style.zoom = 1;
			obj.style.display = "block";
			if (type=="i") {
				if (obj.filters) {
					obj.style.filter = "alpha(opacity=0)";
				} else {
					obj.style.opacity = 0.0;
				}
			}
			timer_stack.push(setInterval(this.goprep,this.fps));
		}
		
		return this;
	}


	this.fade_internal = function(fade_obj, arr_pos, type) {

		return (function() {
			if ((fade_obj.count>0&&type=="o")||(fade_obj.count<100&&type=="i")) {
				fade_obj.count = (type=="o") ? (fade_obj.count-fade_obj.inc) : (fade_obj.count+fade_obj.inc);
				if (fade_obj.obj.filters) {
					fade_obj.obj.style.filter = "alpha(opacity="+fade_obj.count+")";
				} else {
					fade_obj.obj.style.opacity = (fade_obj.count/100);
				}
			} else {
				fade_obj.obj.style.display = (type=="i") ? "block" : "none";
				clearInterval(timer_stack[(arr_pos-1)]);
				timer_stack.remove(arr_pos);
			}
		});
	}


	this.highlight = function(objs,start_opac,end_opac) {
		if (objs[0].filters) {
			for (i=0; i<objs.length; i++) {
				objs[i].style.zoom = 1;
				objs[i].style.filter = "alpha(opacity="+start_opac+")";
				objs[i].onmouseover = function() { 
					this.style.filter = "alpha(opacity="+end_opac+")";
				};
				objs[i].onmouseout = function() { 
					this.style.filter = "alpha(opacity="+start_opac+")";
				};
			}
		} else {
			for (i=0; i<objs.length; i++) {
				objs[i].style.opacity = (start_opac/100);
				objs[i].onmouseover = function() { this.style.opacity = (end_opac/100); };
				objs[i].onmouseout = function() { this.style.opacity = (start_opac/100); };
			}
		}
	}

	this.slide = function(obj,min_height,fps,increment) {
		_this = this;
		this.obj = obj;
		this.fps = fps;
		this.inc = increment;
		this.h = this_pointer.px_split(obj.style.height);
		this.m_h = min_height;
		this.content = obj.firstChild.nextSibling;
		this.content_prep = this_pointer.hide(this.content);
		this.create = function() {
				obj.style.height = (_this.m_h)+"px";
				obj.onmouseover = function() {
					return (function() {
						timer_stack.push(setInterval('_this._expand()',_this.fps));
						clearInterval(timer_stack[timer_stack.length-2]);
						this_pointer.show(_this.content);
					})();

				}
				obj.onmouseout = function() {
					return (function() {
						clearInterval(timer_stack[timer_stack.length-1]);
						timer_stack.push(setInterval('_this._contract()',_this.fps));
						this_pointer.hide(_this.content);
					})();

				}
		}
		this._expand = function() {
			this_pointer.slide_expand(_this,timer_stack.length);
		}
		this._contract = function() {
			this_pointer.slide_contract(_this,timer_stack.length);
		}
	}

	this.slide_expand = function(_obj,len) {
		var temp_split = this_pointer.px_split(_obj.obj.style.height);
		if (parseInt(temp_split) < parseInt(_obj.h)) {
			_obj.obj.style.height = (parseInt(temp_split)+parseInt(_obj.inc))+"px";
		}
		else {
			clearInterval(timer_stack[(len-1)]);
			timer_stack.remove((len-1));
		}
	}

	this.slide_contract = function(_obj,len) {
		var temp_split = this_pointer.px_split(_obj.obj.style.height);
		if (parseInt(temp_split) > parseInt(_obj.m_h)) {
			_obj.obj.style.height = (parseInt(temp_split)-parseInt(_obj.inc))+"px";
		} else {
			clearInterval(timer_stack[(len-1)]);
			timer_stack.remove((len-1));
		}
	}

	this.newsbox = function(imgs,headlines,delay) {
		
		_this = this;
		this.news_array = [['',''],['',''],['','']];
		this.pause = false;
		this.create = function() {
			for (j=0; j<headlines.length; j++) {
				this.news_array[j][0] = headlines[j];
				this.news_array[j][1] = imgs[j];
				headlines[j].onmouseover = function() {
					_this.pause = true;
					this_pointer.news_highlight(this,_this);

				}
				headlines[j].onmouseout = function() {
					_this.pause = false;
				}
			}
		}
		this.count = 1;
		this.delay = delay;
		this.goprep = this_pointer.news_rotate(this,timer_stack.length);
		this.go = function() {
			this.news_array[0][0].style.backgroundColor = "#eaeaea";
			timer_stack.push(setInterval(this.goprep,this.delay));
		}

	}

	this.news_rotate = function(box_obj,arr_pos) {

		return (function() {
			if (box_obj.pause!=true) {
					for (i=0; i<box_obj.news_array.length; i++) {
						if (i==box_obj.count) {
							box_obj.news_array[i][0].style.backgroundColor = "#eaeaea";
							box_obj.news_array[i][1].style.display = "block";
						} else {
							box_obj.news_array[i][1].style.display = "none";
							box_obj.news_array[i][0].style.backgroundColor = "#ffffff";
						}
					}
					box_obj.count = (box_obj.count==2) ? 0 : (box_obj.count + 1);
			} 
		});	
	}


	this.news_highlight = function(obj,box_obj) {
		obj.style.backgroundColor = "#eaeaea";
		for (i=0; i<box_obj.news_array.length; i++) {
			if (obj==box_obj.news_array[i][0]) {
				box_obj.news_array[i][1].style.display = "block";
				box_obj.count = i;
			} else {
				box_obj.news_array[i][1].style.display = "none";
				box_obj.news_array[i][0].style.backgroundColor = "#ffffff";
			}
		}
	}
	

	if (this instanceof sjs) {
		return this.sjs;
	} else  {
		return new sjs();
	}
}
