$(document).ready(function() {

    $("#investment-bar-left span a").mouseover(function() {
        $("#investment-bar-left span.comming-soon").fadeIn("slow");
    }).mouseout(function() {
        $("#investment-bar-left span.comming-soon").fadeOut("slow");
    }).click(function() { return false; });

    var url = document.location.toString();

    $("#section-menu a").each(function() {
        $(this).click(function() {
            var anchor = $(this).attr("href");
            $(".lang-selection a").each(function() {
                var url = $(this).attr("href").split('#')[0];
                var linkWithAnchor = url + anchor;
                $(this).attr("href", linkWithAnchor);
            });

        });
    });

    if (url.match('#')) {
        var urlAnchor = url.split('#')[1];

        $(".lang-selection a").each(function() {
            var linkWithAnchor = $(this).attr("href") + "#" + urlAnchor;
            $(this).attr("href", linkWithAnchor);
        });
    }

    if ($("#contact-form").get(0) != null) {
        $("#contact-form").validate({
            rules: {
                name: "required",
                email: { required: true, email: true, equalTo: "#emailconfirmation" },
                emailconfirmation: { required: true, email: true, equalTo: "#email" },
                comments: "required"
            },
            messages: {
                name: "",
                email: "",
                comments: "",
                emailconfirmation: ""
            }
        });
    }etin
    if ($("#yourmind-form").get(0) != null) {
        $("#yourmind-form").validate({
            rules: {
                name: "required",
                email: { required: true, email: true },
                comments: "required"
            },
            messages: {
                name: "",
                email: "",
                comments: ""
            }
        });
    }
    $(".map-links a").each(function() {
        $(this).click(function() {
            $(".map-links a").each(function() {
                $(this).removeClass("selected");
            });

            $(this).addClass("selected");

            $(".bsas-map").fadeOut().hide();
            $(".hotel-around").fadeOut().hide();
            $("." + $(this).attr("id")).fadeIn();

            // HACK: this is because a weird behavior on Google Maps
			/*
            $("#section-menu-map").show();
            insertGoogleMap("googlemaps-container");
			*/
        });
    });
});

function onSectionClicked(source) {
    $("#section-menu li a").each(function() {
        $(this).removeClass("selected");
        var cssClass = $(this).attr("class");
        var href = cssClass.substring(cssClass.lastIndexOf("-") + 1, cssClass.length);
        $(this).attr("href", "#" + href);
        $(this).bind("click", function() { onSectionClicked($(this)) });
    });

    $("#sections > *").each(function() {
        $(this).fadeOut().hide();
    });
    
    sectionToDisplay = "#" + source.attr("class");
    $(sectionToDisplay).fadeIn();

    source.addClass("selected");
    source.unbind("click");

    // HACK: this is because a weird behavior on Google Maps
	/*
    if (sectionToDisplay == "#section-menu-map") {
        $("#section-menu-map").show();
        insertGoogleMap("googlemaps-container");
    }
	*/
}

function showCurrentSection(defaultSection) {
    var url = document.location.toString();
    var sectionToDisplay = defaultSection;

    $("#sections > *").each(function() {
        $(this).fadeOut().hide();
    });

    if (url.match('#')) {
        var urlAnchor = url.split('#')[1];

        if ($("#section-menu-" + urlAnchor).attr("id") != undefined) {
            sectionToDisplay = "#section-menu-" + urlAnchor;
        }
    }

    $(sectionToDisplay).show();

    onSectionClicked($(sectionToDisplay.replace("#", ".")));

    // HACK: this is because a weird behavior on Google Maps
	/*
    if (sectionToDisplay == "#section-menu-map") {
        $("#section-menu-map").show();
        insertGoogleMap("googlemaps-container");
    }
	*/
}

/*
function insertGoogleMap(container) {
    if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById(container));
        map.setCenter(new GLatLng(-34.585913, -58.437696), 15);
        map.openInfoWindow(map.getCenter(),
                   "<img src='http://www.ownhotels.com/img/logo.png' height='30'/><br/><span>Cabrera 5556 | C1414BUC | Palermo<br/> Buenos Aires - Argentina</span>");
        map.setUIToDefault();
    }
}
*/

function calcTime(city, offset) {
    var date = new Date();
    var utc = date.getTime() + (date.getTimezoneOffset() * 60000);
    var curDateTime = new Date(utc + (3600000 * offset));

    var curHour = curDateTime.getHours();
    var curMin = curDateTime.getMinutes();
    var curSec = curDateTime.getSeconds();
    var curAMPM = " AM";
    var curTime = "";
    if (curHour >= 12) {
        curHour -= 12;
        curAMPM = " PM";
    }

    if (curHour == 0) curHour = 12;
    curTime = curHour + ":" + ((curMin < 10) ? "0" : "") + curMin + ":" + ((curSec < 10) ? "0" : "") + curSec + curAMPM;
        
    return curTime;
}
