var joblet = new Object();

function fadeIfExists(element_name) {
    doIfExists(element_name, function(element) {
	new Effect.Fade(element);
	setTimeout(function() {
	    doIfExists(element_name, function(e) { Element.remove(e) });
	}, 2);
    });
}

function updateIfExists(element_name, innerHtml) {
    doIfExists(element_name, function(element) {
	Element.replace(element, innerHtml);
    });
}

function highlightIfExists(element_name) {
    doIfExists(element_name, function(element) {
	new Effect.Highlight(element);
    });
}

function doIfExists(element_name, callback) {
    var element = $(element_name);
    if (element != null) {
	callback(element);
    }   
}

function zebraStripes() {
    $$("tbody").each(function(tbody) {
	var addZebra = true;
	for(var i = 0; i < tbody.childNodes.length; ++i) {
	    row = tbody.childNodes[i];
	    if (row.tagName && row.tagName.toLowerCase() == "tr") {
		if (addZebra) {
		    Element.extend(row);
		    row.addClassName("alt");
		}
		addZebra = !addZebra;
	    }
	}
    });
}

function doAddQualificationLinks(addText, removeText) {
    $$(".qualification_link").each(function(link, index) {
      link.remove();
    });

    var collection = $$("#qualifications li");
    var span;
    var canRemove = collection.length > 1;
    var last = collection.last();
    var sibling;

    collection.each(function(item, index) {
    span = document.createElement("span");
    Element.extend(span);
    span.addClassName("qualification_link");

    if (canRemove) { span.innerHTML += removeText; }

    sibling = item.descendants().find(function(e) { return e.tagName == "INPUT" });
    if (sibling != null && sibling.next() != null) {
        sibling.parentNode.insertBefore(span, sibling.next());
    } else {
        item.appendChild(span);
    }
    });
    // span should be last list-item's span
    if (span) { span.innerHTML += addText; }

    return false;
}

function resizeableTextareas() {
    if(typeof(TextAreaResizer) == "undefined") {
        return
    }    
    if(/[3-9]\.\d+\.\d+\s+Safari/.test(navigator.userAgent)) {
      return
    }
    
    var textareas = document.getElementsByTagName("textarea");
    var textarea;
    var len = textareas.length;

    for(var i = 0; i < len; ++i) {
      if(textareas[i].resizer != null){
        continue;
      }
      if($(textareas[i]).hasClassName('no_resizer')) {
        continue;
      }
      textareas[i].resizer = new TextAreaResizer(textareas[i]);
    }
}
/*
function addResizeWidget(element) {
    s = document.createElement("span");
    s.setAttribute("class", "resizer");
    s.appendChild(document.createTextNode("+"));

    new Draggable(s, {
	constraint: "vertical",
	ghost: false,
	change: function(d) {
	    var height = getHeight(element);
	    setHeight(element, height + d.currentDelta()[1]);
	},
	snap 
        starteffect: function(element) {
          Draggable._dragging[element] = true;
        },
	endeffect: function(element) {
	    Draggable._dragging[element] = false 
        },
    });
    element.parentNode.insertBefore(s, element.next())
}

// returns height of element in pixels as integer
function getHeight(element) {
    var height;
    if (!element.style.height) {
	element.style.height = document.defaultView.getComputedStyle(element, null).height;
    }
    height = element.style.height;
    
    // strip off 'px' and convert to number
    height = height.substr(0, height.length - 2) * 1;
    
    return height;
}

function setHeight(element, height) {
    element.style.height = height + "px";
}
*/


joblet.update_id = ""
joblet.loading_box = "<div id='joblet_loading_box'></div>"

joblet.will_update = function(update_id) {
  joblet.update_id = update_id
}

joblet.startLoading = function() {
  Element.update(joblet.update_id, joblet.loading_box) 
}

joblet.stopLoading = function() {
  resizeableTextareas();
  Element.remove("joblet_loading_box");
}

joblet.registerLoadings = function() {
  Ajax.Responders.register({
    onCreate: joblet.startLoading,
    onComplete: joblet.stopLoading
  });
}


Event.observe(window, 'load', function() {
    zebraStripes();
    resizeableTextareas();
    joblet.registerLoadings();
});



joblet.submit_to_new_window = function(form_id, submit_url, window_id, window_opts) {
  w = window.open("",window_id,window_opts); 
  w.focus();
  
  f = $(form_id);
  backup_url = f.action;
  f.action = submit_url;
  f.target = window_id;
  f.submit();
  
  f.action = backup_url;
  f.target = "_self"
}

joblet.checkAll = function() {
    $$("input[type=checkbox]").each(function(checkbox){
        if(!checkbox.checked) {
            checkbox.click();
        }
    });
}

joblet.checkNone = function() {
    $$("input[type=checkbox]").each(function(checkbox){
        if(checkbox.checked) {
            checkbox.click();
        }
    });
}




