﻿var map = null;
var pinid = 0;
var HotelIcon = "<div class='MapIcon' onmouseover='this.parentNode.style.marginRight=\"27px\";'><img src='/common/img/ski/MapIcon1.gif' /></div><div class='MapIconPH' onmouseover='this.parentNode.style.marginRight=\"27px\";'></div>";
var PlaneIcon = "<div class='MapIcon' onmouseover='this.parentNode.style.marginRight=\"27px\";'><img src='/common/img/ski/MapIcon2.gif' /></div><div class='MapIconPH' onmouseover='this.parentNode.style.marginRight=\"27px\";'></div>";
var ClustPlaneIcon = "<div class='MapIcon' onmouseover='this.parentNode.style.marginRight=\"50px\";'><img src='/common/img/ski/ClusterIconAirport.gif' /><div class='ClustNum'>{-Num-}</div></div><div class='MapIconPH' onmouseover='this.parentNode.style.marginRight=\"50px\";'></div>";
var ClustHotelIcon = "<div class='MapIcon' onmouseover='this.parentNode.style.marginRight=\"50px\";'><img src='/common/img/ski/ClusterIconResort.gif' /><div class='ClustNum'>{-Num-}</div></div><div class='MapIconPH' onmouseover='this.parentNode.style.marginRight=\"50px\";'></div>";
var ClustBothIcon = "<div class='MapIcon' onmouseover='this.parentNode.style.marginRight=\"50px\";'><img src='/common/img/ski/ClusterIconBoth.gif' /><div class='ClustNum'>{-Num-}</div></div><div class='MapIconPH' onmouseover='this.parentNode.style.marginRight=\"50px\";'></div>";

var bolAllVisible = false;
function GetMap()
{// Initialises the map  
    var startZoom = 4; 
           
    map = new VEMap('myMap');         
    map.SetDashboardSize(VEDashboardSize.Small);
    map.LoadMap();
    map.SetCenterAndZoom(new VELatLong(45.5, 9), startZoom);
    map.AttachEvent('onendzoom', zoomMap);
    doCluster( startZoom );
}
function zoomMap()
{// Handles the map zoom event
    doCluster( map.GetZoomLevel() );
}
function doCluster( zoomLvl )
{// Works out whether to cluster or not
    if( parseInt(zoomLvl) >= 10 && bolAllVisible == false )
    {
        map.DeleteAllShapes();
        AddEasyJetResorts();
        AddEasyJetAirports();
        bolAllVisible = true;
    } else if( parseInt(zoomLvl) < 10 )
    {
        map.DeleteAllShapes();
        addEasyJetCluster(zoomLvl);
        bolAllVisible = false;
    }
}
function addEasyJetCluster(zoomLvl)
{// Adds the correct cluster for each zoomLvl level

    for(var i in arrClusters){
        if(arrClusters[i]._ZoomLevel==zoomLvl){
            AddEasyJetClust(arrClusters[i]._NumAirports,arrClusters[i]._NumResorts, arrClusters[i]._LatLong,arrClusters[i]._Show,arrClusters[i]._Name );
        }
    }
   
    AddSingleResorts(zoomLvl);
    AddSingleAirports(zoomLvl);
}

function AddEasyJetResorts()
{// Adds icons for all the Easy Jet ski resorts       
    for(var i in arrResorts){
        AddSingleResort(i)
    }
}
function AddSingleResorts(zoomlvl)
{
    for(var i in arrResorts){
        if(arrResorts[i]._ZoomLevel<=zoomlvl)
        {
            AddSingleResort(i);
        }
    }

}
function AddSingleResort(i)
{// Adds icon for a single resort. (i) is the position in the hotel array        
    var infobox;
    var shape;
    
    var resort=arrResorts[i];
    
    shape = new VEShape(VEShapeType.Pushpin, resort._LatLong);         
    //Set the icon         
    shape.SetCustomIcon(HotelIcon);                  
    //Set the info box         
    map.ClearInfoBoxStyles();         
    shape.SetTitle("");

    var airports="";
    for(var a in resort._Airports){
        if(airports!="") airports+= " / ";
        airports+=resort._Airports[a];
    }

    infobox = "<div style='left:-40px;top: -90px; border:solid 1px #eee; position: absolute;width:149px;padding-bottom:5px;padding-left:2px; padding-right:2px;background-color: #ffffff;text-align: left;'>";
    infobox += "    <img src='"+resort._Picture+"' style='position: relative; float: left;' />";
    infobox += "    <div style='position: relative; float: left; margin-left: 7px; margin-top: 7px;'>";
    infobox += "        <p style='font-family: Arial; cursor:pointer; color:#818688; font-size: 105%; font-weight: bold;' onclick='javascript:document.location=\""+resort._Link+"\"'>"+resort._Name+"</p>";
    infobox += "        <p style='font-family: Arial;color: #FF6600; font-size: 100%; font-weight: bold;'><span style='color:#acacac;'>" + sResortHeight+ "</span>"+resort._Height+"<br />"+resort._Details+"<br /><span style='color:#acacac;'>"+sClosest+"</span>"+airports+"</p>";
    infobox += "    </div>";
    infobox += "</div>";
    shape.SetDescription(infobox);                 
    //Add the shape the the map         
    map.AddShape(shape);         
    pinid++;
}

function AddEasyJetAirports()
{// Adds all the EasyJet ski airports     
    for(var i in arrAirports)
    {
        AddSingleAirport(i);
    }
}
function AddSingleAirports(zoomlvl)
{
    for(var i in arrAirports){
        if(arrAirports[i]._ZoomLevel<=zoomlvl)
        {
            AddSingleAirport(i);
        }
    }
}  
function AddSingleAirport(code)
{// Adds a single airport. (i) is the position in the airports array       
    var infobox;
    var shape;
    
    var airport=arrAirports[code];
    
    shape = new VEShape(VEShapeType.Pushpin, airport._LatLong);         
    //Set the icon         
    shape.SetCustomIcon(PlaneIcon);                  
    //Set the info box         
    map.ClearInfoBoxStyles();         
    shape.SetTitle("");
    infobox = "<div style='left:-40px; top: -90px; border:solid 1px #eee; position: absolute; width:149px; padding-bottom:5px;background-color: #ffffff; text-align: left;'>";
    infobox += "    <img src='"+airport._Picture+"' style='position: relative; float: left;' />";
    infobox += "    <div style='position: relative; float: left; margin-left: 9px; margin-top: 9px;'><img src='/common/img/ski/MapLink.jpg' style='cursor:pointer;' onclick='javascript:document.location=\""+airport._Link+"\"' /></div>";
    infobox += "    <div style='position: relative; float: left; margin-left: 7px; margin-top: 7px;'>";
    infobox += "        <p style='font-family: Arial; cursor:pointer; color:#999999; font-size: 115%; font-weight: bold;' onclick='javascript:document.location=\""+airport._Link+"\"'>Fly to<br /><span style='font-family: Arial;color: #f26721; font-size: 125%; font-weight: bold;'>"+airport._Name+"</span><br />";
    infobox += "        From<br /><span style='font-family: Arial;color: #f26721; font-size: 125%; font-weight: bold;'>"+airport._Details+"</span></p>";
    infobox += "    </div>";
    infobox += "</div>";
    shape.SetDescription(infobox);                 
    //Add the shape the the map         
    map.AddShape(shape);         
    pinid++;
}     
function AddEasyJetClust( numAirports, numResorts, position, type, name )
{// Adds a cluster icon      
    var infobox;
    var shape;
    var icon = '';
    var numInClust=numAirports+numResorts;
    var sAirports=numAirports==1?'airport':'airports';
    var sResorts=numResorts==1?'resort':'resorts';
    
    switch( type.toLowerCase() )
    {
        case 'both':
            icon = ClustBothIcon.replace('{-Num-}', numInClust.toString());
            infoTxt = numAirports.toString()+' '+sAirports+' and '+numResorts.toString()+' '+sResorts+' ';
            break;
        case 'airport':
            icon = ClustPlaneIcon.replace('{-Num-}', numInClust.toString());
            infoTxt = numInClust.toString()+' '+sAirports+' ';
            break;
        case 'resort':
            icon = ClustHotelIcon.replace('{-Num-}', numInClust.toString());
            infoTxt = numInClust.toString()+' '+sResorts+' ';
            break;
    }

    shape = new VEShape(VEShapeType.Pushpin, position);         
    //Set the icon         
    shape.SetCustomIcon(icon);                  
    //Set the info box         
    map.ClearInfoBoxStyles();         
    shape.SetTitle('');
    infobox = "<div style='left:-80px;top:-30px; border:solid 1px #eee;position: absolute; width:149px; height:85px;padding: 5px 5px 5px 5px;background-color: #ffffff; text-align: left;'>";
    infobox += "    <strong>"+name+"</strong>";
    if(numAirports==1){
        infobox += "    There is "+infoTxt+" in this cluster.";
    }else{
        infobox += "    There are "+infoTxt+" in this cluster.";
    }
    infobox += "    <br /><br />Double click on the icon to zoom in further.";
    infobox += "</div>";
    shape.SetDescription(infobox);                 
    //Add the shape the the map         
    map.AddShape(shape);         
    pinid++;
} 

function Airport(sCode, sName, dLat, dLong, sPic, sLink, iZoomLevel, sPrice){
      this._Code=sCode;
      this._Name=sName;
      this._LatLong=new VELatLong(dLat, dLong);
      this._Picture=sPic;
      this._Link=sLink;
      this._Details=sPrice;
      this._ZoomLevel=iZoomLevel;
}

function Resort(sCode, sName, dLat, dLong, sPic, sLink,sDetails,sHeight,iZoomLevel){
      this._Code=sCode;
      this._Name=sName;
      this._LatLong=new VELatLong(dLat, dLong);
      this._Picture=sPic;
      this._Link=sLink;
      this._Details=sDetails;
      this._Height=sHeight;
      this._ZoomLevel=iZoomLevel;
      this._Airports=new Object();
      
      Resort.prototype.add=function (code,name){
        this._Airports[code]=name;
      }
}  


function Cluster(sCode, sName, dLat, dLong, sShow, iNumAirports, iNumResorts ,iZoomLevel){
      this._Code=sCode;
      this._Name=sName;
      this._LatLong=new VELatLong(dLat, dLong);
      this._Show=sShow;
      this._NumAirports=iNumAirports;
      this._NumResorts=iNumResorts;
      this._ZoomLevel=iZoomLevel;
} 