function loading_layer() {
  smokeScreen = false;
  CloseModal(); 
  load_content = '<div style="background: #fff; padding: 50px; border: 5px solid #ccc;"><img src="/images/indicator.gif" width="20" align="absmiddle"  /> <strong>Saving changes&hellip;</strong></div>'
  var load_load_load = document.createElement('div'); 
  load_load_load.innerHTML = load_content; 
  new ModalDialog('', load_load_load);
}
function checkAskQuestion(Object) {
  var reg = /^Ask a question about/i;
  var ar = reg.exec(Object.value);
  if (ar) {
    return false; // Match.
  }
  return checkEmpty(Object);
}
function checkEmpty(Object) {
  return ((Object.value.length==0) || (Object.value==null)) ? false : true;
}
function checkAddToListSubmit(Object) {
  var reg = /^What are your favorites?/i;
  var ar = reg.exec(Object.value);
  if (ar) {
    return false; // Match.
  }
  return checkEmpty(Object);
}
function decideEntryAction(obj, id){
 if (document.getElementById(id) && document.getElementById(id).value != '') {  
    obj.submit();
    return true;
 }
}
function popWindow(url,name,options){
        var ContextWindow = window.open(url,name,options);
        ContextWindow.opener = this;
        ContextWindow.focus();
}
var navPopState = 0;
function navPopup(){
  if (navPopState == 0){
    document.getElementById('nav-popup-menu').style.display = 'block';
    document.getElementById('nav-account-links').style.height = '100px';
    navPopState = 1;
    document.getElementById('nav-account-links').style.borderTop="#ccc 1px solid";
    document.getElementById('nav-account-links').style.borderRight="#ccc 1px solid";
    document.getElementById('nav-account-links').style.borderBottom="#ccc 1px solid";
    document.getElementById('nav-account-links').style.borderLeft="#ccc 1px solid";

  } else {
    document.getElementById('nav-popup-menu').style.display = 'none';
    //document.getElementById('nav-account-links').style.height = '20px';
    navPopState = 0;
    document.getElementById('nav-account-links').style.borderTop="";
    document.getElementById('nav-account-links').style.borderRight="";
    document.getElementById('nav-account-links').style.borderBottom="";
    document.getElementById('nav-account-links').style.borderLeft="";
  }
}
var beenHerePopState = 0;
function beenHerePopup(){
  if (beenHerePopState == 0){
    document.getElementById('been-here-popup').style.display = '';
    beenHerePopState = 1;
  } else {
    document.getElementById('been-here-popup').style.display = 'none';
    beenHerePopState = 0;
  }
}
var explorePopState = 0;
function explorePopup(){
  if (explorePopState == 0){
    document.getElementById('explore-popup-menu').style.display = '';
    explorePopState = 1;
  } else {
    document.getElementById('explore-popup-menu').style.display = 'none';
    explorePopState = 0;
  }
}
function inputFocus(obj,str){
  if (obj.value == str){
     obj.value = '';
  }
  obj.style.color = '#111';
}
function toggleDisplay(id1,id2) {
	// if id1 is hidden, then show it and hide id2. else do the opposite.
	if (document.getElementById(id1).style.display == 'none') {
		document.getElementById(id1).style.display = 'block'
		document.getElementById(id2).style.display = 'none'
	} else {
		document.getElementById(id1).style.display = 'none'
		document.getElementById(id2).style.display = 'block'
	}
	return false;
}
// For more explicit on and off switching
function turnDisplay(id, onOrOff)  {
    if (onOrOff == 'off') {
		 document.getElementById(id).style.display = 'none'
    } else if (onOrOff == 'on') {
		 document.getElementById(id).style.display = 'block'
    }
    return false;
}

// Yellow Fade Technique (tm 37 Signals)
var Color= new Array();
Color[2] = "ee";
Color[3] = "dd";
Color[4] = "cc";
Color[5] = "bb";
Color[6] = "aa";
Color[7] = "99";

function waittofade() {
	if (document.getElementById('fade')) {
    setTimeout("fadeIn(10)", 1000);
	 }
}

function fadeIn(where) {
    if (where >= 1) {
        document.getElementById('fade').style.backgroundColor = "#ffff" + Color[where];
		  if (where > 1) {
			  where -= 1;
			  setTimeout("fadeIn("+where+")", 200);
			} else {
			  where -= 1;
			  setTimeout("fadeIn("+where+")", 200);
			  document.getElementById('fade').style.backgroundColor = "transparent";
			}

    }
}
function ajax_status(node,status_string,replace){
  if (!node) return false;
  if (typeof node == 'string')
    node = document.getElementById(node);
  if (node) {
    if (replace)
      node.innerHTML = status_string
    else
      node.innerHTML = status_string + node.innerHTML 
  }
}
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
        } else {
            begin += 2;
            var end = document.cookie.indexOf(";", begin);
            if (end == -1){
                end = dc.length;
            }
        }
    return unescape(dc.substring(begin + prefix.length, end));
}
function setCookie(name, value, expires, domain) {
      var curCookie = name + "=" + escape(value) +
              ((expires) ? "; expires=" + expires.toGMTString() : "") +
              "; path=/" +
              ((domain) ? "; domain=" + domain : "");
      document.cookie = curCookie;
}


/**
* Functions dealing with login and signup.
*/
function redirect_to(location) {
	if (/Safari/.test(navigator.userAgent)) {
		var special_redirect_form = document.createElement("form");
		special_redirect_form.action = location;
		special_redirect_form.method = "POST";
		special_redirect_form.name = "hidden_redirect_form";
		document.body.appendChild(special_redirect_form);
		special_redirect_form.submit();
	} else {
		window.location.href = location;
	}
}

var activeModalDialog = null;
function CloseModal() {
  if (activeModalDialog) {
    activeModalDialog.close();
  }
}
 
var repositionModal = true;
var DialogAppearDuration = 0.5;
var Dialog = Class.create();
Dialog.prototype = {
	initialize: function(title, content, options) {
		this.options = Object.extend({
			onclose:     function() {},
			parent:      null,
                    isModal:     false,
                    original_parent: null
	    }, options || {});
	  content = $(content);
		this.content = content;
        this.options.original_parent = content.parentNode;
        
		this.element = document.createElement('div');
		if (!options.parent) {
    		options.parent = document.body;
		}

		// Set initial popup element
		this.element.id = "modalDialog";
		this.element.style.cssText = "position: absolute";
		//this.element.style.visibility = "hidden";
	        this.element.style.display = "none";
		this.element.innerHTML = '<div class="dialog-body"></div>';

		// Body
        var body = Element.down(this.element);
		
		// Add dialog element to document
		options.parent.appendChild(this.element);
		
		// Add content to dialog
		body.appendChild(this.content);
		
		// Position
		this.setPosition();

		// Make visible
		this.element.style.zIndex = 2000;
		//this.element.style.visibility = "visible";
        new Effect.Appear('modalDialog', {duration: DialogAppearDuration});
	},
	setPosition: function() {
		if (!this.element) return;
		var viewportDims = getViewportDimensions();

	        var scrollingOffsets = getScrollXY();

		var left = (scrollingOffsets[0] + viewportDims.width / 2 - this.element.getWidth() / 2);
		this.element.style.left = left + "px";

		var top = (scrollingOffsets[1] + viewportDims.height / 2 - this.element.getHeight() / 2);
		this.element.style.top = top + "px";

		if (repositionModal && this.element) {
			window.setTimeout(this.setPosition.bindAsEventListener(this), 500);
		}
	},
	close: function() {
		if (this.options.isModal) activeModalDialog = null;
		this.element.parentNode.removeChild(this.element);
		if (this.content.placeholder) {
			var holder = this.content.placeholder;
			holder.parentNode.insertBefore(this.content, holder);
			holder.parentNode.removeChild(holder);
			this.content.placeholder = null;
		}

		if (this.options.original_parent) {
            this.options.original_parent.appendChild(this.content);
		}
        
		this.element = null;
		this.options.onclose();
	}
}

var ModalDialog = Class.create();
ModalDialog.prototype = {
	initialize: function (title, content, options) {
		this.options = options || {};
	  activeModalDialog = this;
		this.options.isModal = true;

		// The shade
		this.shade = new Shade();
		this.shade.show();
		this.options.onclose = this.shade.hide.bindAsEventListener();

		this.dialog = new Dialog(title, content, this.options);
	},
	close: function() {
		this.dialog.close();
	}
}

var smokeScreen = false;
var Shade = Class.create();
Shade.prototype = {
	element: null,
	initialize: function() {},
	show: function() {
		this.element = document.createElement('div');
		this.element.id = "modalDialogShade";
        if (smokeScreen) {
            this.element.style.cssText = "z-index:10;position: absolute; top: 0; left: 0; background-color:#fff; opacity: 0.7; filter: alpha(opacity=70);";
        } else {
            this.element.style.cssText = "z-index:10;position: absolute; top: 0; left: 0; background-color:#fff; opacity: 0.01; filter: alpha(opacity=1);";
        }

		this.setSize();
		document.body.appendChild(this.element);
	},
	hide: function() {
		document.body.removeChild($('modalDialogShade'));
		this.element = null;
	},
	setSize: function() {
		if (!this.element) return;
		this.element.style.width = document.body.scrollWidth + 'px';
		this.element.style.height = document.body.scrollHeight + 'px';
		if (this.element) {
			window.setTimeout(this.setSize.bindAsEventListener(this), 500);
		}
	}
}

function getViewportDimensions() {
	var x,y;
	if (self.innerHeight) // all except Explorer
	{
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	return {width: x, height: y};
}

/**
* Obtain scrolling offsets.
*/
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;

  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;

  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}
/** Find object's Y position **/
function findPosY(obj) {
  var curtop = 0;
  if(obj.offsetParent)
      while(1) {
         curtop += obj.offsetTop;
         if(!obj.offsetParent)
           break;
         obj = obj.offsetParent;
       }
   else if(obj.y)
      curtop += obj.y;
   return curtop;
}
function initScroll() {
  scrolldelay = setInterval("scrollWindow()", 100);
}
function scrollWindow() {
  window.scrollBy(0, 100);

  wh = f_clientHeight();
  oh = findPosY($('reply'));
  so = f_scrollTop();

  if ((wh+so) > oh) {
   clearTimeout(scrolldelay);
  }
}
function f_clientWidth() {
  return f_filterResults (
           window.innerWidth ? window.innerWidth : 0,
           document.documentElement ? document.documentElement.clientWidth : 0,
           document.body ? document.body.clientWidth : 0
      );
}
function f_clientHeight() {
  return f_filterResults (
           window.innerHeight ? window.innerHeight : 0,
           document.documentElement ? document.documentElement.clientHeight : 0,
           document.body ? document.body.clientHeight : 0
      );
}
function f_scrollTop() {
  return f_filterResults (
           window.pageYOffset ? window.pageYOffset : 0,
           document.documentElement ? document.documentElement.scrollTop : 0,
           document.body ? document.body.scrollTop : 0
      );
}
function f_filterResults(n_win, n_docel, n_body) {
  var n_result = n_win ? n_win : 0;
  if (n_docel && (!n_result || (n_result > n_docel)))
     n_result = n_docel;
  return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function entryTypeUpdate(entryTypeElement) {
    if (entryTypeElement.value == 6){ // question
        $('entry-title-box').hide();
        $('related-image-fields').hide();
        $('textile-instructions').hide();
        //        $('locals-only-box').show;      
    } else { // not a question
        $('entry-title-box').show();
        $('related-image-fields').show();
        $('textile-instructions').show();
        //        $('locals-only-box').hide;   
    }
    if (entryTypeElement.value == 1 || entryTypeElement.value == 2) {
        $('entry-title-box').show();
    } else {
        $('entry-title-box').hide();
    }
}
