﻿var filialen = 0;
var geocoder;
var map;
var Message = new String('');
var blnInit = true;
var Koopjeshoek = false;
var strArtikelnummer = '';


function getFilialen(Postcode, Straal, Plaatsnaam) {
    var straal = $get(Straal);
    blnInit = false;
    var ret = IMpactProtoType.WebUI.StoreSelector.GetFilialen($get(Postcode).value, straal.options[straal.selectedIndex].value, $get(Plaatsnaam).value, BuildFilialen, OnError, onError);
    return false;
}

function GetKoopjesFilialen(Artikelnummer) {
    blnInit = false;
    var ret = IMpactProtoType.WebUI.StoreSelector.GetKoopjesFilialen(Artikelnummer, BuildFilialen, OnError, OnError);
}

function enableWinkelZoeker(Artikelnummer) {
    strArtikelnummer = Artikelnummer;
    blnInit = true;
    if (Koopjeshoek) {
        $('#HeaderWinkelZoeker').text('Dit artikel is nog te reserveren in onderstaande I\'ts Electronics winkel(s).');
        var ret = IMpactProtoType.WebUI.StoreSelector.GetKoopjesFilialen(Artikelnummer, BuildFilialen, OnError, OnError);

    } else {
        $('#HeaderWinkelZoeker').text('Zoek een It\'s Electronics winkel bij u in de buurt');
        var ret = IMpactProtoType.WebUI.StoreSelector.GetFilialen('', 0, '', BuildFilialen, OnError, OnError);

    }
}

function SetFiliaal(Filiaalnummer) {
    var ret = IMpactProtoType.WebUI.StoreSelector.SetFiliaal(Filiaalnummer, FiliaalSet, OnError, OnError);
}

function FiliaalSet(Plaats) {
    /* Hier moet nog de code komen om het afhaal filiaal te zette */
    if ($get('ctl00_ContentPlaceHolder1_LabelTextStap1') != null) {
        $get('ctl00_ContentPlaceHolder1_AccordionWinkelKiezen_content_winkelzoeker1_Plaatsnaam').value = Plaats;
        __doPostBack('ctl00$ContentPlaceHolder1$AccordionWinkelKiezen_content$winkelzoeker1$LinkButton1', '');
    }
    HideLayer('DojoWinkelZoeker');
}

function BuildFilialen(Result) {
    filialen = 0;
    geocoder = new google.maps.Geocoder();
    if (Result.length > 0) {
        var FiliaalData = JSON.parse(Result);
        if (FiliaalData.length > 0) {
            for (i = 0; i < FiliaalData.length; i++) {
                renderFiliaal(FiliaalData[i]);
            }
        }
    }
    if (Message != null) {
        if (Message.length > 0) {
            alert(Message);
        }
    }
}


function codeAddress(address, title, image, infowindow, filiaalnummer) {
    if (geocoder) {
        geocoder.geocode({ 'address': address }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                // map.setCenter(results[0].geometry.location);
                var marker = new google.maps.Marker({
                    title: title,
                    map: map,
                    position: results[0].geometry.location,
                    icon: image
                });
                google.maps.event.addListener(marker, 'click', function () {
                    infowindow.open(map, marker);
                });
                IMpactProtoType.WebUI.StoreSelector.SetLatLong(results[0].geometry.location.lat(), results[0].geometry.location.lng(), filiaalnummer);
            } else {
                alert("Geocode was not successful for the following reason: " + status);
            }
        });
    }
}


function OnError() {
    alert(" Error");
}

function renderFiliaal(Row) {
    var intZoom = 8;
    var myLatlng = new google.maps.LatLng(Row.latitude, Row.longitude);
    if (filialen == 0) {
        var myOptions = {
            zoom: 8,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }

        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
        Message = Row.Message;
        ShowMessage = true;
    }

    var image = "/images/googlemaps/pointer.png";
    var contentString = '<div class="GoogleFiliaal">' +
    '<div class="GoogleFiliaalImage"><img src="/images/filialen/default.jpg" alt="It\'s Electronics ' + Row.Filiaal + ' ' + Row.XXL + '" />' +
    '</div><div class="GoogleFiliaalInformatie">' +
    '<div class="GoogleFiliaalnaam">It\'s Electronics ' + Row.Filiaal;
    if (!Koopjeshoek) {
    if (Row.Distance >= 0) {
        var afstand = ""
        if (Row.Distance == 0) {
            afstand = "0 - 5km";
            map.setZoom(13);
        } else {
            if (blnInit == false) {
                intZoom = 12 - parseInt(Row.Distance / 5);
            }
            if (intZoom < 8) {
                intZoom = 8;
            }
            map.setZoom(intZoom);
            if (Row.Distance % 5 == 0) {
                afstand = ((parseInt(Row.Distance / 5) - 1) * 5) + ' - ' + (parseInt(Row.Distance / 5) * 5) + 'km'
            } else {
                afstand = ((parseInt(Row.Distance / 5)) * 5) + ' - ' + ((parseInt(Row.Distance / 5) + 1) * 5) + 'km'
            }

        }
        contentString += '&nbsp;<span style="font-weight: normal">Afstand tot filiaal&nbsp;' + afstand;
    }
	}
    contentString += '</div>' +
    '<div class="GoogleAdresgegevens">' + Row.Adres + '<br />' + Row.Postcode + ' ' + Row.Plaats + '<br />Telefoon: ' + Row.Telefoon +
    '</div><div class="GoogleOpeningstijden"><strong>Openingstijden:</strong><br />' + Row.Maandag + '<br />' + Row.Week + '<br />' + Row.Zaterdag + '<br />' + Row.Koopavond +
    '</div>';
    if (Koopjeshoek) {
        contentString += '<div class="GoogleMeerInfo"><a class="ButtonAddToCart" style="color: #FFFFFF;" href="/winkel/koopjeshoek/reserveren.aspx?FiliaalId=' + Row.Filiaalnummer + '&Artikelnummer=' + strArtikelnummer + '">Reserveer het artikel in dit filiaal.</a></div>';
    } else {
        if ($get('ctl00_ContentPlaceHolder1_LabelTextStap1') != null) {
            contentString += '<div class="GoogleMeerInfo"><a href="javascript:SetFiliaal(\'' + Row.Filiaalnummer + '\')">Selecteer dit filiaal als afhaalpunt.</a></div>';
        } else {
            contentString += '<div class="GoogleMeerInfo"><a href="javascript:SetFiliaal(\'' + Row.Filiaalnummer + '\')">Selecteer dit filiaal als afhaalpunt en winkel verder.</a></div>';
        }
    }
    contentString += '</div>';

    var infowindow = new google.maps.InfoWindow({
        content: contentString
    });

    if (Row.latitude == null) {
        codeAddress(Row.Adres + ' ' + Row.Plaats, 'It\'s Electronics ' + Row.Filiaal + ' ' + Row.XXl, image, infowindow, Row.Filiaalnummer);
    } else {
        var marker = new google.maps.Marker({
            title: 'It\'s Electronics ' + Row.Filiaal + ' ' + Row.XXl,
            map: map,
            position: myLatlng,
            icon: image
        });
        google.maps.event.addListener(marker, 'click', function () {
            infowindow.open(map, marker);
        });
    }
    filialen += 1;
}
