var application = {
    lastFirstChildren: function()
    {
        $('.footer div:last-child, '+
          '.left-menu div ul li:last-child, '+
          '.row > div:last-child, '+
          '.breadcrumbs ul li:last-child, '+
          '.main form fieldset:last-child, '+
          '.main form fieldset div:last-child').addClass('last-child');
        
        $('.row > div:first-child, '+
          '.main form fieldset:first-child, '+
          '.left-menu div ul li:first-child').addClass('first-child');
    },
    menu: function()
    {
        var mLeft = $('.menu').offset().left;
        var max = $('.menu').width();
        var wSubMenu = 200;
        
        $('.menu > li').hover(function(){
            var link = $(this).children('a');
            if ((link.offset().left - link.width() - mLeft - max + wSubMenu) > 0)
                link.next().css({left: 'auto', right: 0});
            
            $(this).addClass('hover');
        },function(){
            $(this).removeClass('hover');
        });
    },
    gallery: {
        o: {
            aSpeed: 5000,
            aInterval: 7000,
            aSpeed2: 1000,
            aInterval2: 8000,
            timer: null
        },
        init: function()
        {
            $('.gallery a').fancybox({titlePosition:'inside'});
            
            var w = $(document).width();
            var h = $(document).height()-10;
            $('.tours a').click(function(){
                window.open($(this).attr('href'), "_blank", "width="+w+",height="+h+",menubar=no,resizable=no,scrollbars=no,status=no,location=no,directories=no");
                return false;
            });
            var self = this;
            
            self.animate(self, $('.main-img img'));
            self.animate(self, $('.promo > div > a'), $('.promo-window'));
            
        },
        animate: function (self, list, target)
        {
            if (!list.length)
                return;
            
            list.last().addClass('active').prevAll().hide();
            if (target)
                target.attr('href', list.last().attr('href'));
            
            self.o.timer = setInterval(function(){
                var item = list.filter('.active');
                var next = item.next();
                if (!next.length) next = list.first();
                
                item.fadeOut(self.o.aSpeed, function(){$(this).removeClass('active')}).next();
                next.fadeIn(self.o.aSpeed, function(){
                    $(this).addClass('active')
                    if (target)
                        target.attr('href', next.attr('href'));
                });
            }, self.o.aInterval);
        },
        animate2: function (self, list, target)
        {
            if (!list.length)
                return;
            
            list.last().addClass('active').prevAll().hide();
            target.attr('href', list.last().attr('href'));
            
            self.o.timer = setInterval(function(){
                var item = list.filter('.active');
                var next = item.next();
                if (!next.length) next = list.first();
                
                item.fadeOut(self.o.aSpeed2, function(){
                    $(this).removeClass('active');
                    next.fadeIn(self.o.aSpeed2, function(){$(this).addClass('active')});
                    target.attr('href', next.attr('href'));
                });
            }, self.o.aInterval2);
        }
    },
    formValidate: {
        message: 'Вы заполнили форму не полностью',
        errorClass: 'f-error',
        init: function()
        {
            var self = this;
            $('form').submit(function(){
                var hasError = false;
                $(this).find('.'+self.errorClass).removeClass(self.errorClass);
                $(this).find('.required input, .required textarea, .required select').each(function(){
                    if ($.trim($(this).val()) == ''){
                        $(this).parent().addClass(self.errorClass);
                        hasError = true;
                    }
                });
                if (hasError){
                    alert(self.message);
                    return false;
                }
                return true;
            });
        }
    },
    datepicker: function() {
        var dMS = 86400000;
        $( ".datepicker" ).each(function(){
            var datep = $(this).datepicker({
                showOn: "both",
                buttonImage: "/img/date-picker.png",
                buttonImageOnly: true
            });
        });
        var count = $('#count');
        var checkin = $('#checkin');
        var checkuot = $('#checkout');
        $('#count, #checkin').change(function(){
            var date = new Date()
            date.setTime(checkin.datepicker("getDate").getTime() + count.val() * dMS);
            checkuot.datepicker("setDate", date);
        });
        checkuot.change(function(){
            var date = new Date()
            date.setTime(checkuot.datepicker("getDate").getTime() - count.val() * dMS);
            checkin.datepicker("setDate", date);
        });
        checkin.datepicker("setDate", "+0").change();
        checkuot.datepicker("setDate", "+1").change();
        
        $('.bron').submit(function(){
            var val1 = checkin.val();
            var val2 = checkuot.val();
            
            checkin.val($.datepicker.formatDate('yy-mm-dd', checkin.datepicker("getDate")));
            checkuot.val($.datepicker.formatDate('yy-mm-dd', checkuot.datepicker("getDate")));
            
            setTimeout(function(){ checkin.val(val1); checkuot.val(val2); }, 100);
        });
    },
    externalLinks: function() {
        $("a[href^='http://']:not([href*='" + document.location.hostname + "']), a[rel='external']").click(function(){
            window.open($(this).attr('href'));
            return false;
        });
    }
}
$(document).ready(function(){
    (function() {
        this.lastFirstChildren();
        this.menu();
        this.datepicker();
        this.gallery.init();
        this.formValidate.init();
        this.externalLinks();
    }).call(application);
});

