
if(typeof $ == "undefined"){
	$ = function(){};
	LoadJs("/scripts/jquery.js");
}

function ShowHide(d) {
  if(document.getElementById(d).style.display == "none") { 
    document.getElementById(d).style.display = "block"; 
  } else { 
    document.getElementById(d).style.display = "none"; 
  }
}

function Extend(d) {
  if(document.getElementById(d).style.display == "none") { 
    document.getElementById(d).style.display = "inline"; 
  } else { 
    document.getElementById(d).style.display = "none"; 
  }
}

function MoreLess(d1,d2) {
  if(document.getElementById(d1).style.display == "none") { 
    document.getElementById(d1).style.display = "inline"; 
  } else { 
    document.getElementById(d1).style.display = "none"; 
  }
  if(document.getElementById(d2).style.display == "none") { 
    document.getElementById(d2).style.display = "inline"; 
  } else { 
    document.getElementById(d2).style.display = "none"; 
  }
}


// function ToggleNotesPopup()
// div_id - the id of the notespopup div to toggle
// div_type - the type of popup ('hover' or 'normal'). if 'normal', make sure only one div is open at a time
function ToggleNotesPopup(div_id, div_type, close_others){
	//document.getElementById('div_notespopup_'+div_id).style.left = document.getElementById('span_notespopup_'+div_id).style.left;
	if(div_type == null){
		div_type = "hover";
	}
	if(close_others == null){
		if(div_type == "normal"){
			close_others = true;
		}
		else{
			close_others = false;
		}
	}
	if(document.getElementById('div_notespopup_'+div_id).style.visibility == 'visible'){
		if(div_type == "normal"){
			document.last_div_normal = null;
		}
		document.getElementById('div_notespopup_'+div_id).style.visibility = 'hidden';
		document.getElementById('div_notespopup_'+div_id).style.display = 'none';
	}
	else{
		if(close_others == true){
			if(document.last_div_normal != null){
				document.getElementById('div_notespopup_'+document.last_div_normal).style.visibility = 'hidden';
			}
			document.last_div_normal = div_id;
		}
		document.getElementById('div_notespopup_'+div_id).style.visibility = 'visible';
		document.getElementById('div_notespopup_'+div_id).style.display = 'inline';
		
		// calculate top and left
		var new_top = 0;
		var new_left = 0;
		var element = document.getElementById('span_notespopup_'+div_id);
		while(element.offsetParent != null){
			if(element.id != null && element.id.substring(0, 15) == "div_notespopup_"){break;}
			new_top += element.offsetTop;
			new_left += element.offsetLeft;
			element = element.offsetParent;
		}
		
		document.getElementById('div_notespopup_'+div_id).style.top = new_top+"px";
		document.getElementById('div_notespopup_'+div_id).style.left = new_left+"px";
	}
}


/* START AJAX FUNCTIONS */

ajaxObject = function() {
try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch(e){
try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch(e){
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e){
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e){
try { return new XMLHttpRequest() } catch(e){
throw new Error( "This browser does not support XMLHttpRequest." );
}}}}}}

function ajax_request(url, div_processing){
	if(div_processing != null && div_processing != ""){
		if(document.getElementById(div_processing) != null){
			document.getElementById(div_processing).style.display = "inline";
		}
	}
	
	var response = ajax_request2(url, "GET", "", "", "", "", "");
	
	if(div_processing != null && div_processing != ""){
		if(document.getElementById(div_processing) != null){
			document.getElementById(div_processing).style.display = "none";
		}
	}
	
	return response;
}

function ajax_request2(url, method, params, async, last_modified, optional_headers){
	if(method != "POST"){
		method = "GET";
		params = null;
	}
	if(async == null || async != false){async = true;}
	if(last_modified == ""){last_modified = null;}
	//if(optional_headers == ""){optional_headers = null;}
	
	var request = new ajaxObject();
	request.open(method, url, async);
	
	if(last_modified != null){	
		request.setRequestHeader("If-Modified-Since", last_modified);
	}
	
	if(optional_headers != null && optional_headers != ""){
		for(var i in optional_headers){
			// request.setRequestHeader(i, optional_headers[i]);
		}
	}
	
	if(method == "POST"){
		request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		request.setRequestHeader("Content-length", params.length);
		request.setRequestHeader("Connection", "close");	
	}
	
	request.send(params);
	
	if(!request.getResponseHeader("Date") && last_modified == null){
		var cached = request;
		last_modified = cached.getResponseHeader("Last-Modified");
		last_modified = (last_modified) ? last_modified : new Date(0); // January 1, 1970
		request = ajax_request2(url, method, params, async, last_modified);
		if(request.status == 304){
			request = cached;
		}
	}
	
	var response = request.responseText;
	
	return response;
}

function ajax(name, action, variables){
	document.getElementById('div_response_'+name).innerHTML = "";
	var url = "/ajax.php?action="+action;
	if(!!variables && variables.length > 0){
	  for(var i=0; i<variables.length; i++){
	    if(i%2 == 0){url += "&"+variables[i]+"=";}
		else{url += variables[i];}
	  }
	}
	var response = ajax_request(url, 'div_processing_'+name);
	document.getElementById('div_response_'+name).innerHTML = response;
}

/* END AJAX FUNCTIONS */


function select_radio(radio, option_value){
	if(radio == null){return;}
	var radio_length = radio.length;
	if(radio_length == undefined){
		radio.checked = (radio.value == option_value);
		return;
	}
	for(var i=0; i<radio_length; i++){
		if(radio[i].value == option_value){
			radio[i].checked = true;
		}
		else{
			radio[i].checked = false;
		}
	}
}

function select_option(select, option_value){
	if(select == null){return;}
	var options_length = select.options.length;
	for(var i=0; i<options_length; i++){
		if(select.options[i].value == option_value){
			//select.options[i].selected = true;
			select.selectedIndex = i;
		}
		else{
			//select.options[i].selected = false;
		}
	}
}

function toggle_checkbox(checkbox){
	if(checkbox == null){return;}
	checkbox.checked = !checkbox.checked;
}

function popUpVideo(URL, width, height){
	day = new Date();
	id = day.getTime();
	if(width == null || width == undefined || width == "" || width == 0){width = 800;}
	if(height == null || height == undefined || height == "" || height == 0){height = 480;}
	eval("page" + id + " = window.open(URL, id, 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width="+width+",height="+height+"');"); 
}

function GetElementPosition(element){
	var position = new Array(0, 0);
	if(element == null || element == undefined){
		return position;
	}
	while(element != null){
		// Check for style position
		position[0] += element.offsetLeft;
		position[1] += element.offsetTop;
		if(element.offsetParent != null && element.offsetParent != undefined){
			element = element.offsetParent;
		}
		else{
			break;
		}
	}
	return position;
}

function FixEntities(str){
	str = str.replace(/\&amp;/g,'&');
	str = str.replace(/\&lt;/g,'<');
	str = str.replace(/\&gt;/g,'>');
	str = str.replace(/\&#39;/g,"'");
	return str;
}


function AddEventHandler(exec, object, event_type, exec_return){
	var new_handler = function(event){
		if (typeof exec == 'function') {
			return_val = exec(event);
		} else {
			return_val = eval(exec);
		}
		if(return_val != null){ return return_val; }
	}
	
	if (object.addEventListener && !exec_return) {
		object.addEventListener(event_type, new_handler, false);
	} else if (object.attachEvent && !exec_return) {
		object.attachEvent("on"+event_type, new_handler);
	} else {
		var old_handler = eval("object.on"+event_type);
		var new_handler2 = function(event){
			if (typeof old_handler == 'function') {
				return_val_old = old_handler(event);
			}
			return_val_new = new_handler(event);
			if(typeof return_val_new != "undefined" && return_val_new != null && return_val_new != undefined){
				return return_val_new;
			}
			if(typeof return_val_old != "undefined" && return_val_old != null && return_val_old != undefined){
				return return_val_old;
			}
		}
		eval("object.on"+event_type+" = new_handler2");
	}
}


function AddOnload(exec, object){
	if(!object){ object = window; }
	AddEventHandler(exec, object, "load");
}


function LoadJs(src, object, onload){
	if(!object){ object = document.getElementsByTagName("head")[0]; } // document.body
	var element = document.createElement("script");
	element.type = "text/javascript";
	element.src = src;
	object.appendChild(element);
	if(onload){ AddOnload(onload, element); }
	return element;
}


/* ********** onLoad Events ********** */


function OnLoadNotesPopup(){
	$('.notespopup_container_hover').each(function () {
		var distance = 10;
		var time = 250;
		var hideDelay = 100;

		var hideDelayTimer = null;

		var beingShown = false;
		var shown = false;
		var container = this;
		var trigger = $(this).children('.notespopup_trigger');
		var info = $(this).children('.notespopup_popup').css('opacity', 0);


		$(container).mouseenter(function (event) {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			if (beingShown || shown) {
				// don't trigger the animation again
				return;
			} else {
				container.new_top = event.pageY + distance;
				container.new_left = event.pageX - 30;
				
				popup_parents = $(container).parents('.notespopup_popup');
				if(popup_parents.length>0){
					popup_parents.each(function(){
						container.new_top -= $(this).css("top").replace("px","");
						container.new_left -= $(this).css("left").replace("px","");
					});
				}
				
				// reset position of info box
				beingShown = true;

				info.css({
					top: container.new_top,
					left: container.new_left,
					display: 'block',
					zIndex: 1000+(popup_parents.length*10)
				}) .animate({
					top: '-=' + distance + 'px',
					opacity: 1
				}, time, 'swing', function() {
					beingShown = false;
					shown = true;
				})  ;
			}

			return false;
		}).mouseleave(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				info.animate({
					top: '-=' + distance + 'px',
					opacity: 0
				}, time, 'swing', function () {
					shown = false;
					info.css('display', 'none');
				});

			}, hideDelay);

			return false;
		});
	});
}

AddOnload(OnLoadNotesPopup);

