var loadCalendarContent = false;
function onYouTubePlayerReady(playerId) {
    $('#browsable li').find('#' + playerId).each(function() {
        $(this)[0].addEventListener("onStateChange", "onytplayerStateChange");
    });
}

function onytplayerStateChange(state) {
    if (state == 1) {
        $('#browsable').data('AnythingSlider').startStop(false);
    }
    if (state == 0 || state == 2) {
        $('#browsable').data('AnythingSlider').startStop(true);
    }
}

$(window).load(function() {
    $('#kalender a.cal_pijl').live("click", function() {
        var url = $(this).attr("href");
        $('#kalender').load(url);
        if (loadCalendarContent) {
            $('#content').load("evenementenPartial.htm" + url.substr(url.indexOf("?")));
        }
        return false;
    });
    $(".filterCriteria select").change(function() {
        $(this).closest("form").submit();
    });
//    $("#browsable").scrollable({circular: true, mousewheel: true}).autoscroll({autoplay: true, interval: 6000}).navigator();
    $("#browsable").anythingSlider({resumeOnVideoEnd:false,delay:5,resizeContents:false,autoPlay:true,delay:6000,navigationFormatter: function(index, panel){return "&nbsp;&nbsp;";},buildStartStop:false})
// width:235,height:275
    $("a.wachtlijst").fancybox({
        'scrolling'		: 'no',
        'titleShow'		: false,
        'onClosed'		: function() {
            $("#login_error").hide();
        }
    });

    initMaps();

    $('.aantal').change(function() {
        var aantalrow = $(this).closest('.aantalrow');
        var totaalrow = 0;
        aantalrow.find('.aantal').each(function() {
            var aantal = parseFloat($(this).val() != "" ? $(this).val() : 0.0);
            var price = parseFloat($(this).data('price')) * aantal;
            totaalrow += price;
        });

        aantalrow.find('.totalPriceRow').data('total', totaalrow);
        aantalrow.find('.totalPriceRow').html(totaalrow);

        var totaal = 0;
        $('.totalPriceRow').each(function() {
            var row = parseFloat($(this).data('total') != undefined ? $(this).data('total') : 0);
            totaal += row;
        });

        $('.totalPrice').html(totaal);
    });

    $('.wachtlijst_form').submit(function(){
        var frm = $(this);
        $.post('wachtlijst.htm',frm.serialize(),function(data){frm.closest('div').html(data);});
        return false;
    });
});

var markers=new Array();
var balloons=new Array();
var map = null;
var currentwindow=null;
function initMaps() {
    var zindexInfoWindow = 1;
    $(".map").each(function() {
        var mapOptions = { zoom: $(this).data("zoom-level"), mapTypeId: google.maps.MapTypeId.ROADMAP, disableDefaultUI: true,
            zoomControl: true,
            mapTypeControl: true};

        map = new google.maps.Map(this, mapOptions);
        var mapElement = this;
        var minlatitude = 0;
        var maxlatitude = 0;
        var minlongitude = 0;
        var maxlongitude = 0;
        var locationsSelector = "#" + $(this).data("locations") + " li";
        if ($(locationsSelector).size() == 0) {
            $(this).hide();
        }
        var cnt=0;
        $(locationsSelector).each(function(index, data) {
            var geocoder = new google.maps.Geocoder();
            var address = $(this).data("street") + " " + $(this).data("housenr") + " " + $(this).data("postalcode") + " " + $(this).data("muncipality");
            var dezen=$(this);
            geocoder.geocode({ 'address': address}, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    if ($(data).data("center") != undefined && $(data).data("center") == true) {
                        map.setCenter(results[0].geometry.location);
                    }
                    var marker = new google.maps.Marker({
                        map: map,
                        position: results[0].geometry.location,
                        title: $(data).text()
                    });
                    if (minlatitude == 0 || minlatitude > results[0].geometry.location.lat()) {
                        minlatitude = results[0].geometry.location.lat();
                    }
                    if (maxlatitude == 0 || maxlatitude < results[0].geometry.location.lat()) {
                        maxlatitude = results[0].geometry.location.lat();
                    }
                    if (minlongitude == 0 || minlongitude > results[0].geometry.location.lng()) {
                        minlongitude = results[0].geometry.location.lng();
                    }
                    if (maxlongitude == 0 || maxlongitude < results[0].geometry.location.lng()) {
                        maxlongitude = results[0].geometry.location.lng();
                    }
                    var contentValue = "<div class='mapInfo'>";
                    contentValue += "<strong>" + $(data).text() + "</strong><br/>";
                    contentValue += "" + $(data).data("street") + " " + $(data).data("housenr") + "<br/>";
                    contentValue += "" + $(data).data("postalcode") + " " + $(data).data("muncipality") + "&nbsp;&nbsp;<br/><br/>";
                    if ($(data).data("link")) {
                        contentValue += "<a href='" + $(data).data("link") + "'>Meer info >></a>";
                    }
                    contentValue += "</div>";
                    var infowindow = new google.maps.InfoWindow({
                        content: contentValue
                    });
                    balloons[cnt]=infowindow;

                    var bounds = new google.maps.LatLngBounds(new google.maps.LatLng(minlatitude, minlongitude), new google.maps.LatLng(maxlatitude, maxlongitude));
                    var center = null;
                    var zoomlevel = 10;
                    if ($(data).data("center") != undefined) {
                        center = results[0].geometry.location;
                    } else {
                        center = bounds.getCenter();
                        zoomlevel = map.fitBounds(bounds);
                    }

                    if ($(mapElement).data("zoom-level") != undefined) {
                        zoomlevel = $(mapElement).data("zoom-level");
                    }

                    map.setCenter(center, zoomlevel);

                    google.maps.event.addListener(marker, 'click', function() {
                        if(currentwindow)currentwindow.close();
                        infowindow.open(map, marker);
                        infowindow.setZIndex(zindexInfoWindow++);
                        currentwindow=infowindow;

                    });

                    markers[cnt]=marker;
                    cnt++;

                    dezen.click(function() {
                        if(currentwindow)currentwindow.close();
//                        alert(index);
                        balloons[index].open(map,  markers[index]);
                        balloons[index].setZIndex(zindexInfoWindow++);
                       currentwindow=infowindow;
                    });
                }
            });
        });
    }
            );
}
