// JavaScript Document

//IE tweaks
function getPageYOffset(){
	var offset = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		offset = window.pageYOffset;
	  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		offset = document.body.scrollTop;
	  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		offset = document.documentElement.scrollTop;
	  }

	 return offset;
}


function getWindowHeight() {
	if (self.innerHeight) {
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowHeight = document.body.clientHeight;
	}
	return windowHeight;
}

//alert boxes

function checkAlert(){

	var alertBox = document.getElementById('alertbox');
	if(alertBox.innerHTML.length > 1){
		
		showAlert();
	}
}

function showAlert(){
	var alertBox = document.getElementById('alertbox');
	alertBox.style.display = "block";
	setTimeout('hideAlert()',1000);
}

function hideAlert(){
	var alertBox = document.getElementById('alertbox');
	alertBox.style.display = "none";
}

// float
var float_interval= new Array();
function Float(object_id, top){
	var Editor = document.getElementById(object_id);
	var EditorBG = document.getElementById('editBackground_'+object_id);
	var EditorTitleBar = document.getElementById('titlebar_'+object_id);
	Editor.style.top=(getPageYOffset()+top) + 'px';
	if(EditorTitleBar){
		EditorTitleBar.style.top=(getPageYOffset()+top) + 'px';
	}
	EditorBG.style.top=getPageYOffset() + 'px';
}

// ajax

function loadEditor(title, object_id, url, post_str, editor_width, ht_color, h_color, bt_color, b_color) {
		//alert(exit_action);
		var docbody = document.getElementById('docbody');
		
		
		var container = document.createElement('div');
		container.setAttribute('id','edit_container');
		container.style.position = 'absolute';
		container.style.top = '0px';
		container.style.left = '0px';
		container.style.width = '100%';
		container.style.textAlign = 'center';
		
		
		
		var EditorBG = document.createElement('div');
		EditorBG.setAttribute('id',('editBackground_'+object_id));
		
		EditorBG.style.position = 'absolute';
		EditorBG.style.top = '0px';
		EditorBG.style.left = '0px';
		EditorBG.style.filter = 'alpha(opacity=40)';
		EditorBG.style.opacity = '.40';
		EditorBG.style.backgroundColor = 'black';
		EditorBG.style.display = 'block';
		EditorBG.style.width = "100%";
		EditorBG.style.height = "100%";

		var Editor = document.createElement('div');
		Editor.innerHTML = "Loading ...";
		Editor.setAttribute('id',object_id);
		Editor.setAttribute('class','editor');
		Editor.style.position = 'relative';
		Editor.style.display = 'block';
		Editor.style.margin = '0 auto';
		Editor.style.padding = '20px';
		Editor.style.width = editor_width+'px';
		Editor.style.height = 'auto';
		Editor.style.backgroundColor = b_color;
		Editor.style.color = bt_color;
		Editor.style.border = '1px solid black';
		Editor.style.top = (getPageYOffset()+160) + 'px';
		var EditorTitleBar = document.createElement('div');
		EditorTitleBar.innerHTML = "<button name=\"close\" type=\"button\" onclick=\"hideEditor('"+object_id+"');\" style=\"float:right;font-family:font-family:Arial, Helvetica, sans-serif;\">X</button>"+title;
		//alert(EditorTitleBar.innerHTML);
		EditorTitleBar.setAttribute('id','titlebar_'+object_id);
		EditorTitleBar.style.fontFamily = 'Arial, Helvetica, sans-serif';
		EditorTitleBar.style.position = 'relative';
		EditorTitleBar.style.display = 'block';
		EditorTitleBar.style.margin = '0 auto';
		EditorTitleBar.style.padding = '5px';
		EditorTitleBar.style.paddingLeft = '20px';
		EditorTitleBar.style.paddingRight = '20px';
		EditorTitleBar.style.width = editor_width+'px';
		EditorTitleBar.style.height = '20px';
		EditorTitleBar.style.backgroundColor = h_color;
		EditorTitleBar.style.fontWeight = 'bold';
		EditorTitleBar.style.color = ht_color;
		EditorTitleBar.style.border = '1px solid black';
		EditorTitleBar.style.top = (getPageYOffset()+160) + 'px';
		
		container.appendChild(EditorTitleBar);
		container.appendChild(Editor);
		
		docbody.appendChild(EditorBG);
		docbody.appendChild(container);
		
		getDynamicContent(url, post_str, object_id);
		//alert(EditorBG.id);
		float_interval[object_id] = setInterval('Float(\"'+object_id+'\",100)', 100);
	}
	
function hideEditor(object_id){
	var docbody = document.getElementById('docbody');
	var container = document.getElementById('edit_container');
	var Editor = document.getElementById(object_id);
	var EditorBG = document.getElementById('editBackground_'+object_id);
	var EditorTitleBar = document.getElementById('titlebar_'+object_id);
	
	clearInterval(float_interval[object_id]);
	clearInterval(float_interval['titlebar_'+object_id]);
	
	EditorBG.style.display = 'none';
	Editor.style.display = 'none';
	EditorTitleBar.style.display = 'none';
	Editor.innerHTML = "";
	
	//reload page
	//getDynamicContent('schedule_chart.php','', 'docbody');
	
	//EditorBG.removeNode(false);
	//Editor.removeNode(false);
	//EditorTitleBar.removeNode(false);
	
	container.removeChild(Editor);
	container.removeChild(EditorTitleBar);
	docbody.removeChild(EditorBG);
	docbody.removeChild(container);
	
	
	
	
}


function loadImageWindow(object_id, img_loc, img_width, img_height, caption) {
		//alert(exit_action);
		var docbody = document.getElementById('docbody');
		
		var container = document.createElement('div');
		container.setAttribute('id','edit_container');
		container.style.position = 'absolute';
		container.style.top = '0px';
		container.style.left = '0px';
		container.style.width = '100%';
		container.style.textAlign = 'center';
		
		
		
		var EditorBG = document.createElement('div');
		EditorBG.setAttribute('id',('editBackground_'+object_id));
		
		EditorBG.style.position = 'absolute';
		EditorBG.style.top = '0px';
		EditorBG.style.left = '0px';
		EditorBG.style.filter = 'alpha(opacity=60)';
		EditorBG.style.opacity = '.60';
		EditorBG.style.backgroundColor = 'black';
		EditorBG.style.display = 'block';
		EditorBG.style.width = "100%";
		EditorBG.style.height = "100%";

		var Editor = document.createElement('div');
		Editor.innerHTML = "<img src=\""+img_loc+"\" alt=\"Loading Image...\" onclick=\"javascript:hideImageWindow('"+object_id+"');\" style=\"cursor:pointer;\"width=\""+img_width+"\" height=\""+img_height+"\" />";
		if(typeof(caption)!="undefined"){ 
			Editor.innerHTML +="<div style=\"width:100%;border:1px solid black;background-color:black;padding-top:5px; padding-bottom:5px; \">"+caption+"</div>";
		}
		Editor.setAttribute('id',object_id);
		Editor.setAttribute('class','editor');
		Editor.style.position = 'relative';
		Editor.style.display = 'block';
		Editor.style.margin = '0 auto';
		Editor.style.padding = '0px';
		Editor.style.width = img_width+'px';
		Editor.style.height = img_height+'px';
		Editor.style.backgroundColor = 'black';
		Editor.style.border = '1px solid black';
		
		Editor.style.top = (getWindowHeight()-img_height)/2 + 'px';
		
		container.appendChild(Editor);
		
		docbody.appendChild(EditorBG);
		docbody.appendChild(container);
		
		//float_interval[object_id] = setInterval('Float(\"'+object_id+'\",20)', 100);
	}
	
function hideImageWindow(object_id){
	var docbody = document.getElementById('docbody');
	var container = document.getElementById('edit_container');
	var Editor = document.getElementById(object_id);
	var EditorBG = document.getElementById('editBackground_'+object_id);
	
	clearInterval(float_interval[object_id]);
	
	EditorBG.style.display = 'none';
	Editor.style.display = 'none';
	Editor.innerHTML = "";
	
	container.removeChild(Editor);
	docbody.removeChild(EditorBG);
	docbody.removeChild(container);
	
}


function hideDelete(){
	var deleteit = document.getElementById('delete');
	var deleteitBG = document.getElementById('editBackground_delete');
	
	deleteitBG.style.display = 'none';
	deleteit.style.display = 'none';
	deleteit.innerHTML = "";
	
	deleteitBG.removeNode(false);
	deleteit.removeNode(false);
	
	
}

//ajax
function getDynamicContent(url,post_str, to_object_id){
	
	show_loader = false;
	
	eval("var xmlHttpReq"+to_object_id+" = false;");
	var self = this;
	
	var to_object = document.getElementById(to_object_id);
	to_object.innerHTML = "Loading ...<br />";
	
	
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		eval("self.xmlHttpReq"+to_object_id+" = new XMLHttpRequest();");
	}
	
	// IE
	else if (window.ActiveXObject) {
		eval("self.xmlHttpReq"+to_object_id+" = new ActiveXObject(\"Microsoft.XMLHTTP\")");
	}
	
	eval("var curXHR = self.xmlHttpReq"+to_object_id);
	
	curXHR.open('POST', url, true);
	curXHR.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	curXHR.onreadystatechange = function() {
		if (curXHR.readyState == 4) {
				to_object.innerHTML = curXHR.responseText;
				//Sound.enable();
			   	//Sound.play('',{replace:true});
			   	//Sound.disable();


				//execute any scripts
				var re = new RegExp("<script[^>]*?>([^>]*?)</script>","ig");
				var match;
				while (match = re.exec(curXHR.responseText)) {
					eval(match[1]);
				}

		}else{
			if( (to_object.id != 'docbody') && show_loader){
				
				to_object.innerHTML = "Loading ......<br /><img src=\"images/ajax-loader.gif\" alt=\"Loading.\" \/>";
			}
		}
	}
						
	curXHR.send(post_str);

	
}


function clearInnerHTML(obj_id){
	obj = document.getElementById(obj_id);
	obj.innerHTML = '';
}

var animate_cur_obj = null;
var animate_cur_w = 0;
var animate_cur_h = 0;
var animate_to_w = 0;
var animate_to_h = 0;

var animate_scroll_increment = 0;
var animate_scroll_direction = 1;
var animate_scroll_last;

var animate_on_finish = "";
var aInterval;

function animateResizeObject(obj_id, from_w, from_h, to_w, to_h, on_load, on_finish){
	animate_cur_obj = document.getElementById(obj_id);
	animate_cur_w = from_w;
	animate_cur_h = from_h;
	animate_to_w = to_w;
	animate_to_h = to_h;
	animate_on_finish = on_finish;
	animate_on_load = on_load;
	aInterval = setInterval('resizeObject()',1);
}

function resizeObject(){ ///this will work out better if 'steps' are calculated

	eval(animate_on_load);

	if(animate_cur_w < animate_to_w){
		animate_cur_obj.style.width = animate_cur_w + "px";
		animate_cur_w += 10;
	}else{
		animate_cur_w = animate_to_w;
		animate_cur_obj.style.width = animate_to_w + "px";
	}
	
	if(animate_cur_h < animate_to_h){
		animate_cur_obj.style.height = animate_cur_h + "px";
		animate_cur_h += 10;
	}else{
		animate_cur_h = animate_to_h;
		animate_cur_obj.style.height = animate_to_h + "px";
	}
	
	if( (animate_cur_h == animate_to_h) && (animate_cur_w == animate_to_w)){
		clearInterval(aInterval);
		eval(animate_on_finish);
	}
	
}

function animateScrollObject(obj_id, direction, increment){
	clearInterval(aInterval);
	animate_cur_obj = document.getElementById(obj_id);
	animate_scroll_last = animate_cur_obj.scrollTop;
	animate_scroll_increment = increment;
	animate_scroll_direction = direction;
	aInterval = setInterval('scrollObject()',1);
}

function scrollObject(){
	
	//code for when it hits the end or  scrollHeight
	
	if( ( (animate_scroll_direction == -1) && (animate_cur_obj.scrollTop > animate_scroll_last-animate_scroll_increment )) || ( (animate_scroll_direction ==  1) && (animate_cur_obj.scrollTop < animate_scroll_last+animate_scroll_increment ))){
		animate_cur_obj.scrollTop = animate_cur_obj.scrollTop + (animate_scroll_direction*5);
	}else{
		clearInterval(aInterval);
	}
	
}

function showObject(obj_id){
	obj = document.getElementById(obj_id);
	obj.style.display = 'block';
}

function hideObject(obj_id){
	obj = document.getElementById(obj_id);
	obj.style.display = 'none';
}