﻿/// <reference path="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.4-vsdoc.js" />

$(window).load(function () {
    bindGallery();
    getWebCamDate();
    $("body").removeClass("loading");
});

$(function () {
    $("body").addClass("loading");
    Init();
});

function Init() {
    if (window.location.hash.indexOf(".shtml") > -1) History.replaceState(null, null, window.location.hash.replace(/^.*#/, ''));
    slimBoxAutoLoad();
    
    animateMenu();
    bindGallery();
    if ($("#map")[0]) {
        initLoader();
    }
    /*if ($(".guestbook")[0]) {
        bindGuestBook();
    }*/
    $("#footer .nav a").click(function () { $.scrollTo(0,750) });
}

function bindDirections() {

    var $mapcontrol = $(".mapcontrol .btn");
    if ($mapcontrol[0])
        $mapcontrol.click(selectMap);
}

function selectMap() {

    if ($(this).parents(".column").hasClass("active")) {
        $(".mapcontrol .column").animate({ width: 340, marginTop: 0 }, 500, "easeInQuad", function () { $(this).removeClass("active").removeClass("inactive"); }).siblings("p").slideDown(500, "easeInQuad");
        $(".mapcontrol .column .body p").animate({ fontSize: "1em" }, 500);
        $(".mapcontrol .panel").animate({ padding: 0 }, 250);
        Hotel.setMap(map);
        map.setCenter(cenC);
        map.setZoom(11);
        FromH.setMap();
        FromC.setMap();
    }
    else {
        $.scrollTo("#map", 1500);
        $(".mapcontrol .panel").animate({ padding: 0 }, 250);
        $(".mapcontrol .column").removeClass("active");
        $("div.panel." + this.id).parent()
            .siblings("div.column").addClass("inactive").animate({ width: 200, marginTop: 0, queue: false }, 500, "easeOutQuad").siblings("p").slideUp();
        $("div.panel .body p").animate({ fontSize: "5px" }, 500);
        $("div.panel." + this.id + " .body p").stop().animate({ fontSize: "1em" }, 500);
        $("div.panel." + this.id).stop().animate({ padding: 10 }, 500)
            .parent().addClass("active").removeClass("inactive").stop().animate({ width: 480, marginTop: -30, queue: false }, 500, "easeInQuad")
            .children("p").animate({ fontSize: "1em" }, 500);
        switch (this.id) {
            case "chania":
                Hotel.setMap();
                FromH.setMap();
                FromC.setMap(map);
                break;

            case "heraklion":
                Hotel.setMap();
                FromC.setMap();
                FromH.setMap(map);
                break;
        }
    }
}

var map, FromC, FromH, Hotel, cenC;

function mapsLoaded() {

    cenC = new google.maps.LatLng(35.46671285768566, 24.12660039778984);

    var options = {
        zoom: 11,
        center: cenC,
        mapTypeId: google.maps.MapTypeId.HYBRID,
        overviewMapControl: true,
        overviewMapControlOptions: { opened: true },
        backgroundColor: "transparent"
    }

    map = new google.maps.Map(document.getElementById("map"), options);

    Hotel = new google.maps.KmlLayer('http://www.villa-amalia.gr/mapdata/VillaAmalia.kmz', { "preserveViewport": true });
    FromH = new google.maps.KmlLayer('http://www.villa-amalia.gr/mapdata/FromHeraklion.kmz', { "preserveViewport": false });
    FromC = new google.maps.KmlLayer('http://www.villa-amalia.gr/mapdata/FromChania.kmz', { "preserveViewport": false });

    Hotel.setMap(map);
    if ($("#map.directions")[0])
        bindDirections();
}

function initLoader() {
    var script = document.createElement("script");
    script.src = "https://www.google.com/jsapi?key=ABQIAAAAFgyfjWRXGSvpy-hZQx4y7RQVrNED1efJ-Ip8_saeWo5qauwRjRTOcZT--r7snk10xAD7GyRyjJyICQ&callback=loadMap&sensor=false";
    script.type = "text/javascript";
    document.getElementById("map").appendChild(script);
}

function loadMap() {
    google.load("maps", "3", { "callback": mapsLoaded, "other_params": "sensor=false" });
}

function animateMenu() {
    $("#left .nav a").hover(
        function () {
            $(this).stop().delay(150).animate({ fontSize: "1.2em" }, 500, "easeInQuad");
        },
        function () {

            $(this).stop().animate({ fontSize: "1em" }, 650, "easeOutQuad");
        });
}

function bindGallery() {
    checkButtons();
    $('.gallery .controls div').click(function () {
        var v = $(this).hasClass('gright') ? "+=" : "-=";
        $('.gallery .images').scrollTo(v + '600', 1850,
                {
                    easing: 'easeInQuad',
                    onAfter: checkButtons
                });
    });
}

function checkButtons() {
    var $gal = $('.gallery .images');
    if ($gal[0]) {
        var w = $gal[0].scrollWidth,
        l = $gal.scrollLeft(),
        o = $gal.outerWidth();
        $(".gallery .controls div[class!='gright']").animate({ opacity: ((l == 0) ? 0 : 1) }, 750, 'easeOutCirc');
        $(".gallery .controls div.gright").animate({ opacity: ((w - l == o) ? 0 : 1) }, 750, 'easeOutCirc');
    }
}

function slimBoxAutoLoad() {
    if (!/android|iphone|ipod|series60|symbian|windows ce|blackberry/i.test(navigator.userAgent)) {
        jQuery(function ($) {
            $("a[class~='lightbox'],.lightbox a").slimbox(
            {
                resizeEasing: "easeInOutQuad",
                resizeDuration: 250,
                counterText: "{x} of {y}"
            },
            null, function (el) {
                return (this == el) || (this.parentNode && (this.parentNode == el.parentNode));
            });
        });
    }
}

function getWebCamDate() {
    var request;
    var date;
    $(".webcam").attr('href', $(".webcam").attr('href') + '?' + Math.floor(Math.random() * 1024));
    request = $.ajax({
        type: "HEAD",
        url: $(".webcam").attr('href'),
        success: function () {
            date = request.getResponseHeader("Last-Modified");
            d = new Date(date);
            $(".webcam").attr('title', $(".webcam").attr('title') + d.format('H:i'));
        }
    });
}

function bindGuestBook() {
    $(".guestbook").html('<script src="../js/AC_RunActiveContent.js" type="text/javascript"></script>' +
					'<script src="../js/PopUpWin.js" type="text/javascript"></script>' +
					'<script type="text/javascript">' +
						'AC_FL_RunContent("codebase", "http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0", "width", "730px", "height", "710px", "src", "../swf/Magazine", "quality", "high", "pluginspage", "http://www.macromedia.com/go/getflashplayer", "bgcolor", "#000000", "allowFullScreen", "true", "allowScriptAccess", "sameDomain", "movie", "swf/Magazine");' +
					'</script>');
}
