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; }
        
        var persons = 0;
        
        $('#persdiv .radio').each(function(i,el){
            if($(el).hasClass('on'))
            {
                persons = $('#persdiv :checked').val();
            }
        });
                
        $.post('call/counter', {
                                            ajax: true, 
                                            country: $('#list-countries-selected').val(), 
                                            region: $('#list-regions-selected').val(), 
                                            persons: persons, 
                                            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(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 span').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");
                }
        });
}
 
 
$(document).ready(function() {

    $(".type-icon").live('mouseover',function() {
        var p = $(this);
        if($(p).hasClass("spot"))
        {
            $("#tooltip").html("Op deze camping zijn <br /> kampeerplaatsen beschikbaar");
        }

        if($(p).hasClass("caravan"))
        {
            $("#tooltip").html("Op deze camping zijn <br /> stacaravans beschikbaar");
        }

        if($(p).hasClass("tent"))
        {
            $("#tooltip").html("Op deze camping zijn ingerichte <br /> bungalowtenten beschikbaar");
        }

        var position = p.offset();
        $("#tooltip").css("top",(position.top+10)+"px");
        $("#tooltip").css("left",(position.left+10)+"px");
        $("#tooltip").show();
    });

    $(".type-icon").live('mouseout',function() {
        $("#tooltip").hide();
    });

    $('.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('call/reset', {url: current_url, reset: 'filters'}, function(data) {
            window.location.href = current_url;
        });
        return false;
    });
    
    $('.reset-filter-all').live('click',function() {
        $.post('call/reset', {url: 'campings', reset: 'filters'}, function(data) {
            window.location.href = 'campings';
        });
        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().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;
    });
    
});

        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(); }

