function randomXToY(minVal,maxVal,floatVal)
{
  var randVal = minVal+(Math.random()*(maxVal-minVal));
  return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
}

$(function() {
    $('.no-js').removeClass('no-js').addClass('js');
    var gallery = $('#gallery');
    gallery.galleria({
        width:gallery.width,
        height:500
    });

    $('#eventDate').datepicker({ dateFormat: 'dd.mm.yy' });
    $.datepicker.regional['de'];

    var winHeight = $(window).height() - 100;
    //$('#impressum').css('maxHeight', winHeight);
    
    $('#blog figure').each(function(){
      tempThis = $(this)
      newRotation = randomXToY(-8,8,0)
      tempThis.attr('style','-webkit-transform: rotate(' + newRotation + 'deg); -moz-transform: rotate(' + newRotation + 'deg); -o-transform: rotate(' + newRotation + 'deg); transform: rotate(' + newRotation + 'deg);'
      );
    })
    
    
    
    $('footer a.open').click(function() {

        $(this).next().slideToggle(function() {
            $('#impressum').css('maxHeight', winHeight);
        });
    });
    //
    $('h2.toggleMe').click(function(){
      console.log($(this).siblings('div.hide'))
      $(this).siblings('div.hide').slideToggle();
    })
    var iePresent = $('.ie8,.ie7,.ie6').length;
    if (iePresent > 0) {
        $('label, label span, .checkboxWrapper p').live('click', function() {
            var $this = $(this);
            $this.next('input').trigger('click');

        });
    }

    $('input[type=submit]').click(function() {
        var $this = $(this);
        $this.val(tempThis.attr('data-sending'));
        $this.attr('disabled', 'disabled');
    });

    var checkbox = {
        init : function() {
            $('.checkboxWrapper input').change(function() {
                checkbox.change($(this));
            });

            $('.checkboxWrapper input:checkbox:checked').each(function() {
                checkbox.change($(this));
            });

            $('.baustein input[type="checkbox"]').bind('change', function() {
                checkbox.recalc($(this));
            });

            $('.baustein input[type="checkbox"]').each(function() {
                checkbox.recalc($(this));
            });


        },

        change: function($this) {
            if ($this.closest('div').hasClass('stil') > 0) {
                if ($('div.stil label.checked').length == 2 && $this.siblings('label').hasClass('checked') !== true) {
                    alert('Bitte wählen sie maximal zwei aus.');
                } else {
                    $this.siblings('label').toggleClass('checked');
                }
            } else {
                $this.siblings('label').toggleClass('checked');
            }
        },

        recalc : function($this) {
            var numChecked = $('.baustein input[type="checkbox"]').filter(':checked').length;
            $('span.price-tag').html(numChecked * 85 + ',00€<small>(inkl. 19% USt.)</small>');

        }
    }

    checkbox.init();

    $("a.price-tag").each(function() {
        var $this = $(this);
        $this.attr('data-price', $this.html());
    });

    $("a.price-tag").live("mouseover mouseout", function(event) {
        var $this = $(this);
        if (event.type == "mouseover") {
            $this.html($this.attr('data-hover-text'));
        } else {
            $this.html($this.attr('data-price'));
        }
    });
    $('.baustein input[type="checkbox"]').bind('change', function() {
        var numChecked = $('.baustein input[type="checkbox"]').filter(':checked').length;
        $('span.price-tag').html(numChecked * 85 + ',00€<small>(inkl. 19% USt.)</small>');
    });

    $('a[href*=#]').click(function() {
        if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
            var $target = $(this.hash);
            $target = $target.length && $target || $('[name=' + this.hash.slice(1) + ']');
            if ($target.length) {
                var targetOffset = $target.offset().top;
                $('html,body').animate({scrollTop: targetOffset}, 1000);
                return false;
            }
        }
    });

});
