var isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent);

jQuery(function(){
    
    if($('.block_right_column').height() < $('.block_left_column').height()) $('.block_right_column').css({'min-height':$('.block_left_column').height()});
    
    $('a.button_menu').html('<span></span><span></span><span></span>');
    $('body').append('<div id="block_mobile_overlay"></div><div id="button_top"><a href="#"></a></div>');

    $(window).scroll(function () {
        if ($(this).scrollTop() > 10) {
            $('#button_top').fadeIn();
	}
        else {
            $('#button_top').fadeOut();
        }
    });

    $('#button_top a').click(function () {
        $('body,html').animate({scrollTop: 0}, 400);
        return false;
    });
    
    $(window).load(function(e){screenAdapt();});
    $(window).resize(function(e){screenAdapt();});
    
    $("a.button_menu").click(function(){
        
        var offset = $("#block_outer").offset();
                        
        if(offset.left<0){
            $('#block_mobile_overlay').hide();
            $('#block_mobile_menu').animate({right:'-240px'},200);
            //$('.wrapper').animate({right:'0'},500);
            $("body").removeClass('menu-open');
            $(document).height('auto');
        }
        else {
            $('#block_mobile_overlay').show().css({"right":"240px"});
            $('#block_mobile_menu').animate({right:'0'},200);
            //$('.wrapper').animate({right:'240px'},500);
            $("body").addClass('menu-open');
            $(document).height($(window).height());
        }
        return false;
    });
                    
    $("#block_mobile_overlay").click(function(){ 
        $('#block_mobile_overlay').hide();
        $('#block_project_preview').hide(500);
        $('#block_mobile_menu').animate({right:'-240px'},200);
        //$('.wrapper').animate({right:'0'},500);
        $("body").removeClass('menu-open');
        $(document).height('auto');
        return false;
    });
    
    $("a.button_mobile_menu_close").click(function(){ 
        $('#block_mobile_overlay').hide();
        $('#block_mobile_menu').animate({right:'-240px'},200);
        //$('.wrapper').animate({right:'0'},500);
        $("body").removeClass('menu-open');
        $(document).height('auto');
        return false;
    });
    
    $('a.button_popup_menu').click(function(event){
    	   $(this).parent().find('.block_popup_content').fadeIn('fast');
           event.stopPropagation();
        });
        
    $('.block_popup_content').click(function(){$(this).fadeOut('fast');});
    $('.block_popup_content').mouseleave(function(){$(this).fadeOut('fast');});
    
    $(".lightbox-youtube").grtyoutube({autoplay:true,theme:"dark"});
});

function setEqualHeight(columns){
    var tallestcolumn = 0;
    columns.each(function(){
        currentHeight = $(this).height();
        if(currentHeight >= tallestcolumn){
            tallestcolumn = currentHeight;
        }
    });
    columns.height(tallestcolumn);
}

function screenAdapt(){
    
    $('table.staff tr td').height('auto');
    setEqualHeight($('table.staff tr td'));
    
    _height = $(window).height(); //0.9 * _width;
    $('#form-structura').height(_height);
    
    if($(window).width() > 1024){
        //$('#form-structura').css({'height':'','width':''});
        $('#block_structura').css({'left':'','top':'','-webkit-transform':'','-moz-transform':'','-ms-transform':'','-o-transform':'','transform':''});
    }
    else {
        _width = $(window).width();
        
        _left = -(1000 - _width) / 2 + 'px';
        _top = ( - (900 - _width*0.9 )/2) + 'px';
        _zoom = _width/1000;
        
        
        //$('#form-structura').width(_width);
        $('#block_structura').css({
            'left':_left,
            'top':_top,
            '-webkit-transform':'scale('+_zoom+')',
            '-moz-transform':'scale('+_zoom+')',
            '-ms-transform':'scale('+_zoom+')',
            '-o-transform':'scale('+_zoom+')',
            'transform':'scale('+_zoom+')'
        });
    }
    
    $('#block_mobile_overlay').hide();
    $('#block_mobile_menu').animate({right:'-240px'},200);
    $("body").removeClass('menu-open');
    $(document).height('auto');
    
    //if($(window).width() > 1024) $('#block_mobile_menu').hide();
    //else $('#block_mobile_menu').show();
    
    if($(window).width() > 767){
        $('#block_structura_preview_outer').css('height','');
        $('#block_structura_preview').css({'left':'','top':'','-webkit-transform':'','-moz-transform':'','-ms-transform':'','-o-transform':'','transform':''});
    }
    else {
        _width = $(window).width() - 36;
        _height = 0.9 * _width;
        _left = -(1000 - _width) / 2 + 'px';
        _top = - (900 - _height)/2 + 'px';
        _zoom = _width/1000;
        $('#block_structura_preview_outer').height(_height);
        $('#block_structura_preview').css({
            'left':_left,'top':_top,
            '-webkit-transform':'scale('+_zoom+')',
            '-moz-transform':'scale('+_zoom+')',
            '-ms-transform':'scale('+_zoom+')',
            '-o-transform':'scale('+_zoom+')',
            'transform':'scale('+_zoom+')'
        });
    }
}

function addLink() {
    var body_element = document.getElementsByTagName('body')[0];
    var selection = window.getSelection();
    var text = (document.domain.indexOf('.ru')>0)? '':'Details on VIS GC';
    var pagelink = "<p>"+text+":<br/><a href='"+document.location.href+"'>"+document.location.href+"</a></p>";
    var copytext = selection + pagelink;
    
    var newdiv = document.createElement('div');
    newdiv.style.position = 'absolute';
    newdiv.style.left = '-99999px';
    body_element.appendChild(newdiv);
    newdiv.innerHTML = copytext;
    selection.selectAllChildren(newdiv);
    
    window.setTimeout( function() {
    body_element.removeChild(newdiv);
    }, 0);
}
//document.oncopy = addLink;