﻿/*
* findTravels.js
* Javascript for the findTravels control
* NOTE: This javascript is deeply connected to the googleMaps.js javascript. This is due to the google API loading the map as a callback and not as part of the $.ready function. Changes made to these files should be done with care.
*/

// Flag indicating whether the google map has been initialized or not
var isMapInitialized = false;

// The list of travels representing the travels in the table
var travels = new Array();

// The list of selected tags
var selectedTags = new Array();

function setMarkers() {
	// GoogleMap is located in googleMaps.js
    $.each(GoogleMap.markersWithID, function (index, elem) {
        $.each(travels, function (index2, elem2) {
            if ($.trim(elem.pageID) == $.trim(elem2.pageID)) {
                elem2.marker = elem.marker;
                return;
            }
        });
    });
    $.each(travels, function (index, elem) {
        if (elem.marker != null)
            elem.marker.setVisible(false);
    });
}

// Handles the show/hide of the travels depending on selected tags
function toggleTravels() {
	var count = travels.length;
    if (selectedTags.length > 0) {
    	$.each(travels, function (index, t) {
    		var show = true;
    		if (t.tags.length > 0) {
    			$.each(selectedTags, function (index, selectedTag) {
    				var match = false;
    				$.each(t.tags, function (index, tag) {
    					if ($.trim(tag) == $.trim(selectedTag)) {
    						match = true;
    						return false;
    					}
    				});
    				if (!match) {
    					show = false;
    					count--;
    					return false;
    				}
    			});
    		}
    		else {
    			show = false;
    			count--;
    		}

    		if (show) {
    			$(t.domObject).show();
    			if (t.marker != null && !t.marker.visible) {
    				t.marker.setVisible(true);
    			}
    		}
    		else {
    			$(t.domObject).hide();
    			if (t.marker != null)
    				t.marker.setVisible(false);
    		}

    	});  
    }
    else {
        // show all travels
        $.each(travels, function (index, t) {
            $(t.domObject).show();
            if (t.marker != null)
                t.marker.setVisible(true);
        });        
    }
    $("#travel-count").text(count + " av " + travels.length);
}

$(document).ready(function () {
	// If there are no "travels in focus" the tab and pane are hidden
	if ($('div.travel_in_focus').length == 0) {
		$('#travels_in_focus_tab').parents('li').hide();
		$('#travels_in_focus_pane').hide();
		// the tabs are set from googleMaps.js         
	}
	else {
		$('ul#find_travels_tabs').tabs('.pane');
	}

	// initializes the scrollables on "resor i fokus"
	$(".scrollable").scrollable().navigator();

	// The Travel Class containing the dom object to be displayed and hidden and a list of tags related to the travel
	var Travel = function () {
		this.domObject = null;
		this.tags = new Array();
		this.marker = null;
		this.pageID = 0;
	}

	// Initiates travels
	$('#travels .travel').each(function (index, domEle) {
		var t = new Travel();
		t.domObject = domEle;

		// adds all tags belonging to the travel
		$(domEle).siblings('.data_tags').find('li').each(function (index2, domEle2) {
			t.tags.push($(domEle2).text());
		});
		t.pageID = $(domEle).siblings('.data_id').text();		
		travels.push(t);
	});

	// initial check of selected tags
	$('#tag-container p input').each(function (event) {
		var listItem = $(this).val();
		if ($(this).is(':checked')) {
			selectedTags.push(listItem);
		}
		else {
			selectedTags = $.grep(selectedTags, function (value) {
				return value != listItem;
			});
		}
		toggleTravels();
	});
	// Actions triggered by clicking a tag link
	$('#tag-container p input').click(function (event) {
		var listItem = $(this).val();
		if ($(this).is(':checked')) {
			selectedTags.push(listItem);
		}
		else {
			selectedTags = $.grep(selectedTags, function (value) {
				return value != listItem;
			});
		}
		toggleTravels();		
	});

	// Switching between map view and travel list

	$('.display_controls .button').click(function (event) {
		event.preventDefault();
		$('.display_controls .button').parent('li').toggleClass('current');
		$('#travel_list').toggle();
		$('#google_map_wrapper').toggle();
	});

	//loads the map when clicking the 'hitta din resa' tab for the first time
	$('#find_travel_tab').click(function () {
		if (!isMapInitialized) {
			initializeGoogleMap();
			isMapInitialized = true;
		}
	});

	//Since the map is hidden by default we need to reset the size, center and the zoom properties
	$('#show_on_map').click(function () {		
		if (!isMapInitialized) {
			initializeGoogleMap();
			//			isMapInitialized = true;
			//			google.maps.event.trigger(GoogleMap.map, 'resize');
			//			GoogleMap.map.fitBounds(GoogleMap.bounds);
		}

		//		var EMEA = new google.maps.LatLng(29.84064389983441, 18.28125);
		//		GoogleMap.map.setCenter(EMEA);
		//		GoogleMap.map.setZoom(2);		
	});
});
