﻿// JScript File
/*** Start: Google Maps Integration ***/
var map = null;
//var cLatLong = new GLatLng(53.462, -1.186);
//var cZoom = 13;

var marker = null;
var myPano = null;

var geocoder = null;
var callBack_Count = 0;
var searchesIdsForGetAddreses = [];

/// Do google search for string
function mapSearchAddress(term, id) {
    geocoder.geocode({ 'address': term }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
			
			
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location,
				icon:"/images/Location_Page/markerSmall.png",
				title: term
            });
			
        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });

    //    geocoder.getLocations(
    //        term,
    //        function(results) {
    //            if (results.Status.code == 200) {
    //                for (var idx = 0; idx < results.Placemark.length; idx++) {
    //                    // plot each result
    //                    var latlon = new GLatLng(results.Placemark[idx].Point.coordinates[1], results.Placemark[idx].Point.coordinates[0]);
    //                    var marker = new GMarker(latlon, { title: results.Placemark[idx].address });
    //                    gMap.addOverlay(marker);
    //                    if (idx == 0)
    //                        gMap.setCenter(latlon, cZoom);
    //                }
    //            }
    //        }
    //      );
}

/*** Start: Map load/unload functions ***/
function loadMap() {
        geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(-34.397, 150.644);
        var myOptions = {
            scrollwheel: false,
            zoom: 13,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

        if (searches && searches.length > 0) {
			var idx = 0;
			if(searches[idx].lat != null && searches[idx].lat!= 0  && searches[idx].lon != null && searches[idx].lon != 0){
				map.setCenter(new google.maps.LatLng(searches[idx].lat,searches[idx].lon));
				var marker = new google.maps.Marker({
					map: map,
					position: new google.maps.LatLng(searches[idx].lat,searches[idx].lon),
					icon:"/images/Location_Page/markerSmall.png",
					title: searches[idx].term
				});
				
			}else{
				mapSearchAddress(searches[idx].term, searches[idx].id);
			}
        }

        


//    if (GBrowserIsCompatible()) {
//        gMap = new GMap2(document.getElementById("mapPropertyDetails"));

//        // Configure map controls 
//        //map.setUIToDefault();
//        gMap.addControl(new GLargeMapControl3D());
//        gMap.addControl(new GMenuMapTypeControl());

//        // Set map defaults 
//        gMap.setCenter(cLatLong, cZoom);

//        // Initialize geocoder        
//        geocoder = new GClientGeocoder();
//        // plot apartments on map
//        if (searches && searches.length > 0) {
//            for (var idx = 0; idx < searches.length; idx++) {
//                mapSearchAddress(searches[idx].term, searches[idx].id);
//            }
//        }
//    }
}

function unloadMap() {
    try {
        GUnload();
    }
    catch (err) {
        //Handle errors here
    }
}
/*** End: Map load/unload functions ***/

/*** Start: Attach onload/onunload event handlers ***/

$(window).unload(unloadMap); // .load(loadMap)

/*** End: Attach onload/onunload event handlers ***/

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

/*** End: Google Maps Integration ***/

/*** Start: Tabs & Image Gallery ***/
$(function() {
    var tabContainers = $('div.cp_tabs > div');
    tabContainers.hide().filter(':first').show();

    $('div.cp_tabs ul.cp_tabNavigation a').click(function() {
        tabContainers.hide();
        tabContainers.filter(this.hash).show();
        $('div.cp_tabs ul.cp_tabNavigation a').removeClass('selected');
        $(this).addClass('selected');
        return false;
    }).filter(':first').click();

    var isPrint = getParameterByName("print");
    if (isPrint == "true") {
        $("#pikame").PikaChoose({ auto_play: false, show_prev_next: false, transition: [0], thumb_width: 142, thumb_height: 104 });
        $("#pikame2").PikaChoose({ auto_play: false, show_prev_next: true, transition: [0], thumb_width: 142, thumb_height: 104 });
    } else {
        $("#pikame").PikaChoose({ auto_play: false, show_prev_next: false, transition: [0], thumb_width: 71, thumb_height: 52 });
        $("#pikame2").PikaChoose({ auto_play: false, show_prev_next: true, transition: [0], thumb_width: 71, thumb_height: 52 });
    }

    loadMap();

    $('.video-link').click(function(e) {
        e.stopPropagation();
        tabContainers.hide();
        tabContainers.filter('#fourth').show();
        $('div.cp_tabs ul.cp_tabNavigation a').removeClass('selected');
        $(".cp_tabNavigation a[href='#fourth']").addClass('selected');
        $('html, body').animate({
            scrollTop: $(".cp_tabNavigation a[href='#fourth']").offset().top
        }, 2000);
        return false;
    });

    $('.tripadvisor').click(function(e) {
        e.stopPropagation();
        tabContainers.hide();
        tabContainers.filter('#second').show();
        $('div.cp_tabs ul.cp_tabNavigation a').removeClass('selected');
        $(".cp_tabNavigation a[href='#second']").addClass('selected');
        $('html, body').animate({
            scrollTop: $(".cp_tabNavigation a[href='#second']").offset().top
        }, 2000);
        return false;
    });

	var scrollToReviews = getParameterByName('ta');
	var tabVisible = $('li.reviews-tab:visible').length;
	if (scrollToReviews == 'true' && tabVisible > 0) {
		tabContainers.hide();
        tabContainers.filter('#second').show();
        $('div.cp_tabs ul.cp_tabNavigation a').removeClass('selected');
        $(".cp_tabNavigation a[href='#second']").addClass('selected');
        $('html, body').animate({
            scrollTop: $(".cp_tabNavigation a[href='#second']").offset().top
        }, 2000);
	}
    //    /* some tabs */
    //    /** check for print url and don't hide tabs or add current tab to QS? **/
    //    /* wire up links */
    //    $("div.searchresultstabs2 ul li a").click(function(e) {
    //        /* get this tab */
    //        var $tab = $(this);
    //        /* hide all */
    //        $("div.tab-content").hide();
    //        /* show current tabs content */
    //        /*  get id */
    //        var contentId = $tab.attr("href").split("#")[1];
    //        if (contentId == "Map")
    //            $("#" + contentId).show(-1, function() { loadMap(); });
    //        else
    //            $("#" + contentId).show();
    //        /* unselect the current one */
    //        $("div.searchresultstabs2 ul li a.selected").removeClass("selected");
    //        /* set tab as selected */
    //        $tab.addClass("selected");
    //        /* prevent bubble */
    //        e.preventDefault();
    //    });
    //    /* hide all */
    //    $("div.tab-content").hide();
    //    /* set selected */
    //    var tab = location.hash || "#Summary";
    //    $("div.searchresultstabs2 ul li a[href$=" + tab + "]").click();
    //    /* /some tabs */
    //    /* some images */
    //    var $mainImage = $("img.main-image");
    //    $("ul.thumbs li a").each(function(idx) {
    //        var miu = this.title + "&w=" + $mainImage.attr("width") + "&h=" + $mainImage.attr("height");
    //        /* wire up and pre-load */
    //        $(this).mouseover(function() {
    //            if (miu.indexOf($mainImage.attr("src")) == -1)
    //                $mainImage.attr("src", miu);
    //        }).css("background-image", "url('" + miu + "')");
    //    });

});

