// JavaScript Document
Event.observe(window, 'load', init, false);
var allFuncs = new Object();

// Used to set everything up
function init(){
	// Focus on the Search Box
	//if (document.search)
		//document.search.keywords.focus();
	
	// Make Nifty Corners
	Nifty("div.formError","#FFFFFF","#E7C57B");
	Nifty("div.formSuccess","#FFFFFF","#D1E1E3");
	
	// Menu Hover
	//if (window.attachEvent)
	//	sfHover();
	
	// Event Coordinator
	var coord = $('coordinator_id');
	var add_coord = $('add_new_coordinator');
	if(coord && add_coord){
		if($F('coordinator_id') == 'new'){
			Event.observe('coordinator_id', 'change', showCoordinator, false);
			$('add_new_coordinator').show();					
		}else{
			Event.observe('coordinator_id', 'change', showCoordinator, false);
		}
	}
	
	
	$$('.manage-links').each(function(n){ n.addClassName('hidden') });
	$$('.day_event').each(function(n){
		Event.observe(n, 'mouseover', function(){
			$$("#"+n.id+" .manage-links").each(function(m){
				m.removeClassName('hidden');
			})
		});
		Event.observe(n, 'mouseout', function(){
			$$("#"+n.id+" .manage-links").each(function(m){
				m.addClassName('hidden');
			})
		});		
	});
	
	// Degradable AJAX
	disableActions();
	
	// Attach the click functions to the li's of the categories
	var c = document.getElementsByClassName('category_names');
	for(var i = 0; i < c.length; i++)
		Event.observe(c[i], 'click', CheckClick, false);
	
	// For the e-mailthis link
	if($('emaillink')) {
		$('emaillink').href = 'javascript:void(0)';
		$('emaillink').onclick = ShowEmail;
	}
}

function showCoordinator(){
	if($F('coordinator_id') == 'new'){
		$('add_new_coordinator').toggle();	
	}else{
		$('add_new_coordinator').hide();	
	}
}

// Helper function to get the elements by class
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

// Helper function for IE6 and below to use :hover
sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}

/**
* Automated Degradable AJAX.
* Will replace all special classes with OnClick Events
*/
function disableActions () {
    var links = document.getElementsByTagName("a");
    for (i=0; i<links.length; i++){ 
        var part= links[i];
		var action = getAction(part.className);
		if (action != ""){  
			part.onclick = allFuncs[getAction(part.className)];
			part.href = "javascript:void(0);";
			
		}	
    }
}
/**
* Shortcut to get the Javascript function out of the class name
*/
function getAction(name) {
    allNames = name.split(" ");
    for(x = 0; x < allNames.length; x++)
        if(left(allNames[x], 4) == "func"){ 
			return right(allNames[x], allNames[x].length - 4);
		}
    return "";
 }
 
 
/**
* Copy of vbscripts left function
*/
function left(str, n){
	if (n <= 0)
		return "";
	else if (n > String(str).length)
		return str;
	else
		return String(str).substring(0,n);
}
/**
* Copy of vbscripts right function
*/
function right(str, n){
	if (n <= 0)
	   return "";
	else if (n > String(str).length)
	   return str;
	else {
	   var iLen = String(str).length;
	   return String(str).substring(iLen, iLen - n);
	}
}

/**
* Used to show status of AJAX reqests at work
*/
var myGlobalHandlers = {
	onCreate: function(){
		Element.show('systemWorking');
	},

	onComplete: function() {
		if(Ajax.activeRequestCount == 0){
			Element.hide('systemWorking');
		}
	}
};
Ajax.Responders.register(myGlobalHandlers);


/**
* Basic Scriptaculous Effects
*/
Effect.OpenUp = function(element) {
	element = $(element);
	new Effect.BlindDown(element, arguments[1] || {});
}

Effect.CloseDown = function(element) {
	element = $(element);
	new Effect.BlindUp(element, arguments[1] || {});
}

/**
* Accessing GET variables
*/
function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  }
  // Taken out you don't need this unless your doing testing 
  //alert('Query Variable ' + variable + ' not found');
}

function ChangeCal(url, by_site) {
	var id;
	
	if(document.calendar_select.c.options[document.calendar_select.c.selectedIndex].className == 'custom'){
		window.location.href = document.calendar_select.c.options[document.calendar_select.c.selectedIndex].value;
	}
	
	// Go by the cal_address
	if(by_site == 'false' && document.calendar_select.c.options[document.calendar_select.c.selectedIndex].className != 'custom') { 
		if(url != 'index.php')
			window.location = '/' + document.calendar_select.c.options[document.calendar_select.c.selectedIndex].value + url;
		else
			window.location = '/' + document.calendar_select.c.options[document.calendar_select.c.selectedIndex].value + url;
	}else if(by_site == 'true' && document.calendar_select.c.options[document.calendar_select.c.selectedIndex].className != 'custom'){
		// Get the site_id
		id = document.calendar_select.c.options[document.calendar_select.c.selectedIndex].id.split('_');
		
		// Go by the site_id
		if(url != 'events.php')
			window.location = url + '&c=' + id[1];
		else
			window.location = url + '?c=' + id[1];
	}
}

function CheckClick(e) { 
	var element = Event.element(e);
	var categories = $('list_categories');
	var found_true = false;
	
	
	
	// If All categories is checked uncheck it
	if($('all_categories').checked == true)
		$('all_categories').checked = false;
	
	if(element) {
		if(element.parentNode.childNodes[0].checked == true)
			element.parentNode.childNodes[0].checked = false;
		else if(element.parentNode.childNodes[0].checked == false)
			element.parentNode.childNodes[0].checked = true;
	}
	// Loop through all the categories, if none are checked make all categories checked
	if(categories)
		for(var i=0; i <= categories.childNodes.length; i++)
			if(categories.childNodes[i])
				if(categories.childNodes[i].childNodes[0].checked == true)
					found_true = true;
	
	if(found_true == false)
		$('all_categories').checked = true;
}

function SubmitCat() {
	var keywords = $('q');
	var container = $('form_category');
	
	// If they are submitting a keyword, send it in the post along with the categories
	if(keywords.value != "Search By Keyword" && keywords.value != '') {
		// Create the hidden var of q and send it
		var input = document.createElement('input');
		input.type = 'hidden';
		input.name = 'q';
		input.value = keywords.value;
		
		container.appendChild(input);
	}
}

	//var url = self.opener.document.location;
	//url = url.toString();


function ViewEvent(event, eventdate) {
	top.opener.window.location.href = 'view.php?event=' + event + '&eventdate=' + eventdate;
	
	/*
	if(url.substr(-1, 1) == 'p') {
		url += '?event=' + event + '&eventdate=' + eventdate;
	}else if(url.substr(-1, 1) == '/') { 
		url += 'view.php?event=' + event + '&eventdate=' + eventdate;
	}else{
		url += '&event=' + event + '&eventdate=' + eventdate;
	}
	
	// replace index.php with view.php
	url = url.replace("index.php", "view.php");
	
	self.opener.document.location = url;
	*/
}

allFuncs["Previous"] = function() {
	var current_url = $('previous_url').innerHTML;
	var url = 'inc/connector/all.php';
	var pars = 'direction=previous&url=' + escape(current_url);
	
	var myAjax = new Ajax.Updater('miniCal', url, {method: 'post',parameters: pars, onComplete: disableActions});
}

allFuncs["Next"] = function() { 
	var current_url = $('next_url').innerHTML; 
	var url = 'inc/connector/all.php';
	var pars = 'direction=previous&url=' + escape(current_url);
	
	var myAjax = new Ajax.Updater('miniCal', url, {method: 'post',parameters: pars, onComplete: disableActions});
}

function ShowEmail() {
	var form = $('form');
	
	if(form.style.display == 'none')
		Effect.BlindDown(form, {duration: .3});
	else
		Effect.BlindUp(form, {duration: .3});
}

function ToggleRequest() {
	$('deny_text').toggle();	
}