/***
credit: http://olbertz.de/jquery/googlemap.js
modified by: Pamela Lu-Stone (pamela.stone@stanford.edu)
****/
var clickHandler;
var map;
var lat;
var lng;
var locations;
var bounds;

$(document).ready(function() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(0, 0), 13);
        $.get('http://localhost:8888/drupal/themes/adt/locations_src.php', processLocations );
    }
    
    /* $('a#new_location').click(function() {
        $('a#new_location').hide();
        $('div#info').show('slow');
        clickHandler = GEvent.addListener(map, "click", function(marker, point) {
            setNewMarker(point);
        });
    });
    
   $('a#save').click(function() {
        $('div#formular').hide('slow');
        $.post('locations_src',
               { type: 'upload',
                 name: $('form').find('input').get(0).value,
                 latitude: lat,
                 longitude: lng
               }, 
               processLocations );        
    });
    
    $('a#cancel').click(function() {
        $('div#formular').hide('slow');
    });*/
    
    $('a#zoom_show_all').click(function() {
        zoomShowAll();
    });
});
/*
function setNewMarker(point) {
    $('div#formular').show('slow');
    $('a#new_location').show();
    $('div#info').hide();
    lat = point.lat();
    lng = point.lng();
    $('div#formular').find('p:nth-of-type(0) ').html('Latitude=<b>'+lat+'</b>, Longitude=<b>'+lng+'</b>');
    $('div#formular').show();
    GEvent.removeListener(clickHandler);
}
*/
function processLocations(content) {alert('here');
    eval("locations = "+content);
    var quartername='';
    var link='';
    link=link+'<table id=maptable><tr>';
    
    locations.forEach(function(element, index, array) {
        //if(quartername=='' ){    	 
       	 	link = link+ '<td valign=top><strong>'+element.store_name+'</strong><br />';
       	 	// $('p#location_list').append(link);
       	 	//quartername=element.quarter_name;  
       // }
        //if(quartername!=element.quarter_name){    	 
       	 	//link = link+ '</td><td valign=top><strong>'+element.quarter_name+'</strong><br />';
       	 	 $('p#location_list').append(link);
       	 	//quartername=element.quarter_name;  
        //}
        
        var marker = new GMarker(new GLatLng(element.latitude, element.longitude), {title: element.name});
        map.addOverlay(marker);
        GEvent.addListener(marker, 'click', function() {
        	var link;
        	
            //if ($.cookie('prestulinks')=='y'){
            link='Student Name: <b>'+element.store_name+' '+element.store_name+
            '</b><br />Quarter: <b>'+element.store_name+'</b><br />City: <b>'+element.store_name+
            '</b><br /><a href="poster/index.php?student_id='+element.student_id+'">View Details</a>';
            /*}
            else{
            link='Student Name: <b>'+element.student_firstname+' '+element.student_lastname+
            '</b><br />Quarter: <b>'+element.quarter_name+'</b><br />City: <b>'+element.name+
            '</b>';
            }*/
            marker.openInfoWindowHtml(link);       
        });
        link = link+'<a href="#" onclick="moveMapTo('+index+')">'+element.name+'</a><br />';
        //$('p#location_list').append(link);
        
       
    });
    link=link+'</td></tr></table>';
   
   $('p#location_list').html(link);
   
    zoomShowAll();
}

function moveMapTo(index) {
    map.setCenter(new GLatLng(locations[index].latitude, locations[index].longitude), 11);
}

function zoomShowAll() {
    bounds = new GLatLngBounds();
    map.setCenter(new GLatLng(0,0),0);
    
    locations.forEach(function(elemet, id, array) {
        bounds.extend(new GLatLng(locations[id].latitude, locations[id].longitude));
    });
    map.setZoom(map.getBoundsZoomLevel(bounds));
    var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
    var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
    map.setCenter(new GLatLng(clat,clng));
}
