﻿(function (window, undefined) {

    // Define a local copy of jQuery
    var snowell = function (selector, context) {
        return new snowell.fn.init(selector, context);
    },
        userAgent = "fisch"; // navigator.userAgent;

    snowell.fn = snowell.prototype = {
        init: function (selector, context) {

            var match, elem, ret, doc;
            alert("init snowell");

        },

        size: function () {
            ///	<summary>
            ///		The number of elements currently matched.
            ///		Part of Core
            ///	</summary>
            ///	<returns type="Number" />

            alert(userAgent);
        },
        test: 'hoi'
    },

    snowell.cart = {
        bla: null,
        init: function () {
            this.bla = "fischli";
        },

        book: function () {
            alert(this.bla);
        }
    },

    snowell.location = {
        markers: new Array(),
        shops: new Array(),
        options: {
            shopIdPrefix: null,
            hotelSearchBox: null,
            defaultSearchBoxValue: null,
            hotelSearchSubmit: null,
            countryCode: null,
            hotelSearchIconUrl: null,
            hotelNoSearchResults: null
        },

        lastMarker: null,
        bounds: null,
        googleMap: null,
        googleGeocoder: null,
        googleMapOptions: {
            navigationControl: true,
            navigationControlOptions: {
                style: google.maps.NavigationControlStyle.SMALL
            },
            mapTypeId: google.maps.MapTypeId.TERRAIN,
            mapTypeControlOptions: {
                mapTypeIds: [google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.TERRAIN, google.maps.MapTypeId.SATELLITE],
                style: google.maps.MapTypeControlStyle.DROPDOWN_MENU,
                position: google.maps.ControlPosition.TOP_RIGHT
            },
            scaleControlOptions: {
                position: google.maps.ControlPosition.RIGHT
            }
        },

        init: function (targetDiv) {
            this.googleMap = new google.maps.Map(targetDiv, this.googleMapOptions);
            this.googleGeocoder = new google.maps.Geocoder();
            this.bounds = new google.maps.LatLngBounds();
            this.loadShops();
        },

        addShop: function (shop) {
            this.shops[this.shops.length] = shop;
        },

        loadShops: function () {
            $.each(this.shops, function (index, s) {
                var position = new google.maps.LatLng(s.lat, s.lng);
                var markerIcon = new google.maps.MarkerImage(s.iconUrl);
                var hoverIcon = new google.maps.MarkerImage(s.hoverIconUrl);

                snowell.location.bounds.extend(position);
                snowell.location.googleMap.fitBounds(snowell.location.bounds);

                s.div.bind("mouseover mouseout", function () {
                    snowell.location.toggle(s.id);
                });

                var marker = new google.maps.Marker({
                    position: position,
                    map: snowell.location.googleMap,
                    icon: markerIcon,
                    title: s.title,
                    zIndex: 1000 - index,
                    // Coordi
                    shape: {
                        coord: [9, 0, 6, 1, 4, 2, 2, 4, 0, 8, 0, 12, 1, 14, 2, 16, 5, 19, 7, 23, 8, 26, 9, 30, 9, 34, 11, 34, 11, 30, 12, 26, 13, 24, 14, 21, 16, 18, 18, 16, 20, 12, 20, 8, 18, 4, 16, 2, 15, 1, 13, 0],
                        type: 'poly'
                    },
                    shop: {
                        id: s.id,
                        title: s.title,
                        position: position,
                        markerIcon: markerIcon,
                        hoverIcon: hoverIcon,
                        div: s.div
                    }
                });

                // Set initial shop icon and add it to watch list
                snowell.location.setShopIcon(marker.shop.markerIcon, marker);
                snowell.location.markers[snowell.location.markers.length] = marker;

                google.maps.event.addListener(marker, "mouseover", function () {
                    snowell.location.toggle(marker.shop.id);
                });
                google.maps.event.addListener(marker, "mouseout", function () {
                    snowell.location.toggle(marker.shop.id);
                });
            });
        },

        setShopIcon: function (icon, marker) {
            marker.setIcon(icon);
            marker.shop.div.find("img.marker").attr("src", icon.url);
            return true;
        },

        toggle: function (id) {
            $.each(this.markers, function (index, marker) {
                if (marker.shop.id == id) {
                    if (marker.icon == marker.shop.hoverIcon) {
                        marker.shop.div.css("backgroundColor", "#ffffff");
                        snowell.location.setShopIcon(marker.shop.markerIcon, marker);
                    } else {
                        marker.shop.div.css("backgroundColor", "#ffffff");
                        snowell.location.setShopIcon(marker.shop.hoverIcon, marker);
                    }
                }
            });
        },

        initGeoSearch: function () {
            var defaultText = snowell.location.options.defaultSearchBoxValue;
            // Show / hide text help text and capture enter for submit
            snowell.location.options.hotelSearchBox.bind({
                keypress: function (e) {
                    var code = (e.keyCode ? e.keyCode : e.which);
                    if (code == 13) { snowell.location.performGeoSearch(); }
                },
                focus: function () {
                    if ($(this).attr("value") == defaultText) $(this).attr("value", "");
                },
                blur: function () {
                    if ($(this).attr("value") == "") $(this).attr("value", defaultText);
                }
            }).attr("value", defaultText);

            // Search button submit handler
            snowell.location.options.hotelSearchSubmit.bind({
                click: function () { snowell.location.performGeoSearch(); }
            });
        },

        performGeoSearch: function () {
            var query = this.options.hotelSearchBox.val();
            if (/\s*^\-?\d+(\.\d+)?\s*\,\s*\-?\d+(\.\d+)?\s*$/.test(query)) {
                return;
            }
            var map = this.googleMap;
            var address = query + ", " + snowell.location.options.countryCode;
            if (this.googleGeocoder) {
                this.googleGeocoder.geocode({ "address": address }, function (results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        if (this.lastMarker != null) this.lastMarker.setMap(null);
                        map.setCenter(results[0].geometry.location);
                        var marker = new google.maps.Marker({
                            map: map,
                            position: results[0].geometry.location,
                            icon: new google.maps.MarkerImage(snowell.location.options.hotelSearchIconUrl)
                        });
                        this.lastMarker = marker;
                    } else {
                        alert(snowell.location.options.hotelNoSearchResults);
                    }
                });
            }
        }
    };

    snowell.fn.init.prototype = snowell.fn;
    window.snowell = snowell;
})(window);
