(function() {
    var u = navigator.userAgent.toLowerCase(),
    is = function(t) { 
        return (u.indexOf(t) != -1)
    };

    document.getElementsByTagName("html")[0].className += ([
        (!(/opera|webtv/i.test(u)) && /msie (\d)/.test(u)) ? (' ie ie' + RegExp.$1)
        :is('firefox/2')?' gecko ff2'
        :is('firefox/3')?' gecko ff3'
        :is('gecko/')?' gecko'
        :is('opera/9')?' opera opera9':/opera (\d)/.test(u)?' opera opera'+RegExp.$1
        :is('konqueror')?' konqueror'
        :is('chrome/')?' webkit chrome'
        :is('applewebkit/')?' webkit safari'
        :is('mozilla/')?' gecko':'',
        (is('x11')||is('linux'))?' linux'
        :is('mac')?' mac'
        :is('win')?' win':''
    ].join(''));
})();   
;

(function($) {
    $.fn.placeholder = function(text) {
        var text = text || 'Введите текст для поиска';
        return this.each(function() {
            var el = $(this);
            el.focus(function() {
                if (el.val() == text) {
                    el.removeClass('placeholder');
                    el.val('');
                }
            }).blur(function() {
                if (el.val() == '' || el.val() == text) {
                    el.val(text);
                    el.addClass('placeholder');
                }
            }).trigger('blur')
            
            if (el.parents('form').attr('id') == 'webform-client-form-54') {
                el.parents('form').submit(function() {
                    if (el.val() == '' || el.val() == text) {
                        el.focus();
                        return false;
                    }
                });
            }
        });
    };
        
    function initSlidePanel() {
        if ($('#toolbar').size() > 0)
            return;
        
        var height = 254;
        var opened = false;
        var link = $('.region-header .contacts a');
        var region = $('.region-top');
        var div = $('<div />').width(1).height(0).insertAfter(region);
                
        link.click(function() {
            if (!opened) {
                opened = true;
                
                region.animate({ top: 0 }, 300);
                div.animate({ height: height }, 300);
                link.parent().addClass('active');
            }
            else {
                opened = false;
                
                region.animate({ top: -1*height }, 300);
                div.animate({ height: 0 }, 300);
                link.parent().removeClass('active');
            }
            
            return false;
        });
    }
    
    function initPortfolioBlock() {
        var block = $('#block-views-portfolio-block-portfolio-front');
        var image = block.find('.image');
        var a = block.find('.pager').find('.pager-previous, .pager-next').find('a');
        var disabled = false;
        
        a.click(function() {
            if (disabled)
                return false;
                
            disabled = true;
            image.addClass('loading');
            
            var timer = setInterval(function() {
                if (!image.is(':visible')) {
                    clearInterval(timer);
                    initPortfolioBlock();
                    disabled = false;
                }
            }, 200);
        });
    }
    
    function initProjectPage() {
        var node = $('#block-system-main .node-project');
        var image = node.find('.project-top .image');
        var images = node.find('.project-bottom .images');
        var active = image.find('img.active');
        var activePreview = images.find('a.active');
        
        var offset = image.offset();
        
        images.find('a.image').click(function() {
            var newImage = $(this);
            
            active.removeClass('active');
            activePreview.removeClass('active');
            
            activePreview.insertAfter(newImage);
            
            activePreview = newImage;
            activePreview.addClass('active');
            var id = activePreview.attr('id').replace('-preview', '');
            active = $('#' + id).addClass('active');
            
            $(document).scrollTop(offset.top);
                        
            return false;
        });
        
        $(document).keydown(function(e) {
            if (e.ctrlKey) {
                if (e.keyCode == 37) {
                    var link = node.find('.pager .prev a');
                    if (link.length > 0)
                        window.location = link.attr('href');
                    return false;
                }
                else if (e.keyCode == 39) {
                    var link = node.find('.pager .next a');
                    if (link.length > 0)
                        window.location = link.attr('href');
                    console.log(link);
                    return false;
                }
            }
        });
    }
    
    
    $(document).ready(function() {
        $('#block-search-form input.form-text').placeholder();
        $('#block-multiblock-1 input.form-text').placeholder('поиск');
        $('body.page-search #edit-basic input.form-text').placeholder();
        initSlidePanel();
        
        if ($('body').hasClass('front'))
            initPortfolioBlock();
            
        if ($('body').hasClass('node-type-project'))
            initProjectPage();
    });
})(jQuery);
;

