﻿
$(function () {
    $(".backtotop").click(function (event) {
        scrollWin("body", 1000);
        event.preventDefault();
    })
    $("a[href=#cities_within]").click(function (event) {
        scrollWin("#cities_within", 1000);
        event.preventDefault();
    })
    $('.sr-list .lowest_rates, .lowest_rates').click(function (e) {
        $('#lowest_rates').dialog({ 'width': 370, modal: false, title: lrg_title, dialogClass: 'facility_search_dialog' });
        _gaq.push(['_trackEvent', 'Popups', 'Open', 'Lowest Rates']);
        e.preventDefault();
    });
    $('#resultstop .lowest_rates').click(function (e) {
        $('#lowest_rates').dialog({ modal: false, title: lrg_title, dialogClass: 'facility_search_dialog' });
        _gaq.push(['_trackEvent', 'Popups', 'Open', 'Repeater Lowest Rates']);
        e.preventDefault();
    });
    $("a[href=#ratesinfo]").click(function (event) {
        $('#ratesinfo').dialog({ 'width': 370, 'height': 230, 'title': 'Room Rate Information', dialogClass: 'facility_search_dialog' });
        _gaq.push(['_trackEvent', 'Popups', 'Open', 'Repeater Rates Info']);
        event.preventDefault();
    })
    $("a[href=#seasonal_offers]").click(function (event) {
        $('#seasonal_offers').dialog({ 'width': 420, 'height': 280, 'title': 'Seasonal Offers', dialogClass: 'facility_search_dialog' });
        _gaq.push(['_trackEvent', 'Popups', 'Open', 'Repeater Seasonal Offers']);
        event.preventDefault();
    })
    $("a[href=#preferred]").click(function (event) {
        $('#preferred').dialog({ 'width': 300, 'height': 200, 'title': 'Premium Collection', dialogClass: 'facility_search_dialog' });
        _gaq.push(['_trackEvent', 'Popups', 'Open', 'Preferred Hotel']);
        event.preventDefault();
    })
    $('.ddlshow').change(function (event) {
        insertParam('show', $(this).val(), true);
    });
    $('.ddlsortby').change(function (event) {
        insertParam('sort', $(this).val(), true);
    });
    $('#eu_specials a.close').click(function (e) {
        $('#eu_specials').fadeOut();
        e.preventDefault();
    });
    $('#hotel_specials a.close').click(function (e) {
        $('#hotel_specials').fadeOut();
        e.preventDefault();
    });

    var tooltip;
    $('.sr-item .image').mousemove(
        function (e) {
            var fold = $(window).height() + $(window).scrollTop();
            var image = $(this);
            //if (image.attr('src').endsWith('default_image.jpg'))
            //    return;

            if (!tooltip) {
                tooltip = $("<div/>");
                tooltip.append(image.clone())
                tooltip.append('<div class="sr_tooltip_title">' + image.attr('alt') + '</div>');
                tooltip.css({ 'position': 'absolute', 'z-index' : '9999' }).addClass('sr_tooltip').hide().appendTo('body');
            }
            tooltip.attr('src', image.attr('src'));

            if ((e.pageY + tooltip.height()) > fold)
                tooltip.css({ 'left': e.pageX + 15, 'top': (e.pageY + 15) - tooltip.height() });
            else
                tooltip.css({ 'left': e.pageX + 15, 'top': e.pageY + 15 });

            tooltip.fadeIn(500);

        }).mouseout(function () {
            if (tooltip)
                tooltip.remove();
            tooltip = null;
        });

    $('.iframeit').click(function (e) {
        e.preventDefault();
        var iframe = $("<div/>").append($('<iframe/>').attr('src', $(this).attr('href')).css({ 'width': '100%', 'height': '100%', 'border': '0' }));
        iframe.dialog({ width: 900, height: 500, title: $(this).attr('title'), modal: true });
    });
});

function openFrame(url) {
    var iframe = $("<div/>").append($('<iframe/>').attr('src', url).css({ 'width': '100%', 'height': '100%', 'border': '0' }));
    iframe.dialog({ width: 400, height: 300, title: $(this).attr('title'), modal: false });
}

// Google maps and other functions

var premium_icon = new google.maps.MarkerImage(marker_pr,
          new google.maps.Size(26, 30), new google.maps.Point(0, 0), new google.maps.Point(0, 36));

var premium_shadow = new google.maps.MarkerImage(marker_pr_shadow,
          new google.maps.Size(30, 27), new google.maps.Point(0, 0), new google.maps.Point(-10, 32));

var superior_icon = new google.maps.MarkerImage(marker_su,
          new google.maps.Size(24, 28), new google.maps.Point(0, 0), new google.maps.Point(0, 33));

var superior_shadow = new google.maps.MarkerImage(marker_su_shadow,
          new google.maps.Size(30, 24), new google.maps.Point(0, 0), new google.maps.Point(-9, 32));

var standard_icon = new google.maps.MarkerImage(marker_st,
          new google.maps.Size(22, 26), new google.maps.Point(0, 0), new google.maps.Point(0, 32));

var standard_shadow = new google.maps.MarkerImage(marker_st_shadow,
          new google.maps.Size(27, 20), new google.maps.Point(0, 0), new google.maps.Point(-8, 32));

var shape = {
    coord: [1, 1, 1, 20, 18, 20, 18, 1],
    type: 'poly'
};

//Only one infowindow with different content
var ib;

function createMap(obj, ests, includeInfoWindows) {
    var disableDefaultUI = true;
    var scaleControl = false;

    if (includeInfoWindows) {
        disableDefaultUI = false;
        scaleControl = true;
    } 
    var myOptions = {
        disableDefaultUI: disableDefaultUI,
        scaleControl: scaleControl,
        scrollwheel: false,
        center: new google.maps.LatLng(avg_latitude, avg_longitude),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(obj, myOptions);
    
    var closeBoxMargin = "6px 4px 0 0";
    var pixelOffset = new google.maps.Size(-130, -6);
    if(ie7){
        closeBoxMargin = "0 0 -20px 260px";
        pixelOffset =  new google.maps.Size(-130, -20);
    }
    var myInfoboxOptions = {
        disableAutoPan: false
        , maxWidth: 0
        , pixelOffset: pixelOffset
        , zIndex: null
        , boxStyle: {
            width: "280px"
        }
        , infoBoxClearance: new google.maps.Size(60, 40)
        , closeBoxMargin: closeBoxMargin
        //, closeBoxURL: "http://lwtest.ds6467.dedicated.turbodns.co.uk/content/gm_close.jpg"
        , isHidden: false
        , pane: "floatPane"
        , enableEventPropagation: false

    };
    ib = new InfoBox(myInfoboxOptions);
    
    setMarkers(map, ests, includeInfoWindows);



    if (ests.length > 3) {
        var southWest = new google.maps.LatLng(min_latitude, min_longitude);
        var northEast = new google.maps.LatLng(max_latitude, max_longitude);
        var bounds = new google.maps.LatLngBounds(southWest, northEast);
        map.fitBounds(bounds);

    } else {
        map.setZoom(14);
    }

    //Set min zoom level for maps, both small and large. Must not be zoomed in too much without user input as when this occurs you cant see surrounding area
    /*google.maps.event.addListener(map, 'bounds_changed', function() {
        if (map.getZoom() >= 13) {
            map.setZoom(13);
        }
    });*/
    
}
function setMarkers(map, locations, includeInfoWindows) {
    for (var i = 0; i < locations.length; i++) {
        var est = locations[i];
        createMarker(map, est, includeInfoWindows)
    }
}

function createMarker(map, est, includeInfoWindows) {
    var name = est[0];
    var latitute = est[1];
    var longitude = est[2];
    var zIndex = est[3];
    var listingType = est[4];
    var thumbnail_image = est[5];
    var description = est[6];
    var rating_name = est[7];
    var rating_image = est[8];
    var details_link = est[9];
    var bookingurl_link = est[10];
    var url_link = est[11];
    var hotel_id = est[12];
    var isPremium = listingType.toLowerCase() == 'premium';
    var isSuperior = listingType.toLowerCase() == 'superior';
    var isStandard = listingType.toLowerCase() == 'standard';
    
    //decide which icon for this marker
    var icon = premium_icon;
    var shadow = premium_shadow;
    if (isSuperior) {
        icon = superior_icon;
        shadow = superior_shadow;
    } else if (isStandard) {
        icon = standard_icon;
        shadow = standard_shadow;
    }
    //create the marker
    var myLatLng = new google.maps.LatLng(latitute, longitude);

    var marker = new google.maps.Marker({
        position: myLatLng,
        map: map,
        icon: icon,
        shadow: shadow,
        shape: shape,
        title: name,
        zIndex: zIndex
    });

    if (includeInfoWindows) {

        var contentString =
            '<div id="googlebox" class="' + listingType.toLowerCase() + '">' +
            ' <div class="title">' +
            '  <h1>' + name + '</h1>' +
            ' </div>' +
            ' <div class="content">' +
            '  <a href="' + details_link + '"><img src="' + thumbnail_image + '" class="image" alt="' + name + '" /></a>' +
            '  <div class="text">' +
            '   <p>' + description + '</p>';
        if (isStandard)
            contentString += '   <a class="details" href="' + details_link + '">View More Details</a> ';
        else
            contentString += '   <a class="details" href="' + details_link + '">Full Details</a> ';

        if (!isStandard) {
            if (bookingurl_link != '')
                contentString += '   <a class="check" href="' + bookingurl_link + '">Check Availability</a>'
            else
                contentString += '   <a class="check" href="' + url_link + '">View Website</a>'
        }
        contentString += '  </div>' +
            ' </div>';

        //if (!isStandard)
        //    contentString += ' <div class="footer"><a href="' + details_link + '">' + gm_red_text + '</a></div>';

        contentString += '</div>';

        google.maps.event.addListener(marker, 'click', function() {
            map.panTo(marker.getPosition());
            ib.setContent(contentString);
            
            ib.open(map, marker);
        });

        $('a.viewonmap[rel=' + hotel_id + '], a.viewbestdeal[rel=' + hotel_id + ']').live('click', function(event) {
        
            ib.setContent(contentString);
            ib.open(map, marker);
      
            event.preventDefault();

        });
    }
    return marker;
}

