/*--GLOBAL--*/
var oLang = {
    "sLengthMenu": "Näytä _MENU_ tulosta per sivu",
    "sZeroRecords": "Ei löytynyt",
    "sInfo": "Näytä _START_ / _END_ yhteensä: _TOTAL_ tulosta",
    "sInfoEmpty": "Näytä 0 / 0 yht: 0 tulosta",
    "sInfoFiltered": "(filtered from _MAX_ total records)",
    "sSearch": "Hae listalta:",
    "oPaginate": {
        "sNext": "Seuraava",
        "sPrevious": "Edellinen"
    }
}

var map = false;
/* BAARIPELIT.FI -juttuja 14.7.2014 */
$(document).ready(function () {
    onResize();
});

$(window).resize(function () {
    onResize();
});

function onResize() {
    var wrapperWidth = $('div#mapwrapper').width();
    if (map) {
        google.maps.event.trigger(map, "resize");
    }

    if ($(window).width() <= 767) {
        $('#pubimap').height(wrapperWidth);
        //console.log(wrapperWidth);
        $('img.salli').hide();
        $('#rightwrapper').css('margin-top', wrapperWidth / 1.4);
    } else {
        $('img.salli').show();
        $('#rightwrapper').css('margin-top', 0);
    }
}


jQuery(document).ready(function ($) {
    $('body').on('click', '.make_order_musabingo', function (ev) {
        ev.preventDefault();
        //alert("hashdha");
        var modal = $('#musabingoOrderModal');
        $(modal).modal({
            backdrop: 'static'
        });
        var pelin_nimi = $(this).attr('data-peli');
        $('#musabingoOrderModal').find('input.pelin_nimi').val(pelin_nimi);
        $("#contactForm").find('.success_message').hide();
        //$("#orderForm").find('.form-group').removeClass('has-success');
        $('#musabingoOrderModal').modal('show');
    });

    $('#contactFormMusabingo').submit(function (ev) {
        ev.preventDefault();
        var frm = $(this);
        var btn = $("form#contactFormMusabingo").find('input:submit').css('visibility', 'hidden');
        btn.hide();
        var msg = $("form#contactFormMusabingo").find('.success_message').html('<img src="/themes/pubivisa/img/hloader.gif"/>').show();
        $.ajax({
            type: frm.attr('method'),
            url: "/index.php/tools/packages/pubivisa/yhteydenotto/liidit/musabingo_liidi/",
            data: frm.serialize(),
            dataType: 'json',
            success: function (data) {
                if (data.success == 0) {
                    msg.html("<div class='alert alert-error'>" + data.msg + "</div>");
                }
                if (data.success == 1) {
                    //console.log(data);
                    //orderForm.resetForm();
                    $("form#contactFormMusabingo")[0].reset();
                    $("#contactFormMusabingo").find('.form-group').removeClass('has-success');
                    //orderForm.destroy();
                    btn.show();

                    window.location = "https://www.baaripelit.fi/materiaalipankki-kiitossivu";
                    $('#musabingoOrderModal').modal('hide');
                }
            }
        });

    });
});

/* ------------------- Login  --------------------- */
jQuery(document).ready(function ($) {

    /*------------------- Login -------------- */
    var lLoader = $("#btn_login_loader").hide();

    $('#uPassword').bind('keypress', function (e) {
        if (e.keyCode == 13) {
            doLogin();
        }
    });

    $("#btn_login").bind('click', function (e) {
        doLogin();
        return false;
    });

    var doLogin = function () {
        lLoader.show();

        var btn = $('#btn_login').hide();
        $('.alert-error').fadeOut(function () {
            $(this).remove();
        });

        $.ajax({
            type: "POST",
            url: "/index.php/login/do_login/",
            data: {'uName': $("#uName").val(), 'uPassword': $("#uPassword").val(), 'format': 'JSON'},
            dataType: 'json'
        }).done(function (obj) {
            if (obj.success > 0) {
                $(".login_wrapper").hide();
                location.reload();
            } else {
                lLoader.fadeOut(function () {
                    btn.show();
                    var error = '<div class="alert alert-error login-alert-top z10"> \
					<button data-dismiss="alert" class="close" aria-hidden="true" type="button">×</button> \
					<span class="msg"><strong>Hups!</strong><br/>' + obj.error + '</span> \
					</div>'
                    btn.parent().append(error);
                });
            }
        });
        return false;
    }

});

/* ------------------- PubiMap  --------------------- */
jQuery(document).ready(function ($) {
    // Initialize the Google Maps API v3

    var lat = '64.9146659';
    var lng = '26.0672553';
    var iw;
    var map;
    var filtertype = 'all';
    var dayfilter = 'all';
    var markers = new Array();
    var mc;
    //console.log("lat = "+lat);
    //console.log("lng = "+lng);



    $('#mapwrapper').on('click', '.btn-day-filter', function () {



        $('.btn-day-filter.active').not(this).removeClass('active');
        dayfilter = $(this).attr('data-day');
        for (var i = 0; i < markers.length; i++) {
            markers[i].setMap(null);
        }
        markers = new Array();
        mc.clearMarkers();

        lat = '64.9146659';
        lng = '26.0672553';
        //if ($.cookie('lat') > 0 && $.cookie('lng') > 0) {
        // console.log('location cookie');
        //lat = $.cookie('lat');
        // lng = $.cookie('lng');
        //$('#omasijainti').html($.cookie('omasijainti'));
        $('#coords_status').fadeOut();
        map.setCenter(new google.maps.LatLng(lat, lng));
        addVisaRavintolat(lat, lng, true);
        markersAdded = true;
        /*   return; /*
         } else if (geoip_latitude() > 0 && geoip_longitude() > 0) {
         lat = geoip_latitude();
         lng = geoip_longitude();
         $('#omasijainti').html(geoip_city());
         map.setCenter(new google.maps.LatLng(lat, lng));
         addVisaRavintolat(lat, lng, true);
         markersAdded = true;
         $('#coords_status').fadeOut();
         $.cookie('lat', lat);
         $.cookie('lng', lng);
         $.cookie('omasijainti', geoip_city());
         }*/
    });

    $(document).on('change', '#ohjelma_filter', function () {
        filtertype = $(this).val();
        for (var i = 0; i < markers.length; i++) {
            markers[i].setMap(null);
        }
        markers = new Array();
        mc.clearMarkers();

        // if ($.cookie('lat') > 0 && $.cookie('lng') > 0) {
        // console.log('location cookie');
        // lat = $.cookie('lat');
        // lng = $.cookie('lng');

        lat = '64.9146659';
        lng = '26.0672553';

        //$('#omasijainti').html($.cookie('omasijainti'));
        $('#coords_status').fadeOut();
        map.setCenter(new google.maps.LatLng(lat, lng));
        addVisaRavintolat(lat, lng, true);
        markersAdded = true;
        return; /*
         //} /*else if (geoip_latitude() > 0 && geoip_longitude() > 0) {
         lat = geoip_latitude();
         lng = geoip_longitude();
         $('#omasijainti').html(geoip_city());
         map.setCenter(new google.maps.LatLng(lat, lng));
         addVisaRavintolat(lat, lng, true);
         markersAdded = true;
         $('#coords_status').fadeOut();
         $.cookie('lat', lat);
         $.cookie('lng', lng);
         $.cookie('omasijainti', geoip_city());
         }*/
    });


    if ($('#pubimap').length) {
        iniMap();
    }

    function iniMap() {
        var lat = '64.9146659';
        var lng = '26.0672553';
        var geocoder = new google.maps.Geocoder();
        var markersAdded = false;
        iw = new google.maps.InfoWindow();
        // IP pohjainen paikannus Google (toimii joka kerta mutta heittää hirveesti)
        if (google.loader.ClientLocation != null) {
            // lat = google.loader.ClientLocation.latitude;
            // lng = google.loader.ClientLocation.longitude;

            lat = '64.9146659';
            lng = '26.0672553';
        }
        var styleArr = new Array();
        map = new google.maps.Map($('#pubimap')[0], {
            styles: styleArr,
            zoom: 5,
            center: new google.maps.LatLng(lat, lng),
            mapTypeId: google.maps.MapTypeId.ROADMAP, //TERRAIN
            scrollwheel: false,
            navigationControlOptions: {
                position: google.maps.ControlPosition.RIGHT
            },
            zoomControlOptions: {
                style: google.maps.ZoomControlStyle.DEFAULT,
                position: google.maps.ControlPosition.LEFT_CENTER
            },
            panControl: true,
            panControlOptions: {
                position: google.maps.ControlPosition.LEFT_BOTTOM
            }
        });
        // Create the search box and link it to the UI element.
        /* var input = document.getElementById('pac-input');
         var searchBox = new google.maps.places.SearchBox(input);
         map.controls[google.maps.ControlPosition.TOP_RIGHT].push(input);
         
         // Bias the SearchBox results towards current map's viewport.
         map.addListener('bounds_changed', function () {
         searchBox.setBounds(map.getBounds());
         });
         
         var markers = [];
         // Listen for the event fired when the user selects a prediction and retrieve
         // more details for that place.
         searchBox.addListener('places_changed', function () {
         var places = searchBox.getPlaces();
         
         if (places.length == 0) {
         return;
         }
         
         // Clear out the old markers.
         markers.forEach(function (marker) {
         marker.setMap(null);
         });
         markers = [];
         
         // For each place, get the icon, name and location.
         var bounds = new google.maps.LatLngBounds();
         places.forEach(function (place) {
         if (!place.geometry) {
         console.log("Returned place contains no geometry");
         return;
         }
         var icon = {
         url: place.icon,
         size: new google.maps.Size(71, 71),
         origin: new google.maps.Point(0, 0),
         anchor: new google.maps.Point(17, 34),
         scaledSize: new google.maps.Size(25, 25)
         };
         
         // Create a marker for each place.
         markers.push(new google.maps.Marker({
         map: map,
         icon: icon,
         title: place.name,
         position: place.geometry.location
         }));
         
         if (place.geometry.viewport) {
         // Only geocodes have viewport.
         bounds.union(place.geometry.viewport);
         } else {
         bounds.extend(place.geometry.location);
         }
         });
         map.fitBounds(bounds);
         }); */



        //autocomplete
        /* var autocomplete = new google.maps.places.Autocomplete($('#karttaHaku')[0]);
         
         autocomplete.bindTo('bounds', map);
         
         google.maps.event.addListener(autocomplete, 'place_changed', function() {
         
         var place = autocomplete.getPlace();
         if (!place.geometry) {
         if ($(".pac-container .pac-item").length) {
         
         firstResult = $(".pac-container .pac-item:first").text();
         geocoder.geocode({"address": firstResult}, function(results, status) {
         if (status == google.maps.GeocoderStatus.OK) {
         map.setCenter(results[0].geometry.location);
         addVisaRavintolat(results[0].geometry.location.lat(), place.geometry.location.lng(), false);
         }
         });
         
         }
         return;
         }
         map.setCenter(place.geometry.location);
         
         addVisaRavintolat(place.geometry.location.lat(), place.geometry.location.lng(), false);
         });
         
         */

        if ($.cookie('lat') > 0 && $.cookie('lng') > 0) {
            // console.log('location cookie');
            lat = '64.9146659';
            lng = '26.0672553';
            //$('#omasijainti').html($.cookie('omasijainti'));
            $('#coords_status').fadeOut();
            map.setCenter(new google.maps.LatLng(lat, lng));
            addVisaRavintolat(lat, lng, true);
            markersAdded = true;
            return;
        }

        // IP Pohjainen paikannus GEO IP
        /*
         if (geoip_latitude() > 0 && geoip_longitude() > 0) {
         lat = geoip_latitude();
         lng = geoip_longitude();
         $('#omasijainti').html(geoip_city());
         map.setCenter(new google.maps.LatLng(lat, lng));
         addVisaRavintolat(lat, lng, true);
         markersAdded = true;
         $('#coords_status').fadeOut();
         $.cookie('lat', lat);
         $.cookie('lng', lng);
         $.cookie('omasijainti', geoip_city());
         }*/


        //$('#omasijainti').html(geoip_city());
        map.setCenter(new google.maps.LatLng(lat, lng));
        addVisaRavintolat(lat, lng, true);
        markersAdded = true;
        $('#coords_status').fadeOut();
        //$.cookie('lat', lat);
        //$.cookie('lng', lng);
        //$.cookie('omasijainti', geoip_city());

        //HTML5 / selain / mobiili kysytty tarkka sijainti
        /*if (isMobile()) {
         if (navigator.geolocation) {
         //  console.log('navigator alustetaan');
         navigator.geolocation.getCurrentPosition(function (position) {
         
         
         lat = position.coords.latitude;
         lng = position.coords.longitude;
         
         //$.cookie('lat', lat);
         //$.cookie('lng', lng);
         
         $('#coords_status').fadeOut();
         
         map.setCenter(new google.maps.LatLng(lat, lng));
         
         
         
         }, locationFail);
         }
         
         }*/


    }
    function isMobile() {

        if (navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/BlackBerry/i) || navigator.userAgent.match(/iPhone|iPad|iPod/i) || navigator.userAgent.match(/IEMobile/i))
            return true;
        return false;
    }

    function locationFail(error) {
        // console.log('navigator error');
        $('#coords_status').fadeOut();
    }

    function addMarker(data) {
        // var visat = data.visailut.length;

        // status tilat
        // 1 = aktiivinen
        // 2 = tuleva kausi
        var ohjelma = data.tila;
        //console.log(data);
        var icon;

        var shape = {
            coord: [1, 1, 1, 40, 18, 40, 18, 1],
            type: 'poly'
        };
        var image;

        var html = '<h2 style="color:#666;">' + data.nimi + '</h2>';
        if (data.logo) {
            html += data.logo;
        }
        html += '<p>' + data.osoite + '</p>';

        $(ohjelma).each(function (k, v) {
            var tila = v;

            if (typeof tila.pahkina != 'undefined' && tila.pahkina.status == 1 && typeof tila.keno == 'undefined' && typeof tila.bingo == 'undefined' && typeof tila.musabingo == 'undefined') {
                image = {
                    url: "/themes/pubivisa/images/marker_green.png",
                    // This marker is 20 pixels wide by 32 pixels tall.
                    size: new google.maps.Size(70, 64),
                    // The origin for this image is 0,0.
                    origin: new google.maps.Point(0, 0),
                    // The anchor for this image is the base of the flagpole at 0,32.
                    anchor: new google.maps.Point(35, 32)
                };
            } else if (typeof tila.pahkina != 'undefined' && tila.pahkina.status == 2 && typeof tila.keno == 'undefined' && typeof tila.bingo == 'undefined' && typeof tila.musabingo == 'undefined') {
                image = {
                    url: "/themes/pubivisa/images/marker_yellow.png",
                    // This marker is 20 pixels wide by 32 pixels tall.
                    size: new google.maps.Size(70, 64),
                    // The origin for this image is 0,0.
                    origin: new google.maps.Point(0, 0),
                    // The anchor for this image is the base of the flagpole at 0,32.
                    anchor: new google.maps.Point(35, 32)
                };
            } else if (typeof tila.pahkina != 'undefined' && tila.pahkina.status == 3 && typeof tila.keno == 'undefined' && typeof tila.bingo == 'undefined' && typeof tila.musabingo == 'undefined') {
                image = {
                    url: "/themes/pubivisa/images/marker_red.png",
                    // This marker is 20 pixels wide by 32 pixels tall.
                    size: new google.maps.Size(70, 64),
                    // The origin for this image is 0,0.
                    origin: new google.maps.Point(0, 0),
                    // The anchor for this image is the base of the flagpole at 0,32.
                    anchor: new google.maps.Point(35, 32)
                };
            } else if (typeof tila.keno != 'undefined' && tila.keno.status == 1 && typeof tila.pahkina == 'undefined' && typeof tila.bingo == 'undefined' && typeof tila.musabingo == 'undefined') {
                image = {
                    url: "/themes/baarikeno/images/marker_green.png",
                    // This marker is 20 pixels wide by 32 pixels tall.
                    size: new google.maps.Size(70, 64),
                    // The origin for this image is 0,0.
                    origin: new google.maps.Point(0, 0),
                    // The anchor for this image is the base of the flagpole at 0,32.
                    anchor: new google.maps.Point(35, 32)
                };
            } else if (typeof tila.keno != 'undefined' && tila.keno.status == 2 && typeof tila.pahkina == 'undefined' && typeof tila.bingo == 'undefined' && typeof tila.musabingo == 'undefined') {
                image = {
                    url: "/themes/baarikeno/images/marker_yellow.png",
                    // This marker is 20 pixels wide by 32 pixels tall.
                    size: new google.maps.Size(70, 64),
                    // The origin for this image is 0,0.
                    origin: new google.maps.Point(0, 0),
                    // The anchor for this image is the base of the flagpole at 0,32.
                    anchor: new google.maps.Point(35, 32)
                };
            } else if (typeof tila.keno != 'undefined' && tila.keno.status == 3 && typeof tila.pahkina == 'undefined' && typeof tila.bingo == 'undefined' && typeof tila.musabingo == 'undefined') {
                image = {
                    url: "/themes/baarikeno/images/marker_red.png",
                    // This marker is 20 pixels wide by 32 pixels tall.
                    size: new google.maps.Size(70, 64),
                    // The origin for this image is 0,0.
                    origin: new google.maps.Point(0, 0),
                    // The anchor for this image is the base of the flagpole at 0,32.
                    anchor: new google.maps.Point(35, 32)
                };
            } else if (typeof tila.bingo != 'undefined' && tila.bingo.status == 1 && typeof tila.pahkina == 'undefined' && typeof tila.keno == 'undefined' && typeof tila.musabingo == 'undefined') {
                image = {
                    url: "/themes/baaribingo/images/marker_green.png",
                    // This marker is 20 pixels wide by 32 pixels tall.
                    size: new google.maps.Size(70, 64),
                    // The origin for this image is 0,0.
                    origin: new google.maps.Point(0, 0),
                    // The anchor for this image is the base of the flagpole at 0,32.
                    anchor: new google.maps.Point(35, 32)
                };
            } else if (typeof tila.bingo != 'undefined' && tila.bingo.status == 2 && typeof tila.pahkina == 'undefined' && typeof tila.keno == 'undefined' && typeof tila.musabingo == 'undefined') {
                image = {
                    url: "/themes/baaribingo/images/marker_yellow.png",
                    // This marker is 20 pixels wide by 32 pixels tall.
                    size: new google.maps.Size(70, 64),
                    // The origin for this image is 0,0.
                    origin: new google.maps.Point(0, 0),
                    // The anchor for this image is the base of the flagpole at 0,32.
                    anchor: new google.maps.Point(35, 32)
                };
            } else if (typeof tila.bingo != 'undefined' && tila.bingo.status == 3 && typeof tila.pahkina == 'undefined' && typeof tila.keno == 'undefined' && typeof tila.musabingo == 'undefined') {
                image = {
                    url: "/themes/baaribingo/images/marker_red.png",
                    // This marker is 20 pixels wide by 32 pixels tall.
                    size: new google.maps.Size(70, 64),
                    // The origin for this image is 0,0.
                    origin: new google.maps.Point(0, 0),
                    // The anchor for this image is the base of the flagpole at 0,32.
                    anchor: new google.maps.Point(35, 32)
                };
            } else if (typeof tila.musabingo != 'undefined' && tila.musabingo.status == 1 && typeof tila.pahkina == 'undefined' && typeof tila.keno == 'undefined' && typeof tila.bingo == 'undefined') {
                //if (data.nakyy_nettisivulla_musabingo == 1) {
                image = {
                    url: "/themes/musabingo/images/marker_green1.png",
                    // This marker is 20 pixels wide by 32 pixels tall.
                    size: new google.maps.Size(70, 64),
                    // The origin for this image is 0,0.
                    origin: new google.maps.Point(0, 0),
                    // The anchor for this image is the base of the flagpole at 0,32.
                    anchor: new google.maps.Point(35, 32)
                };
                //}

            } else if (typeof tila.musabingo != 'undefined' && tila.musabingo.status == 2 && typeof tila.pahkina == 'undefined' && typeof tila.keno == 'undefined' && typeof tila.bingo == 'undefined') {
                //if (data.nakyy_nettisivulla_musabingo == 1) {
                image = {

                    url: "/themes/musabingo/images/marker_yellow1.png",
                    // This marker is 20 pixels wide by 32 pixels tall.
                    size: new google.maps.Size(70, 64),
                    // The origin for this image is 0,0.
                    origin: new google.maps.Point(0, 0),
                    // The anchor for this image is the base of the flagpole at 0,32.
                    anchor: new google.maps.Point(35, 32)
                };
                //}

            } else if (typeof tila.musabingo != 'undefined' && tila.musabingo.status == 3 && typeof tila.pahkina == 'undefined' && typeof tila.keno == 'undefined' && typeof tila.bingo == 'undefined') {
                //if (data.nakyy_nettisivulla_musabingo == 1) {
                image = {
                    url: "/themes/musabingo/images/marker_red.png",
                    // This marker is 20 pixels wide by 32 pixels tall.
                    size: new google.maps.Size(70, 64),
                    // The origin for this image is 0,0.
                    origin: new google.maps.Point(0, 0),
                    // The anchor for this image is the base of the flagpole at 0,32.
                    anchor: new google.maps.Point(35, 32)
                };
                //}

            } else {

                if ((typeof tila.pahkina != 'undefined' && tila.pahkina.status == 1) ||
                        (typeof tila.keno != 'undefined' && tila.keno.status == 1) ||
                        (typeof tila.bingo != 'undefined' && tila.bingo.status == 1)
                        || (typeof tila.musabingo != 'undefined' && tila.musabingo.status == 1)) {
                    image = {
                        url: "/themes/baaripelit/images/baaripelit_vihrea_osoitin.png",
                        // This marker is 20 pixels wide by 32 pixels tall.
                        size: new google.maps.Size(70, 64),
                        // The origin for this image is 0,0.
                        origin: new google.maps.Point(0, 0),
                        // The anchor for this image is the base of the flagpole at 0,32.
                        anchor: new google.maps.Point(35, 32)
                    };
                } else if ((typeof tila.pahkina != 'undefined' && tila.pahkina.status == 2) ||
                        (typeof tila.keno != 'undefined' && tila.keno.status == 2) ||
                        (typeof tila.bingo != 'undefined' && tila.bingo.status == 2)
                        || (typeof tila.musabingo != 'undefined' && tila.musabingo.status == 2)) {
                    image = {
                        url: "/themes/baaripelit/images/baaripelit_keltainen_osoitin.png",
                        // This marker is 20 pixels wide by 32 pixels tall.
                        size: new google.maps.Size(70, 64),
                        // The origin for this image is 0,0.
                        origin: new google.maps.Point(0, 0),
                        // The anchor for this image is the base of the flagpole at 0,32.
                        anchor: new google.maps.Point(35, 32)
                    };
                } else {
                    image = {
                        url: "/themes/baaripelit/images/baaripelit_punainen_osoitin.png",
                        // This marker is 20 pixels wide by 32 pixels tall.
                        size: new google.maps.Size(70, 64),
                        // The origin for this image is 0,0.
                        origin: new google.maps.Point(0, 0),
                        // The anchor for this image is the base of the flagpole at 0,32.
                        anchor: new google.maps.Point(35, 32)
                    };
                }
            }




            if (typeof tila.pahkina != 'undefined' && ohjelma.pahkina.status == 1) {
                html += '<strong>Aktiivinen visakausi</strong>';
                html += '<p>' + data.visailut.pahkina.kausi + '</p><strong>Visapäivät:</strong><br/>';
                html += '<ul>';
                if (data.visailut.pahkina.paivat) {
                    $.each(data.visailut.pahkina.paivat, function (i, row) {
                        html += '<li>' + row + ' ' + ' klo ' + data.tila.pahkina.alkamisaika + '</li>';
                    });
                }
                html += '</ul>';
            }

            if (typeof tila.pahkina != 'undefined' && ohjelma.pahkina.status == 2) {
                html += '<strong>Seuraava visakausi:</strong>';
                html += '<p>' + data.visailut.pahkina.kausi + '</p><strong>Visapäivät:</strong><br/>';
                html += '<ul>';
                if (data.visailut.pahkina.paivat) {
                    $.each(data.visailut.pahkina.paivat, function (i, row) {
                        html += '<li>' + row + ' ' + ' klo ' + data.tila.pahkina.alkamisaika + '</li>';
                    });
                }
                html += '</ul>';
            }

            if (typeof tila.pahkina != 'undefined' && ohjelma.pahkina.status == 3) {
                // html += '<strong>Ravintolalle ei ole ilmoitettu uutta visakautta</strong><br/><br/>';
            }

            if (typeof tila.pahkina != 'undefined') {
                html += '<a class="btn btn-small btn-yellow" href="http://www.baaripahkina.fi/ravintolat/' + data.link + '">Näytä visan tiedot</a><br/>';
            }

            if (typeof tila.keno != 'undefined' && ohjelma.keno.status == 1) {
                //var ajat = data.visailut.keno.klo.split('|');
                //console.log(ajat);
                html += '<div class="clear20"></div><strong>Kenokausi</strong>';
                html += '<p>' + data.visailut.keno.kausi + '</p><strong>Kenopäivät:</strong><br/>';
                html += '<ul>';
                if (data.visailut.keno.paivat) {
                    $.each(data.visailut.keno.paivat, function (i, row) {
                        //console.log(row);
                        html += '<li>' + row + ' ' + '</li>';
                    });

                }
                html += 'klo ' + data.visailut.keno.klo;
                html += '</ul>';
            }

            if (typeof tila.keno != 'undefined' && ohjelma.keno.status == 2) {
                html += '<div class="clear20"></div><strong>Seuraava Kenokausi alkaa: </strong>';
                html += '<p>' + data.visailut.keno.alkaa + ' klo ' + data.visailut.keno.klo + '</p>';
                /*
                 html += '<strong>Ravintolalla on tuleva kenokausi</strong><br/><br/>';
                 html += '<p>' + data.visailut.keno.kausi + '</p><strong>Kenopäivät:</strong><br/>';
                 html += '<ul>';
                 if (data.visailut.keno.paivat) {
                 $.each(data.visailut.keno.paivat, function (i, row) {
                 html += '<li>' + row + ' ' + ' klo ' + data.tila.keno.alkamisaika + '</li>';
                 });
                 }
                 html += '</ul>';*/
            }

            if (typeof tila.keno != 'undefined' && ohjelma.keno.status == 3) {
                //   html += '<strong>Ravintolalle ei ole ilmoitettu uutta kenokautta</strong><br/><br/> ';
            }
            if (typeof tila.keno != 'undefined') {
                html += '<a class="btn btn-small btn-yellow" href="http://www.baarikeno.fi/kenoravintolat/' + data.link + '">Näytä kenon tiedot</a><br/>';
            }

            if (typeof tila.bingo != 'undefined' && ohjelma.bingo.status == 1) {
                html += '<strong>Bingokausi</strong>';
                html += '<p>' + data.visailut.bingo.kausi + '</p><strong>Bingopäivät:</strong><br/>';
                html += '<ul>';
                if (data.visailut.bingo.paivat) {
                    $.each(data.visailut.bingo.paivat, function (i, row) {
                        html += '<li>' + row + '</li>';
                    });
                }
                html += 'klo ' + data.visailut.bingo.klo;
                html += '</ul>';
            }

            if (typeof tila.bingo != 'undefined' && ohjelma.bingo.status == 2) {
                html += '<div class="clear20"></div><strong>Seuraava Bingokausi alkaa: </strong>';
                html += '<p>' + data.visailut.bingo.alkaa + ' klo ' + data.visailut.bingo.klo + '</p>';
                /*
                 html += '<ul>';
                 if (data.visailut.bingo.paivat) {
                 $.each(data.visailut.bingo.paivat, function (i, row) {
                 html += '<li>' + row + ' ' + ' klo ' + data.tila.bingo.alkamisaika + '</li>';
                 });
                 }
                 html += '</ul>';
                 */
            }

            if (typeof tila.bingo != 'undefined' && ohjelma.bingo.status == 3) {
                //html += '<strong>Ravintolalle ei ole ilmoitettu uutta bingokautta</strong><br/><br/> ';
            }
            if (typeof tila.bingo != 'undefined') {
                html += '<a class="btn btn-small btn-yellow" href="http://www.baaribingo.fi/bingoravintolat/' + data.link + '">Näytä bingon tiedot</a><br/>';
            }

            if (typeof tila.status != 'undefined' && tila.status == 3) {
                //html += '<strong>Ravintola ei ole aktiivinen</strong><br/><br/> ';
            }


            if (typeof tila.musabingo != 'undefined' && ohjelma.musabingo.status == 1) {
                //console.log("nimi : " + data.nimi + " | nakyy_nettisivulla_musabingo :" + data.nakyy_nettisivulla_musabingo);
                //if (data.nakyy_nettisivulla_musabingo == 1) {
                html += '<strong>Musabingokausi</strong>';
                html += '<p>' + data.visailut.musabingo.kausi + '</p><strong>Musabingopäivät:</strong><br/>';
                html += '<ul>';
                if (data.visailut.musabingo.paivat) {
                    $.each(data.visailut.musabingo.paivat, function (i, row) {
                        html += '<li>' + row + '</li>';
                    });
                }
                html += 'klo ' + data.visailut.musabingo.klo;
                html += '</ul>';
                //}

            }

            if (typeof tila.musabingo != 'undefined' && ohjelma.musabingo.status == 2) {
                if (data.nakyy_nettisivulla_musabingo == 1) {
                    html += '<div class="clear20"></div><strong>Seuraava MusaBingokausi alkaa: </strong>';
                    html += '<p>' + data.visailut.musabingo.alkaa + ' klo ' + data.visailut.musabingo.klo + '</p>';
                }


            }

            if (typeof tila.musabingo != 'undefined' && ohjelma.musabingo.status == 3) {
                //html += '<strong>Ravintolalle ei ole ilmoitettu uutta bingokautta</strong><br/><br/> ';
            }
            if (typeof tila.musabingo != 'undefined') {
                //if (data.nakyy_nettisivulla_musabingo == 1) {
                html += '<a class="btn btn-small btn-yellow" href="http://www.musabingo.fi/musabingoravintolat/' + data.link + '">Näytä musabingon tiedot</a><br/>';

                //}
            }






        });
        var shadow = new google.maps.MarkerImage("/themes/pubivisa/images/shadow-marker.png",
                new google.maps.Size(103.0, 64.0),
                new google.maps.Point(0, 0),
                new google.maps.Point(35.0, 32.0)
                );


        var m = new google.maps.Marker({
            position: new google.maps.LatLng(data.lat, data.lng),
            map: map,
            title: data.nimi.replace(/&nbsp;/gi, ''),
            shadow: shadow,
            icon: image,
            //animation: google.maps.Animation.DROP,
            'nakyy_nettisivulla_musabingo': data.nakyy_nettisivulla_musabingo,
            'filetertype': data.filtertype
        });
        if (data.filtertype == 'peli_musabingo') {
            if (data.nakyy_nettisivulla_musabingo == 0) {
                // hide marker on map
                m.setMap(null);
            }
        }
        google.maps.event.addListener(m, 'click', function () {
            iw.setContent('<div style="padding:10px;">' + html + '</div>');
            iw.open(map, this);


        });

        return m;
    }
    function addVisaRavintolat(lat, lng, marker) {
        var d = new Date();
        var n = d.getTime();


        var ULRL = "/index.php/tools/packages/pubivisa/etusivu/baaripelitravintolat/";
        var DATAT = {'lat': lat, 'lng': lng, 'filtertype': filtertype, 'dayfilter': dayfilter, 'dateN': n};
        //console.log(map);
        $.post(ULRL, DATAT).done(function (data) {
            // console.log(data.ravintolat);
            //var tapahtumat = '';
            $.each(data.ravintolat, function (i, row) {
                if (marker) {
                    var m = addMarker(row);

                    if (filtertype == 'peli_musabingo') {
                        //console.log(m.nakyy_nettisivulla_musabingo);
                        if (m.nakyy_nettisivulla_musabingo == 1) {
                            markers.push(m);
                        }
                    } else {
                        markers.push(m);
                    }

                }

            });

            var mcOptions = {
                gridSize: 40,
                maxZoom: 5,
                styles: [{
                        height: 64,
                        anchor: [37, 0],
                        textSize: 14,
                        url: "/themes/pubivisa/images/m1.png",
                        width: 70
                    }]
            }
            mc = new MarkerClusterer(map, markers, mcOptions);
            /* if (filtertype == 'peli_musabingo') {
             
             for (var it in markers) {
             if(markers[it].nakyy_nettisivulla_musabingo == 0){
             markers[it].setVisible(false);
             }
             console.log(markers[it]);
             //markers[it].setVisible(false);
             }
             mc.repaint();
             } else {
             
             }*/

        }).fail(function () {

            //console.log("Tapahtui virhe");
        }).always(function () {
            // console.log("finished");
        });

        /*  $.ajax({
         type: "POST",
         url: "/index.php/tools/packages/pubivisa/etusivu/baaripelitravintolat/?dateN="+n,
         data: {'lat': lat, 'lng': lng, 'filtertype': filtertype, 'dayfilter': dayfilter,'dateN': n},
         dataType: 'json',
         cache: false
         }).done(function (data) {
         //console.log(data);
         var tapahtumat = '';
         $.each(data.ravintolat, function (i, row) {
         if (marker) {
         var m = addMarker(row);
         markers.push(m);
         }
         
         });
         var mcOptions = {
         gridSize: 40,
         maxZoom: 10,
         styles: [{
         height: 64,
         anchor: [37, 0],
         textSize: 14,
         url: "/themes/pubivisa/images/m1.png",
         width: 70
         }]
         }
         
         
         mc = new MarkerClusterer(map, markers, mcOptions);
         
         
         });*/



    }
    $('#karttaHaku').click(function () {
        locationFail();
    });


    $('#visalist').on('click', '.visa-ilmottaudu', function () {
        $('#modal_ilmottaudu').modal('show');
        return false;
    });


    function getStyleArr() {
        return [{
                "featureType": "administrative.country",
                "elementType": "labels",
                "stylers": [{
                        "visibility": "off"
                    }]
            }, {
                "featureType": "landscape.natural.landcover",
                "stylers": [{
                        "visibility": "off"
                    }]
            }, {
                "featureType": "landscape",
                "stylers": [{
                        "visibility": "off"
                    }]
            }, {
                "featureType": "landscape.natural.landcover",
                "stylers": [{
                        "visibility": "off"
                    }]
            }, {
                "featureType": "poi.park",
                "stylers": [{
                        "visibility": "off"
                    }]
            }, {
                "featureType": "poi.business",
                "stylers": [{
                        "visibility": "off"
                    }]
            }, {
                "featureType": "transit.station",
                "stylers": [{
                        "visibility": "off"
                    }]
            }, {
                "featureType": "transit.line",
                "stylers": [{
                        "visibility": "off"
                    }]
            }, {}];
    }
});




jQuery(document).ready(function ($) {

    /* ------------------- Fancybox --------------------- */
    (function () {

        $('[rel=image]').fancybox({
            type: 'image',
            openEffect: 'fade',
            closeEffect: 'fade',
            nextEffect: 'fade',
            prevEffect: 'fade',
            helpers: {
                title: {
                    type: 'inside'
                }
            }
        });

        $('[rel=image-gallery]').fancybox({
            nextEffect: 'fade',
            prevEffect: 'fade',
            helpers: {
                title: {
                    type: 'inside'
                },
                buttons: {},
                media: {}
            }
        });


    })();


    /* ------------------ Back To Top ------------------- */

    jQuery('#footer-menu-back-to-top a').click(function () {
        jQuery('html, body').animate({scrollTop: 0}, 300);
        return false;
    });


    /* --------------------- Tabs ------------------------ */

    (function () {

        var $tabsNav = $('.tabs-nav'),
                $tabsNavLis = $tabsNav.children('li'),
                $tabContent = $('.tab-content');

        $tabsNav.each(function () {
            var $this = $(this);

            $this.next().children('.tab-content').stop(true, true).hide()
                    .first().show();

            $this.children('li').first().addClass('active').stop(true, true).show();
        });

        $tabsNavLis.on('click', function (e) {
            var $this = $(this);

            $this.siblings().removeClass('active').end()
                    .addClass('active');

            $this.parent().next().children('.tab-content').stop(true, true).hide()
                    .siblings($this.find('a').attr('href')).fadeIn();

            e.preventDefault();
        });

    })();


});

/* ------------------ Tooltips ----------------- */

jQuery(document).ready(function () {

    $('.tooltips').tooltip({
        selector: "a[rel=tooltip]"
    })

});

/* ------------------ Progress Bar ------------------- */

jQuery(document).ready(function ($) {

    $(".meter > span").each(function () {
        $(this)
                .data("origWidth", $(this).width())
                .width(0)
                .animate({
                    width: $(this).data("origWidth")
                }, 1200);
    });
});



jQuery(document).ready(function ($) {

    $('.picture a').hover(function () {
        $(this).find('.image-overlay-zoom, .image-overlay-link').stop().fadeTo('fast', 1);
    }, function () {
        $(this).find('.image-overlay-zoom, .image-overlay-link').stop().fadeTo('fast', 0);
    });

});

/* -------------------- Isotope --------------------- */
jQuery(document).ready(function ($) {

    $('#portfolio-wrapper').imagesLoaded(function () {

        var $container = $('#portfolio-wrapper');
        $select = $('#filters select');

        // initialize Isotope
        $container.isotope({
            // options...
            resizable: false, // disable normal resizing
            // set columnWidth to a percentage of container width
            masonry: {columnWidth: $container.width() / 12}
        });

        // update columnWidth on window resize
        $(window).smartresize(function () {
            //$('#pubimap').height($(window).innerHeight() / 1.5);
            $container.isotope({
                // update columnWidth to a percentage of container width
                masonry: {columnWidth: $container.width() / 12}
            });
        });


        $container.isotope({
            itemSelector: '.portfolio-item'
        });

        $select.change(function () {

            var filters = $(this).val();

            $container.isotope({
                filter: filters
            });

        });

        var $optionSets = $('#filters .option-set'),
                $optionLinks = $optionSets.find('a');
        $optionLinks.click(function () {
            var $this = $(this);
            // don't proceed if already selected
            if ($this.hasClass('selected')) {
                return false;
            }
            var $optionSet = $this.parents('.option-set');
            $optionSet.find('.selected').removeClass('selected');
            $this.addClass('selected');

            // make option object dynamically, i.e. { filter: '.my-filter-class' }
            var options = {},
                    key = $optionSet.attr('data-option-key'),
                    value = $this.attr('data-option-value');
            // parse 'false' as false boolean
            value = value === 'false' ? false : value;
            options[ key ] = value;
            if (key === 'layoutMode' && typeof changeLayoutMode === 'function') {
                // changes in layout modes need extra logic
                changeLayoutMode($this, options)
            } else {
                // otherwise, apply new options
                $container.isotope(options);
            }

            return false;

        });

    });

    $.extend($.fn.dataTableExt.oStdClasses, {
        "sWrapper": "dataTables_wrapper form-inline"
    });


    $("#wrap").fitVids();


    /*$(".navbar-baaripelit li.dropdown").hover(function () {
     $(this).addClass('open');
     }, function () {
     //$(this).removeClass('open');
     });*/

    /*$('body').on('click', '.navbar-baaripelit .dropdown-toggle', function (ev) {
        //ev.preventDefault();
        $('.bpLi2').removeClass('open');
        ev.stopPropagation();
        console.log("hola 1");
        //$(this).closest('li').toggleClass('open');
        $('.bpLi2').removeClass('open');
        $('.dropdown-toggle').dropdown('toggle');

    });*/

    /*$('body').on('click', '.navbar-baaripelit .dropdown-toggle2', function (ev) {
        //ev.preventDefault();
        ev.stopPropagation();
        $('.bpLi2').removeClass('open');
        $('.baaripelit_dropdown').removeClass('open');

        //console.log("hjj 2 ");
        $(this).closest('li').toggleClass('open');
        //$(this).closest('.dropdown').dropdown('toggle');
    });*/

    $(document).click(function () {
        $('.bpLi2').removeClass('open');
    });

    //$('.dropdown-toggle').dropdown('toggle');

    // fiskataan ui-dialog listbox bugia
    $('body').on('click', '.ccm-buttons.dialog-buttons .btn', function (e) {
        //console.log('okck');
        $('.mceListBoxMenu.mceNoIcons.defaultSkin[role="listbox"]').remove();
        $('.mceListBoxMenu.mce_backcolor.defaultSkin[role="listbox"]').remove();
    });
});


