﻿
$(function () {
    if ($("#map_canvas").length > 0)
        if ($("#map_canvas").get(0))
            createMap($("#map_canvas").get(0), ests, true, 12);

    var $tabs = $("#tabs").tabs({
        show: function (event, ui) {
            if (ui.panel.id == "map") {
                if ($("#map_canvas2").get(0))
                    createMap($("#map_canvas2").get(0), ests, false, 14);
            }
        }
    });
    $('.showmap, #map_canvas').click(function (e) { // bind click event to link
        var tabs_count = $tabs.tabs("length")
        $tabs.tabs('select', tabs_count - 1); // switch to third tab
        e.preventDefault();
    });

    $('.gallery img').click(function (e) {
        $('img.main').attr('src', $(this).attr('src'));
    });
    $('.call_us').click(function (e) {
        var call_us = $(this);
        $.ajax({
            url: call_us.attr('href'),
            success: function (data) {
                $('#est_phone').find('.telephone').text(data);
                $('#est_phone').dialog({ 'width': 300, 'height': 150, 'title': 'Call Us Now!' });
            }
        });
        e.preventDefault();
    });
    
    $("a[href=#icons_explained]").click(function (event) {
        $('#icons_explained').dialog({ 'width': 532, 'title': 'Icons Explained', dialogClass: 'icons_explained_dialog', resizable: false });
        event.preventDefault();
    })
    $("a[href=#ratesinfo]").click(function (event) {
        $('#ratesinfo').dialog({ 'width': 300, 'height': 180, 'title': 'Room Rate Information', dialogClass: 'facility_search_dialog' });
        _gaq.push(['_trackEvent', 'Popups', 'Open', 'Repeater Rates Info']);
        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();
    })

    $(".add_review").click(function (e) {
        $('<iframe style="width:100%;height:100%" frameborder="0" scrolling="no" src="' + $(this).attr('href') + '" />').dialog({ title: 'Add a Guest Review', width: 325, height: 300 });
        e.preventDefault();
    });

    $('.show_availability, .show_website').click(function (e) {
        var post_website = insertParam('post', 'website', false);
        var redirect = $(this).attr('href');

        $.ajax({
            type: "POST",
            url: post_website,
            success: function (data) {

                window.location = redirect;
            }
        });
        e.preventDefault();
    });

    var tooltip;

    $('.iframeit').click(function (e) {
        var iframe = $("<div/>").append($('<iframe/>').attr('src', $(this).attr('href')).css({ 'width': '100%', 'height': '100%', 'border': '0' }));
        iframe.dialog({ width: 600, height: 400, title: $(this).attr('title') });
        e.preventDefault();
    });
    $("a.backtotop").click(function (event) {
        scrollWin("body", 500);
        event.preventDefault();
    })
    $("a.show_reviews").click(function (event) {
        var tabs_count = $tabs.tabs("length")
        $tabs.tabs('select', tabs_count - 2); // switch to third tab
        scrollWin(".est_details", 500);
        event.preventDefault();
    })
});



// 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, 30));

var premium_shadow = new google.maps.MarkerImage(marker_pr_shadow,
          new google.maps.Size(26, 30), new google.maps.Point(0, 0), new google.maps.Point(0, 30));

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, 30));

var superior_shadow = new google.maps.MarkerImage(marker_su_shadow,
          new google.maps.Size(24, 28), new google.maps.Point(0, 0), new google.maps.Point(0, 30));

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, 30));

var standard_shadow = new google.maps.MarkerImage(marker_st_shadow,
          new google.maps.Size(22, 26), new google.maps.Point(0, 0), new google.maps.Point(0, 30));

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, disableDefaultUI, zoom) {
    
    var myOptions = {
        zoom: zoom,
        disableDefaultUI: disableDefaultUI,
        scaleControl: !disableDefaultUI,
        scrollwheel: false,
        center: new google.maps.LatLng(latitude, 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, false);
}
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,TYPE MISMATCH ERROR*/
        shape: shape,
        title: name,
        zIndex: zIndex
    });

    if (includeInfoWindows) {

        var contentString =
            '<div id="googlebox" class="' + listingType.toLowerCase() + '">' +
            ' <div class="title">' +
            '  <h1>' + name + '</h1>' +
            '  <img alt="' + rating_name + '" src="' + rating_image + '" class="rating-icon" />' +
            ' </div>' +
            ' <div class="content">' +
            '  <a href="' + details_link + '" target="_blank"><img src="' + thumbnail_image + '" class="image" alt="' + name + '" /></a>' +
            '  <div class="text">' +
            '   <p>' + description + '</p>';
        if (isStandard)
            contentString += '   <a class="details" href="' + details_link + '" target="_blank">View More Details</a> ';
        else
            contentString += '   <a class="details" href="' + details_link + '" target="_blank">Full Details</a> ';

        if (!isStandard) {
            if (bookingurl_link != '')
                contentString += '   <a class="check" href="' + bookingurl_link + '" target="_blank">Check Availability</a>'
            else
                contentString += '   <a class="check" href="' + url_link + '" target="_blank">View Website</a>'
        }
        contentString += '  </div>' +
            ' </div>';

        if (!isStandard)
            contentString += ' <div class="footer"><a href="' + details_link + '" target="_blank">' + gm_red_text + '</a></div>';

        contentString += '</div>';

        google.maps.event.addListener(marker, 'click', function() {

            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;
}
