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

//navHover = function() {
///	var lis = $('navigation_middle_content_2').getElementsByTagName("LI");
//	for (var i=0; i<lis.length; i++) {
//		lis[i].onmouseover=function() {
//			this.className+=" iehover";
//		}
//		lis[i].onmouseout=function() {
//			this.className=this.className.replace(new RegExp(" iehover\\b"), "");
//		}
//	}
//}
//if (window.attachEvent) window.attachEvent("onload", navHover);

function remove_error_messages(){
	if($('errorExplanation')){	
	$('errorExplanation').remove();
	$$('.fieldWithErrors').each(function(element){element.insert({after: element.innerHTML});element.remove();})
	}
}

function show_text_on_mouse_position(event,target_element,text){
	document.getElementById(target_element).style.left=Event.pointerX(event)+10+'px';
	document.getElementById(target_element).style.top=Event.pointerY(event)+'px';
	document.getElementById(target_element).innerHTML=text;
	$(target_element).show();
}

function change_dom_attribue_value(parent_dom,child_class,attribute,value){
	var dom_elements = $(parent_dom).getElementsByClassName(child_class)
	for(i=0;i < dom_elements.length;i++)
	{
		dom_elements[i].style[attribute] = value
	}


}

function update_header(id){
	switch(id){
		case 0:	
			$('header_level2_logo').style.display = 'block';
			$('header_level2').style.textAlign = 'left';
			$('header_level2_logo').style.styleFloat   = 'left';
			$('header_level2_logo').style.cssFloat   = 'left';
			break;	
		case 1:
			$('header_level2_logo').style.display = 'block';
			$('header_level2_logo').style.styleFloat   = 'right';
			$('header_level2_logo').style.cssFloat   = 'right';
			$('header_level2').style.textAlign = 'center';
			break;	
		case 2:
			$('header_level2_logo').style.display = 'none';
			$('header_level2').style.textAlign = 'left';
			$('header_level2_logo').style.styleFloat   = 'none';
			$('header_level2_logo').style.cssFloat   = 'none';
			break;	
		case 3:
			$('head_level2_logo').style.display = 'none';
			$('head_level2').style.textAlign = 'center';
			$('head_level2_logo').style.styleFloat   = 'none';
			$('head_level2_logo').style.cssFloat   = 'none';
			break;	
		case 4:
			$('head_level2_logo').style.display = 'none';
			$('head_level2').style.textAlign = 'right';
			$('head_level2_logo').style.styleFloat   = 'none';
			$('head_level2_logo').style.cssFloat   = 'none';
			break;	
		case 5:
			$('head_level2_logo').style.display = 'block';
			$('head_level2').style.textAlign = 'center';
			$('head_level2_logo').style.styleFloat   = 'none';
			$('head_level2_logo').style.cssFloat   = 'none';
			break;	
		
	}
}


var Subform = Class.create({
  lineIndex: 1,
  parentElement: "",
  class_name_1: "",
  class_name_2: "",
  initialize: function(rawHTML, parentElement,lineIndex,class_name1, class_name2) {
    this.rawHTML        = rawHTML;
    this.parentElement  = parentElement;
	this.class_name_1	= class_name1;
	this.class_name_2	= class_name2;
	this.lineIndex = lineIndex - 1;
  },
  parsedHTML: function() {
  	this.lineIndex++;
    return this.rawHTML.replace(/INDEX/g, this.lineIndex).replace(/CLASS_NAME/g,(((this.lineIndex % 2) ==  0) ? this.class_name_1 : this.class_name_2));
  },
  add: function() {
    new Insertion.Bottom($(this.parentElement), this.parsedHTML());
  }
});

function change_formation(match_id,championship_team_id,formation_type){
	if(formation_type == "")
	{
		$('formation_'+match_id+'_'+championship_team_id+'_formation_entries').hide();
		$('formation_'+match_id+'_'+championship_team_id+'_bench_entries').hide();
	}
	else
	{
		$('formation_'+match_id+'_'+championship_team_id+'_formation_entries').show();
		$('formation_'+match_id+'_'+championship_team_id+'_bench_entries').show();
		for (i = 11; i < 19; i++) {
			$('formation_'+match_id+'_'+championship_team_id+'_position_'+i).show();
		}
		var y_max = 0;
		for(i=0;i < maximum_number_of_players_in_startformation;i++)
		{
			if(i < formations[formation_type].length)
			{
				$('formation_'+match_id+'_'+championship_team_id+'_position_'+i).show();
				$('formation_'+match_id+'_'+championship_team_id+'_position_'+i).style.left = (formations[formation_type][i][0]*70)+'px';
				$('formation_'+match_id+'_'+championship_team_id+'_position_'+i).style.top = (formations[formation_type][i][1]*30+20)+'px';
				if(formations[formation_type][i][1]*30 > y_max){
					y_max = formations[formation_type][i][1]*30;
				} 
			}
			else{
				remove_formation_player(match_id, championship_team_id, i);
				$('formation_'+match_id+'_'+championship_team_id+'_position_'+i).hide();
			}
			
		}
		$('formation_'+ match_id+'_' + championship_team_id+'_formation_entries').style.height = (y_max + 80)+'px';
	}
}

function add_formation_player(match_id, championship_team_id, player_id, player_name){
	var formation_prefix = get_formation_prefix(match_id, championship_team_id);
	// ließt die aktuelle position_id aus dem hidden-field aus
	var position_id = $('formation_' + match_id + '_' + championship_team_id + '_new_position_id').value
	// versteckt den 'kein Spieler'-Link
	$('formation_' + match_id + '_' + championship_team_id + '_position_' + position_id + '_no_player').hide();
	// zeigt  den Spieler mit allen details
	$('formation_' + match_id + '_' + championship_team_id + '_position_' + position_id + '_player').show();
	// versteckt die Spielerauswahlbox
	$('formation_' + match_id + '_' + championship_team_id + '_players').hide();
	// versteckt den ausgewählten Spieler
	$('formation_' + match_id + '_' + championship_team_id + '_player_' + player_id + '').hide();
	// setzt den neuen Spielernamen
	$('formation_' + match_id + '_' + championship_team_id + '_position_' + position_id + '_playername').innerHTML = player_name;
	// setzt die neue player_id
	if($('matchday_matches_' + match_id + '_'+formation_prefix+'_formation_attributes_' + championship_team_id + '_new_formation_entry_attributes_'+ position_id +'_player_id')){
		$('matchday_matches_' + match_id + '_'+formation_prefix+'_formation_attributes_' + championship_team_id + '_new_formation_entry_attributes_'+ position_id +'_player_id').value = player_id;
	}
	else{
		$('matchday_matches_' + match_id + '_'+formation_prefix+'_formation_attributes_' + championship_team_id + '_existing_formation_entry_attributes_'+ position_id +'_player_id').value = player_id;
	}
	
}

function remove_formation_player(match_id, championship_team_id, position_id){
	var formation_prefix = get_formation_prefix(match_id, championship_team_id);
	var formation_entry_prefix = get_formation_entry_prefix(match_id, championship_team_id, position_id, formation_prefix);
	var player_id = $('matchday_matches_' + match_id + '_'+formation_prefix+'_formation_attributes_' + championship_team_id + '_'+formation_entry_prefix+'_formation_entry_attributes_'+ position_id +'_player_id').value
	// löscht die player_id
	$('matchday_matches_' + match_id + '_'+formation_prefix+'_formation_attributes_' + championship_team_id + '_'+formation_entry_prefix+'_formation_entry_attributes_'+ position_id +'_player_id').value = '';
	// löscht die Spielzeit
	if($('matchday_matches_' + match_id + '_'+formation_prefix+'_formation_attributes_' + championship_team_id + '_'+formation_entry_prefix+'_formation_entry_attributes_'+ position_id +'_minute_in')){
		$('matchday_matches_' + match_id + '_'+formation_prefix+'_formation_attributes_' + championship_team_id + '_'+formation_entry_prefix+'_formation_entry_attributes_'+ position_id +'_minute_in').value = '';
	}
	$('matchday_matches_' + match_id + '_'+formation_prefix+'_formation_attributes_' + championship_team_id + '_'+formation_entry_prefix+'_formation_entry_attributes_'+ position_id +'_minute_out').value = '';
	$('matchday_matches_' + match_id + '_'+formation_prefix+'_formation_attributes_' + championship_team_id + '_'+formation_entry_prefix+'_formation_entry_attributes_'+ position_id +'_yellow_card').checked = '';
	$('matchday_matches_' + match_id + '_'+formation_prefix+'_formation_attributes_' + championship_team_id + '_'+formation_entry_prefix+'_formation_entry_attributes_'+ position_id +'_red_card').checked = '';
	$('matchday_matches_' + match_id + '_'+formation_prefix+'_formation_attributes_' + championship_team_id + '_'+formation_entry_prefix+'_formation_entry_attributes_'+ position_id +'_yellow_red_card').checked = '';
	// zeigt den 'kein-Spieler'-Link
	$('formation_' + match_id + '_' + championship_team_id + '_position_' + position_id + '_no_player').show();
	// löscht den Spielernamen
	$('formation_' + match_id + '_' + championship_team_id + '_position_' + position_id + '_playername').innerHTML = '';
	// versteckt den Spieler wieder
	$('formation_' + match_id + '_' + championship_team_id + '_position_' + position_id + '_player').hide();
	// zeigt den gelöschten Spieler wieder in der Spielerauswahlbox an
	if($('formation_' + match_id + '_' + championship_team_id + '_player_' + player_id))
	{
		$('formation_' + match_id + '_' + championship_team_id + '_player_' + player_id).show();	
	}
	// versteckt details
	hide_formation_details(match_id, championship_team_id, position_id);
}

function get_formation_prefix(match_id, championship_team_id){
	if($('matchday_matches_'+match_id+'_new_formation_attributes_'+championship_team_id+'_formation_type')){
		return 'new';
	}
	else{
		return 'existing';
	}
}

function get_formation_entry_prefix(match_id, championship_team_id, position_id){
	var formation_prefix = get_formation_prefix(match_id, championship_team_id);
	if($('matchday_matches_'+match_id+'_'+formation_prefix+'_formation_attributes_'+championship_team_id+'_new_formation_entry_attributes_'+position_id+'_player_id')){
		return 'new';
	}
	else{
		return 'existing';
	}
}

function show_players_box(match_id,championship_team_id,position_id){
	$('formation_'+match_id+'_'+championship_team_id+'_players').show();
	$('formation_'+match_id+'_'+championship_team_id+'_players').style.top = document.viewport.getScrollOffsets()[1]+50+'px';
	$('formation_'+match_id+'_'+championship_team_id+'_new_position_id').value = position_id;
}

function toggle_formation_details(match_id,championship_team_id,position_id){
	var z_index = $('formation_'+ match_id +'_'+ championship_team_id+'_position_'+ position_id).style.zIndex;
	if(z_index > 100){
		hide_formation_details(match_id, championship_team_id, position_id);
	}
	else{
		show_formation_details(match_id, championship_team_id, position_id);
	}
}
function hide_formation_details(match_id, championship_team_id, position_id){
	var z_index = parseInt($('formation_'+ match_id +'_'+ championship_team_id+'_position_'+ position_id).style.zIndex);
	$('formation_'+ match_id +'_'+ championship_team_id+'_position_'+ position_id+'_details').hide();
	if(z_index > 100){
		$('formation_'+ match_id +'_'+ championship_team_id+'_position_'+ position_id).style.zIndex = (z_index-100);
	}
	
}
function show_formation_details(match_id, championship_team_id, position_id){
	var z_index = parseInt($('formation_'+ match_id +'_'+ championship_team_id+'_position_'+ position_id).style.zIndex);
	$('formation_'+ match_id +'_'+ championship_team_id+'_position_'+ position_id+'_details').show();
	if (z_index < 100) {
		$('formation_'+ match_id +'_'+ championship_team_id+'_position_'+ position_id).style.zIndex = (z_index+100);
	}
}

// steuert die Checkboxes bei den Grundeinstellungen für Vereine (Mitglieder etc.)
function toggle_input(control_checkbox_id, input_to_toggle_id, order) {
	var input_to_toggle = window.document.getElementById(input_to_toggle_id)
	var control_checkbox = window.document.getElementById(control_checkbox_id)
	if (order == 'reversed') {
		if (control_checkbox.checked == true) {
			input_to_toggle.disabled = true;
			input_to_toggle.checked = false;
			input_to_toggle.value = "";
		}
		else {
			input_to_toggle.disabled = false;
		}
	}
	else {
		if (control_checkbox.checked == true) {
			input_to_toggle.disabled = false;
		}
		else {
			input_to_toggle.disabled = true;
			input_to_toggle.checked = false;
			input_to_toggle.value = "";
		}
	}
	if ($('sportclub_activation_code').checked == false || $('sportclub_activation_code').disabled == true) {
		$('sportclub_code_for_auto_membership').disabled = true;
	}
	else {
		$('sportclub_code_for_auto_membership').disabled = false;
	}
}
