// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
 function validateLogin(button) {
      if ($F('uname') && $F('password')) {
        $(button).hide(); $('loginLoad').show();
        return true;
      } else {
        alert('Please enter your username and password.');
        return false;
      }
    }

function collectionIsOpen(id) {
	return document.getElementById('collectionExpanded'+id).className == "show"
}

function postingIsOpen(id) {
	return document.getElementById('postingExpanded'+id).className == "show"	
}

function openCollection(id) {
	document.getElementById('collectionContent'+id).className = "show";
	document.getElementById('collectionExpanded'+id).className = "show";
	document.getElementById('collectionCollapsed'+id).className = "hide";	
}

function closeCollection(id) {
	document.getElementById('collectionContent'+id).className = "hide";
	document.getElementById('collectionExpanded'+id).className = "hide";
	document.getElementById('collectionCollapsed'+id).className = "show";	
}

function openPosting(id) {
	document.getElementById('postingContent'+id).className = "show";
	document.getElementById('postingExpanded'+id).className = "show";
	document.getElementById('postingCollapsed'+id).className = "hide";	
}

function closePosting(id) {
	document.getElementById('postingContent'+id).className = "hide";
	document.getElementById('postingExpanded'+id).className = "hide";
	document.getElementById('postingCollapsed'+id).className = "show";	
}

function onDelete() {
	var idString = $('itemToDeleteId').innerHTML;
	var divId = $('itemToDeleteDivId').innerHTML;

	if(idString.indexOf('posting') >= 0 || idString.indexOf('freePosting') >= 0) {
		document.getElementById("deletingPosting").className = "show";
		new Ajax.Request('/ajax/delete', {
			parameters: {
				id: idString.replace(/[^\d]/,'')
			},
			onSuccess: function(transport) {
				$('deleteConfPanelPosting').className = 'hide';
				$(divId).className = 'hide';
			}			
		});
	} else if(idString.indexOf('collection') >= 0) {
		document.getElementById("deleting").className = "show";
		new Ajax.Request('/ajax/delete_collection', {
			parameters: {
				id: idString.replace(/[^\d]/,'')
			},
			onSuccess: function(transport) {
				$('deleteConfPanel').className = 'hide';
				$(divId).className = 'hide';
			}			
		});		
	}

}





function showme(popup)
	{
		myPopup = document.getElementById(popup);
		myPopup.className = "showme";
	}

function hideme(popup)
	{
		myPopup = document.getElementById(popup);
		myPopup.className = "hide";
	}













function showHidePopup(popup)
	{
		myPopup = document.getElementById(popup);
		
		if (myPopup.className == "popupTip")
			{
				myPopup.className = "hide";
				
			} else {
				myPopup.className = "popupTip";
				myPopup.style.zIndex = currentZ_001+1;
			}
	}


function showHide(n, o, p)
{
	myDiv = document.getElementById(n);
	myExpanded = document.getElementById(o);
	myCollapsed = document.getElementById(p);
	
	var id = n.replace(/[^\d]/g,'');
	if (myDiv.className == "show")
		{
			if(n.indexOf('posting') >= 0) {
				new Ajax.Request('/ajax/posting_state', {
					parameters: {
						id: id,
						state: 'closed'
					}
				});
			} else if(n.indexOf('collection') >= 0) {
				new Ajax.Request('/ajax/collection_state', {
					parameters: {
						id: id,
						state: 'closed'
					}
				});
			}
			myDiv.className = "hide";
			if(myExpanded) {
				myExpanded.className = "hide";
			}
			if(myCollapsed) {
				myCollapsed.className = "show";
			}
			
		} else {
			if(n.indexOf('posting') >= 0) {
				new Ajax.Request('/ajax/posting_state', {
					parameters: {
						id: id,
						state: 'open'
					}
				});
			} else if(n.indexOf('collection') >= 0) {
				new Ajax.Request('/ajax/collection_state', {
					parameters: {
						id: id,
						state: 'open'
					}
				});
			}				

			myDiv.className = "show";
			if(myExpanded) {
				myExpanded.className = "show";
			}
			if(myCollapsed) {
				myCollapsed.className = "hide";
			}
		}
}

function popWindow(url, w, h)
{
	if (w == null)
		{
			w = 500;
		}
	
	if (h == null)
		{
			h = 500;
		}
		
	window.open(url,'mywin','left=20,top=20,width='+w+',height='+h+',toolbar=0,resizable=0,scrollbars=1');
}

function setOverlay(overlay) {
  if (overlay != window.currentOverlay) closeOverlay();
  window.currentOverlay = overlay;
}

function closeOverlay() {
  if (window.currentOverlay && window.currentOverlay.hideOverlay) {
    window.currentOverlay.hideOverlay();
    return true;
  }
  return false;
}

/* Adds hidden fields to a form from the given parameters,
 * preserving nesting. e.g:
 *
 * {a: {c: 'foo'}, b: 'bar'}
 * 
 * becomes:
 * 
 * a[c]=foo&b=bar
 */
function populateForm(form, parameters) {
  (function(object, basename) {
    for (var attribute in object) switch (typeof object[attribute]) {
      case 'object':
        arguments.callee(object[attribute], attribute);
        break;
      default:
        form.appendChild(new Element('input', {
          type: 'hidden',
          name: basename ? (basename + '[' + attribute + ']') : attribute,
          value: object[attribute]
        }));
    }
  })(parameters);
}

function getUrlParam( param, url )
{
  target_url = window.location.href;
  if (arguments.length = 2) {
    target_url = arguments[1];
  }
  param = param.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+param+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( target_url );
  if( results == null )
    return null;
  else
    return results[1];
}


function ToggleLoginWidget() {
	var open_login_widget_link = document.getElementById('open_login_widget_link');
	var close_login_widget_link = document.getElementById('close_login_widget_link');
	var login_toggle = document.getElementById('login_toggle');
	var login_widget = document.getElementById('login_widget');
	
	if (login_widget.className == 'hide')
	{
		open_login_widget_link.className = 'hide';
		close_login_widget_link.className = 'show';
		login_widget.className = 'show';
		login_toggle.className = 'login_toggle_active';
		document.getElementById("uname").focus();
	}
	else
	{
		open_login_widget_link.className = 'show';
		close_login_widget_link.className = 'hide';
		login_widget.className = 'hide';
		login_toggle.className = 'login_toggle';
	}
}

function ToggleCalendar() {
	var calendar_widget = document.getElementById('calendar_box');
	var calendar_toggle = document.getElementById('calendar_toggle');
	if (calendar_widget.className == 'hide')
	{
		calendar_widget.className = 'calendar_box show';
		calendar_toggle.className = 'calendar_toggle_active';
	}
	else
	{
		calendar_widget.className = 'hide';
		calendar_toggle.className = 'calendar_toggle';
	}
}


function toggleActionLinks(actions_id)
	{
		var myelement = document.getElementById(actions_id);
		if (myelement.className == 'show')
			{
				myelement.className = 'hide';
			}
		else
			{
				myelement.className = 'show';
			}
	}