// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
//needs prototype.js to work
Object.extend(Array.prototype, { 
  uniq: function() 
 {
	  
    return this.inject([], function(array, value) {
      return array.include(value) ? array : array.concat([value]);
    });
  }
});

Dynamo = {}

Dynamo.add_project_tags = function(o) {
  var txt = $("project_tags");
  var tags = txt.value.split(",").map(function(){ return arguments[0].strip(); }).reject(function(x){ if (x == '') return true; })
  tags.push(o.value);
  txt.value = tags.uniq().compact().join(", ");
}

//Dynamo.add_caption = function(o) {
  //var txt = $("project_tags");
  //var tags = txt.value.split(",").map(function(){ return arguments[0].strip(); }).reject(function(x){ if (x == '') return true; })
  //tags.push(o.value);
  //txt.value = tags.uniq().compact().join(", ");
//}



Dynamo.update_project_image = function(o) {
  // swap images
  var project_image_src = $("project_image").src;
  var project_image_alt = $("project_image").alt;
  $("project_image").src = o.getElementsByTagName('img')[0].src.replace(/_th\./, '_dsp.');
  $("project_image").alt = o.getElementsByTagName('img')[0].alt.replace(/_th\./, '_dsp.');
  o.getElementsByTagName('img')[0].src = project_image_src.replace(/_dsp\./, '_th.');
  o.getElementsByTagName('img')[0].alt = project_image_alt.replace(/_dsp\./, '_th.');
  document.getElementById('cap').innerHTML = $("project_image").alt;
  
  
  //caption = $("project_image").alt;
  //alert(caption);
  //document.getElementById('cap').firstChild.nodeValue = caption;

  // swap delete links
  // most ugly!
  var big_parent = $("project_image").parentNode;
  var small_parent = o.parentNode;
  var big_delete = big_parent.getElementsByTagName('A'); big_delete = big_delete[big_delete.length-1]
  var small_delete = small_parent.getElementsByTagName('A'); small_delete = small_delete[small_delete.length-1]
  var project_image_delete = big_delete.href;
  big_delete.href = small_delete.href;
  small_delete.href = project_image_delete;
}

$('ul.thumbnails').click(function(e){
			e.preventDefault();
			var link = e.target;
			link.blur();
			if( link.title )
				$(this).parent().find('span.message').text(link.title);
		});

Dynamo.rating_click = function(o) {
  if (!o.checked) return;

  if (o.id.replace(/\D/g, '') == '4') {
    [1, 2, 3].each(function(criterion_id){
      $('rating[' + criterion_id + ']').checked = false;
    })
  } else {
    $('rating[4]').checked = false;
  }
}

Dynamo.filter_submit = function(o) {
	//search_url = "/projects"
	//if (o.tagged.value != ''){
	//	search_url += "/tagged/"+o.tagged.value ;
	//}
	//if (o.order.value != ''){
	//	search_url += "?order="+o.order.value ;
	//}
	//window.location.href = search_url
}

Dynamo.focus_login = function() {
  Form.getInputs('login_form', 'text', 'email')[0].focus();
}


