// array of user inputted icons
var marker = {};


jQuery().ready(function($){

	// sets up sortable object
	$("#teamroster").sortable();

	// index of each icon (initialized at -1 to allow first icon to be A[charcode-0])
	var count= -1;
			

	// Create a base icon for all of our markers that specifies the
	// shadow, icon dimensions, etc.
	var letterIcon = new GIcon();
	letterIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	letterIcon.iconSize = new GSize(20, 34);
	letterIcon.shadowSize = new GSize(29, 34);
	letterIcon.iconAnchor = new GPoint(10, 35);
	letterIcon.infoWindowAnchor = new GPoint(9, 2);
	letterIcon.infoShadowAnchor = new GPoint(18, 25);





	$('#leagueSelect').change(
		function() {
			// removes all leagues so only one will be added back
			removeLeagues_all()




			// shows the correct league
			switch( $(this).val() ) {


				case 'Bixby North':
					// add league boundaries
					map.addOverlay(bixbypolygon);
					// Center the map on league's field
					map.setCenter( new GLatLng(bixby.latt, bixby.long), 13 );
					// width, height
					map.panBy( new GSize(135, 15) );
					break;


				case 'Greater Bellflower':
					// add league boundaries
					map.addOverlay(gbpolygon);
					// Center the map on league's field
					map.setCenter( new GLatLng(gb.latt, gb.long), 13 );
					// width, height
					map.panBy( new GSize(-15, 30) );
					break;


				case 'Hawaiian Gardens':
					// add league boundaries
					map.addOverlay(hgllpolygon);
					// Center the map on league's field
					map.setCenter( new GLatLng(hg.latt, hg.long), 14 );
					// width, height
					map.panBy( new GSize(-75, -10) );
					break;


				case 'Lakewood':
					// add league boundaries
					map.addOverlay(lkwdpolygon);
					// Center the map on league's field
					map.setCenter( new GLatLng(lkwd.latt, lkwd.long), 13 );
					// width, height
					map.panBy( new GSize(125, 50) );
					break;


				case 'Lakewood Village':
					// add league boundaries
					map.addOverlay(lvpolygon);
					// Center the map on league's field
					map.setCenter( new GLatLng(lv.latt, lv.long), 13 );
					// width, height
					map.panBy( new GSize(230, -35) );
					break;


				case 'Long Beach':
					// add league boundaries
					map.addOverlay(lbpolygon);
					// Center the map on league's field
					map.setCenter( new GLatLng(lb.latt, lb.long), 12 );
					// width, height
					map.panBy( new GSize(-45, -110) );
					break;
				
				
				case 'Plaza':
					// add league boundaries
					map.addOverlay(plazapolygon);
					// Center the map on league's field
					map.setCenter( new GLatLng(plaza.latt, plaza.long), 13 );
					// width, height
					map.panBy( new GSize(-90, -18) );
					break;


				case 'West Long Beach':
					// add league boundaries
					map.addOverlay(wlbpolygon);
					// Center the map on league's field
					map.setCenter( new GLatLng(wlb.latt, wlb.long), 13 );
					// width, height
					map.panBy( new GSize(-50, -135) );
					break;


				default:
					// add league boundaries
					map.addOverlay(bixbypolygon);
					map.addOverlay(gbpolygon);
					map.addOverlay(hgllpolygon);
					map.addOverlay(lkwdpolygon);
					map.addOverlay(lvpolygon);
					map.addOverlay(lbpolygon);
					map.addOverlay(plazapolygon);
					map.addOverlay(wlbpolygon);
					// Center the map on league's field
					map.setCenter( new GLatLng(wlb.latt, wlb.long), 12 );
					// width, height
					map.panBy( new GSize(-200, 10) );
					break;

			} // end switch


			if( $(this).val() != 'all') {
				// assigns league value to lgTitle tag
				$('#lgTitle').html( $(this).val() );
			} // end if
			else {
				$('#lgTitle').html('');
			} // end else
		} // end function
	); // ends change




	// updates division and teams text
	$('#divisionSelect').change(
		function() {
			divisionTeam();
		} // end function
	); // end change



	// updates division and teams text
	$('#team').keyup(
		function() {
			divisionTeam();
		} // end function
	); // end keyup
	
	

	// removess all leagues
	function removeLeagues_all() {
		map.removeOverlay(bixbypolygon);
		map.removeOverlay(gbpolygon);
		map.removeOverlay(hgllpolygon);
		map.removeOverlay(lkwdpolygon);
		map.removeOverlay(lvpolygon);
		map.removeOverlay(lbpolygon);
		map.removeOverlay(plazapolygon);
		map.removeOverlay(wlbpolygon);
	} // end function





	$('#rosterbox form').submit(
		function() {


			$('#lgTitle').html( $('leagueSelect').val() ); 


	teamDiv = '';

	if( $('#divisionSelect').val() != '' ) {
		teamDiv = $('#divisionSelect').val() + ' ';
	}


	teamDiv += $('#team').val();



	$('#division_team').html( teamDiv );




			// if error message previously exists remove it
			if( $('#maperror').length > 0 )
				$('#maperror').animate({opacity: 'hide', height: 'hide'}, 250);



			address = $('.playeraddress').val();

			// Create new geocoding object
			geocoder = new GClientGeocoder();

			// Retrieve location information, pass it to addToMap()
			geocoder.getLocations(address, 


				// This function adds the point to the map
				function(response) {
   
					// attempt to add marker to map   
   					try {


						// if name is not filled in throw error
						if( $('.playername').val() == '') {
							throw('name');
						} // end if


						// increments array position
						count++;
							
						staffChk = '';

				
						// Retrieve the object
						place = response.Placemark[0];

						// Retrieve the latitude and longitude
						point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);

						// Center the map on this point
						map.setCenter(point, 13);

				
						// Create a lettered icon for this point using our icon class
						var letter = String.fromCharCode("A".charCodeAt(0) + count);
						letterIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";

						// adds latest person to the arrays
						iconAddress = "http://www.google.com/mapfiles/marker" + letter + ".png";


						// Set up our GMarkerOptions object
						markerOptions = { icon:letterIcon };


						// markeroptions identifies custom icon to use on marker
						marker[count] = new GMarker(point, markerOptions);


						// add marker to map
						map.addOverlay(marker[count]);
							
							
						rosterlength = $("#teamroster").sortable('toArray');
	
						if( $('#rosterbox form input[type="checkbox"]').attr('checked') )
							staffChk = ' coach'
							
							
						addLi = '<li class="player vcard' + staffChk + '" id="roster_' + (rosterlength.length-1) + '" style="background-image: url(' + iconAddress + ');">';
						addLi += '<strong class="fn">' + $('.playername').val() + '</strong>';
						addLi += '<small class="adr">' + $('.playeraddress').val() + '</small>';
						addLi += '<a href="javascript:editPlayer(' + count + ');">edit</a><a href="javascript:removePlayer(' + count + ');">remove</a>';
						addLi += '</li>';
							
							
						$('#teamroster').append( addLi );


   					} // end try
   					catch(err) {


						error = '<div id="maperror">';
						error += '<h1>An error occurred.</h1>';
						error += '<p>';



						switch(err) {

							case 'name':
								error += 'Your location was not found on the map, remember both a name and a location are required.';
								break;

							case 'TypeError: response.Placemark is undefined':
								error += 'Your location was not found on the map, remember both a name and a location are required.';
								break;
		
							default: 
								error += err;
								break;	

						} // end switch


						error += '</p>';
						error += '</div>';


						$('#rosterbox').prepend(error);
	
						
						$('#rosterbox #maperror').animate({opacity: 'show', height: 'show'}, 250);


					} // end catch

					// keeps form from refreshing the page	  
					return false;

				} // end addtopmap

		    ); // end getLocations call

			// keep form from submitting and refreshing the page
			return false;

		} // end function
	); // end submit






	// resets player name, address and checkbox
	$('#rosterbox form a').click(
		function() {
			$('.playername').val('');
			$('.playeraddress').val('');
			$('#rosterbox form input[type="checkbox"]').attr('checked', false);
		
			return false;
		} // end function
	); // end click










	$('.printfriend_edit').toggle(
		function() {


		/*	TURNS ON PRINT.CSS
		-------------------------------------------------*/

			// stylesheet to toggle
			title = 'printmap';

			// cycle through all link tags
			for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {

				// if stylesheet and if it has a title
				if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {

					// turn off stylesheets to begin with
					a.disabled = true;

					// if stylesheet title is correct enable it
					if(a.getAttribute("title").indexOf(title) != -1) 
						a.disabled = false;
				} // end if
			} // end for
			
			
			

		/*	TURNS ON PRINT.CSS
		-------------------------------------------------*/









			// checks to see if map container has been resized and adjusts the map
			map.checkResize();
						
			
			// toggle printer friendly text			
			$('.printfriend_edit').html('back to normal version');


			// shows the correct league with new zoom level and pan level
			switch( $('#leagueSelect').val() ) {

				case 'Bixby North':
					// add league boundaries
					map.addOverlay(bixbypolygon);
					// Center the map on league's field
					map.setCenter( new GLatLng(bixby.latt, bixby.long), 14 );
					// width, height
					map.panBy( new GSize(245, 35) );
					break;


				case 'Greater Bellflower':
					// add league boundaries
					map.addOverlay(gbpolygon);
					// Center the map on league's field
					map.setCenter( new GLatLng(gb.latt, gb.long), 14 );
					// width, height
					map.panBy( new GSize(-15, 80) );
					break;


				case 'Hawaiian Gardens':
					// add league boundaries
					map.addOverlay(hgllpolygon);
					// Center the map on league's field
					map.setCenter( new GLatLng(hg.latt, hg.long), 15 );
					// width, height
					map.panBy( new GSize(-148, -10) );
					break;
	

				case 'Lakewood':
					// add league boundaries
					map.addOverlay(lkwdpolygon);
					// Center the map on league's field
					map.setCenter( new GLatLng(lkwd.latt, lkwd.long), 14 );
					// width, height
					map.panBy( new GSize(225, 125) );
					break;


				case 'Lakewood Village':
					// add league boundaries
					map.addOverlay(lvpolygon);
					// Center the map on league's field
					map.setCenter( new GLatLng(lv.latt, lv.long), 13 );
					// width, height
					map.panBy( new GSize(230, -35) );
					break;


				case 'Long Beach':
					// add league boundaries
					map.addOverlay(lbpolygon);
					// Center the map on league's field
					map.setCenter( new GLatLng(lb.latt, lb.long), 12 );
					// width, height
					map.panBy( new GSize(-45, -110) );
					break;
				
				
				case 'Plaza':
					// add league boundaries
					map.addOverlay(plazapolygon);
					// Center the map on league's field
					map.setCenter( new GLatLng(plaza.latt, plaza.long), 13 );
					// width, height
					map.panBy( new GSize(-90, -18) );
					break;


				case 'West Long Beach':
					// add league boundaries
					map.addOverlay(wlbpolygon);
					// Center the map on league's field
					map.setCenter( new GLatLng(wlb.latt, wlb.long), 13 );
					// width, height
					map.panBy( new GSize(-50, -265) );
					break;


				default:
					// add league boundaries
					map.addOverlay(bixbypolygon);
					map.addOverlay(gbpolygon);
					map.addOverlay(hgllpolygon);
					map.addOverlay(lkwdpolygon);
					map.addOverlay(lvpolygon);
					map.addOverlay(lbpolygon);
					map.addOverlay(plazapolygon);
					map.addOverlay(wlbpolygon);
					// Center the map on league's field
					map.setCenter( new GLatLng(wlb.latt, wlb.long), 12 );
					// width, height
					map.panBy( new GSize(-200, 10) );
					break;

			} // end switch


		
		}, //end function

		function() {
		


		/*	TURNS ON PRINT.CSS
		-------------------------------------------------*/

			// stylesheet to toggle
			title = 'printmap';

			// cycle through all link tags
			for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {

				// if stylesheet and if it has a title
				if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {

					// turn off stylesheets to begin with
					a.disabled = false;

					// if stylesheet title is correct enable it
					if(a.getAttribute("title").indexOf(title) != -1) 
						a.disabled = true;
				} // end if
			} // end for
			
			
			

		/*	TURNS ON PRINT.CSS
		-------------------------------------------------*/







			// checks to see if map container has been resized and adjusts the map
			map.checkResize();


			// toggle printer friendly text			
			$('.printfriend_edit').html('printer friendly version');



			// shows the correct league
			switch( $('leagueSelect').val() ) {


				case 'Bixby North':
					// add league boundaries
					map.addOverlay(bixbypolygon);
					// Center the map on league's field
					map.setCenter( new GLatLng(bixby.latt, bixby.long), 13 );
					// width, height
					map.panBy( new GSize(135, 15) );
					break;


				case 'Greater Bellflower':
					// add league boundaries
					map.addOverlay(gbpolygon);
					// Center the map on league's field
					map.setCenter( new GLatLng(gb.latt, gb.long), 13 );
					// width, height
					map.panBy( new GSize(-15, 30) );
					break;


				case 'Hawaiian Gardens':
					// add league boundaries
					map.addOverlay(hgllpolygon);
					// Center the map on league's field
					map.setCenter( new GLatLng(hg.latt, hg.long), 14 );
					// width, height
					map.panBy( new GSize(-75, -10) );
					break;


				case 'Lakewood':
					// add league boundaries
					map.addOverlay(lkwdpolygon);
					// Center the map on league's field
					map.setCenter( new GLatLng(lkwd.latt, lkwd.long), 13 );
					// width, height
					map.panBy( new GSize(125, 50) );
					break;


				case 'Lakewood Village':
					// add league boundaries
					map.addOverlay(lvpolygon);
					// Center the map on league's field
					map.setCenter( new GLatLng(lv.latt, lv.long), 13 );
					// width, height
					map.panBy( new GSize(230, -35) );
					break;


				case 'Long Beach':
					// add league boundaries
					map.addOverlay(lbpolygon);
					// Center the map on league's field
					map.setCenter( new GLatLng(lb.latt, lb.long), 12 );
					// width, height
					map.panBy( new GSize(-45, -110) );
					break;
				
				
				case 'Plaza':
					// add league boundaries
					map.addOverlay(plazapolygon);
					// Center the map on league's field
					map.setCenter( new GLatLng(plaza.latt, plaza.long), 13 );
					// width, height
					map.panBy( new GSize(-90, -18) );
					break;


				case 'West Long Beach':
					// add league boundaries
					map.addOverlay(wlbpolygon);
					// Center the map on league's field
					map.setCenter( new GLatLng(wlb.latt, wlb.long), 13 );
					// width, height
					map.panBy( new GSize(-50, -135) );
					break;


				default:
					// add league boundaries
					map.addOverlay(bixbypolygon);
					map.addOverlay(gbpolygon);
					map.addOverlay(hgllpolygon);
					map.addOverlay(lkwdpolygon);
					map.addOverlay(lvpolygon);
					map.addOverlay(lbpolygon);
					map.addOverlay(plazapolygon);
					map.addOverlay(wlbpolygon);
					// Center the map on league's field
					map.setCenter( new GLatLng(wlb.latt, wlb.long), 12 );
					// width, height
					map.panBy( new GSize(-200, 10) );
					break;

			} // end switch







		
		} // end function
	); // end toggle








}); // end jQuery








function divisionTeam() {

teamDiv = '';

	if( $('#divisionSelect').val() != '' ) {
		teamDiv = $('#divisionSelect').val() + ' ';
	}


	teamDiv += $('#team').val();



	$('#division_team').html( teamDiv );

} // end function














function editPlayer(index) {


	id = '#roster_' + index;
	
	// puts name back into form
	name = id + ' strong';
	$('.playername').val( $(name).html() );
	
	// puts address back into form
	address = id + ' small';
	$('.playeraddress').val( $(address).html() );
	
	
	// if coach checks the coach checkbox
	if( $(id).attr('class').indexOf('coach') != -1 ) {
		id = '#rosterbox input[type="checkbox"]';
		$(id).attr('checked', true);
	} // end if
	
	
	
//$('#rosterbox form').effect("highlight", {}, 5000);
$('#rosterbox form').animate({backgroundColor: '#ff0000'}, 1000); 
	
	// removes the player
	removePlayer(index);


//$('#rosterbox form').trigger('submit');



} // end function








function removePlayer(index) {



				marker[index].hide(); 
				
				id = '#roster_' + index;
				
//				$(id).animate({backgroundColor: '#ff0000'}, 250);

//				$(id).effect('shake', 300).animate({opacity: 'hide'}, 200);
				
				$(id).hide();				
				
//				$(id).remove();

}

