// JavaScript Document

jQuery(document).ready(init);
/*window.onscroll = scroller;
function scroller() {
	if (dialog.active) {
		dialog.y = get_YOffset();
		dialog.updatePosition();
	}
}*/

function init() {
	loadPage('home');
	check_login();
	//setTimeout('banner.init()',100);
}

var current = new Object();
var loggedin = false;

/*function check_login() {
	var el = document.getElementById('nav_students');
	if (loggedin) {
		el.style.display = 'inline-block';
	} else {
		el.style.display = 'none';
	}
}*/

function loadPage(pagename, subpage) {
	if (pagename != current.page) {
		var url = 'inc/'+pagename+'.php';
		ajaxRequest('inc/banner.php','?page='+pagename,'banner','banner.init()');
		if (subpage) {
			ajaxRequest(url,'','content','updatePage('+subpage+')');
		} else {
			ajaxRequest(url,'','content');
		}
		updateNav(pagename);
		current.page = pagename;
	} else if (subpage) {
		updatePage(subpage);
	}
}
function pageReady(subpage) {
	alert('page ready '+subpage);
}
function updatePage(subpage) {
	//if (!subpage) subpage=1;
	var container = document.getElementById('page');
	var snlink = document.getElementById('subnav_'+subpage);
	if (current.subPage) {
		var oldPage = document.getElementById('subnav_'+current.subPage);
		oldPage.removeAttribute('class');
	}
	snlink.setAttribute('class','active');
	ajaxRequest('inc/page.php','?subpage='+subpage,'page');
	current.subPage = subpage;
}
function updateNav(pagename) {
	var navlink = document.getElementById('nav_'+pagename);
	navlink.setAttribute('class', 'active');
	if (current.page) {
		var oldPage = document.getElementById('nav_'+current.page);
		oldPage.removeAttribute('class');
	}
}
var dialog = new Object();
dialog.typeSet = new Array();
dialog.typeSet[0] = 'video';
dialog.typeSet[1] = 'events';
dialog.typeSet[2] = 'login';
dialog.typeSet[3] = 'help';
dialog.list = new Array();
dialog.ct = 0;
dialog.active = 0;

function make_dialog(type, contentid) {
	if (!contentid) contentid = 0;
	dialog.ct ++;
	var temp = new dialog_template(type,contentid);
	temp.id = dialog.ct;
	switch(type) {
		case 'video':
			temp.url = 'inc/video.php';
			temp.urlparams = '?id='+contentid;
			break;
		case 'events':
			temp.url = 'inc/events.php';
			temp.urlparams = '?dialog=true';
			break;
		case 'news':
			temp.url = 'inc/news.php';
			temp.urlparams = '?dialog=true';
			break;
		default:
			temp.url = false;
			break;
	}
	dialog.list[dialog.ct] = new Object();
	dialog.list[dialog.ct] = temp;
	dialog.list[dialog.ct].init();
}
function dialog_template(input_type,input_id) {
	this.type = input_type;
	this.content_id = input_id;
	this.html = '';
	this.active = false;
	this.minimized = false;
	this.scrollbar_y = false;
	this.location = function() {
		var loc = 'dialog.list['+this.id+']';
		return loc;
	}
	this.init = function() {
		this.y = get_YOffset();
		//alert('init '+this.type+': '+this.id+' @ yval='+this.y);
		if (this.url) ajaxRequest(this.url,this.urlparams,'',this.location()+'.build(ajaxRequest.responseText)');
	}
	this.open_ = function(contents) {
		this.build(contents);
	}
	this.build = function(contents) {
		//alert(dialog.active);
		if (dialog.active) {
				dialog.list[dialog.active].minimize();
		}
		if (contents) this.contents = contents;
		/*var opa = this.active == true ? 1 : 0;*/
		var opa = 1;
		var html = '<div id="clearwindow" style="opacity: '+opa+'; filter: alpha(opacity='+opa*100+');"><div id="popup_'+this.id+'" class="popup" style="width: 500px; height: 400px;"">';
		html = html + '<div class="titlebar"><span>'+this.type+': '+this.id+'</span>';
		html = html + '<div class="btnset"><a class="minimize" onClick="JavaScript: '+this.location()+'.minimize()">&mdash;</a>';
		html = html + '<a class="close" onClick="JavaScript: '+this.location()+'.close_()">X</a></div></div>';
		
		html = html + '<div id="scrollbox_'+this.id+'" class="scrollbox" style="width:auto"><div id="scrollcontents_'+this.id+'" class="scrollcontents">';
		html = html + this.contents+'</div></div>';
		html = html + '<div id="scrollbar_'+this.id+'" class="scrollbar" style="display: inline-block"><a id="scroller_'+this.id+'" style="height:50px"></a></div>';

		html = html + '</div></div>';
		this.html = html;
		updateHTML('add',document.body,this.html);
		this.active = true;
		dialog.active = this.id;
		
		var aThumb = document.getElementById('scroller_'+this.id);
		var scrolldiv = document.getElementById('scrollcontents_'+this.id);
		var win_ht = document.getElementById('scrollbox_'+this.id).offsetHeight;
		var content_ht = scrolldiv.offsetHeight;
		var maxscroll = content_ht-win_ht;
		var scrollbar_ht = parseInt((win_ht*win_ht)/content_ht);
		var scrollbar_max_y = win_ht - scrollbar_ht;
		
		if (scrollbar_ht>win_ht) {
			document.getElementById('scrollbar_'+this.id).style.display = 'none';
			document.getElementById('scrollbox_'+this.id).style.width = '100%';
			document.getElementById('popup_'+this.id).style.height = content_ht <= 200 ? '221px' : (content_ht+21)+'px';
			return;
		} else {
			document.getElementById('scrollbox_'+this.id).style.width = '479px';
		}
		
		aThumb.style.height = scrollbar_ht + "px";
		this.scrollbar_ht = scrollbar_ht;
		var scrollbar_y = this.scrollbar_y
		var scrollbox_id = this.id;
		if (scrollbar_y != false) {
			document.getElementById('scroller_'+this.id).style.top = scrollbar_y+'px';
			var scrollval = scrollbar_y/scrollbar_max_y;
			var scroll_y = scrollval*maxscroll;
			
			scrolldiv.style.top = scroll_y * (-1) +"px";
		}
		
		Drag.init(aThumb, null, 0, 0, 0, scrollbar_max_y);
		aThumb.onDrag = function(x, y) {
			scrollbar_y = y;
			var scrollval = scrollbar_y/scrollbar_max_y;
			var scroll_y = scrollval*maxscroll;
			
			scrolldiv.style.top = scroll_y * (-1) +"px";
		}
		aThumb.onDragEnd = function(x, y) {
			//alert(scrollbox_id);
			dialog.list[scrollbox_id].scrollbar_y = scrollbar_y;
			//alert(dialog.list[scrollbox_id].scrollbar_y+','+scrollbar_y);
		}
	}
	this.minimize = function() {
		var el = document.getElementById('clearwindow');
		el.parentNode.removeChild(el);
		this.minimized = true;
		
		var html = '';
		html = html + '<div id="dialog_'+this.id+'" class="min_dialog"><span>'+this.type+': '+this.id+'</span>';
		html = html + '<div class="btnset"><a class="maximize" onClick="JavaScript: '+this.location()+'.maximize()">+</a>';
		html = html + '<a class="close" onClick="JavaScript: '+this.location()+'.close_()">X</a></div></div>';
		html = html + '';
		updateHTML('add',document.getElementById('min_area'),html);
		dialog.active = 0;
	}
	this.maximize = function() {
		this.close_();
		this.minimized = false;
		this.build();
	}
	this.updatePosition = function() {
		document.getElementById('clearwindow').setAttribute('style','top:'+this.y+'px');
	}
	this.close_ = function() {
		if (!this.minimized) {
			var el = document.getElementById('clearwindow');
			el.parentNode.removeChild(el);
			dialog.active = 0;
			this.active = false;
		} else {
			var el = document.getElementById('dialog_'+this.id);
			el.parentNode.removeChild(el);
		}
	}
}

var banner = new Object();
banner.moving = false;
//banner.slider = document.getElementById('slider');
banner.init = function() {
	var slider = document.getElementById('slider');
	//if (slider !== null) {
		var sliderdist = window.innerWidth - slider.offsetLeft -1;
		var feature_slider = document.getElementById('feature_slider');
		feature_slider.style.width = 718*feature_slider.childNodes.length+'px';
		document.getElementById('banner').style.width = window.innerWidth;
		document.getElementById('feature_slider').style.left = '0px';
		slider.style.left = sliderdist +'px';
		move('slider',180,20,sliderdist);
		banner.autoDelay = 6000;
		banner.autoTimer = setTimeout('banner.next()',banner.autoDelay+1000);
	//}
}
banner.next = function() {
	clearTimeout(banner.autoTimer);
	if (banner.moving) {
		banner.autoTimer = setTimeout('banner.next()',banner.autoDelay);
		return;
	}
	var feature_slider = document.getElementById('feature_slider');
	var slider_left = feature_slider.style.left;
	slider_left = 1 * slider_left.substring(0,slider_left.indexOf('px'));
	//alert(0-feature_slider.offsetWidth);
	if (slider_left <= 718-feature_slider.offsetWidth) {
		move('feature_slider',0,100,-1*slider_left);
		banner.autoTimer = setTimeout('banner.next()',banner.autoDelay);
		return;
	}
	move('feature_slider',180,40,718);
	banner.autoTimer = setTimeout('banner.next()',banner.autoDelay);
}
banner.prev = function() {
	clearTimeout(banner.autoTimer);
	if (banner.moving) {
		banner.autoTimer = setTimeout('banner.next()',banner.autoDelay);
		return;
	}
	var feature_slider = document.getElementById('feature_slider');
	var slider_left = feature_slider.style.left;
	slider_left = 1 * slider_left.substring(0,slider_left.indexOf('px'));
	if (slider_left >= 0) {
		move('feature_slider',180,100,718*feature_slider.childNodes.length-718);
		banner.autoTimer = setTimeout('banner.next()',banner.autoDelay);
		return;
	}
	move('feature_slider',0,40,718);
	banner.autoTimer = setTimeout('banner.next()',banner.autoDelay);
}

var f_delay = 33;
function move(elid,angle_degrees,velocity,dist,ease) {
	banner.moving = true;
	// -1 = ease both; 0 = no ease; 1 = ease in; 2 = ease out
	if (!ease) ease = 0;
	var el = document.getElementById(elid);
	el.angle_degrees = angle_degrees;
	el.angle = Math.PI * el.angle_degrees / 180;
	el.avg_vel = velocity;
	el.dist_total = dist;
	el.dist_traveled = 0;
	el.ease = ease;
	
	if (!el.x) {
		if (!el.style.left) el.style.left = 0+'px';
		el.x = el.style.left;
		el.x = 1 * el.x.substring(0,el.x.indexOf('px'));
	}
	
	if (!el.y) {
		if (!el.style.top) el.style.top = 0+'px';
		el.y = el.style.top;
		el.y = 1 * el.y.substring(0,el.y.indexOf('px'));
	}
	
	if (!el.style.position) el.style.position = 'relative';
	
	setTimeout('moveloop(\''+elid+'\')',f_delay);
}
function moveloop(elid) {
	var el = document.getElementById(elid);
	var progress = el.dist_traveled/el.dist_total;
	el.dist_left = el.dist_total-el.dist_traveled;
	//el.accel = progress*2 - 1;
	//el.vel = el.avg_vel*el.accel;
	//if (el.vel < 0.01) el.vel = 0;
	el.vel = el.avg_vel;
	
	if (el.dist_traveled >= el.dist_total) {
		banner.moving = false;
		return;
	}
	
	if (el.dist_left < el.vel) el.vel = el.dist_left;
	
	el.y_offset = 1*(el.vel * Math.sin(el.angle));
	if (el.y_offset > 0 && el.y_offset < 0.001) el.y_offset = 0;
	el.x_offset = 1*(el.vel * Math.cos(el.angle));
	if (el.x_offset > 0 && el.x_offset < 0.001) el.x_offset = 0;
	
	el.x += el.x_offset;
	el.y += el.y_offset;
	var new_x = parseInt(el.x);
	var new_y = parseInt(el.y);
	el.style.left = el.x + 'px';
	el.style.top = el.y + 'px';
	
	el.dist_traveled += el.vel;
	
	setTimeout('moveloop(\''+elid+'\')',f_delay);
}

function get_YOffset() {
	var ScrollTop = document.body.scrollTop;
	if (ScrollTop == 0)
	{
    	if (window.pageYOffset)
	      	ScrollTop = window.pageYOffset;
    	else
			ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}
	return ScrollTop;
}

function get_centerOffset(elid) {
	var win_x = window.innerWidth;
	var el = document.getElementById(elid);
	var el_width = el.offsetWidth;
	var x_offset = (win_x - el_width)/2;
	return x_offset;
}

function ajaxRequest(url, parameters, elementid, onready) {
	//var alertstring = url + parameters + '; ' + elementid;
	//alert(alertstring);
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function() {
		if(ajaxRequest.readyState == 4){
			if (ajaxRequest.status == 200) {
				if (elementid) {
					var ajaxDisplay = document.getElementById(elementid);
					ajaxDisplay.innerHTML = ajaxRequest.responseText;
				}
				if (onready) eval(onready);
			} else {
				var ajaxDisplay = document.getElementById(elementid);
				//alert(ajaxRequest.responseText);
				ajaxDisplay.innerHTML = '<p>There was a problem with the request.</p>';
			}
		}
	}
	ajaxRequest.open("GET", url + parameters, true);
	ajaxRequest.send(null);
}
function updateHTML(funct, container, HTMLstring) {
	switch(funct) {
		case 'add':
			var oldHTML = container.innerHTML;
			var newHTML = oldHTML + HTMLstring;
			container.innerHTML = newHTML;
			return 'Append HTML';
			break;
		case 'clr':
			container.innerHTML = "";
			return 'Clear HTML';
			break;
		case 'replace':
			container.innerHTML = HTMLstring;
			return 'Replace HTML';
			break;
		default:
			alert('updateHTML: undefined function (' + funct + ')');
			return false;
	}
}

var TimeToFade = 1000.0;
function fade(elid) {
	var el = document.getElementById(elid);
	if (el == null)
		return;
	
	if (el.fadeState == null) {
		if (el.style.opacity == '0') {
			el.fadeState = -2;
		} else if (el.style.opacity == null || el.style.opacity == '' || el.style.opacity == '1') {
			el.fadeState = 2;
		}
	}
	
	if (el.fadeState == 1 || el.fadeState == -1) {
		el.fadeState *= -1;
		el.fadeTimeLeft = TimeToFade - el.fadeTimeLeft;
	} else {
		el.fadeState = el.fadeState == 2 ? -1 : 1;
		el.fadeTimeLeft = TimeToFade;
		//alert(el.fadeState+','+el.fadeTimeLeft);
		setTimeout("animateFade("+new Date().getTime()+",'"+elid+"')",33);
	}
}
function animateFade(lastTick,elid) {
	alert('opacity '+el.style.opacity+', fadeState '+el.fadeState);
	var curTick = new Date().getTime();
	var elapsedTicks = curTick - lastTick;
	var el = document.getElementById(elid);
	
	if (el.fadeTimeLeft <= elapsedTicks) {
		el.style.opacity = el.fadeState == 1 ? '1' : '0';
		el.style.filter = 'alpha(opacity=' + (el.fadeState == 1 ? '100' : '0') + ')';
		el.fadeState = el.fadeState==1?2:-2;
		return;
	}
	
	el.fadeTimeLeft -= elapsedTicks;
	var newOpVal = el.fadeTimeLeft/timeToFade;
	if (el.fadeState = 1) newOpVal = 1 - newOpVal;
	
	el.style.opacity = newOpVal;
	el.style.filter = 'alpha(opacity='+newOpVal*100+')';
	setTimeout("animateFade("+curTick+",'"+elid+"')",33);
}
function getRow(arr,phrase) {
	var result = false;
	for (row = 0; row < arr.length; row++) {
		if (arr[row] == phrase) result = row;
	}
	return result;
}
