﻿// JScript File

var localSearch = new GlocalSearch();
var map;
var icon = new GIcon();
icon.image = "http://www.jigsawconferences.co.uk/images/bed_icon.png";
icon.iconSize = new GSize(44, 42);
icon.shadow = null;
icon.iconAnchor = new GPoint(22, 21);
icon.infoWindowAnchor = new GPoint(22, 1);




var homeIcon = new GIcon();
homeIcon.image = "http://www.jigsawconferences.co.uk/images/venue_icon.png";
homeIcon.iconSize = new GSize(44, 42);
homeIcon.shadow = null;
homeIcon.iconAnchor = new GPoint(22, 21);
homeIcon.infoWindowAnchor = new GPoint(22, 1);
            
            
            
var points = new Array();  
var directions;
var loadedHotels = 0;


String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); } 

function Rating(title, count, image) {
    this.title = title;
    this.count = count;
    this.image = image;


}

function RateCard(name, description, postcode, ref) {
   this.name = name;
   this.description = description;
   this.postcode = postcode;
   this.ref = ref;

    this.getDirections = function() {
        var obj = this;
        var postcode = document.getElementById('inputPostCode').value;
        usePointFromPostcode(postcode, function(point) {obj.addDirections.apply(obj, new Array(point))});
    };
    
    this.addDirections = function(point) {
        map.getInfoWindow().hide();
        var directionsDiv = $('divMapDirections');
        var infoDiv = $('divMapInfo');
        directionsDiv.style.display = 'block';
        
        infoDiv.style.display = 'none';
        directions = new GDirections(map, document.getElementById('divGoogleDirections'));
                var query = "from: 53.43, -3.06 to: " + this.point.toString();
                var query = "from: " + point.lat() + ", " + point.lng() + " to: " + this.point.lat() + ", " + this.point.lng();
                directions.load(query, { "locale": "en_GB" });
        obj = this;
        document.getElementById('divCloseDirections').onclick = function() {obj.resetView.apply(obj);};
    };
    
    this.resetView = function() {   
        var directionsDiv = $('divMapDirections');
        var infoDiv = $('divMapInfo');
        directionsDiv.style.display = 'none';
        infoDiv.style.display = 'block';
        directions.clear();
        setBoundsToMarkers();
    }
    
}
    
RateCard.prototype.addPlaceDetails = function() {
        var div = new Element('div');
        div.setProperty("class", "divPlaceName");
        var link = document.createElement('p');
        var img = document.createElement('img');
        
        img.src = this.icon.image;
        GEvent.bindDom(div, "click", this, this.showInfoWindow);
        div.onmouseover = function() {div.style.backgroundImage = "url('../images/map/map_bg_ovr.gif')";};
        div.onmouseout = function() {div.style.backgroundImage = "none";};
        link.appendChild(img);
        link.innerHTML += "<span>"+this.name+"</span>";
        
        div.appendChild(img);
        div.innerHTML += this.name;
        $('divPlaces').appendChild(div);
}


RateCard.prototype.addMarkerAtPoint = function(point) {
        this.marker = new GMarker(point, this.icon);
        this.point = point;
        
        map.addOverlay(this.marker);
        GEvent.bind(this.marker, "click", this, this.showInfoWindow);
        this.addPlaceDetails();
        //this.showInfoWindow();
        points.push(point);
        //addNextVenue();
}


RateCard.prototype.showInfoWindow = function() {
         //alert("hi");
         var html = "<div style='width: 300px'><b>" + this.name + "</b><br/>";
         html += this.description;
         
         
         
         html += "<hr/>";
         html += "<b>Enter your postcode</b><br/>";
         html += "<input id='inputPostCode'/>";
         html += "<a href='#' id='linkDirections'>Get Directions</a></div>";
         this.marker.openInfoWindowHtml(html);
         var obj = this;
         document.getElementById('linkDirections').onclick = function() {obj.getDirections.apply(obj); return false;};

};

RateCard.prototype.icon = homeIcon;








    
Hotel.prototype = new RateCard();  
Hotel.prototype.constructor = Hotel; 
Hotel.prototype.parent = RateCard.prototype; 
Hotel.prototype.icon = icon;        

function Hotel(id, name, address1, address2, town, county, country, postcode, proximity, rate, ratings) {
    this.postcode = postcode;
    this.id = id;
    this.name = name;
    this.address1 = address1;
    this.address2 = address2;
    this.town = town;
    this.county = county;
    this.country = country;
    this.proximity = proximity;
    this.rate = rate;
    this.marker;
    this.ratings = ratings;



    this.showInfoWindow = function() {
         //alert("hi");
         var html = "<b>" + this.name + "</b>";
         html += "<p>";
         html += this.address1  + "<br/>";
         if (this.address2.trim() != "")
            html += address2 + "<br/>";
            
         if (this.town.trim() != "")
            html += town + "<br/>";
            
         if (this.county.trim() != "")
            html += county + "<br/>";
            
         if (this.country.trim() != "")
            html += country + "<br/>";
            
         
         html += postcode + "<br/>";
         
         
         html += "<b>Booking Hotline:</b> +44 (0)800 158 44 00<br/>";
         for (i = 0; i < this.ratings.length; i++) {
            html += "<b>" + this.ratings[i].title + ":</b>";
            for (z = 0; z < this.ratings[i].count; z++) {
                html += "<img src='../images/" + this.ratings[i].image + "'/>";
            
            }
            html += "<br/>";
         
         }
         
         html += "<b>Proximity to Venue:</b> " + this.proximity + "<br/>";
         html += "<b>Discounted Rate:</b> " + this.rate + "<br/>";
         html += "<a href='https://www.jigsawconferences.co.uk/DotCom/BookingForm.aspx?vid=" + this.id + "&rcref=" + rateCard.ref + "'><img style='margin: 4px auto;display: block' src='../images/button-book.png'/></a>";
         html += "<hr/>";
         html += "<b>Enter your postcode</b><br/>";
         html += "<input id='inputPostCode'/>";
         html += "<a href='#' id='linkDirections'>Get Directions</a>";
         this.marker.openInfoWindowHtml(html, {maxWidth:340});
         var obj = this;
         document.getElementById('linkDirections').onclick = function() {obj.getDirections.apply(obj); return false;};

    };
    
  
    
    
    
    
}


Hotel.prototype.addMarkerAtPoint = function(point) {
        this.parent.addMarkerAtPoint.apply(this, [point]);
        addNextVenue();
   
};




     
    function load() {
     
        

// Display the map centered on that location with zoom level 3.
// Include your application ID. 
if (GBrowserIsCompatible()) {
        var divMapHolder = $('divMapHolder');
        divMapHolder.style.display = "block";
        divMapHolder.style.top = (window.getHeight() / 2) - (divMapHolder.getSize().size.y / 2) + "px";
        divMapHolder.style.left = (window.getWidth() / 2) - (divMapHolder.getSize().size.x / 2) + "px";
        if (loadedHotels == 1) {
            return;
        }
        $('divCloseWindow').onclick = function() {divMapHolder.style.display = 'none';};
        GEvent.addDomListener($('linkResetView'), "click", function() {map.getInfoWindow().hide(); setBoundsToMarkers();});
        map = new GMap2(document.getElementById("divMap"));
        map.addControl(new GSmallMapControl());

        //map.setCenter(new GLatLng(37.4419, -122.1419), 13);
        usePointFromPostcode(rateCard.postcode, function(point) {
            
            map.setCenter(point, 13);
            

            //icon.image = "http://www.google.com/intl/en_ALL/mapfiles/kml/shapes/schools_maps.png";
            //icon.iconSize = new GSize(32, 32);

            
            
            rateCard.addMarkerAtPoint(point)
            //rateCard.addPlaceDetails();
            addNextVenue();
            
        
        });
        
        

        
      }

    }
    
   
   
   function addNextVenue() {
        if (hotels.length != 0) {
        var hotel = hotels.pop();
       
        usePointFromPostcode(hotel.postcode, function(point) {
            hotel.addMarkerAtPoint(point)
        });
        }else {
        
        setBoundsToMarkers();
        loadedHotels = 1;
        
        
        }
        
   }
   
   function setBoundsToMarkers() {
        var bounds = new GLatLngBounds();
        
        for (i = 0; i < points.length; i++) {
            bounds.extend(points[i]);
        
        
        }
        
        
        
        var center = bounds.getCenter();
        

        map.setZoom(map.getBoundsZoomLevel(bounds));

        map.setCenter(center); 
        $('divLoading').style.display = 'none';
        
   
   }


function usePointFromPostcode(postcode, callbackFunction) {
  
  localSearch.setSearchCompleteCallback(null,
    function() {
      
      if (localSearch.results[0]) {    
        var resultLat = localSearch.results[0].lat;
        var resultLng = localSearch.results[0].lng;
        var point = new GLatLng(resultLat,resultLng);
        callbackFunction(point);
      }else{
        alert("Postcode not found!");
      }
    });  
    
  localSearch.execute(postcode + ", UK");
}

function listPlaces() {




}
