/* site-wide javascript */

/* detect old browsers */
// derived from:
//   Browser Detect  v2.1.6
//   documentation: http://www.dithered.com/javascript/browser_detect/index.html
//   license: http://creativecommons.org/licenses/by/1.0/
//   code by Chris Nott (chris[at]dithered[dot]com)

function BrowserDetect() {
   var ua = navigator.userAgent.toLowerCase(); 

   // browser engine name
   this.isGecko       = (ua.indexOf('gecko') != -1 && ua.indexOf('safari') == -1);

   // browser name
   this.isSafari      = (ua.indexOf('safari') != - 1);
   this.isOpera       = (ua.indexOf('opera') != -1); 
   this.isAol         = (ua.indexOf('aol') != -1); 
   this.isIE          = (ua.indexOf('msie') != -1 && !this.isOpera && (ua.indexOf('webtv') == -1) ); 
   this.isMozilla     = (this.isGecko && ua.indexOf('gecko/') + 14 == ua.length);
   this.isNS          = ( (this.isGecko) ? (ua.indexOf('netscape') != -1) : ( (ua.indexOf('mozilla') != -1) && !this.isOpera && !this.isSafari && (ua.indexOf('spoofer') == -1) && (ua.indexOf('compatible') == -1) && (ua.indexOf('webtv') == -1) && (ua.indexOf('hotjava') == -1) ) );
       
   // browser version
   this.versionMinor = parseFloat(navigator.appVersion); 
   
   // correct version number
   if (this.isGecko && !this.isMozilla) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('/', ua.indexOf('gecko/') + 6) + 1 ) );
   }
   else if (this.isMozilla) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('rv:') + 3 ) );
   }
   else if (this.isIE && this.versionMinor >= 4) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('msie ') + 5 ) );
   }
   else if (this.isKonqueror) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('konqueror/') + 10 ) );
   }
   else if (this.isSafari) {
      this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf('safari/') + 7 ) );
   }
   else if (this.isOpera) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('opera') + 6 ) );
   }
   
   this.versionMajor = parseInt(this.versionMinor); 
     
   // specific browser shortcuts
   this.isNS7up = (this.isNS && this.versionMajor >= 7);  
   this.isIE55 = (this.isIE && this.versionMinor == 5.5);
   this.isIE6up = (this.isIE && this.versionMajor >= 6);
   
   this.isIE4xMac = (this.isIE4x && this.isMac);
}
var browser = new BrowserDetect();
if (!document.getElementById || 
   (browser.isIE && !(browser.isIE55 || browser.isIE6up)) ||
   (browser.isNS && !browser.isNS7up) ||
   (browser.isSafari && !browser.versionMinor >= 125))
{
   loadPage("UnsupportedBrowser");
}

function loadPage(pageName) {
   if (pageName != '') {
      document.location=pageName;
   }
}

function getElement(elementName) {
   return document.getElementById(elementName);
}

function simpleHide(elementName) {
	// "if (elementName instanceof HTMLElement)" breaks in IE
	var element = getElement(elementName) || elementName
	
	element.style.display = 'none';
}

function hide(elementName) {
	// "if (elementName instanceof HTMLElement)" breaks in IE
	var element = getElement(elementName) || elementName
	
  	$(element).slideUp("fast")
}

function simpleShow(elementName) {
	// allow for elementName to be an object
	// "if (elementName instanceof HTMLElement)" breaks in IE
	var element = getElement(elementName) || elementName
	
	element.style.display = 'inline';
}

function show(elementName) {
	// allow for elementName to be an object
	// "if (elementName instanceof HTMLElement)" breaks in IE
	var element = getElement(elementName) || elementName

	$(element).slideDown("fast")
}

function handleOtherSelect(selectId, otherSpanId, otherId) {
	var select = getElement(selectId);  
	if (select.options[select.selectedIndex].value=='other') { 
	  getElement(otherId).value = ''; // clear the 'other' text field
	  show(otherSpanId); 
	} 
	else { 
	  hide(otherSpanId);  
	  getElement(otherId).value = select.options[select.selectedIndex].text;  
	}
}

function bookmarkPage(title, url) {
   if (window.external) {
      window.external.AddFavorite(url, title);
   }
   else if (window.sidebar) {
      window.sidebar.addPanel(title, url, "");
   }
}

/* add the tag to the field
   if the tag already exists, remove it */
//function toggleTag(field, tag) {
//	if (field.value == '') {
//	  field.value = tag;
//	}
//	else {
//	  var newTagList = new Array();
//	  var tags = field.value.split(/\s*,\s*/);
//	  var foundTag = false;
//	  for (var i = 0; i < tags.length; i++) {
//	  	if (tags[i] == tag) {
//	  		foundTag = true;
//	  	}
//	  	else {
//			newTagList.push(tags[i]);
//	  	}			
//	  }
//	  
//	  if (!foundTag) {
//	  	newTagList.push(tag);
//	  }
//
//	  field.value = newTagList.join(", ");
//	}
//}

function toggle_tag(input_element, tag) {
  if (input_element.value.blank()) {
    input_element.value = tag
  } else {
    // make an array from comma-separated list of tags
    tags = input_element.value.split(',').collect( function(t) { return t.strip(); } );
    if (tags.include(tag)) {
      // remove the clicked element from the tags array and reset the value of input
      input_element.value = tags.reject( function(t) { return t == tag; } ).join(', ');
    } else {
      input_element.value += (', ' + tag)
    }
  }
}

/* add the tag to the field
   if the tag already exists, remove it */
function toggleTag(tag) {
  var field = null;
	if (currentOpenBookmark) {
		field = getFirstKlass("tagsField", currentOpenBookmark)
	}
	else {
		field = getFirstKlass("tagsField")
	}
	
	if (field.value == '') {
	  field.value = tag;
	}
	else {
	  var newTagList = new Array();
	  var tags = field.value.split(/\s*,\s*/);
	  var foundTag = false;
	  for (var i = 0; i < tags.length; i++) {
	  	if (tags[i] == tag) {
	  		foundTag = true;
	  	}
	  	else {
			newTagList.push(tags[i]);
	  	}			
	  }
	  
	  if (!foundTag) {
	  	newTagList.push(tag);
	  }

	  field.value = newTagList.join(", ");
	}
}

// make rounded corners with ruzeeborders.js
function makeRoundedCorners() {
	RUZEE.Borders.add({
	  '.box, .whiteBox, .hiliteBox': { borderType:'simple', cornerRadius:8 }
	});

	RUZEE.Borders.render(); 
}

var currentOpenBookmark = null

function getKlass(className) {
	var elements = document.getElementsByClassName(className)
	return elements
}

function getKlass(className, parentElement) {
	var elements = document.getElementsByClassName(className, parentElement)
	return elements
}

function getFirstKlass(className, parentElement) {
	return (getKlass(className, parentElement))[0]
}

function hideAll(className, parentElement) {
	getKlass(className, parentElement).each(
		function(node) { hide(node) }
	)
}

function showAll(className, parentElement) {
	getKlass(className, parentElement).each(
		function(node) { show(node) }
	)
}

function hideCurrentOpenBookmark() {
	if (currentOpenBookmark) {
    hideAll('editBookmarkForm', currentOpenBookmark)
		showAll('bookmarkDisplay', currentOpenBookmark)
		currentOpenBookmark = null
	}	
}

function editBookmark(obj) {
	// first close all open edit forms
  hideCurrentOpenBookmark()
	Element.hide('createBookmarkForm') // make sure create form is hidden
	
	// get the parent bookmark node (this obviously needs to be changed if the html changes)
  // var bookmarkNode = obj.parentNode.parentNode.parentNode.parentNode
  // Commented out above and added below w/ update to Prototype 1.6
  var bookmarkNode = $(obj).up('li.bookmark');
  
  // hideAll('bookmarkDisplay', bookmarkNode)
  // Commented out above and added below w/ update to Prototype 1.6
  $(bookmarkNode).down('div.bookmarkDisplay').hide();
  
  // showAll('editBookmarkForm', bookmarkNode)
  // Commented out above and added below w/ update to Prototype 1.6
  $(bookmarkNode).down('div#editBookmarkForm').show();
  
  currentOpenBookmark = bookmarkNode
}

function autocheck_task_notification(task_id, leader_id, low_priority_value) {
	// figure out which dom elements to check
	if (task_id) {
		var task_assigned_to = 'task_assigned_to_' + task_id;
		var task_priority = 'task_priority_' + task_id;
		var notify_by_email = 'notify_by_email_' + task_id;
	} else {
		var task_assigned_to = 'task_assigned_to';
		var task_priority = 'task_priority';
		var notify_by_email = 'notify_by_email';
	}
	
	// set up our booleans
	var task_assigned_to_leader = parseInt($(task_assigned_to).value) == leader_id;
	var low_priority_task = parseInt($(task_priority).value) == low_priority_value;
	
	// check/uncheck notify by email checkbox
  if (task_assigned_to_leader || low_priority_task) {
    $(notify_by_email).checked = false;
  } else {
    $(notify_by_email).checked = true;
  }
}

function show_task_notification(leader_selected, task_id) {
    if (!leader_selected) {
        $("email_notification_" + task_id).style.visibility = 'visible';
    } else {
        $("email_notification_" + task_id).style.visibility = 'hidden';
    }
}

function show_topic_notification() {
	// show 'notify by email' checkbox if any suggested topics are checked
	if ($$('input.suggestion').pluck('checked').any()) {
		$('div#notify_container').show();
	} else {
		$('div#notify_container').hide();
	}
}