// Global functions

var contentDivs = ['content', 'sidebar', 'navigation'];

function getMaxDivHeight() {
	return contentDivs.map(function(n) { if (e = $(n)) return e.clientHeight; }).max();
}

function setDivHeights() {
	var maxHeight = getMaxDivHeight();
	contentDivs.each(function(n) {
		if ((e = $(n)) && maxHeight > e.clientHeight) {
			e.style.height = maxHeight + 'px';
		}
	});
}

function stretchZoneSidebar() {
	var sidebar = $('zone-sidebar');
	if (sidebar) {
		sidebar.style.height = $('sidebar').clientHeight + 'px';
	}
}

function applyFormClasses() {
	$$('input').each(function(el) {
		el.addClassName(el.type);
	});
}

Event.observe(window, "load", setDivHeights);
Event.observe(window, "load", stretchZoneSidebar);
Event.observe(window, "load", applyFormClasses);


// Business Opportunities Search
function toggleAllOpportunities() {
	var checked = $('all_business_opportunities').checked;
	$$('.type-option input').each(function (el) { el.checked = checked; });
}

function updateAllOpportunities() {
	var allChecked = true;
	$$('.type-option input').each(function (el) {
		if (el.id != 'all_business_opportunities' && !el.checked) { allChecked = false; }
	});
	$('all_business_opportunities').checked = allChecked;
}


// Business Opportunities Post
function uploadImage(url) {
	var fileField = $('business_image_image');

	if (fileField.value != '') {
		var form = $('business-opportunity-form');
		var oldAction = form.action;

		fileField.hide();
		$('upload_status').show();

		form.target = 'upload_iframe';
		form.action = url;
		form.submit();
		form.target = '';
		form.action = oldAction;
	}
}


// Business Opportunities Show
function createThumbnailLinks() {
	$$('#thumbnails img').each(function(img) {
		img.onclick = function() {
			var newThumb = $$('#main-photo img').first();
			var src = newThumb.src.replace('/large/', '/small/');
			newThumb.src = img.src.replace('/small/', '/large/');
			img.src = src;
		};
	});
}

Event.observe(window, "load", createThumbnailLinks);
Event.observe(window, "load", function() {
	$$('.tabs').each(function(tabs) {
		new Control.Tabs(tabs);
	});
});



// Forum post limit control
function limitCharCount(n_max) {
  textarea = document.getElementById("forum_post_message");
  if(textarea.value.length > n_max)
    textarea.value = textarea.value.substr(0, n_max);
  
  chars_left = document.getElementById("characters-left");
  chars_left_val = n_max - textarea.value.length;
  
  chars_left.innerHTML = chars_left_val + (chars_left_val != 1 ? " characters left" : " character left");
}

Ajax.Responders.register({
	onCreate: function() {
		if($('busy') && Ajax.activeRequestCount>0)
		Effect.Appear('busy',{duration:0.5,queue:'end'});
	},
	onComplete: function() {
		if($('busy') && Ajax.activeRequestCount==0)
		Effect.Fade('busy',{duration:0.5,queue:'end'});
	}
});

function showSpouse() {
  document.getElementById('spouse-form').style.display = 'block';
}

function hideSpouse() {
  document.getElementById('spouse-form').style.display = 'none';
}


// Surveys
function surveyRadioSelection(question, choice) {
  choices = $$('question-' + question + ' .choice');
  
  for (var i = 1; i <= choices.length; i++) {
    comment_field = $('comment-for-' + question + '-' + i);
    
    if (comment_field) {
      if (choice == i) { comment_field.show(); }
      else { comment_field.hide(); }
    }
  }
}

function toggleQuestionChoices(id, value) {
  if (value == 'checkbox') {
    $('choices_' + id).show();
    $('new_choice_' + id).show();
  } else {
    $('choices_' + id).hide();
    $('new_choice_' + id).hide();
  }
}

function announcements_check_all_migrants() {
  all = $('recipients[migrants]');
  chinese = $('recipients[migrants_chinese]');
  korean = $('recipients[migrants_korean]');
  
  if (all.checked) {
    chinese.checked = false;
    chinese.disabled = true;
    korean.checked = false;
    korean.disabled = true;
  } else {
    chinese.disabled = false;
    korean.disabled = false;
  }
}
