// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function attachPlaceholder(id) {
	/* we use the placeholder extension on safari */
	var detect = navigator.userAgent.toLowerCase();
	if (detect.indexOf("safari") > 0) return;
	/* attach listeners */
	a=document.getElementById(id)
 	a.onfocus=function(){ this.className=''; }
 	a.onblur=function(){ if (this.value == '') this.className='empty'; }
 	a.onchange=function(){ if (this.value != '') this.className=''; }
	a.onblur();
	/* catch any auto-form filling */
	window.onload = (function(prev,ele) { return function() {
		if (ele.value != '') ele.className = '';
		if (prev) prev();
	}})(window.onload,a);
}

function roundCorners(el) {
	var x, background = el.style.backgroundColor; 
	var index = new Array(0,1,1,1,2,2,3,4,5,7,10);
	for(x=0;x<11;x++) {
		var d = "<div style='background: " + background + 
			"; border-style: solid; border-color: white; height: 1px; overflow: hidden; border-width: 0 " +
			index[x] + "px'>&nbsp;</div>";
		new Insertion.Top(el, d);
		new Insertion.Bottom(el, d);
	}
}

function disableSelection(element) {
    element.onselectstart = function() {
        return false;
    };
    element.unselectable = "on";
    element.style.MozUserSelect = "none";
    element.style.cursor = "default";
}	

function disableSelection(element) {
	    element.onselectstart = function() {
	        return false;
	    };
	    element.unselectable = "on";
	    element.style.MozUserSelect = "none";
	    element.style.cursor = "default";
}
function toggleAll(cb) {
  var val = cb.checked;
	var frm = document.f1;
	var len = frm.elements.length;
	var i=0;
	for( i=0 ; i<len ; i++) {
		if (frm.elements[i].name=='contact_ids[]') {
			frm.elements[i].checked=val;
		}
	}
}


var WritingHelper = {
  swapStatus: function(hiddenField) {
    button = $(hiddenField + "_button");
    src = button.src;
    var on = false;
    if (src.indexOf('on-active') != -1) {
      on = false
      button.src = "/images/off-inactive-button.gif"
    }
    else {
      on = true
      button.src = "/images/on-active-button.gif"
    }
    $(hiddenField).value =  on ?  "true" : "false";
    return false;
  },
  
  // Called from an observer. Might be better on keypress. Not sure.
  setPageTitle: function(elmToSet, field) {
    $(elmToSet).innerHTML = $(field).value;
  }, 
  
  setCategory: function(value) {
    if (value != '') {
      $('category').innerHTML = value + " / ";
    }
  }
}

var ConstraintListButtons = {
  disableButtons: function() {

    ConstraintListButtons.removeEvents();
    
    // Show disabled state with a class.
    $$('a.update_button').each(function(elm) {
      elm.className = "update_disabled";
    })

    
    var form = $('update_form');
    
    // Disabling the buttons stops the form from being submitted.
    // So, just run the ajax request manually, yo.
    new Ajax.Request(form.action, {
        asynchronous:'true', 
        evalScripts:'true',
        method:'post',
        parameters: Form.serialize(form),
        onLoading: function() {
          Element.show('search_spinner'); 
          Element.hide('matching_urbis_users');
          $('submit_button').disabled = true;
        }, 
        onComplete: function() {
          Element.show('matching_urbis_users');
          hideSpinnerAndResetButtonState('search_spinner');
        }
    });
    setTimeout('ConstraintListButtons.enableButtons();', 3000); return false;
  },
  
  enableButtons: function() {
    $$('a.update_disabled').each(function(elm) {
      elm.className = "update_button";
      elm.disabled = true;
    })
    
    ConstraintListButtons.hookUpEvents();
    return false;
  },
  
  hookUpEvents: function() {
    Event.observe('age_update', 'click', ConstraintListButtons.disableButtons);
    Event.observe('gender_update', 'click', ConstraintListButtons.disableButtons);
    Event.observe('relationship_update', 'click', ConstraintListButtons.disableButtons);
    Event.observe('reviewer_update', 'click', ConstraintListButtons.disableButtons);
  },
  
  removeEvents: function() {
    Event.stopObserving('age_update', 'click', ConstraintListButtons.disableButtons);
    Event.stopObserving('gender_update', 'click', ConstraintListButtons.disableButtons);
    Event.stopObserving('relationship_update', 'click', ConstraintListButtons.disableButtons);
    Event.stopObserving('reviewer_update', 'click', ConstraintListButtons.disableButtons);
  }
  
}


Event.observe(window, 'load', function() {
  ConstraintListButtons.hookUpEvents();
});


function revealComment(reviewId) {
  new Ajax.Request('/review/remote_reveal/' + reviewId, {asynchronous:true, evalScripts:true, 
    onLoading: function(){
      $('spinner').show(); 
      $('reveal_bubble_' + reviewId).hide();
    },
   onComplete: function(){
     $('review-' + reviewId + '-comments').show();
    }}); 

   return false;
}

function hideSpinnerAndResetButtonState(spinner) {
  Element.hide(spinner); 
  if (!$('match_amount')) {
    $('submit_button').disabled = false;
  }
}