/**
 * Version 0.10
 * based on jQuery
 * base object for hotelreview of reisen.de
 *
 */

(function($){
    $.hotelreview = {
        refine : {
            init : function() {
                $('.fbpanel').click(function(){
                    $(this).next('.fbcontainer').toggleClass('hide');
                    $(this).next('.fbcontainer').next('.fbcontainerAct').toggleClass('hide');
                });
                
                $('.fbcontainer label').hover(function(){
                    $(this).addClass('labelhover');
                },
                function(){
                    $(this).removeClass('labelhover');
                });
                
                $('.fbregionsbox input[type="radio"]').click(function(){
                   $.hotelreview.refine.getRegionByCat(this);
                });
                
                $('.fbregionsbel input[type="radio"]').change(function(){
                   $.hotelreview.refine.hideRegion(this);
                });
                
                $('#filterform').submit(function(){
                    var lastChecked = $('.fbregionsbox input:checked:last');
                    if (lastChecked.length > 0) {
                        $('#filterform').attr('action',$('#filterform').attr('action') + 'id/' + $(lastChecked).val() + '/name/' + $(lastChecked).attr('title').replace(/\s\/\s/gi,'-') +'/');
                        return true;
                    }
                    $('#filterform').attr('action',$('#filterform').attr('action') + 'id/25000/name/Hotels/');
                    return true;
                });
            },
            
            togglePreloader : function(relation) {
                if ($('#fbregions' +relation+ 'load').css('display') == 'none') {
                    $('#fbregions' +relation+ 'load').css('display', 'block');
                } else {
                    $('#fbregions' +relation+ 'load').css('display', 'none');
                }
            },
            
            getRegionByCat :  function(element) {
                var relation = $(element).attr('rel');
                var catCode = $(element).val();
                relation++;
                if ($('#fbregions' + relation + 'cont').hasClass('hide')) {
                    $('#fbregions' + relation + 'cont').prev().trigger('click');
                }
                if ($('#fbregions' + relation + 'load').css('display') == 'none') {
                    this.togglePreloader(relation);
                }
                
                $('#fbregions' + relation + 'cont .fbregionsbox').empty();
                $('#fbregions' + relation + 'cont .fbregionsbel').hide();
                $.ajax({
                    type : "GET",
                    url : 'hotelcategory/getfilterregions/cat/' + catCode,
                    dataType : 'json',
                    success : function(json) {
                        $.hotelreview.refine.setRegions(relation, json);
                    }
                });
            },
            
            setRegions : function (relation, data) {
                this.togglePreloader(relation);
                $('#fbregions' + relation + 'cont .fbregionsbel').show();
                $.each(data, function(index, region){
                    var inputElement = '<label><input type="radio" title="'+ region.name +'" value="'+ region.categoryid +'" name="filter[category]['+ relation+']" rel="'+ relation +'"/>'+ region.name +'</label>';
                    $('#fbregions' + relation + 'cont .fbregionsbox').append(inputElement);
                });
                
                $('#fbregions' + relation + 'cont .fbregionsbox input[type="radio"]').click(function(){
                   $.hotelreview.refine.getRegionByCat(this);
                });
                
                 $('#fbregions' + relation + 'cont .fbregionsbox label').hover(function(){
                    $(this).toggleClass('labelhover');
                },
                function(){
                    $(this).toggleClass('labelhover');
                });
            },
            
            hideRegion : function(element) {
                var relation = $(element).attr('rel');
                relation++;
                $('#fbregions' + relation + 'cont').prev().trigger('click');
            }
        }
    }
})(jQuery);

jQuery(document).ready(function(){
    jQuery.hotelreview.refine.init();
});
