//  var url = "http://www.alphakanal.de:8080/locme/";
//  var url = "http://localhost:8080/locme/";

var url = "http://snapshot.alphakanal.de/";

function snapshot_display(id)
{
    var html = "";

    if (snapshot_data.length)
    {
        for (var i = 0; i < snapshot_data.length; i++)
        {
            var user	= snapshot_data[i];
            var	link	= url + "?key=" + user.key;

            //	html += snapshot_buildImageTag(user, "", link, "", true, snapshot_buildInfoTag(user, "inline"));
            html += snapshot_buildImageTag(user, "", link, "", true, null);
        }
    }
    else if (snapshot_data.key)
    {
        var user    = snapshot_data;
        var link    = "http://blog.alphakanal.de/category/snapshot";

        html = snapshot_buildImageTag(user, "", link, "", false, snapshot_buildInfoTag(user, "inline"));
    }

    document.getElementById(id).innerHTML = html;
}

function snapshot_display_small(id)
{
    var	html = "";
	
    html += snapshot_buildImageTag(snapshot_data, "hoverMe", "", "hidden", false, "");

    html += "<table>";
    html += "<tr><td>" + snapshot_buildThumbTag(snapshot_data, "", "", "thumb", true) + "</td><td>" + snapshot_buildInfoTag(snapshot_data, "") + "</td></tr>";
    html += "</table>";

    document.getElementById(id).innerHTML = html;
}

function snapshot_buildThumbTag(user, id, link, css, action)
{
    var html		= "";
    var key		= user.key;
	
    var	myId		= (id && id != "") ? "id='" + id + "' " : "";
    var	myAction	= action ? " onmouseover='snapshot_hover(\"hoverMe\", true)' onmouseout='snapshot_hover(\"hoverMe\", false)'" : "";
		
    html += "<span class='thumbBox'>";
    //  html += "<img " + myId + "class='" + css + "' src='" + url + "image?key=" + key + "_1' width='57px' height='57px'"+ myAction + ">";
    html += "<img " + myId + "class='" + css + "' src='http://www.alphakanal.de/snapshot/" + key + "_1' width='57px' height='57px'"+ myAction + ">";

    if (link && link != "")
        html += "<a href='" + link + "'><span class='snapshot_info'></span></a>"
		
    html += "</span>";
    
    return html;
}

function snapshot_buildInfoTag(user, css)
{
    var	html	= "";

    var	latlong	= user.latitude + "," + user.longitude;
    var gmaps   = "http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=" + user.name + "%40" + latlong + "&z=12";
	
    html += "<table class='" + css + "'>";
    html += "<tr><td>user</td><td> : " + user.name + "</td></tr>";
    html += "<tr><td>time</td><td> : " + user.loctime + "</td></tr>";
    html += "<tr><td>maps</td><td> : <a href='" + gmaps + "'>" + latlong + "</a></td></tr>";

    if (user.message != "")
        html += "<tr><td>talk</td><td> : " + user.message + "</td></tr>";

    html += "</table>";
	
    return html;
}

function snapshot_buildImageTag(user, id, link, css, action, info)
{
    var html		= "";
    var key		= user.key;
    var	latlong		= user.latitude + "," + user.longitude;
	
    var	myId		= (id && id != "") ? "id='" + id + "' " : "";
    var	myAction	= action ? " onclick='snapshot_flip(this,\"" + key + "\",\"" + latlong + "\")'" : "";
		
    html += "<span class='imageBox'>";
    //  html += "<img " + myId + "class='" + css + "' src='" + url + "image?key=" + key + "_0' width='320px' height='320px'"+ myAction + ">";
    html += "<img " + myId + "class='" + css + "' src='http://www.alphakanal.de/snapshot/" + key + "_0.png' width='320px' height='320px'"+ myAction + ">";

    if (link && link != "")
        html += "<a href='" + link + "'><span class='link'></span></a>"
	
    if (info && info != "")
        html += info;
		
    html += "</span>";
    
    return html;
}

function snapshot_flip(object, key, latlong)
{
    //  get your own key from google at http://code.google.com/apis/maps/signup.html.
    //  this one will only work with http://snapshot.alphakanal.de/ as host.
    var google_key = "ABQIAAAAkFW8apn2CX92NK5g8APKmRQ_jtKYgEtJ90AFCz8_LZvG3KhNVRQZS6CQF-zkFXVj2XtztfnQ0NQSGw";

    //  reverse geocoding
    //  "http://maps.google.com/maps/geo?q=" + latlong + "&output=json&oe=utf8&sensor=true_or_false&key=" + google_key;

    if (object.src.indexOf("staticmap?") != -1)    
        object.src = "http://www.alphakanal.de/snapshot/" + key + "_0";
    else
       object.src = "http://maps.google.com/staticmap?maptype=mobile&center=" + latlong + "&markers=" + latlong + ",midred&zoom=14&size=320x320&key=" + google_key;
}

function snapshot_hover(id, flag)
{
    var object = document.getElementById(id);
    object.style.display = flag ? "inline" : "none";
}
