(function($){
    $.fn.placeholder = function() {
        return this.each(function() {
            if( $(this).data('_placeholder') ){
                return;
            }

            $(this).data('_placeholder', true).parents('form').submit(function(){
                $(this).find('.placeholder.off').each(function(){
                    if( $(this).val() == $(this).data('_placeholder_temporary') )
                        $(this).val('');
                    else if( $(this).data('o') ) {
                        $(this).hide();
                        $($(this).data('o')).show();
                    }
                    else
                        $(this).removeClass('off');
                });
            });

            if( $(this).attr('type') == 'password' ) {
                var input = $("<input class='placeholder off'>").attr('value', $(this).attr('value')).insertAfter(this).data('o',this);

                input.focus(function(){
                    $(this).hide();
                    $($(this).data('o')).show().focus();
                });

                $(this).hide().attr('value','').data('o',input).blur(function(){
                    if( $(this).attr('value')=='' ){
                        $(this).hide();
                        $($(this).data('o')).show();
                    }
                });

                if( $.browser.mozilla ){
                    var
                    passw = $(this),
                    func = function(){
                        if( passw.val() != '' ){
                            input.hide();
                            passw.show();
                        }
                        setTimeout(func, 100);
                    };
                    func();
                }
            } else {
                $(this).addClass('off').data('_placeholder_temporary', $(this).attr('value')).bind({
                    focus: function(){
                        if( $(this).is('.off') ){
                            $(this).removeClass('off')
                            .attr('value','');
                        }
                    },
                    blur: function(){
                        if( $(this).attr('value')=='' ){
                            $(this).addClass('off')
                            .attr('value', $(this).data('_placeholder_temporary'));
                        }
                    }
                });
            }
        });
    };
    $.fn.tooltip = function() {
        return this.live({
            mouseenter: function(e) {
                var bb = $("<div class='tooltip-box'></div>").text($(this).text()).appendTo($('body'))
                .css({left: e.pageX+20, top: e.pageY+10}).hide();
                if( this.getAttribute('tooltip-content') )
                    bb.html(this.getAttribute('tooltip-content'));
                setTimeout(function(){bb.show();}, 500);
            },
            mouseleave: function() {
                $("body>.tooltip-box").fadeOut(300, function(){$(this).remove()});
            }
        });
    };
    $.fn.splash = function(s){
        var o = {closeLabel: '�������'};
        $.extend(o, s);
        return this.each(function(){
            $("#splash").remove();
            var
            splash = $("<div id='splash'><div><div><div></div></div></div>").appendTo($('body')).hide(),
            content = splash.children().children().children().append(this),
            close = $('<div class="splash-actions"><a href="." class="close-splash">'+o.closeLabel+'</a></div>').appendTo(content);

            close.children('a').click(function(){
                if( $.browser.msie && $.browser.version < 8 )
                    splash.remove();
                else
                    splash.fadeOut(300, function(){$(this).remove()});
                return false;
            });
            if( $.browser.msie && $.browser.version < 8 )
                splash.show();
            else
                splash.fadeIn(300);
        });
    }
})(jQuery);


var makeHeight = function ()
{
    var maxHeight = 0;
    $(".text_height").each(function(){
        if( $(this).height() > maxHeight )
            maxHeight = $(this).height();
    });

    $(".text_height").each(function(){
        $(this).height(maxHeight) > maxHeight;
    });
},
bannerRotate = function(){
    var imgs = $(".head-img>img:not(:visible)"),
    next = imgs.eq( Math.floor(Math.random()*imgs.length) ),
    curr = $(".head-img>img:visible");

    if( next.length ) {
        next.fadeIn(1000);
        curr.fadeOut(1000);

        setTimeout(bannerRotate, 5000);
    }
};


jQuery(function($){

    makeHeight();
    setTimeout(bannerRotate, 5000);




    var lang = location.toString().replace(/^http:\/\/[^\/]+\//,'').replace(/\/.*$/,'');

    $(".placeholder").placeholder();
    $(".tooltip").tooltip();

    var div = $("<div></div>").appendTo('body').hide();
    var els = $("div.smoking, div.no-smoking").appendTo(div);
    $("input.no-smoking, input.smoking").change(function(){
        var nos = $("input.no-smoking").attr('checked');
        var s = $("input.smoking").attr('checked');

        if( !s && !nos ){
            if( $(this).hasClass('smoking') )
                $("input.no-smoking").attr('checked','checked');
            else
                $("input.smoking").attr('checked','checked');

            nos = $("input.no-smoking").attr('checked');
            s = $("input.smoking").attr('checked');
        }

        var c = 0;
        $(".badges").remove();
        var badge = $("<div class='three cols badges'>").insertAfter('.booking.rooms-list>div:first-child');
        els.each(function(){
            if( (nos && $(this).hasClass('no-smoking')) || (s && $(this).hasClass('smoking')) ){
                if( c%3 == 0 && c != 0 )
                    badge = $("<div class='three cols badges'>").insertAfter( badge ).show();

                $(this).clone().appendTo(badge);
                c++;
            }
        })
    });
    $("input.no-smoking").trigger('change');

    $('body>div:first-child>.aside')
    .css({paddingBottom: 140+$("body>.footer>.aside>.bottom").height()});
    $('body>div:first-child>.article')
    .css({paddingBottom: 140+$("body>.footer>.article>.bottom").height()});

    $(".submit").click(function(){$(this).parents('form').submit();return false;})

    $(".calendar .next, .calendar .prev").live('click', function(){
        $(this).parents(".calendar").animate({opacity:.2}, 300)
        .load('/'+lang+'/calendar/calendar/' + this.getAttribute("rel"), function(){
            $(this).stop().children().unwrap().css({display:'block', opacity: .2}).animate({ opacity: 1 }, 150);
        });
        return false;
    });

    $(".events .change_date").click(function(){
        $(".calendar").slideToggle();
        return false;
    });
    
	$(".redactor_file_link").bind('click', function(){
		window.open('/upload/redactor/files/' + this.getAttribute('rel'), '_newtab');
	});
});
