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

$(document).ready(function(){
	//Until I find a nicer way (selecting the clubs out of the works)
	var last = $("#works div:last-child");
	last.addClass("club");
	last.prev().addClass("club");
	last.prev().prev().addClass("club");
	last.prev().prev().prev().addClass("club");
	//End
	var filters = [];
	var visible = false;
	
	var highlight_icon = "http://www.olohuone.org/images/2010/salmiakki.png";
	var default_icon = "http://www.olohuone.org/images/2010/salmiakki-blue.png";
	
	function create_marker( latlng ) {
		var marker = new google.maps.Marker({
	        position: latlng,
			icon: default_icon
	    });
		marker.point_type = "marker";
	    return marker;
	};

	function create_line( latlng ) {
		var line = new google.maps.Polyline({
		    path: latlng,
		    strokeColor: "#008BC2",
		    strokeOpacity: 1.0,
		    strokeWeight: 2
	  	});
		line.point_type = "polyline";
	    return line;
	};

	var turku2011cafe = create_marker( new google.maps.LatLng(60.448011804727, 22.266050790985123) );
	var vahatori = create_marker( new google.maps.LatLng(60.451123117908345, 22.27287433071901) );
	var map_points = {
		"work_0" : [ create_marker( new google.maps.LatLng(60.44096262568342, 22.2476401083069) ) ],
		"work_1" : [ vahatori, create_marker( new google.maps.LatLng(60.447368868677756, 22.263996218879715) ), create_marker( new google.maps.LatLng(60.450086046626616, 22.273475145538345) ) ],
		"work_2" : [ create_marker( new google.maps.LatLng(60.443010855603454, 22.356076454360977) ), create_marker( new google.maps.LatLng(60.4320960590212, 22.333932136734024) ), create_marker( new google.maps.LatLng(60.458704372901785, 22.335326885421768) ), create_marker( new google.maps.LatLng(60.458598575518984, 22.308676456649795) ), create_marker( new google.maps.LatLng(60.458831329305966, 22.278528427322403) ), create_marker( new google.maps.LatLng(60.47137111748364, 22.230323766906753) ), create_marker( new google.maps.LatLng(60.462428221158255, 22.200143551071182) ) ],
		 "work_3" : [ create_marker( new google.maps.LatLng(60.415441551824884, 22.258969759185806) ), create_marker( new google.maps.LatLng(60.446170274910735, 22.265664552886978) ), create_marker( new google.maps.LatLng(60.45156227586933, 22.267070030410782) ), create_marker( new google.maps.LatLng(60.45321832416265, 22.254034494598404) ), create_marker( new google.maps.LatLng(60.453795011253895, 22.262220596511856) ), vahatori, create_marker( new google.maps.LatLng(60.447106928120775, 22.26410887165834) ), create_marker( new google.maps.LatLng(60.45093792903648, 22.27582476063539) ), create_marker( new google.maps.LatLng(60.44969978202322, 22.262306427200333) ), create_marker( new google.maps.LatLng(60.44846158781375, 22.267692302902237) ), create_marker( new google.maps.LatLng(60.4511813199071, 22.263314937789932) ), create_marker( new google.maps.LatLng(60.43713044830878, 22.241460298736587) ) ],
		"work_4" : [ create_marker( new google.maps.LatLng(60.451445873132684, 22.26410887165834) ), create_marker( new google.maps.LatLng(60.45082681520656, 22.264956449707046) ), turku2011cafe ],
	 	"work_5" : [ create_marker( new google.maps.LatLng(60.44693494579841, 22.263942574699417) ) ], 
		"work_6" : [ create_marker( new google.maps.LatLng(60.45014954173824, 22.26035914345552) ) ],
		"work_7" : [ create_marker( new google.maps.LatLng(60.44807265809708, 22.266453122337357) ) ],
		"work_8" : [ create_marker( new google.maps.LatLng(60.434901861125454, 22.2271802179413) ), create_marker( new google.maps.LatLng(60.442492194601485, 22.24913141651918) ), create_marker( new google.maps.LatLng(60.44892723903646, 22.27685472889711) ), create_marker( new google.maps.LatLng(60.453541058567076, 22.258701538284317) ) ],
		"work_9" : [ create_line( [ new google.maps.LatLng(60.43897249052261, 22.239786600311295), new google.maps.LatLng(60.44350304665303, 22.250000452240005), new google.maps.LatLng(60.44720747121063, 22.260729288299576), new google.maps.LatLng(60.44862562480153, 22.264934992034927), new google.maps.LatLng(60.45150407455316, 22.273475145538345), new google.maps.LatLng(60.45228713806917, 22.27433345242311), new google.maps.LatLng(60.45192735447375, 22.274977182586685), new google.maps.LatLng(60.45220248346406, 22.276693796356216) ] ) ],
		"work_10" : [ turku2011cafe ],
		"work_11" : [ create_marker( new google.maps.LatLng(60.4523188834892, 22.27636120243837) ) ],     
		"work_12" : [ create_marker( new google.maps.LatLng(60.45038764730164, 22.271747802932754) )], 
		"work_13" : [],
		"work_14" : [],
		"work_15" : [ create_marker( new google.maps.LatLng(60.44755143205944, 22.26359388752748) ), create_marker( new google.maps.LatLng(60.447106928120775, 22.26410887165834) ), turku2011cafe, create_line( [  new google.maps.LatLng(60.447318597421535, 22.26382992192079), new google.maps.LatLng(60.44871558021728, 22.268915390213028), new google.maps.LatLng(60.44895898774045, 22.26990244313051), new google.maps.LatLng(60.44949871095977, 22.271425937850967), new google.maps.LatLng(60.45025007541128, 22.272434448440567), new google.maps.LatLng(60.4508638531921, 22.272949432571426) ] ), vahatori ],
		"work_16" : [ vahatori ] ,
		"info" : [ vahatori ]
	};
	
	function toggle_on_map( map ) {
		for( map_point in map_points ) {
			for( var i = 0; i < map_points[map_point].length; i++ ) {
				if( map ) {
					map_points[map_point][i].setMap( map );
				} else {
					map_points[map_point][i].setMap();
				}
			};
		};
	};

	function show_on_map( work_id ) {
		toggle_on_map();
		for( var i = 0; i < map_points[work_id].length; i++ ) {
			map_points[work_id][i].setMap( map );
		};
	};

	function highlight_on_map( work_id ) {
		for( var i = 0; i < map_points[work_id].length; i++ ) {
			var temp = map_points[work_id][i];
			switch( temp.point_type ) {
				case "marker":
					var icon = (temp.getIcon() === default_icon) ? highlight_icon : default_icon; 
					temp.setIcon( icon );
					break;
				case "polyline":
					var color = (temp.get('strokeColor') === '#008BC2') ? '#000000' : '#008BC2';
					temp.setOptions( { strokeColor: color } );
					break;
			}
		}
	};

	function toggle_back(){
		$("#chooser").slideDown("fast");
		if( filters.length ) {
			filter_list();
		} else {
			$(".work .details", this.parentNode).slideUp("fast");
		}
		$(".description", this.parentNode).slideUp("fast");
		$(".back", this.parentNode).hide();
		toggle_on_map( map );
	};
	
	$(".work a.link").hover( 
		function(event) {
			highlight_on_map( this.parentNode.id );
		}
	);
	
	$(".work a.link").toggle(
		function (event) {
			$(".details, .description", this.parentNode).slideDown("fast");
			$(this.parentNode).siblings().hide();
			$("#chooser").slideUp("fast");
			$(".back", this.parentNode).show();
			show_on_map( this.parentNode.id );
			event.preventDefault();
		},
		function (event) {
			$(this.parentNode).siblings().show();
			toggle_back();
			event.preventDefault();
		}
	);
	
	$(".back, .top").click(function() {
		toggle_back();
		$(".work").show();
		$("#works").children().show();
	});
	
	function filter_list() {
		$(".work").each( function() {
			var work = this.id;
			jQuery.each(filters, function(index, tag){
				if($("#" + work + " ." + tag).length) { visible = true; }
				else { visible = false; return false; }
				return true;
			});
			if( visible ) {
				$("#" + work + " .details").show();
				$("#" + work).slideDown();
			}
			else { $("#" + work).slideUp(); }
		});
	};
	
	$(".toggler").click(function (event) {
		var tag = this.id;
		if($(this).hasClass("active_toggler")) {
			$(this).removeClass("active_toggler");
			filters = jQuery.grep(filters, function(value) { return value != tag; });
			$("#works h3").show();
		}
		else {
			$(this).addClass("active_toggler");
			filters.push(tag);
			$("#works h3").hide();
		}
		if( filters.length ) {
			filter_list();
/*
			$(".work").each(function(){
				var work = this.id;
				jQuery.each(filters, function(index, tag){
					if($("#" + work + " ." + tag).length) { visible = true; }
					else { visible = false; return false; }
					return true;
				});
				if( visible ) {
					$("#" + work + " .details").show();
					$("#" + work).slideDown();
				}
				else { $("#" + work).slideUp(); }
			});
*/
		}
		else {
			$(".work:hidden").slideDown();
			$(".work .details").slideUp();
		}
		event.preventDefault();
	});
	
	/*
		Google Maps section
	*/

	var latlng = new google.maps.LatLng(60.45143, 22.268733);
    var myOptions = {
        zoom: 12,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map"), myOptions);

	/*
		Remove these in some point
	*/

	if( !document.getElementById('markers') ) {

		toggle_on_map( map );
		
	} else {
		
		var markers = [];
		var markers_dom = document.getElementById('markers');
		var reitti = false;
		var reitti_latlng = [];
		
		function update_reitti() {
			reitti_latlng = [];
			for( var i = 0; i < markers.length; i++ ) {
				reitti_latlng.push( markers[i].getPosition() );
			}
			if( reitti ) reitti.setPath( reitti_latlng );
		};

		$('#add_marker').click( function(event) {
		    var marker_dom = document.createElement('span');
		    marker_dom.setAttribute('id', "marker_" + (markers.length + 1));
		    marker_dom.setAttribute('latlng', latlng);
		    marker_dom.appendChild( document.createTextNode( "new google.maps.LatLng" + latlng + ", " ) );
		    markers_dom.appendChild( marker_dom );

		    var marker_point = new google.maps.Marker({
		        position: map.getCenter(), 
		        draggable: true,
				map: map,
		        title: "marker_" + (markers.length + 1)
		    });
		    markers.push(marker_point);

			if( reitti ) update_reitti();
		    google.maps.event.addListener( marker_point, 'dragend', function(e) {
		        marker_dom.innerHTML = "new google.maps.LatLng" + e.latLng + ", ";
			    marker_dom.setAttribute('latlng', e.latLng);
				if( reitti ) update_reitti();
		    });
			event.preventDefault();
		} );

		$('#create_line').click( function() {
			update_reitti();
			reitti = create_line( reitti_latlng );
			reitti.setMap( map );
		} );
	
	}
});
