var slides;
var fades = new Array();
var fadeDuration = 1000;
var MODULES_URL = 'http://kresge.collegeforcreativestudies.edu/includes/modules';
var BASE_IMAGES = 'http://kresge.collegeforcreativestudies.edu/includes/modules/base/images';
var mouseX, mouseY;
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

BrowserDetect.init();

function add_filtered_multiselect_item(listID, itemID, text, variableName, sortableList) {
	
	var newID = variableName+"_"+itemID;
	if (document.getElementById(newID) == undefined) {
		var parent = $(listID);
		var newLI = document.createElement("li");
		newLI.setAttribute("lid",itemID);
		newLI.id = newID;
		newLI.innerHTML = '<a href=\'javascript:delete_filtered_multiselect_item("'+listID+'", "'+itemID+'", "'+variableName+'")\'>'
						+(sortableList ? '<img src="http://kresge.collegeforcreativestudies.edu/includes/modules/base/images/admin/icons-16/delete.png" alt="Delete Item" /></a> ' : '')
						+(sortableList ? '<img class="drag" src="http://kresge.collegeforcreativestudies.edu/includes/modules/base/images/admin/drag.gif" alt="Drag" />' : '')
						+'<span>'+text+'</span>'
						+(!sortableList ? '</a>' : '');
		parent.appendChild(newLI);

		//need to add to javascript Sortables object
		if (sortableList) {
			sortableList = new Sortables($(listID), {
				onComplete: function(){
					var values = get_list_order(listID, "lid");
					$(variableName).value = values.join('_&_');
				}
			 });
		}
		//also mark as "selected" in hidden field
		var values = get_list_order(listID, "lid");

		$(variableName).value = values.join('_&_');
	}
}

function add_orderable_list_item(listID, itemID, text, variableName, sortableList) {
	var isDivider = false;
	if (itemID == -1){ //is a divider?
		isDivider = true;
		itemID = - Math.floor(Math.random()*1000+1);
	}
	var newID = variableName+"_"+itemID;
	if (document.getElementById(newID) == undefined || isDivider == true) { //allow multiple dividers
		var parent = $(listID);
		var newLI = document.createElement("li");
		var lidVal = (isDivider ? -1 : itemID);
		newLI.setAttribute("lid",lidVal);
		newLI.id = newID;

		var itemForm = "items_specialForm";
		newLI.innerHTML = '<a href=\'javascript:delete_orderable_list_item("'+listID+'", "'+itemID+'", "'+variableName+'");\'>'
							+'<img src="http://kresge.collegeforcreativestudies.edu/includes/modules/base/images/admin/icons-16/delete.png" alt="Delete Item" /></a> '
						+'<img class="drag" src="http://kresge.collegeforcreativestudies.edu/includes/modules/base/images/admin/drag.gif" alt="Drag" />'
						+'<span>'+text+'</span>';
		parent.appendChild(newLI);

		//need to add to javascript Sortables object
		if (sortableList) {
			sortableList = new Sortables($(listID), {
				onComplete: function(){
					var values = get_list_order(listID, "lid");
					$(variableName).value = values.join('_&_');
				}
			 });
		}
		//also mark as "selected" in hidden field
		var values = get_list_order(listID, "lid");
		$(variableName).value = values.join('_&_');
	}
	//else, msg "They're already in the list."
}

function addIEFavorite(bookmarkID, url, who, txt) {
	var browserName = navigator.appName;
	var browserVersion = parseInt(navigator.appVersion);

	if ((browserName == "Microsoft Internet Explorer") && (browserVersion >= 4)) {
		window.external.AddFavorite(url, who);
	} else {
		alert("Use CTRL-D to bookmark in Firefox.");
	}
}

function change_class(){
	args = change_class.arguments;
	for(i = 0; i < args.length; i += 2){
		if(document.getElementById(args[i])){
			document.getElementById(args[i]).className = args[i + 1];
		}
	}
}

function create_multi_selects(){
	var elements = $ES("ul", "body");
	var multiSelects = elements.filterByClass("multiSelect");
	
	for(var i = 0; i < multiSelects.length; i++){	
		var id = multiSelects[i].id
		var pos = id.indexOf("ID");
		var name = id.substr(0, pos);
		
		eval("var MSL_" + name + " = new MultiSelectList('MSL_" + name + "');");
	}
}

function create_slides(){
	var elements = $ES("a", "body");
	
	var slideElements = elements.filterByClass("slideVertical");
	
	var slides = new Array();
	
	for(i = 0; i < slideElements.length; i++){
		// store the target
		var target = slideElements[i].target;
		
		if($defined($(target))){
			var hideFirst = ((slideElements[i].className.indexOf("hideFirst") > -1) || slideElements[i].className.indexOf("hideNow") > -1 ? true : false);
			// set up the slide on the target id
			slides[i] = new SlideWrapper(slideElements[i].id, target, hideFirst, "vertical", false);
	
			$(target).style.display = "";
		}
	}
	
	elements = undefined;
}

function delete_filtered_multiselect_item(listID, itemID, variableName) {
	var deleteID = variableName+"_"+itemID;
	var parent = $(listID);
	if ($(deleteID) == undefined) {
		alert ("Item '"+deleteID+"' is not defined.");
	} else {
		var item = $(deleteID);
		item.style.display = "none";
		item.id = "";
		parent.removeChild(item);
		var orderedList = get_list_order(listID, "lid");
		$(variableName).value = orderedList.join('_&_');
	}
}

function delete_orderable_list_item(listID, itemID, variableName) {
	var deleteID = variableName+"_"+itemID;
	var parent = $(listID);

	//remove item from list and refresh hidden field with new order
	if ($(deleteID) == undefined) {
		alert ("Item '"+deleteID+"' is not defined.");
	} else {
		var item = $(deleteID);
		item.style.display = "none";
		item.id = "";
		parent.removeChild(item);
		var orderedList = get_list_order(listID, "lid");
		$(variableName).value = orderedList.join('_&_');
	}
}

function escape_values(string){
	var str = "";
	var parts = string.split("&");
	for(var i = 0; i < parts.length; i++){
		var newparts = parts[i].split("=");
		if(i != 0){
			str += "&";
		}
		str += newparts[0] + "=" + escape(newparts[1]);
	}
	return str;
}

function evaluate_js(response){
	//alert("evaluate_js");
	// now find the scripts that are needed
	var startTag = "<script type='text/javascript'>";
	var endTag = "</script>";
	
	var pos = response.indexOf(startTag);

	while(pos > -1){
		var endPos = response.indexOf(endTag, pos + 1);
		if(endPos > -1){
			var start = pos + startTag.length;
			var js = response.substr(start, endPos - start);
			//alert(js)
			eval(js);
			pos = response.indexOf(startTag, endPos);
		}
	}
}

function evaluate_js_headers(response){
	var startTag = "<script type='text/javascript' src='";
	var endTag = "' />";
	
	var pos = response.indexOf(startTag);

	while(pos > -1){
		var endPos = response.indexOf(endTag, pos + 1);
		if(endPos > -1){
			var start = pos + startTag.length;
			var jsFile = response.substr(start, endPos - start);
			alert(jsFile);
			var script = document.createElement('script');
			script.type = 'text/javascript';
			script.src = jsFile;
			document.getElementsByTagName('head')[0].appendChild(script);
			pos = response.indexOf(startTag, endPos);
		}
	}
}

function event_mouse_move(){
	if (BrowserDetect.browser != "Explorer") document.captureEvents(Event.MOUSEMOVE);
	// Set-up to use getMouseXY function onMouseMove
	document.onmousemove = get_mouse_xy;
}

function fade_in(id){
	if($defined($(id)) && ($(id).style.display == "" || $(id).style.display == "none")){
		$(id).style.display = "block";
	}
	fade_to(id, 0, 100);
}

function fade_to(id, start, end){
	if($defined($(id))){
		// if slide isn't already defined, define it
		if(!$defined(fades[id])){
			fades[id] = new Fx.Style(id, 'opacity', {duration:fadeDuration});
		}
		fades[id].start(start, end);
	}
}

function fade_out(id){
	fade_to(id, 100, 0);
}

function filter_adminList(module, id, queryStr){
//default params
	if (typeof(module) == 'undefined' || typeof(id) == 'undefined')
		return 0;
	base_ajax(id, MODULES_URL + "/"+module+"/controllers/adminList.php", "justListItems=true&"+queryStr);
}

function filter_search(form, id){
	var queryStr = form.toQueryString();
	
	base_ajax(id, form.redirectURL.value, "mode=search&" + queryStr, "evaluate_js(response);");
	return false;
}

function get_cookie(cookiename){
	var allcookies = document.cookie;
	var pos = allcookies.indexOf(cookiename + "=");
	if(pos != -1){
		var start = pos + (cookiename.length + 1);
		var end = allcookies.indexOf(";", start);
		if(end == -1){ end = allcookies.length; }
		var value = allcookies.substring(start, end);
		value = unescape(value);
		return value;
		
	}else{
		return false;
	}
}

function get_cookie_filter(module) {
	return get_cookie('cms_'+module+'_filter'); 
}

function get_list_order(theList, attributeName){
	var ar = new Array();
	var children = $(theList).childNodes;
	// clear the styles first
	for(i = 0; i < children.length; i++){
		if(children[i].nodeName == "LI"){	// make sure it is a list tag
			if(children[i].getAttribute(attributeName) != null){
				ar.push(children[i].getAttribute(attributeName));
			}
		}
	}
	return ar;	// return the array with the order
}

function get_mouse_xy(e) {
  if (BrowserDetect.browser == "Explorer") { // grab the x-y pos.s if browser is IE
    mouseX = event.clientX + document.body.scrollLeft
    mouseY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    mouseX = e.pageX
    mouseY = e.pageY
  }  
  // catch possible negative values in NS4
  if (mouseX < 0){mouseX = 0}
  if (mouseY < 0){mouseY = 0}  

  return true
}

function get_selection_parts(area){
	//if(document.selection && !(BrowserDetect.browser == "Explorer" && (BrowserDetect.version == "6" || BrowserDetect.version == "7"))){ // The current selection 
	if(document.selection){ // The current selection 
		var range = document.selection.createRange(); 
		// We'll use this as a 'dummy' 
		var stored_range = range.duplicate(); 
		// Select all text 
		//stored_range.moveToElementText(area); 
		// Now move 'dummy' end point to end point of original range
		stored_range.setEndPoint( 'EndToEnd', range ); 
		// Now we can calculate start and end points 
		area.selectionStart = stored_range.text.length - range.text.length; 
		area.selectionEnd = area.selectionStart + range.text.length;
	}
	
	
	var s = area.value.substr(0, area.selectionStart);
	var sel = area.value.substr(area.selectionStart, area.selectionEnd - area.selectionStart);
	var e = area.value.substr(area.selectionEnd);
	
	//if(sel == "" || (BrowserDetect.browser == "Explorer" && (BrowserDetect.version == "6" || BrowserDetect.version == "7"))){
	if(sel == ""){
		return false;
	}else{
		return {start : s, selected : sel, end : e};
	}	
}

// creates and manages an html select form element, from a
function MultiSelectList(id){
	var parts = id.split("_");
	this.listID = id;
	this.variableName = parts[1];
	//alert(this.listID + "ID");
	//alert(this.variableName + "ID");
	this.list = $(this.variableName + "ID");
	this.children = this.list.childNodes;
	this.listItems = new Array();
	this.select;
	this.selectedList = new Array();
	
	this.clear_list = function(){
		
		for(i = 0; i < this.selectedList.length; i++){

			// reset the css class
			this.highlight(this.selectedList[i], false);
			
			// update the select form field
			this.form_select(this.selectedList[i], false);
		}
		
		this.selectedList = new Array();
	}
	
	this.click = function(index){
		// if the item is already selected
		var position = this.is_selected(index);
		//alert(this.selectedList.join(","));
		if(position != -1){
			//alert("removing: length=" + this.selectedList.length);
			//alert("this.selectedList.splice(" + position + ",1);")
			
			// take the item out of the array
			this.selectedList.splice(position,1);
			// reset the css class
			this.highlight(index, false);
			// update the select form field
			this.form_select(index, false);
		}else{	// if new
			//alert("adding: length=" + this.selectedList.length);
			// add to the selected list
			this.selectedList.push(index);
			// highlight it
			this.highlight(index, true);
			// select the form field option
			this.form_select(index, true);
		}
	}
	
	this.form_select = function(index, selected){
		//alert("msoption-" + this.listID + "-" + index);
		//alert($("msoption-" + this.listID + "-" + index).selected);
		$("msoption-" + this.listID + "-" + index).selected = selected;
		//alert($("msoption-" + this.listID + "-" + index).selected);
	}
	
	this.highlight = function(index, color){
		var elem = $("msli-" + this.listID + "-" + index);
		
		if(color == true){
			elem.className = "highlight";
		}else{
			elem.className = "normal";
		}
	}
	
	// deciphers the next image, and displays it
	this.init = function(){		
		var multiSelect = "<select id='" + this.listID + "Select' name='" + this.variableName + "[]' multiple  style='display: none;'>";

		// loop through the list and read the values			
		for(i = 0; i < this.children.length; i++){
			if(this.children[i].nodeName == "LI"){	// make sure it is a list tag
				// add the list item
				this.listItems.push(this.children[i]);
				var currentIndex = this.listItems.length - 1;

				if (this.children[i].getAttribute("disabled") != 'disabled'){ //added by RJS 12/8/08 
				
					// set the id of the list item for later reference
					this.children[i].setAttribute("id", "msli-" + this.listID + "-" + currentIndex);
					//alert(this.children[i].id);
					// create the link for the list item, and wrap it with a link
					var link = document.createElement('a');
					link.setAttribute("href", "javascript:" + this.listID + ".click(" + currentIndex + ");");
					
					// store the contents of the list item
					var nodeText = this.children[i].firstChild.nodeValue;
					var linkText = document.createTextNode(nodeText);
					link.appendChild(linkText);
					
					var temp = (this.children[i].getAttribute("selected") != null ? " selected='selected'" : "");
					// update the multiselect string
					multiSelect += "<option id='msoption-" + this.listID + "-" + currentIndex + "' value='" + this.children[i].getAttribute("lid") + "'" + temp + ">" + nodeText + "</option>";
					//alert("<option id='msoption-" + this.listID + "-" + currentIndex + "' value='" + this.children[i].getAttribute("lid") + "'>" + nodeText + "</option>");
					// remove the plain text, add new text with link wrapper
					this.children[i].removeChild(this.children[i].firstChild);
					this.children[i].appendChild(link);
					
					// check if the value is selected
					if(this.children[i].getAttribute("selected") != null){
						// add to the selected list
						this.selectedList.push(currentIndex);
						
					}

				}
			}
		}
		// close the select
		multiSelect += "</select>";
		// put the hidden select after the list
		this.list.innerHTML += multiSelect;
		//this.list.outerHTML += multiSelect;
		// store reference to the new select box
		this.select = document.getElementById(this.listID + "Select");
		// show all the ones that are already selected
		this.show_all_selected();
	}
	
	this.is_selected = function(val){
		for(var i = 0; i < this.selectedList.length; i++){
			if(val == this.selectedList[i]){
				return i;
			}
		}
		return -1;
	}
	
	this.join = function(){
		var str = "";
		for(var i = 0; i < this.selectedList.length; i++){
			str += this.listItems[this.selectedList[i]].getAttribute("lid");
			if(i + 1 < this.selectedList.length){
				str += ",";
			}
		}
		return str;
	}
	
	this.show_all_selected = function(){
		
		for(var i = 0; i < this.selectedList.length; i++){
			// highlight it
			this.highlight(this.selectedList[i], true);
			// select the form field option
			this.form_select(this.selectedList[i], true);
		}
	}
	
	// run the constructor
	this.init();
}

function popUp(URL,width,height) {
	var left = (screen.width - width) / 2;
	var top = (screen.height - height) / 2;
	var size = "width=" + width + ", height=" + height +" ,left=" + left + " ,top="+ top;
	insert_window = window.open(URL, 'popup', 'toolbar=0,scrollbars=1,statusbar=0,menubar=0,resizable=1,'+size);
	insert_window.focus();
}

function pod_accordion_click(headerText,podID){
	if(typeof(pageTracker) != "undefined"){
		var labelText = headerText + " - [" + podID + "]";
		pageTracker._trackEvent("Pod-Accordion", "click", labelText);
	}
}

function say_hi(){
	alert("hi");
}

function set_cookie(name, value, domain, expires){
	var days;
	if(expires == undefined || expires == -1){ 
		days = expires == -1 ? -1 : 1; //default life of cookie, in days
	}else{
		days = expires; //life of cookie, in days
	}
	expires = new Date();
	expires.setTime( expires.getTime() + (days * 1000 * 60 * 60 * 24) );
	
	var str = name + "=" + value +"; path=/; expires=" + expires.toGMTString();
	
	str += ((domain != "" && domain != undefined)  ? "; domain=" + domain : "");

	document.cookie = str;
}

function set_cookie_filter(module, val) {
	set_cookie('cms_'+module+'_filter', val); 
}

function set_height(id, h){
	$(id).style.height = h + "px";
}

function set_left(id, h){
	$(id).style.left = h + "px";
}

function set_top(id, h){
	$(id).style.top = h + "px";
}

function set_width(id, w){
	$(id).style.width = w + "px";
}


function show_form_errors(form, jsonObj){
	var fields = jsonObj.fields;
	// reset the fields
	for(var i = 0; i < fields.length; i++){
		if($("field_" + fields[i].field) != undefined){
			var temp = $("field_" + fields[i].field).className;
			if (temp.indexOf("warning") >= 0){
				$("field_" + fields[i].field).className = temp.replace("warning", "");

				// reset the error message
				if($("message_" + fields[i].field)){
					$("message_" + fields[i].field).innerHTML = "";
				}
			}
		}
	}
	
	var errors = jsonObj.errors;
	for(var i = 0; i < errors.length; i++){
		// change style to warning
		if($("field_" + errors[i].field) != undefined && $("field_" + errors[i].field).className.indexOf("warning") < 0){
			$("field_" + errors[i].field).className += " warning";
		}
		// output the error message
		if($("message_" + errors[i].field)){
			
			var jsonMessage = errors[i].message + "";
			var errorMessage = jsonMessage.replace(/&lt;/g,"<");
			errorMessage = errorMessage.replace(/&gt;/g, ">");
			errorMessage = errorMessage.replace(/&#039;/g, "'");
			
			$("message_" + errors[i].field).innerHTML = errorMessage;
		}
	}
}

function reveal_email(part1, part2, subject){
	if(part1 != "" && part2 != ""){
		document.location = 'mailto:' + part2.reverse() + "@" + part1.reverse() + subject;
	}
}

function showHide(id){
	var elem = $(id);
	if(elem.style.display == "none" || elem.style.display == ""){
		showHideDivs(id, "show");
	}else{
		showHideDivs(id, "hide");
	}
}

function hideShow(id){
	var elem = $(id);
	if(elem.style.display == "block" || elem.style.display == ""){
		showHideDivs(id, "hide");
	}else{
		showHideDivs(id, "block");
	}
}

function showHideDivs() { //v6.0
	var i,p,v,obj,args=showHideDivs.arguments;
	for (i=0; i < (args.length-1); i+=2) {
		if((obj = $(args[i])) != undefined){ 
			v=args[i+1];
			if (obj.style) { 
				obj=obj.style; 
				v=(v=='show')?'block':(v=='hide')?'none':v; 
			}
			obj.display=v;
		}
	}
}

function switch_to_https(){
	var url = window.location.href;
	if(url.indexOf("https") == -1 && url.indexOf("/admin/") < 0){
		p = url.indexOf("p");
		window.location = "https" + url.substr(p + 1);
	}
}

function submit_form(id){
	$(id).submit();
}

function try_text_focus(form, field){
	if($defined($(form))){
		eval("var field = $('" + form + "')." + field + ";");
		if(field != undefined){
			field.focus();
		}
	}	
}

function upper_case_first(text){
	return text.substr(0, 1).toUpperCase() + text.substr(1);
}

function validate_require_all(form) {
	var readyVar = true;
	for (i=0; i<form.elements.length; i++) {
		if (form.elements[i].type != "hidden" && form.elements[i].type != "submit")
			if (form.elements[i].value=="") {
				form.elements[i].parentNode.className += " warning";
				readyVar = false;
			}
	}
	return readyVar;
}

String.prototype.reverse = function(){
	var newString = "";
	var counter = this.length;
	
	for(var i = counter; i > 0; i--){
	   newString += this.substring(i - 1, i); 
	}
	
	return newString;
}

Array.prototype.find = function(item){
	for(var i = 0; i < this.length; i++){
		if(this[i] == item){
			return true;
		}
	}
	return false;
}

// handle memory leaks
Function.prototype.closure = function(obj){
  // Init object storage.
  if (!window.__objs)
  {
    window.__objs = [];
    window.__funs = [];
  }

  // For symmetry and clarity.
  var fun = this;

  // Make sure the object has an id and is stored in the object store.
  var objId = obj.__objId;
  if (!objId)
    __objs[objId = obj.__objId = __objs.length] = obj;

  // Make sure the function has an id and is stored in the function store.
  var funId = fun.__funId;
  if (!funId)
    __funs[funId = fun.__funId = __funs.length] = fun;

  // Init closure storage.
  if (!obj.__closures)
    obj.__closures = [];

  // See if we previously created a closure for this object/function pair.
  var closure = obj.__closures[funId];
  if (closure)
    return closure;

  // Clear references to keep them out of the closure scope.
  obj = null;
  fun = null;

  // Create the closure, store in cache and return result.
  return __objs[objId].__closures[funId] = function ()
  {
    return __funs[funId].apply(__objs[objId], arguments);
  };
};

