var current_url = window.location.pathname;
var google_maps = false;
var pid = 1; 
 
var do_facils = false;
 
function counter()
{

        if(do_facils)
        {
            var filter_facilities = $('#facil :checked').map(function(i,n) {
                return $(n).val();
            }).get();         
        } else { var filter_facilities = 0; }

        $.post(basehref+'/call/counter', {
                                            ajax: true, 
                                            country: $('#list-countries-selected').val(), 
                                            persons: $('#list-persons-selected').val(),
                                            types: $('#list-types-selected').val(),  
                                            locations: $('#list-locations-selected').val(), 
                                            facilities: filter_facilities 
                                        }, function(data) {
            $('#searchright').html(data['total']);
            $('#facil').html(data['facilities']);
        });
}
 
function resetfilter()
{
    $('#reset-filters').trigger('click');
    return false;
}
 
function filter(id)
{
    if(id!=false)
    {
        pid = 1;
        var selected_id = $(id).attr('id');
            selected = $('#'+selected_id+' :first');
        if($(selected).hasClass('radio'))
        {
            $('#filter-persons .radio').each(function(k,el){
                $(el).removeClass('ron');
                $(el).next().attr('checked', false);
            });
        
            var radio = selected_id.replace('filter','radio');
            if($(selected).hasClass('ron'))
            {
                $('#'+radio).attr('checked', false);
                $(selected).removeClass('ron');
            } else {
                $('#'+radio).attr('checked', true);
                $(selected).addClass('ron');
            }
        } else {
            var checkbox = selected_id.replace('filter','checkbox');
            if($(selected).hasClass('con'))
            {
                $('#'+checkbox).attr('checked', false);
                $(selected).removeClass('con');
            } else {
                $('#'+checkbox).attr('checked', true);
                $(selected).addClass('con');
            }
        }
    }
        var filter_persons = $('#filter-persons :checked').map(function(i,n) {
            return $(n).val();
        }).get();
                
        var filter_types = $('#filter-types :checked').map(function(i,n) {
            return $(n).val();
        }).get(); 
        
        var filter_locations = $('#filter-locations :checked').map(function(i,n) {
            return $(n).val();
        }).get(); 
        
        var filter_facilities = $('#filter-facilities :checked').map(function(i,n) {
            return $(n).val();
        }).get(); 

        $.post(basehref+current_url, { ajax: true, persons: filter_persons, types: filter_types, locations: filter_locations, facilities: filter_facilities, page: pid }, function(data) {
                $('#filter-selections .selection-count').remove();
                $('#filter-selections .selection-list').remove();
                $('#filter-persons').remove();
                $('#filter-types').remove();
                $('#filter-locations').remove();
                $('#filter-facilities').remove();
                $('#filter-selections .selection-persons').after(data['selection']['persons']);
                $('#filter-selections .selection-types').after(data['selection']['types']);
                $('#filter-selections .selection-locations').after(data['selection']['locations']);
                $('#filter-selections .selection-facilities').after(data['selection']['facilities']);
                $('#overview-data header').html(data['count']);
                $('#overview-list ul').html(data['data']);
                $('#filter-regions').after(data['filter']);
                $('.pagination').html(data['pagination']);
                var show_filter = false
                $('#filter-selections .selection-count').each(function(i,el){
                    if($(el).html() != "Geen") { show_filter = true; return false; }
                });
                if(show_filter)
                {
                    $('#filter-selections').css('display',"block");
                }
                else
                {
                    $('#filter-selections').css('display',"none");
                }
                $.scrollTo("#overview-data",800);
        });
}
 
 
$(document).ready(function() {

    $('.infoBox').live('mouseover',function(){
        $('.infoBox').each(function(i,el){
            $(el).css('z-index','0');
        });
        $(this).css('z-index','9999');
    });

    $('#filter-countries .more').live('click',function(){
        $(this).remove();
        $('#filter-countries li').show();
        return false;
    });

    $('#filter-regions .more').live('click',function(){
        $(this).remove();
        $('#filter-regions li').show();
        return false;
    });

    $('.images .s').live('mouseover',function() {
        var img_old = $(this).parent().children(':first')[0];
        var img_src = $(img_old).attr('src');
        $(img_old).attr('src',$(this).attr('src').replace(img_small,img_big));
        $(this).attr('src',img_src.replace(img_big,img_small));
    });

    $('#overview-tabs > li > a').live('click',function() {
        var selected = $(this).attr('id').replace('overview-tab-','');
        $('#overview-tabs > li > a').each(function(i,el) {
            $(el).parent().removeClass('active');
        });
        $(this).parent().addClass('active');
        $('.pagination').css('visibility','hidden');
        $('#overview-data .tab-item').each(function(i,el) {
            var el = $(el).attr('id').replace('overview-','');
            if(el == selected) { $('#overview-'+el).css('display','block');
            }else{ $('#overview-'+el).css('display','none'); }
            if(selected=='list') { $('.pagination').css('visibility','visible'); }
            if(selected=='map' && google_maps == false) { overview_maps(); google_maps = true; }
        });
        return false;
    });
    
    $('#park-tabs > li > a').live('click',function() {
        var selected = $(this).attr('id').replace('park-','');
        $('#park-tabs > li > a').each(function(i,el) {
            $(el).parent().removeClass('active');
        });
        $(this).parent().addClass('active');
        $('#park-tabs-info .tab-item').each(function(i,el) {
            var el = $(el).attr('id');
            if(el == selected) {
            if(el=='tab-map' && google_maps == false) { initialize(); google_maps = true; }
            if(el=='tab-reviews' || el=='tab-prices') { $('#park-tabs-info').css('overflow-y','scroll'); } else { $('#park-tabs-info').css('overflow-y','hidden'); }
            $('#'+el).css('display','block');
            }else{$('#'+el).css('display','none');}
        });
        return false;
    });    
    
    $('#filter-selections .selection-count').live('mouseover',function() {
        $('#filter-selections .selection-count').each(function(k,el) {
            $(el).next().css('display','none');
        });
        var pos = $(this).position();
        $(this).next().css('left',pos.left+'px');
        $(this).next().css('display','block');
    });
    
    $('.selection-list').live('mouseleave',function() {
        $(this).css('display','none');
    });
    
    $('#filter-selections .selection-list li').live('click',function() {
        var rr = $(this).attr("id").replace('active','filter');
        $('#'+rr).trigger('click');
    });
    
    $('#reset-filters').live('click',function() {
        $.post(basehref+'/call/reset', {url: current_url, reset: 'filters'}, function(data) {
            window.location.href = basehref+current_url;
        });
        return false;
    });
    
    $('.reset-filter-all').live('click',function() {
        $.post(basehref+'/call/reset', {url: 'kindervakantie', reset: 'filters'}, function(data) {
            window.location.href = basehref+'/kindervakantie';
        });
        return false;
    });
    
    $('#filter div').live('click',function() {
        filter(this);
    });
    
    $('.pagination a').live('click',function(){
        var sel = $(this).html();
        if(sel=="Vorige")
        { 
            if(pid>0) pid--;
        }
        else if(sel=="Volgende")
        { 
            if(pid<mid) pid++;
        }
        else
        {
            pid = sel;
        }
        filter(false);
        return false;
    });
    
    $('#gotoImageTab').click(function(){
        $('#park-tab-images').trigger('click');
        return false;
    });
    
    $('#park-reviews').click(function(){
        var el = $(this).children(':first')[0];
        if(el.tagName != "A")
        {
            $('#park-tab-reviews').trigger('click');
        }
        return false;
    });
    
    
    $('.selectbox').live('click',function(){
    
        if($(this).next().css('display') == "none")
        {
            if($('#'+$(this).next().attr('id')+' li').length > 0)
                $(this).next().css('display','block');
        }
        else
        {
            $(this).next().css('display','none');
        }
    });
    
       
    
    $('.selectrows').live('mouseleave',function(){
        $(this).css('display','none');
    });
    
    $('.rowitem').live('click',function(){  
        $(this).parent().prev().html($(this).html());
        $(this).parent().hide();
        var value = $(this).attr('id').split('-')[1];
        var selected = $(this).parent().attr('id');
        $('#'+selected+'-selected').val(value);
        $('#'+selected+'-selected').trigger('change');
        if(selected == 'list-countries')
        {
            $('#list-regions-selected').attr('selectedIndex', 0);
        }        
        counter();
    });
    
    $('#facil div').live('click',function(){
        var selected = $(this).attr('id').replace('f_','');
        if($('#'+selected).is(':checked'))
        {
            $('#'+selected).attr('checked', false);
            $('#'+selected).prev().removeClass('on').addClass('off');
        }
        else
        {
            $('#'+selected).attr('checked',true);
            $('#'+selected).prev().removeClass('off').addClass('on');
        }
        counter();
    });
    
    $('#list-countries-selected').live('change',function(){
        var country = $(this).val();
        $.post('call/getregions/'+country, { ajax: true }, function(data) {
                $('#list-regions-selected').html(data['options']);
                $('#list-regions').html(data['lis']);
                $('#regions-selected').html("Kies uw regio");
                if(country == 0)
                {
                    $('#regions-selected').html("Selecteer eerst een land");
                    $('#list-regions-selected').html('');
                    $('#list-regions').html('');
                }   
            });            
    });
    
    $("#facildiv span.radio").live('click',function(){ 
        $('#facildiv span.radio').each(function(i,el){
            $(this).removeClass('on').addClass('off');
            $(this).next().attr('checked',false);
        });
        $(this).removeClass('off').addClass('on');
        $(this).next().attr('checked',true);
        if($('#f_facil2').is(':checked'))
        {
            $('#facil').slideUp();
            $('#facildiv span.radio').each(function(i,el){
                $(this).next().attr('checked',false);
            });
            do_facils = false;
            counter();
        } 
        else
        {
            do_facils = true;
            $('#facil').slideDown();
        }
    });
    
    $("#persdiv span.radio").live('click',function(){ 
        $('#persdiv span.radio').each(function(i,el){
            $(this).removeClass('on').addClass('off');
            $(this).next().attr('checked',false);
        });
        $(this).removeClass('off').addClass('on');
        $(this).next().next().attr('checked',true);
        counter();
    });
    
    
    $('#zoektabs a').click(function(){
        $('#zoektabs a').each(function(i,el){
            $(el).removeClass('zoektab').addClass('zoektab-active');
        });
        if($(this).attr('id')=='search-on-map')
        {
            $('#zoeknboekform').hide();    
            $('#maps').show();
            if(!google_maps) { google_maps = true; index_maps(); }
        }
        else
        {
            $('#maps').hide();
            $('#zoeknboekform').show();   
        }
        $(this).removeClass('zoektab-active').addClass('zoektab');        
        return false;
    });
    
    $(".childfriendlynessScore").live("mouseover",function() {
        $(this).next().show();
    });

    $(".childfriendlynessScore").live("mouseleave",function() {
        $(this).next().hide();
    });
    
});

        function Slider ()
        {
            this.bigimg = document.getElementById('bigimg');
            this.currentPhoto = this.bigimg.src;
            this.allPhotos = document.getElementById('thumbs').children;
            var allPhotosSrc = new Array();
            
            for (var i = 0; i < this.allPhotos.length; i++){
                newsrc = this.allPhotos[i].src.replace('77x58', '333x217');
                allPhotosSrc[i] = newsrc;
                if (allPhotosSrc[i] == this.currentPhoto){ this.currentPhoto = i; }
                this.allPhotos[i].photoID = i;
                this.allPhotos[i].onclick = function (){
                    slider.currentPhoto = this.photoID;
                    slider.bigimg.src = allPhotosSrc[this.photoID];
                }
            }
            
            this.nextPhoto = function(){
                var nextphoto = this.currentPhoto +1;
                if (nextphoto >= allPhotosSrc.length){ nextphoto = 0; }
                this.currentPhoto = nextphoto;
                this.bigimg.src = allPhotosSrc[nextphoto];
            }
            
            this.prevPhoto = function (){
                var prevphoto = this.currentPhoto -1;
                if (prevphoto < 0){ prevphoto = allPhotosSrc.length-1; }
                this.currentPhoto = prevphoto;
                this.bigimg.src = allPhotosSrc[prevphoto];
            }
        }
        var slider;
        function nextPhoto() { slider.nextPhoto(); }
        function prevPhoto() { slider.prevPhoto(); }

;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);
