//<![CDATA[
/* A lot of the code is taken from other tutorials. */
//var map;
//var minimap;
var mapTypeControl;
var topRight;
var zoomlevel;
var id;
var index = -1;
var gmarkers = [];
var idmarkers = [];
var xhair;
var map_moving = 0;
var minimap_moving = 0;


var CAT_ICONS = [];
CAT_ICONS["DEFAULT_ICON"] = tinyIcon("red");
CAT_ICONS["filial"] = tinyIcon("green");

var Zonas = new Array();
//Zonas["CATALUNYA"] = [41.013066,2.021484, 7];
Zonas["CATALUNYA"] = [41.104191,2.449951,7];
//Zonas["CATALUNYA"] = [40.979898,2.625732, 7];

/**
* Marker icon
*/
function tinyImage(opt_color, opt_preload) {
    var color = opt_color || "red";
    var src_ = "http://labs.google.com/ridefinder/images/mm_20_" + color + ".png";
    if (opt_preload) {
        var preImage = new Image();
        preImage.src = src_;
    }
    return src_;
}

function tinyIcon(opt_color) {
    var tiny = new GIcon();
    tiny.image = tinyImage(opt_color);
    tiny.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
    tiny.iconSize = new GSize(12, 20);
    tiny.shadowSize = new GSize(22, 20);
    tiny.iconAnchor = new GPoint(6, 20);
    tiny.infoWindowAnchor = new GPoint(5, 1);
    tiny.imageMap = [4, 0, 0, 4, 0, 7, 3, 11, 4, 19, 7, 19, 8, 11, 11, 7, 11, 4, 7, 0];
    tiny.transparent = "http://maps.google.com/mapfiles/transparent.png?bogus=.png";
    return tiny;
}

var icons = ["black", "brown", "red", "orange", "yellow", "green", "blue", "purple", "gray", "white"];
for (var color in icons) {
    tinyImage(icons[color], true);
}

// create the crosshair icon, which will indicate where we are on the minimap
// Lets not bother with a shadow
var Icon = new GIcon();
Icon.image = "xhair.png";
Icon.iconSize = new GSize(21, 21);
Icon.shadowSize = new GSize(0, 0);
Icon.iconAnchor = new GPoint(11, 11);
Icon.infoWindowAnchor = new GPoint(11, 11);
Icon.infoShadowAnchor = new GPoint(11, 11);

var blueIcon = new GIcon(G_DEFAULT_ICON);
blueIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png";
markerOptions = {
    icon: blueIcon
};
blueIcon.iconSize = new GSize(32, 32);


// ====== Handle the Map movements ======
// Variables that log whether we are currently causing the maps to be moved
// This function handles what happens when the main map moves
// If we arent moving it (i.e. if the user is moving it) move the minimap to match
// and reposition the crosshair back to the centre
/*function Move() {
    minimap_moving = true;
    if (map_moving == false) {
        minimap.setCenter(map.getCenter());
        xhair.setPoint(map.getCenter());
        xhair.redraw(true);
    }
    minimap_moving = false;
}
// This function handles what happens when the mini map moves
// If we arent moving it (i.e. if the user is moving it) move the main map to match
// and reposition the crosshair back to the centre
function MMove() {
    map_moving = true;
    if (minimap_moving == false) {
        map.setCenter(minimap.getCenter());
        xhair.setPoint(minimap.getCenter());
        xhair.redraw(true);
    }
    map_moving = false;
}*/

function createMarker(point, tab1title, tab2title, canaltitle, canaltitlelink, canal_tienda, id, marker_id, name, address_title, address, email, phone, fax, txt_videos, videos, url, descripcion, tipo, idioma)
{
    if (tipo == "filial") {
        var marker = new GMarker(point, {
            icon: CAT_ICONS[tipo] || CAT_ICONS["DEFAULT_ICON"],
            title: name
        });
        var htmltab1 = "<div class='tabMapContent'><div class='nom'>" + name + "</div><div><p>" +address.replace(/_br_/g, '<br/>') + "</p>";
	if (phone != '') htmltab1 = htmltab1 + "<p><strong>Tel:</strong> "+ phone + "</p>";
	if (fax != '') htmltab1 = htmltab1 + "<p><strong>Fax:</strong> "+ fax + "</p>";
	if (email != '') htmltab1 = htmltab1 +" <p><strong>E-mail:</strong> "+ email +" </p>";
	if (url != '') htmltab1 = htmltab1 +" <p> <a href='"+ url +"' target='_blank'>"+ url + "</a></strong></p>";
	htmltab1 = htmltab1 + "</div></div>";

        GEvent.addListener(marker, "click",
        function() {
            marker.openInfoWindowHtml(htmltab1);
        });
    }
    // save the info we need to find this marker
    gmarkers.push(marker);
    idmarkers[marker_id.toLowerCase()] = marker;
    return (marker);
}
function getMarks(url, idMarkerSelected) {
    map.closeInfoWindow();
    /*close any InfoWindows that happen to be open */
    map.clearOverlays();
    var request = GXmlHttp.create();
    /* use the XML methods */
    //alert('entro en la funcion getMarkers');
    request.open("GET", url + '&noCache=' + new Date().getTime(), true);
    request.onreadystatechange = function() {
        if (request.readyState == 4) {
            //alert(request.readyState);
            var xmlDoc = request.responseXML;
            // alert(xmlDoc);
            /* php returns 'XML' */
            var markers = xmlDoc.documentElement.getElementsByTagName("marker");
            //alert(markers.length);
            for (var loop1 = 0; loop1 < markers.length; loop1++)
            {
                var lat = parseFloat(markers[loop1].getAttribute("lat"));
                /* data is string. */
                var lng = parseFloat(markers[loop1].getAttribute("lng"));
                var point = new GLatLng(lat, lng);
                var canaltitle = markers[loop1].getAttribute("canaltitle");
                var canaltitlelink = markers[loop1].getAttribute("canaltitlelink");
                var canal_tienda = markers[loop1].getAttribute("canal_tienda");
                var id = markers[loop1].getAttribute("id");
                var marker_id = "filial_" + markers[loop1].getAttribute("id");
                var name = markers[loop1].getAttribute("name");
                var address_title = markers[loop1].getAttribute("address_title");
                var address = markers[loop1].getAttribute("address");
                var address_title = markers[loop1].getAttribute("address_title");
                var email = markers[loop1].getAttribute("email");
                var phone = markers[loop1].getAttribute("phone");
                var fax = markers[loop1].getAttribute("fax");
                var txtvideos = markers[loop1].getAttribute("txt_videos");
                var videos = markers[loop1].getAttribute("videos");
                var url = markers[loop1].getAttribute("url");
                var descripcion = markers[loop1].getAttribute("description");
                var tab1title = markers[loop1].getAttribute("tab1title");
                var tab2title = markers[loop1].getAttribute("tab2title");
                var tipo = markers[loop1].getAttribute("tipo");
                var idioma = markers[loop1].getAttribute("idioma");
                var marker = createMarker(point, tab1title, tab2title, canaltitle, canaltitlelink, canal_tienda, id, marker_id, name, address_title, address, email, phone, fax, txtvideos, videos, url, descripcion, tipo, idioma);
                map.addOverlay(marker);
            }
        }
        if (idMarkerSelected != undefined && idmarkers[idMarkerSelected]) {
            //alert(idMarkerSelected);
            GEvent.trigger(idmarkers[idMarkerSelected], "click");
        }
    };
    request.send(null);
}
function centrarMapa(zona) {
    map.setCenter(new GLatLng(Zonas[zona][0], Zonas[zona][1]), Zonas[zona][2]);
    //minimap.setCenter(new GLatLng(Zonas[zona][0], Zonas[zona][1]), 3);
}
function getMarkers(zona, idMarkerSelected) {
    getMarks('/Tau/get_marks.xml?zona=' + zona, idMarkerSelected);
}
function getVideoMarkers(id_canal) {
    getMarks('/Tau/get_marks.xml?canal=' + id_canal);
}
function showMarker(id) {
    // open the info window
    //alert('idfilial'+id);
    if (idmarkers[id]) {
        GEvent.trigger(idmarkers[id], "click");
    }
}
jQuery.noConflict();
jQuery(document).ready(function() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("googlemap"));
        map.addControl(new GMapTypeControl());
        mapTypeControl = new GMapTypeControl();
        centrarMapa("CATALUNYA");
        map.addControl(new GLargeMapControl());
        // Create the minimap 
	//minimap = new GMap2(document.getElementById("minimap"));
        //minimap.setCenter(new GLatLng(53.25, -2.35), 3);
        // Add the crosshair marker at the centre of teh minimap and keep a reference to it
        //xhair = new GMarker(minimap.getCenter(), Icon);
        //minimap.addOverlay(xhair);
        // Listen for when the user moves either map
        //GEvent.addListener(map, 'move', Move);
        //GEvent.addListener(minimap, 'moveend', MMove);
        getMarks('/get_marks.xml?filials=S');
    }
});
//]]>
