﻿// JScript File
var img_normal = "images/icon/marker32_normal.png";
var img_orange = "images/icon/marker32_orange.png";
var img_green = "images/icon/marker32_green.png";
var img_red = "images/icon/marker32_red.png";
var img_country = "images/icon/marker_country.png";

// convert all characters to lowercase to simplify testing
var agt=navigator.userAgent.toLowerCase();

// *** BROWSER VERSION ***
// Note: On IE5, these return 4, so use is_ie5up to detect IE5.
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);
var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie3    = (is_ie && (is_major < 4));
var is_ie4    = (is_ie && (is_major == 4) && (agt.indexOf("msie 4")!=-1) );
var is_ie4up  = (is_ie && (is_major >= 4));
var is_ie5    = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")!=-1) );
var is_ie5_5  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.5") !=-1));
var is_ie5up  = (is_ie && !is_ie3 && !is_ie4);
var is_ie5_5up =(is_ie && !is_ie3 && !is_ie4 && !is_ie5);
var is_ie6    = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) );
var is_ie6up  = (is_ie && !is_ie3 && !is_ie4 && !is_ie5 && !is_ie5_5);

var infoDivCreated = "0";
var infoDiv;
var highlightCircle;
var currentMarker;
var currentStyle;
var offSetX = 310;
var offSetY = 25;
var map;

var markerslist = [];
var markerstextlist = [];
var markerstypelist = [];
var markersurllist= [];

function ChangeOffSet(x, y)
{
    offSetX = x;    
    offSetY = y;
}

function initInfoDiv()
{    
    infoDiv = document.getElementById("info_div");
}

function init() {
  if(jQuery.browser.msie)
  {
    if(jQuery.browser.version.substr(0,3) < 8.0)
    {
        if (window.Event)
        {
            document.captureEvents(Event.MOUSEMOVE);
        }
     }
  }
  set_images_location_path();
  document.onmousemove = getPosition;
}

function set_images_location_path(){

    img_normal = static_vinivi_host_url + img_normal;
    img_green = static_vinivi_host_url + img_green;
    img_country = static_vinivi_host_url + img_country;
    img_orange = static_vinivi_host_url + img_orange;
    img_red = static_vinivi_host_url + img_red;
}

function loadMap(url, gmap)
{
    GDownloadUrl(url, function(data, responseCode) {        
        var xml = GXml.parse(data);        
        var placemarks = xml.documentElement.getElementsByTagName("Placemark");
        //Parse each placemark        
        for (var i = 0; i < placemarks.length; i++) {
            var placemark = placemarks[i];     
            placemark.normalize();
            var IdData = placemark.getAttribute("id").split("|");
            var markerURL = IdData[1];
            var type;            
            var text;
            var point
            
            if(is_ie)      
            {
                type = placemark.childNodes[1].firstChild.nodeValue;
                text = placemark.childNodes[0].firstChild.nodeValue;
                point = placemark.childNodes[2].childNodes[0].firstChild.nodeValue;            
            }
            else
            {
                type = placemark.childNodes[3].firstChild.nodeValue;
                text = placemark.childNodes[1].firstChild.nodeValue;
                point = placemark.childNodes[5].childNodes[1].firstChild.nodeValue;                                            
            }            
            var coordinates = point.split(",");
            createMarker(type,markerURL,text,coordinates[0], coordinates[1],IdData, i);                        
        }
        
        
        $(markerslist).each(function(i,marker){
        
            GEvent.addListener(marker, "mouseover", function() {   
                var markerOffset = map.fromLatLngToDivPixel(marker.getLatLng());
                showInfoWindowWithCoord(markerstextlist[i], markerOffset.x, markerOffset.y);

                currentStyle = markerstypelist[i];
                currentMarker = marker;
                highlightCurrentMarker();
            
            });
            
             GEvent.addListener(marker, "click", function() {                
                    window.location = markersurllist[i];
             });
         
            GEvent.addListener(marker, "mouseout", function() {
                map.removeOverlay(highlightCircle);   
                
                $("#info_div").mouseover(function(){
                     map.addOverlay(highlightCircle);
                     
                });
            
                $("#info_div").mouseout(function(){
                   map.removeOverlay(highlightCircle);
                   
                });
                
                                      
              });
	    });
    });
}


function createMarker(type, url, text, longitude, latitude,idPlace, loop)
{
    var icon = getIcon(type);
    var latlng = new GLatLng(latitude, longitude);
    var marker;
    
    
    
    if(type == "#countryStyle")
        marker = new GMarker(latlng,{title: text, icon: icon});
    else
    {
        marker = new GMarker(latlng, icon);        
        markerslist[loop] = marker;
        markerstextlist[loop] = text;
        markerstypelist[loop] = type;
        markersurllist[loop] = url;
    }
    map.addOverlay(marker);
}
function showQtip()
{
    $(document).ready(function(){
     $('id').each(function() {
         $(this).qtip({
                                
            content: {text:infoDiv.innerHTML},                               
            style: { name: 'light', tip: 'topMiddle', width: { min: 250 } },
            position: {
            corner: {
                      tooltip: 'topMiddle', // Use the corner...
                      target: 'bottomMiddle' // ...and opposite corner
                     }
                     }
                       
              });     
         });
    });

}

function showInfoWindow(text)
{
    if(infoDivCreated == "1")
    {        
        
        infoDiv.style.visibility = "visible";
        //var divLeft = document.getElementById("x").value - offSetX;
        //var divTop = ((document.getElementById("y").value*1)+(offSetY*1));    
        //alert("Left: " + divLeft + " Top: " + divTop);
        infoDiv.style.top = divTop + "px";
        infoDiv.style.left = divLeft + "px";
        infoDiv.innerHTML = text;  
    }
}

function showInfoWindowWithCoord(text, xCoord, yCoord)
{
   
    if(infoDivCreated == "1")
    { 
       
        $(infoDiv)
            //.fadeIn('slow')
            .css({ top:yCoord + 250, left:xCoord, visibility:"visible" })
            .html(text);
        //infoDiv.style.visibility = "visible";
        //infoDiv.style.top = (yCoord + 250) + "px";
        //infoDiv.style.left = xCoord + "px";
        //infoDiv.innerHTML = text;  
    }
}


function getPosition(e) {
    e = e || window.event;
    var cursor = {x:0, y:0};
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    } 
    else {
        var de = document.documentElement;
        var b = document.body;
        cursor.x = e.clientX + 
            (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
        cursor.y = e.clientY + 
            (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
    }
    document.getElementById("x").value = cursor.x;
    document.getElementById("y").value = cursor.y;
}

function getIcon(type)
{
    var icon = new GIcon();
    icon.image = getIconPath(type);        
    if(type == "#countryStyle")
    {
        icon.iconSize = new GSize(12, 20);
        icon.iconAnchor = new GPoint(6, 20);
    }
    else
    {
        icon.iconSize = new GSize(32, 32);
        icon.iconAnchor = new GPoint(6, 20);
        icon.infoWindowAnchor = new GPoint(5, 1);        
    }
    return icon;
}

function getIconPath(type)
{
    switch(type)
    {
        case "#countryStyle":
            return img_country;
        case "#normalStyle":
            return img_normal;        
        case "#redStyle":
            return img_red;        
        case "#orangeStyle":
            return img_orange;        
        case "#greenStyle":
            return img_green;        
    }
}


function highlightCurrentMarker(){
  var markerPoint = currentMarker.getPoint();
  
  var polyPoints = Array();  
  //map.removeOverlay(highlightCircle);
  
  var mapNormalProj = G_NORMAL_MAP.getProjection();  
  var mapZoom = map.getZoom();  
  var clickedPixel = mapNormalProj.fromLatLngToPixel(markerPoint, mapZoom);
  var polySmallRadius = 20;
  
  if(currentStyle == "#countryStyle")  
    polySmallRadius = 10;
  

  var polyNumSides = 20;
  var polySideLength = 18;
  
  for (var a = 0; a<(polyNumSides+1); a++) {
    var aRad = polySideLength*a*(Math.PI/180);    
    var polyRadius = polySmallRadius; 
    
   	var pixelX;
   	var pixelY;
    if(currentStyle == "#countryStyle")  
    {
        pixelX = clickedPixel.x + polyRadius * Math.cos(aRad);
        pixelY = clickedPixel.y + polyRadius * Math.sin(aRad);
    }
    else
    {
        pixelY = clickedPixel.y + polyRadius * Math.sin(aRad);
        pixelX = 5 + clickedPixel.x + polyRadius * Math.cos(aRad);
    }
         	
    //var pixelY = clickedPixel.y + polyRadius * Math.sin(aRad);
    var polyPixel = new GPoint(pixelX,pixelY);
    var polyPoint = mapNormalProj.fromPixelToLatLng(polyPixel,mapZoom);
    polyPoints.push(polyPoint);
  }  
   
  highlightCircle = new GPolygon(polyPoints,"#000000",2,0.0,"#FF0000",.5);    
   
  map.addOverlay(highlightCircle);
}
