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

function submit_creative_work(to_tab) {
  creative_work_form = $('#creative_work');
  if (to_tab)	addHiddenElement(creative_work_form, to_tab);
  creative_work_form.submit();
}

function addHiddenElement(aForm, aName, aValue) {
  hiddenElem = document.createElement("input");
  hiddenElem.setAttribute("type", "hidden");
  hiddenElem.setAttribute("name", aName);
  hiddenElem.setAttribute("value", aValue);
  aForm.append(hiddenElem);
}

/**
 * Returns the value of the selected radio button in the radio group.
 * 
 * @param {radio group name} radioGroup
 */
function getRadioGroupValue(radioGroup) {
  return $("input[name='" + radioGroup + "']:checked").val();
}

function getLicense(commercial, modification) {
	var license = "by";
	if (commercial == 'n') license += "-nc";
	if (modification == "sa") {
		license += "-sa";
	} else if (modification == "n") {
		license += "-nd";
	}
	return license;
}

function getLicenseImage(license) {
	return "/images/cc-" + license + "-80x15.png";
}

function getLicenseText(license) {
	switch(license) {
		case 'by':
		  return 'Attribution (by)';
		case 'by-sa':
		  return 'Attribution Share Alike (by-sa)';
		case 'by-nd':
		  return 'Attribution No Derivatives (by-nd)';
		case 'by-nc':
		  return 'Attribution Non-commercial (by-nc)';
		case 'by-nc-sa':
		  return 'Attribution Non-commercial Share Alike (by-nc-sa)';
		case 'by-nc-nd':
		  return 'Attribution Non-commercial No Derivatives (by-nc-nd)';
	}
}

function updateSelectedLicense() {
	var commercial = getRadioGroupValue('creative_work[commercial_license]');
	var modification = getRadioGroupValue('creative_work[modification_license]');
	var license = getLicense(commercial, modification);
	$('#selected_license > img').attr("src", getLicenseImage(license));
	$('#selected_license > label').text(getLicenseText(license));
}

/**
 * Tag Recommendations
 */
function toggleTag(aLink, aTagName, aTagsFieldId) {
  var tagsField = $("#" + aTagsFieldId)[0];
  var tags = extractTagsFromInput(tagsField);
  
  var stringIndex = stringInArray(aTagName, tags);
  if (stringIndex > -1) {
    tagsField.value = jQuery.grep(tags, function (a, i) {
      return i != stringIndex;
    }).join(", ");
    jQuery(aLink).removeClass('selected_tag');
  } else {
    tags.push(aTagName);
    tagsField.value = tags.join(", ");
    jQuery(aLink).addClass('selected_tag');
  }
}

function highlightSelectedTags(aTagsFieldId) {
  var tagsField = $("#" + aTagsFieldId)[0];
  var appliedTags = extractTagsFromInput(tagsField);
    
  var tagLinks = $('span.tags a');
  var tagName, tagLink;
  for (var i = 0, length = tagLinks.length; i < length; i++) {
    tagLink = jQuery(tagLinks[i]);
    tagName = tagLink.text();
    if (stringInArray(tagName, appliedTags) > -1) {
      tagLink.addClass('selected_tag');
    }
  }
}

// trims tags, removes empty strings
function extractTagsFromInput(aTagsField) {
  var extractedTags = aTagsField.value.split(',');
  
  extractedTags = jQuery.map(extractedTags, function(a) {
    b = jQuery.trim(a);
    if (b.length > 0)
      return b;
  });
  
  return extractedTags;
}

// string-type-checking and case-insensitive
function stringInArray(aString, anArray) {
  for (var i = 0, length = anArray.length; i < length; i++) {
    var myRegExp = new RegExp("^" + aString +"$", "i");
    if ( typeof anArray[i] == 'string' ) {
      if (anArray[i].match(myRegExp))
        return i;
    }
  }
  return -1;
}

function ajaxify_pagination() {
  $('div.pagination a').click(function(){
    $.getScript($(this).attr('href'));
    return false;
  });
}

// make link-containing divs clickable
function clickify_divs() {
  $('div.clickify').click(function() {
    link = $(this).children('a').click();
  });
}

function ajax_submit_with_loader(form, loader_container) {
  $(form).ajaxSubmit();
  $(form + ' input').hide();
  $(loader_container).append('<img src="/images/ajax-loader-big.gif" alt="loading..." />');
}

function pop_messages(aHeight) {
  $('#messages_container #messages:has(div)').dialog({
    width: 380,
    height: (aHeight ? aHeight : 170)
  });
}

$(document).ready(function() {
  oneline_messages_box = $('#messages:has(div)');
  $('#messages_container #messages:has(div)').dialog({
    width: 380,
    height: 170,
    open: function(event, ui) {
      setTimeout("oneline_messages_box.dialog('destroy')", 2500);
    }
  });
  $('.form_error_messages:has(div)').dialog({
    width: 380,
    height: 220
  })
});