/// <reference name="MicrosoftAjax.debug.js" />

jQuery(function() {
    $(document).ready(function() {
        if ($(".scrollableArea").length) {
            $(".scrollableArea").jStockTicker({ interval: 45 });
        }
        // Duobox
        if ($('.duobox').length) {
            $('.duobox').add_layer("url('/css/htn/solobox_bg.gif')");
            $('.duobox').add_layer("url('/css/htn/solobox_t.gif') top repeat-x");
            $('.duobox').add_layer("url('/css/htn/solobox_l.gif') left repeat-y");
            $('.duobox').add_layer("url('/css/htn/solobox_b.gif') bottom repeat-x");
            $('.duobox').add_layer("url('/css/htn/duobox_r.gif') right repeat-y");

            $('.duobox').add_layer("url('/css/htn/solobox_tl.gif') top left no-repeat");
            $('.duobox').add_layer("url('/css/htn/duobox_tr.gif') top right no-repeat");
            $('.duobox').add_layer("url('/css/htn/duobox_br.gif') bottom right no-repeat");
            $('.duobox').add_layer("url('/css/htn/solobox_bl.gif') bottom left no-repeat");
        }

        // Solobox
        if ($('.solobox').length) {
            $('.solobox').add_layer("url('/css/htn/solobox_bg.gif')");
            $('.solobox').add_layer("url('/css/htn/solobox_t.gif') top repeat-x");
            $('.solobox').add_layer("url('/css/htn/solobox_l.gif') left repeat-y");
            $('.solobox').add_layer("url('/css/htn/solobox_b.gif') bottom repeat-x");
            $('.solobox').add_layer("url('/css/htn/solobox_r.gif') right repeat-y");

            $('.solobox').add_layer("url('/css/htn/solobox_tl.gif') top left no-repeat");
            $('.solobox').add_layer("url('/css/htn/solobox_tr.gif') top right no-repeat");
            $('.solobox').add_layer("url('/css/htn/solobox_br.gif') bottom right no-repeat");
            $('.solobox').add_layer("url('/css/htn/solobox_bl.gif') bottom left no-repeat");
        }

        // Solobox_search
        if ($('.solobox_search').length) {
            $('.solobox_search').add_layer("url('/css/htn/solobox_bg.gif')");
            $('.solobox_search').add_layer("url('/css/htn/solobox_t.gif') top repeat-x");
            $('.solobox_search').add_layer("url('/css/htn/solobox_l.gif') left repeat-y");
            $('.solobox_search').add_layer("url('/css/htn/solobox_b.gif') bottom repeat-x");
            $('.solobox_search').add_layer("url('/css/htn/solobox_r.gif') right repeat-y");

            $('.solobox_search').add_layer("url('/css/htn/solobox_search.gif') top left no-repeat");
            $('.solobox_search').add_layer("url('/css/htn/solobox_tr.gif') top right no-repeat");
            $('.solobox_search').add_layer("url('/css/htn/solobox_br.gif') bottom right no-repeat");
            $('.solobox_search').add_layer("url('/css/htn/solobox_bl.gif') bottom left no-repeat");
        }


        //sponsoroverzicht
        if ($('.sponsoroverzicht').length) {
            $('.sponsoroverzicht').add_layer("url('/css/htn/whitebox_tl.gif') top left no-repeat");
            $('.sponsoroverzicht').add_layer("url('/css/htn/whitebox_tr.gif') top right no-repeat");
        }

        if ($(".button_rubrieken").length) {
            $(".button_rubrieken").click(function() {
                $(".rubriekenmenu_inner").slideToggle();
            });
        }

        if ($("#accordion").length) {
            $("#accordion").accordion({
                autoHeight: false
            });

        }

        if ($(".rubriek_activator").length) {
            $(".rubriek_activator").click(function() {
                var layerName = this.value;
                var layer = null;
                switch (layerName) {
                    case "hotellos":
                        layer = Map.get_hotellosLayer();
                        break;
                    case "schools":
                        layer = Map.get_schoolsLayer();
                        break;
                    case "jobs":
                        layer = Map.get_jobsLayer();
                        break;
                    case "sponsors":
                        layer = Map.get_sponsorsLayer();
                        break;
                }
                if (layer != null)
                    Map.setLayerVisibility(layer, this.checked);
            });
        }
        //$("div#logoParade").smoothDivScroll({
        //  autoScroll: "always", 
        //	autoScrollDirection: "endlessloop", 
        //	pauseAutoScroll: "mouseover"
        //});
    });
});

var Map = {
    map: null,
    lat: 52,
    lon: 6,
    zoom: 9,
    finalZoomLevel: 10,
    hotellosLayer: null,
    schoolsLayer: null,
    jobsLayer: null,
    sponsorsLayer: null,
    centerDefault: true,
    findPlaceResults: null,
    minX: null,
    minY: null,
    maxX: null,
    maxY: null,
    counter: 0,
    init: function(target, newZoomLevel) {
        try {
            map = new VEMap(target);
        } catch (e) { }
        if (map == undefined) return;

        if (newZoomLevel !== undefined) Map.zoom = newZoomLevel;

        map.AttachEvent("oninitmode", function() {
            if (Map.centerDefault) {
                $(".center-location").each(function() {
                    if (this.value != "") {
                        Map.search(this.value);
                    }
                });
            }
        });
        map.SetDashboardSize(VEDashboardSize.Normal);
        map.LoadMap();
        map.SetScaleBarDistanceUnit(VEDistanceUnit.Kilometers);
        map.SetCenterAndZoom(new VELatLong(Map.lat, Map.lon), Map.zoom - 1);
        map.ShowMessageBox = false;
        this.hotellosLayer = this.createLayer();
        this.schoolsLayer = this.createLayer();
        this.jobsLayer = this.createLayer();
        this.sponsorsLayer = this.createLayer();
    },
    get_hotellosLayer: function() { return this.hotellosLayer; },
    get_schoolsLayer: function() { return this.schoolsLayer; },
    get_jobsLayer: function() { return this.jobsLayer; },
    get_sponsorsLayer: function() { return this.sponsorsLayer; },
    createLayer: function() {
        var layer = new VEShapeLayer();
        map.AddShapeLayer(layer);
        return layer;
    },
    setLayerVisibility: function(layer, visible) {
        if (visible)
            layer.Show();
        else
            layer.Hide();
    },
    GetCoordinates: function(layer, resultsArray, places, hasMore, veErrorMessage) {
        Map.counter -= 1;

        if (places == null) { return };
        findPlaceResults = places[0].LatLong;
        if (Map.minX == null || places[0].LatLong.Longitude <= Map.minX)
            Map.minX = places[0].LatLong.Longitude;
        if (Map.maxX == null || places[0].LatLong.Longitude >= Map.maxX)
            Map.maxX = places[0].LatLong.Longitude;
        if (Map.minY == null || places[0].LatLong.Latitude <= Map.minY)
            Map.minY = places[0].LatLong.Latitude;
        if (Map.maxY == null || places[0].LatLong.Latitude >= Map.maxY)
            Map.maxY = places[0].LatLong.Latitude;

        //        var myShape = new VEShape(VEShapeType.Pushpin, findPlaceResults);
        //        myShape.SetDescription(places[0].LatLong + ', ' + Map.minX + ', ' + Map.minY + ', ' + Map.maxX + ', ' + Map.maxY);
        //        map.AddShape(myShape);


        if (Map.counter == 0) {
            var topLeft = new VELatLong(Map.maxY, Map.minX);
            var bottomRight = new VELatLong(Map.minY, Map.maxX);
            setTimeout(function() { map.SetMapView([topLeft, bottomRight]) }, 1500);
        }
    },
    search: function(input) {
        if (input == "") { return };
        this.counter += 1;
        map.ShowMessagBox = false;
        map.Find(null, input, null, null, null, null, true, true, false, false, Map.GetCoordinates);
        this.centerDefault = false;
    },
    clearPins: function() {
        map.DeleteAllShapes();
    },
    zoomIn: function() {
        map.ZoomIn();
    },
    setCenter: function(center) {
        map.Find(null, center, VEFindType.Businesses, null, null, null, false, false, false, false,
			    function(layer, resultsArray, places, hasMore, veErrorMessage) {
			        if (null != places) {
			            map.SetCenter(places[0].LatLong);
			            map.SetZoomLevel(Map.zoom);
			        }
			    }
		    );
    },
    addPin: function(lat, lon, icon, header, infobox, layer) {
        if (lat == "" || lon == "")
            return;
        var shape = new VEShape(VEShapeType.Pushpin, new VELatLong(lat, lon));

        //Set the icon
        shape.SetCustomIcon(icon);
        shape.SetTitle("");
        shape.SetDescription(infobox);
        //Set the info box

        var target = layer;
        target.AddShape(shape);
        return shape.GetID();
    },
    addHotello: function(location, image, text, url, center) {
        var icon = "<div><img src='/css/htn/icon-user.png' alt='pin' /></div>";
        var infobox = "";

        infobox += "<div class='wide_pin'>";
        infobox += "<div class='wide_pin_top'></div>";
        infobox += "<div class='wide_pin_middle'>";
        infobox += "<div class='wide_pin_content'>";

        if (image == "" || image == null)
            image = "/css/htn/unknown.jpg";

        if ("" !== text) {
            infobox += "<img src='" + image + "' alt='' style='max-height: 200px;'  /><p>" + text + "</p>";
        }
        if ("" !== url) {
            infobox += "<p class='details'><a href='" + url + "'>meer</a></p>";
        }

        infobox += "</div>";
        infobox += "</div>";
        infobox += "<div class='wide_pin_bottom'></div>";
        infobox += "</div>";

        var type = this.get_hotellosLayer();

        map.Find(null, location, VEFindType.Businesses, null, null, null, false, false, false, false,
			function(layer, resultsArray, places, hasMore, veErrorMessage) {
			    if (null != places) {
			        if (center) {
			            map.SetCenter(places[0].LatLong);
			            map.SetZoomLevel(Map.zoom);
			        }
			        id = Map.addPin(places[0].LatLong.Latitude, places[0].LatLong.Longitude, icon, '', infobox, type);
			    } else {
			        //alert("no results! " + veErrorMessage);
			    }
			}
		);
    },
    addSponsor: function(name, location, image, url, text) {
        var icon = "<div><img src='/css/htn/icon-sponsor.png' alt='pin' /></div>";
        var infobox = "";

        infobox += "<div class='wide_pin'>";
        infobox += "<div class='wide_pin_top'></div>";
        infobox += "<div class='wide_pin_middle'>";
        infobox += "<div class='wide_pin_content'>";

        if (image == "" || image == null)
            image = "/css/htn/unknown.jpg";

        if ("" !== text) {
            infobox += "<img src='" + image + "' alt='' style='width: 100%; padding-bottom: 8px;' /><p class='wide_pin_description_text'>" + text + "</p>";
        }
        if ("" !== url) {
            infobox += "<p class='details'><a href='" + url + "'>bezoek</a></p>";
        }

        infobox += "</div>";
        infobox += "</div>";
        infobox += "<div class='wide_pin_bottom'></div>";
        infobox += "</div>";

        var locations = location.split(',');
        if (locations.length > 1) {
            var type = this.get_sponsorsLayer();
            Map.addPin(locations[0], locations[1], icon, '', infobox, type)
        }
    },
    addJob: function(name, description, url, location) {
        var icon = "<div><img src='/css/htn/icon-market.png' alt='pin' /></div>";
        var infobox = "";

        infobox += "<div class='wide_pin'>";
        infobox += "<div class='wide_pin_top'></div>";
        infobox += "<div class='wide_pin_middle'>";
        infobox += "<div class='wide_pin_content'>";

        infobox += "<p><strong>" + name + "</strong></p><p>" + description + "</p>";
        if ("" !== url) {
            infobox += "<p class='details'><a href='" + url + "'>bekijk</a></p>";
        }

        infobox += "</div>";
        infobox += "</div>";
        infobox += "<div class='wide_pin_bottom'></div>";
        infobox += "</div>";

        var locations = location.split(',');
        if (locations.length > 1) {
            var type = this.get_jobsLayer();
            Map.addPin(locations[0], locations[1], icon, '', infobox, type)
        }
    },
    addSchool: function(name, location, image, url, text) {
        var icon = "<div><img src='/css/htn/icon-school.png' alt='pin' /></div>";
        var infobox = "";

        infobox += "<div class='wide_pin'>";
        infobox += "<div class='wide_pin_top'></div>";
        infobox += "<div class='wide_pin_middle'>";
        infobox += "<div class='wide_pin_content'>";

        if (image == "" || image == null)
            image = "/css/htn/unknown.jpg";

        if ("" !== text) {
            infobox += "<img src='" + image + "' alt='' /><p>" + text + "</p>";
        }
        if ("" !== url) {
            infobox += "<p class='details'><a href='" + url + "'>bezoek</a></p>";
        }

        infobox += "</div>";
        infobox += "</div>";
        infobox += "<div class='wide_pin_bottom'></div>";
        infobox += "</div>";

        var locations = location.split(',');
        if (locations.length > 1) {
            var type = this.get_schoolsLayer();
            Map.addPin(locations[0], locations[1], icon, '', infobox, type)
        }
    }
};
function Random(r) {
    return Math.ceil(Math.random() * r);
}

function searchKeyDown(sender, e) {
    var keynum;
    var keychar;
    var numcheck;

    if (window.event) { // IE
        keynum = e.keyCode;
    }
    else if (e.which) { // Netscape/Firefox/Opera
        keynum = e.which;
    }
    if (keynum == 13) {
        document.location = '/hotellos?query=' + sender.value;
        return false;
    }
    else if (sender.id == 'btnZoek') {
        var el = document.getElementById('txtSearchQuery');
        document.location = '/hotellos?query=' + el.value;
        return false;
    }
    return true;
}