/* 
JavaScript Document for www.bolnet.ca
Author: James Nicol, Glossopteris Web Designs, www.glossopteris.com. February 2006
*/

/*----- Behaviour Registers to assign actions to page elements on load -----*/
var bolnet_rules = {

	'#contact_link' : function(el) {
		el.onclick = function() { showcontactpage(); return false; }
	},
	'#larger_size' : function(el) {
		el.onclick = function(){ setActiveStyleSheet('large','text'); return false; }
	},
	'#default_size' : function(el) {
		el.onclick = function(){ setActiveStyleSheet('small','text'); return false; }
	},
	'#google_search' : function(el) {
		el.onclick = function() { submitGsearch(); return false; }
	},
	'#page_search' : function(el){
		el.onclick = function(){ page_search(); return false; }
	},
	'.page_links a' : function(el) {
		el.onclick = function() { bolnet_scroll(this); return false; }
	},
	'a.pl_top' : function(el) {
		el.onclick = function(el) { bolnet_scroll(this); return false; }
	},
	'#cite_link' : function(el) {
		el.onclick = function(el) { MM_openBrWindow('how_to_cite.php','popup','width=400,height=400'); return false; }
	}	

}

/*----- Google Site Search code -----*/
var domainroot="www.bolnet.ca"

function Gsitesearch(){ 
	document.site_search.q.value="site:"+domainroot+" "+document.site_search.search_text.value; 
}
function submitGsearch() {
	document.site_search.q.value="site:"+domainroot+" "+document.site_search.search_text.value; 
	document.site_search.submit();
}
window.onsubmit = function(){
	Gsitesearch();
}

/*------ Image preloader -----*/
function preload_images () {

	MM_preloadImages(	
					 	'images/minus_icon.gif',
						'images/page_links_bullet.gif',					 
					 	'images/minus_icon_02.gif',
						'images/page_links_bullet_02.gif'					 
					 )

}

Behaviour.register(bolnet_rules);
Behaviour.addLoadEvent(set_page_style);
Behaviour.addLoadEvent(preload_images);

/*----- Page Search function -----*/
function page_search() {
	document.stream_search.submit();
}


/*----- People popup functions -----*/

var xPos = 200;
var yPos = 125;

Event.observe(document, "mousedown", get_mouse_pos, false);

function people_popup(el){

	if($('people_popup')){ hide_people_popup(); }
	
	var elmnt = document.createElement("div");
	var container = $('container');
	container.appendChild(elmnt);
	elmnt.setAttribute('id','people_popup');
	elmnt.style.display = 'none';
	elmnt.style.left = xPos+100+'px';
	elmnt.style.top = yPos-50+'px';
	
	var get_array = el.href.split('?');
	var pars = get_array[1];
	var url = 'html_includes/people_popup.php';
	var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onComplete: show_people_popup} );

}

function show_people_popup(original_request){

	$('people_popup').innerHTML = original_request.responseText;
	$('people_popup').style.display = 'block';
	fadeInDiv('people_popup',0);
	new Draggable('people_popup',{handle: $('pp_h2'), revert:false});

}

function get_mouse_pos(e){
	
	xPos = Event.pointerX(e);
	yPos = Event.pointerY(e);
	
}

function hide_people_popup(){

	var container = $('container');
	container.removeChild($('people_popup'));

}

var people_popup_rules = {
	'a.pp_link' : function(el){
		el.onclick 	= function(){
			people_popup(this); 
			return false; 
		}
	}
};

Behaviour.register(people_popup_rules);


/*----- Some Scriptaculous effects -----*/
function bolnet_scroll(el){
	var ref = el.href.split('#');
	new Effect.ScrollTo(ref[1],{offset:-30}); return false;
}

/*----- Browse page effects -----*/
var browse_rules = {
	'span.folder_li' : function(el){
		el.onclick 	= function(){ show_folder_elements(this); return false; }
	}
};

Behaviour.register(browse_rules);

function show_folder_elements(el) {

	var el_id = el.id.split('_');
	var num = el_id[1];
	if($('element_'+num).style.display == 'none'){
		el.style.background = 'url(../../../images/minus_icon.gif) no-repeat top left'
		new Effect.BlindDown('element_'+num);
		setCookie('element_'+num,'open');
	}else{
		el.style.background = 'url(../../../images/plus_icon.gif) no-repeat top left'
		new Effect.BlindUp('element_'+num);	
		setCookie('element_'+num,'closed');
	}

}

function hide_folder_elements(){
	var fe = document.getElementsBySelector('ul.table_contents');
	for(var i=0; i<fe.length; i++){
		if(readCookie(fe[i].id)!='open'){ fe[i].style.display = 'none'; }
		else{ 
			fe[i].parentNode.style.background = 'url(../../../images/minus_icon.gif) no-repeat top left'; 
		}
	}
}

Behaviour.addLoadEvent(hide_folder_elements);

/*----- AJAX script for Calendar -----*/
/*
function update_calendar(month) {
	
	var url = 'html_includes/ajax_calendar.php';
	var month_val = month.split('?');
	var pars = month_val[1];
	var myAjax = new Ajax.Updater('calendar', url, {method: 'get', parameters: pars}); 

}

function change_calendar(el) {

	new Effect.Opacity('calendar', { duration:1.0, transition:Effect.Transitions.linear, from:1.0, to:0 });
	update_calendar(el.href);
	new Effect.Opacity('calendar', { duration:1.0, transition:Effect.Transitions.linear, from:0, to:1.0 });

}
*/

function change_calendar(el){
	
	var url = 'html_includes/ajax_calendar.php';
	var month_val = el.href.split('?');
	var pars = month_val[1];
	var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onComplete: update_calendar} );
	
}

function update_calendar(original_request){

	new Effect.Opacity('calendar', { duration:1.0, transition:Effect.Transitions.linear, from:1.0, to:0 });
	$('calendar').innerHTML = original_request.responseText;
	new Effect.Opacity('calendar', { duration:1.0, transition:Effect.Transitions.linear, from:0, to:1.0 });

}

function events_day(day){

	if($('events_day_box')){ hide_events_day(); }
	
	var elmnt = document.createElement("div");
	var container = $('container');
	container.appendChild(elmnt);
	elmnt.setAttribute('id','events_day_box');
	elmnt.style.display = 'none';

	var url = 'html_includes/events_day_build.php';
	var pars = 'day=' + day;
	var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onComplete: showEventBox} );

}

function showEventBox(original_request){

	$('events_day_box').innerHTML = original_request.responseText;
	$('events_day_box').style.display = 'block';
	fadeInDiv('events_day_box',0);

}

function hide_events_day(){

	var container = $('container');
	container.removeChild($('events_day_box'));

}

var calendar_rules = {
	'#prev_month' : function(el){
		el.onclick = function(){ 
			change_calendar(this); 
			return false;
		}
	},
	'#next_month' : function(el){
		el.onclick = function(){ 
			change_calendar(this); 
			return false;
		}
	}
};


Behaviour.register(calendar_rules);

function MM_openBrWindow(theURL,winName,features) 
{ //v2.0
  window.open(theURL,winName,features);
}