
var gmap=null;

// Called when a new map pos is set.

function tick_list()
{
    clear_all_titles();
    hamta_stopp();
}



/*
 * We cant react to a signle keypres, becouse if you write 
 * uppsala in a form, you will trigger 'p' twice.
 * so what i do here, is to count how many times the same 
 * button is pressed at once. And only react when they
 * push it 4 times
 */
var key_id=0;
var key_pr=0;
function handle_key(event)
{
    var KeyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    
    if(key_id == KeyCode)
    {
	key_pr++;
    }
    else
    {
	key_id = KeyCode;
	key_pr = 0;
    }
    
    if(key_pr < 3)
	return(false);
    key_pr = 0;
    
    switch(KeyCode)
    {
	case 66: // 'b'
	    //go_back();
	    break;
	case 67: // 'c'
	    clear_all_agents();
	    break;
	case 80: // 'p'
	    var center = gmap.getCenter();
	    alert("Position center: " + center.toString() + " zoom: " + gmap.getZoom());
	    break;
	case 83: // 's'
	    // Reload all stops
	    tt=parseInt(Math.random()*99999999);
	    clear_all_titles();
	    hamta_stopp();
	    break;
    }    

    // False makes sure mozilla/iw wont handle res
    return(true);
}


/*
 * Used with the search form abow, makes sure
 * pressing enter wont trigger action but search_for_stop();
 */
function handle_enter(e)
{
    if(e.keyCode==13)
    {
	search_for_stop();
	return(false);
    }
    return(true);
}

/*
 * Ignore enter, some f**ing browsers does
 * submit the form, and change page on enter
 */
function handle_enter_ignore(e)
{
    if(e.keyCode==13)
    {
	return(false);
    }
    return(true);
}

/*
 * Trigger when user klicks log, centers on the map position first opened.
 */
function reset_view()
{
    gmap.setCenter(new GLatLng(map_start_lat, map_start_long), map_start_z);
}

/*
 * Open a popup, with a guide, for the costumers
 * to request new stops.
 */
function ny_stopp_guide()
{
    window.open('/ny_stopp_guide/step1.php', 'nytt_stopp', 'width=600,height=400');
}

/*
 * Open a popup, with a trip, that the user can print out.
 */
function print_tur(tur_var_id)
{
    window.open('tur/?t='+tur_var_id, 'turlista', 'width=740,height=800,scrollbars=yes');
}

/*
 * Just puts the Welcome-message on the info-div.
 */
function set_start_info()
{
    back_add("set_start_info()");
    // Status message
    window.status="Glassbilspejl, skriven av Jimmy Wennlund.";
    info_set("<center><h1>Hej Kund!</h1></center><h3><br>På den här sidan kan du följa glassbilarna med gps-precision och se exakt var din glassbil befinner sig just nu.</h3><h3><br>För att hitta ditt stopp, sök på ditt gatunamn ovanför här eller dra och zooma med scroll-hjulet i kartan.<br>När du kommer närmare ser du stoppen markerade med små flaggor.</h3><center><img class=\"t\" style='margin-top: 10px; margin-bottom: 10px' src=\"images/icon-sms.png\" title='Gör en sökning på din gata, och klicka på den här symbolen!'></center><h3>Om du klickar p&aring; en h&aring;llplats, sedan p&aring; sms-bevakning och anger ditt mobilnummer, s&aring; blir du V-I-P kund och f&aring;r gratis sms med extra förmåner till din mobil exakt 10 minuter innan glassbilen kommer hem till dig!<br><br>När du får ditt sms är det bara att plocka fram stövlarna, och vandra ut till oss. Glassigt, eller hur?</h3><h1><br>Välkommen!</h1><br><center><h5><a href='JavaScript: remove_sms_kund()'>Avbeställ sms</a></h5></center>");  
}


function init_engine_big()
{
    if((is_ie && !is_ie6up) ||!NiftyCheck())
    {
	alert("För att bäst kunna visa den här sidan, rekomenderas att du uppgraderar din Internet Explorer till version 7.0 eller senare.");
    }
    
    
    // Hide Non-Javascript supported content
    document.getElementById('compatible').style.display="none";
    document.getElementById('uncompatible').style.display="inline";
    
    // Take away scroll-bars, this is a fullscreen app.
    window.document.body.scroll = 'no';

    document.getElementById('right').onmouseover=function() 
    {
        // Disable scroll-wheel by zooming.
        gmap.disableScrollWheelZoom();
    }
    document.getElementById('map').onmouseover=function()
    {
        // Enable scroll-wheel by zooming.
        gmap.enableScrollWheelZoom();
    }    

    /*
     * If ie version below 7, replace png with gif for menu.
     */
    if(is_ie && !is_ie7up)
    {
	document.getElementById("back_menu").src="images/back_menu.gif";
    }    
    
    /*
     * Put welcome message.
     */
    set_start_info();

    /*
     * correctPNG() will solv a shitty bug in ie, 
     * that prevent transparent png-pics.
     *  Bug, dont call this when map is loading.
     */
    correctPNG();

    // create the map
    if(!gmap)
	gmap = new GMap2(document.getElementById("map"));

    // Put up map-controls from google.
    var pos1 = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10,10));
    var c1=new GLargeMapControl();
    gmap.addControl(c1, pos1);

    var pos2 = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(80,10));
    var c2=new GMapTypeControl();
    gmap.addControl(c2, pos2);
    
    // Creates a map overview control that is fixed botton right corner.
    //var c3=new GOverviewMapControl();
    //gmap.addControl(c3);

    gmap.addMapType(G_PHYSICAL_MAP);
    gmap.removeMapType(G_SATELLITE_MAP);
    
    // Enable scroll-wheel by zooming.
    gmap.enableScrollWheelZoom();

    // Resize gmap, and divs.
    // must be called before setCenter, or the center before resize wont be the center anymore.
    wresize(); 
    
    // Set map-center
    gmap.setCenter(new GLatLng(map_start_lat, map_start_long), map_start_z);
    
    // Will reload list of cars
    go_round();

    // IF we have data for filling in the search forms, do so.
    if(gata && gata.length>2)
    {
	    document.search.gata.value = gata;
	    if(city && city.length>2)
		document.search.ort.value = city;
	    else if(pnr && pnr.length>4)
		document.search.ort.value = pnr;
	    setTimeout('search_for_stop();', 400);
    }

    // If we have a stop to show, show that
    if (request_stopp_id>0)
    {
	setTimeout("klick_stopp(request_stopp_id,false);", 500);
    }

    // To reload the page, and free bad browsers memory time to time
    setTimeout('toggle_desc()', 600); // Will hide the flag descriptors
    setInterval('try { tick_list(); } catch(e) {}', 450000); // Will reload all flags
    setInterval('try { query_agents_in_view(); } catch(e) {}', 4100); // Will get new position of all cars
    setInterval('try { query_all_agents(); } catch(e) {}', 60000); // If cars list dialog is open, it reloads
    setInterval('try { clear_all_agents(); } catch(e) {}', 240000); // Every 4 min, stale old agents are removed.


    // Listen on events that happens with the map.
    GEvent.addListener(gmap, "moveend", function() {console.info("Moved map center: "+ gmap.getCenter().lat()+","+gmap.getCenter().lng()); hamta_stopp();});
    GEvent.addListener(gmap, "zoomend", function() {console.info("New zoom: "+gmap.getZoom()); hamta_stopp(); toggle_desc();});
}


/*
 * A javascript, that will round the sharp edges an create 
 * smooth boxes.
 */
function go_round()
{
    Rounded("div#flaggdesc","all","transparent","#FFFFFF","border #508F0F");

}

function idag()
{
    back_add("idag()");
    VisaBilar=1;
    reset_view();
    query_all_agents();
}

/*
 * The user may want to close the info-win if its in the way.
 */
function info_hide()
{
    VisaBilar=0;
    var info = document.getElementById('dinfo'); 
    info.style.display="none";
    wresize();
}

/*
 * Updates the info-window with content.
 */
function info_set(content)
{
    VisaBilar=0;
    var dinfo = document.getElementById('dinfo'); 
    //var text = "";
    //text += "<img align='right' style='margin-right: 4px; margin-top: 0px;' src='images/close.gif' onclick='info_hide();' title='Dölj'>";
    //text += content;
    dinfo.innerHTML=content;
    dinfo.scrollTop=0;
    dinfo.style.display="";
    wresize();
    correctPNG();
}

/*
 * Just show the flag-descption only when you zoom in.
 */
function toggle_desc()
{
    var z=gmap.getZoom();
    var d = document.getElementById('flaggdesc'); 
    if(z>12)
    {
	d.style.display="inline";
    } else {
	d.style.display="none";
    }
}
