var unsaved_changes = false;

downstream = [];
downstream[1] = ['3_1','7_1','15_1','31_2'];
downstream[2] = ['3_2','7_1','15_1','31_2'];
downstream[3] = ['7_1','15_1','31_2'];
downstream[4] = ['6_1','7_2','15_1','31_2'];
downstream[5] = ['6_2','7_2','15_1','31_2'];
downstream[6] = ['7_2','15_1','31_2'];
downstream[7] = ['15_1','31_2'];
downstream[8] = ['10_1','14_1','15_2','31_2'];
downstream[9] = ['10_2','14_1','15_2','31_2'];
downstream[10] = ['14_1','15_2','31_2'];
downstream[11] = ['13_1','14_2','15_2','31_2'];
downstream[12] = ['13_2','14_2','15_2','31_2'];
downstream[13] = ['14_2','15_2','31_2'];
downstream[14] = ['15_2','31_2'];
downstream[15] = ['31_2'];
downstream[16] = ['18_1','22_1','30_1','62_1'];
downstream[17] = ['18_2','22_1','30_1','62_1'];
downstream[18] = ['22_1','30_1','62_1'];
downstream[19] = ['21_1','22_2','30_1','62_1'];
downstream[20] = ['21_2','22_2','30_1','62_1'];
downstream[21] = ['22_2','30_1','62_1'];
downstream[22] = ['30_1','62_1'];
downstream[23] = ['25_1','29_1','30_2','62_1'];
downstream[24] = ['25_2','29_1','30_2','62_1'];
downstream[25] = ['29_1','30_2','62_1'];
downstream[26] = ['28_1','29_2','30_2','62_1'];
downstream[27] = ['28_2','29_2','30_2','62_1'];
downstream[28] = ['29_2','30_2','62_1'];
downstream[29] = ['30_2','62_1'];
downstream[30] = ['62_1'];
downstream[31] = ['63_1'];
downstream[32] = ['34_1','38_1','46_1','62_2'];
downstream[33] = ['34_2','38_1','46_1','62_2'];
downstream[34] = ['38_1','46_1','62_2'];
downstream[35] = ['37_1','38_2','46_1','62_2'];
downstream[36] = ['37_2','38_2','46_1','62_2'];
downstream[37] = ['38_2','46_1','62_2'];
downstream[38] = ['46_1','62_2'];
downstream[39] = ['41_1','45_1','46_2','62_2'];
downstream[40] = ['41_2','45_1','46_2','62_2'];
downstream[41] = ['45_1','46_2','62_2'];
downstream[42] = ['44_1','45_2','46_2','62_2'];
downstream[43] = ['44_2','45_2','46_2','62_2'];
downstream[44] = ['45_2','46_2','62_2'];
downstream[45] = ['46_2','62_2'];
downstream[46] = ['62_2'];
downstream[47] = ['49_1','53_1','61_1','31_1'];
downstream[48] = ['49_2','53_1','61_1','31_1'];
downstream[49] = ['53_1','61_1','31_1'];
downstream[50] = ['52_1','53_2','61_1','31_1'];
downstream[51] = ['52_2','53_2','61_1','31_1'];
downstream[52] = ['53_2','61_1','31_1'];
downstream[53] = ['61_1','31_1'];
downstream[54] = ['56_1','60_1','61_2','31_1'];
downstream[55] = ['56_2','60_1','61_2','31_1'];
downstream[56] = ['60_1','61_2','31_1'];
downstream[57] = ['59_1','60_2','61_2','31_1'];
downstream[58] = ['59_2','60_2','61_2','31_1'];
downstream[59] = ['60_2','61_2','31_1'];
downstream[60] = ['61_2','31_1'];
downstream[61] = ['31_1'];
downstream[62] = ['63_2'];
downstream[63] = ['63_3'];
/*
function make_pick(question_id, pick_answer_id, unpick_answer_id,page) {
    // For the full picks page where we osubmit via a form
    var form_field = $('question'+question_id+'_answer_id');
    if (form_field != null) {
        form_field.value = pick_answer_id;
    }
    
    // For the preview pages where we do Ajax
    $('option_'+question_id+'_'+pick_answer_id).removeClassName('saving');
    $('option_'+question_id+'_'+unpick_answer_id).removeClassName('saving');
    
    $('option_'+question_id+'_'+pick_answer_id).addClassName('selected');
    $('option_'+question_id+'_'+unpick_answer_id).addClassName('unselected');
    $('option_'+question_id+'_'+pick_answer_id).removeClassName('unselected');
    $('option_'+question_id+'_'+unpick_answer_id).removeClassName('selected');

    $('pt_'+question_id+'_'+pick_answer_id).addClassName('selected');
    $('pt_'+question_id+'_'+unpick_answer_id).addClassName('unselected');
    $('pt_'+question_id+'_'+pick_answer_id).removeClassName('unselected');
    $('pt_'+question_id+'_'+unpick_answer_id).removeClassName('selected');
    
	unsaved_changes = true;

}
*/
function set_picked_team(elem_id) {
	var the_ids = elem_id.split('_');
	/* reset save button */
	try { $('match_' + the_ids[1]).ancestors()[0].getElementsByTagName('button')[0].removeClassName('saved'); }catch(e){ };
	/* get the other team in match and remove any downteam selections */
	try {
		(the_ids[2] == 1) ? other_team = 2 : other_team = 1;
	
		/* if other matchteam div is not empty */
		if ($('matchteam_'+the_ids[1]+'_'+other_team).getElementsByTagName('div') != null && $('matchteam_'+the_ids[1]+'_'+other_team).getElementsByTagName('div')[0] != null) {
			//$('matchteam_'+the_ids[1]+'_'+other_team).getElementsByTagName('div')[0].removeClassName('selected');
		
			var other_id_parts = $('matchteam_'+the_ids[1]+'_'+other_team).getElementsByTagName('div')[0].id.split('_');
			var other_id = other_id_parts[3];
		
			/* remove any downstream selections for the other team team */
			downstream[the_ids[1]].each(function(match_pos){
				var mp = match_pos.split('_');
				if ($('mt_'+match_pos) != null && $('mt_'+match_pos).value != null && $('mt_'+match_pos).value != "0" && $('mt_'+match_pos).value == other_id) {
					$('mt_'+match_pos).value = "0";
					$('matchteam_'+match_pos).innerHTML = '';
				}
				if ($('ptid_' + mp[0]) != null && $('ptid_' + mp[0]).value != "0" && $('ptid_' + mp[0]).value == other_id) { 
					$('ptid_' + mp[0]).value = "0"
				}
			}.bind(this));
		}
	
		/* set hidden form field for picked team */
		$('ptid_'+the_ids[1]).value = the_ids[3];
	
		/* set team for next round */
		if (the_ids[1] == 63) {
			clone_html = $('matchteam_'+the_ids[1]+'_'+the_ids[2]).innerHTML.gsub(the_ids[1]+'_'+the_ids[2],downstream[the_ids[1]][0]);
			$('matchteam_'+downstream[the_ids[1]][0]).innerHTML = clone_html;
			$('matchteam_63_3').getElementsByClassName('pct')[0].innerHTML = '';
		
		} else {
			if ($('mt_'+downstream[the_ids[1]][0]) != null) {
				$('mt_'+downstream[the_ids[1]][0]).value = the_ids[3]
				/* clone innerhtml change id and move to next */	
				clone_html = $('matchteam_'+the_ids[1]+'_'+the_ids[2]).innerHTML.gsub(the_ids[1]+'_'+the_ids[2],downstream[the_ids[1]][0]);
				$('matchteam_'+downstream[the_ids[1]][0]).innerHTML = clone_html;
				try{
					$('matchteam_'+downstream[the_ids[1]][0]).getElementsByClassName('pct')[0].innerHTML = ''
					$('matchteam_'+downstream[the_ids[1]][0]).getElementsByClassName('pct')[0].innerHTML = team_round_pct[the_ids[3]][match_rounds[downstream[the_ids[1]][0].split('_')[0]]];
				}catch(e){
					 try { $('matchteam_'+downstream[the_ids[1]][0]).getElementsByClassName('pct')[0].innerHTML = '' }catch(err){}
					//alert(e.message)
				}
				if (downstream[the_ids[1]][0] == '63_1' || downstream[the_ids[1]][0] == '63_2') {
					$('matchteam_63_3').innerHTML = '';
					$('ptid_63').value = "0";
				} 
			}
		}
	} catch(e) {}
}

function safe_to_leave() {
    if (unsaved_changes) {
        if (confirm('You have not saved your picks. Are you sure you wish to leave?')) {
            return true;
        } else {
            return false;
        }
    } else {
        return true;
    }
}

/*
function gate_links() {
    // Get all links
    var all_links = document.getElementsByTagName('a');
    if (all_links != null) {
        // Loop through all links
        var number_of_links = all_links.length;
        for (var i=0; i<number_of_links; i++) {
            // Only apply to links that aren't part of the game
            if (!(Element.hasClassName(all_links[i], 'dans_take_link') || Element.hasClassName(all_links[i],'save_picks_button') || Element.hasClassName(all_links[i], 'pick_handle') || Element.hasClassName(all_links[i], 'pick_toggle') || Element.hasClassName(all_links[i], 'sign_out'))) {
                all_links[i].onclick = function() {
                    if (safe_to_leave()) {
                        return true;
                    } else {
                        return false;
                    }
                }
            }
        }
    }
}

function save_picks(page) {
    var picks_form = $('picks_form');
    if (picks_form == null) { return false; }

    // set unsaved changes flag to false
    unsaved_changes = false;
    new Ajax.Request('/seth_davis_challenge/save_picks', {asynchronous:true, evalScripts:true, onLoading:function(request){
		display_ajax_spinner('saving_picks_loading', 'Saving Picks');
	}, parameters:Form.serialize(picks_form) }); return false;
};


function entries_locked() {
    alert('Sorry, entries have been locked. You may no longer create or change your picks.');
};

function toggle_dans_take(obj,id){
	Element.toggle('dans_take_' + id)
	if (obj.hasClassName('closed')) {
		obj.removeClassName('closed')
		obj.addClassName('open')
	} else {
		obj.removeClassName('open')
		obj.addClassName('closed')
	}
}
*/
function go_to_region(id) {
	
	if (id == 1) {
		if ($('pick_bracket_wrap') != null) {
			new Effect.Move ($('pick_bracket_wrap'),{ x: 0, y: 0, mode: 'absolute', duration: 2});
			[1,2,3,4,5].each(function(i) {
				$('reg_nav_btn_' + i).removeClassName('active')
			});
			$('reg_nav_btn_' + id).addClassName('active');
			$('region_rounds_header').removeClassName('right');
		} else {
			new Ajax.Request('/seth_davis_challenge/pick_regions', {
				asynchronous:true, 
				evalScripts:true,
				onLoading:function(request){
					display_ajax_spinner('pick_bracket_window', 'Loading Region ...');
				},
				onComplete:function(req){
					if ($('pick_bracket_wrap') != null) {
						go_to_region(id);
						$('final_four_header').hide();
						$('region_rounds_header').show();
					}
				}
			});
		}
	} else if (id == 2) {
		if ($('pick_bracket_wrap') != null) {
			new Effect.Move ($('pick_bracket_wrap'),{ x: -940, y: 0, mode: 'absolute', duration: 2});
			[1,2,3,4,5].each(function(i) {
				$('reg_nav_btn_' + i).removeClassName('active')
			});
			$('reg_nav_btn_' + id).addClassName('active');
			$('region_rounds_header').addClassName('right');
		} else {
			new Ajax.Request('/seth_davis_challenge/pick_regions', {
				asynchronous:true, 
				evalScripts:true,
				onLoading:function(request){
					display_ajax_spinner('pick_bracket_window', 'Loading Region ...');
				},
				onComplete:function(req){
					if ($('pick_bracket_wrap') != null) {
						go_to_region(id)
						$('final_four_header').hide();
						$('region_rounds_header').show();
					}
				}
			});
		}
	} else if (id == 3) {
		if ($('pick_bracket_wrap') != null) {
			new Effect.Move ($('pick_bracket_wrap'),{ x: -940, y: -445, mode: 'absolute', duration: 2});
			[1,2,3,4,5].each(function(i) {
				$('reg_nav_btn_' + i).removeClassName('active')
			});
			$('reg_nav_btn_' + id).addClassName('active');
			$('region_rounds_header').addClassName('right');
		} else {
			new Ajax.Request('/seth_davis_challenge/pick_regions', {
				asynchronous:true, 
				evalScripts:true,
				onLoading:function(request){
					display_ajax_spinner('pick_bracket_window', 'Loading Region ...');
				},
				onComplete:function(req){
					if ($('pick_bracket_wrap') != null) {
						go_to_region(id)
						$('final_four_header').hide();
						$('region_rounds_header').show();
					}
				}
			});
		}
	} else if (id == 4) {
		if ($('pick_bracket_wrap') != null) {
			new Effect.Move ($('pick_bracket_wrap'),{ x: 0, y: -445, mode: 'absolute', duration: 2});
			[1,2,3,4,5].each(function(i) {
				$('reg_nav_btn_' + i).removeClassName('active')
			});
			$('reg_nav_btn_' + id).addClassName('active');
			$('region_rounds_header').removeClassName('right');
		} else {
			new Ajax.Request('/seth_davis_challenge/pick_regions', {
				asynchronous:true, 
				evalScripts:true,
				onLoading:function(request){
					display_ajax_spinner('pick_bracket_window', 'Loading Region ...');
				},
				onComplete:function(req){
					if ($('pick_bracket_wrap') != null) {
						go_to_region(id)
						$('final_four_header').hide();
						$('region_rounds_header').show();
					}
				}
			});
		}
	} else if (id == 5) {
		if ($('final_four_wrap') == null) {
			new Ajax.Request('/seth_davis_challenge/pick_final_four', {asynchronous:true, evalScripts:true, onLoading:function(request){
				display_ajax_spinner('pick_bracket_window', 'Loading Final Four ...');
			}});
			[1,2,3,4,5].each(function(i) {
				$('reg_nav_btn_' + i).removeClassName('active')
			});
			$('reg_nav_btn_' + id).addClassName('active')
			$('region_rounds_header').hide();
			$('final_four_header').show();
		}
	}
}

function go_to_view_picks(entry_id) {
	window.location = '/seth_davis_challenge/view_picks/' + entry_id;
}

function showFinalFourLoading() {
	display_ajax_spinner('pick_bracket_window', 'Loading Final Four ...');
}

function show_team_preview(team_id,region_id) {
	try { $('pop_up').remove(); } catch(e) {};
	pop_up_html = '<div id="pop_up"> \
		<div id="pop_up_bg"></div> \
		<div id="pop_up_content"> \
			<div id="team_profile"><div class="spinner"></div></div> \
		    <div id="close_button" onclick="$(\'pop_up\').remove();">close</div> \
		</div> \
	</div>';
	var container_id = 'final_four_wrap'
	if (region_id > 0 && region_id < 5) {
		container_id = 'region_' + region_id;
	} else {
		container_id = 'final_four_wrap';
	}
	new Insertion.Top($(container_id), pop_up_html);
	new Ajax.Request('/seth_davis_challenge/team_pop_up', {
		parameters:{ team_id : team_id },
		evalScripts:true,
		asynchronous:true
	});
	
}

function close_team_preview() {
	
}

/* admin function */
function add_remove_bracket_team(team_id) {
	var doit = true;
	if ($('team_action_'+team_id).hasClassName('remove')) {
		var doit = confirm('Are you sure?');
		if (doit) {
			//delete team
			new Ajax.Request('/seth_davis_challenge/remove_team', {
				parameters:{ team_id : team_id },
				evalScripts:true,
				asynchronous:true
			});
		} else {
			return false;
		}
	} else 	if ($('team_action_'+team_id).hasClassName('add')) {
		// add team
		new Ajax.Request('/seth_davis_challenge/add_team', {
			parameters:{ team_id : team_id },
			evalScripts:true,
			asynchronous:true
		});
		//$('team_action_'+team_id).removeClassName('add');
		//$('team_action_'+team_id).addClassName('remove')
		//$('teamid_'+team_id).removeClassName('off');
		//$('teamid_'+team_id).addClassName('on');
	} else {
		alert('no')
	}
}



