$(document).ready(function(){
            $(".line_serv").children(".drop_sec_menu").css('display', 'none').css('opacity', 0);
            $(".drop_about").children(".drop_menu_about").css('display', 'none').css('opacity', 0);

        // о компании 
          $("#menu_top li.drop_top_menu").mouseenter(function(){
                if($(this).children('ul').children('li').length > 0) {
                    $(this).children(".drop_menu_about").stop();
                    $(this).children(".drop_menu_about").css('display','block');
					var IE='\v'=='v';
                    if (IE) $(this).children(".drop_menu_about").animate({ opacity: "0.7" }, 300);
					else $(this).children(".drop_menu_about").animate({ opacity: "1.0" }, 300);
                }
          }); 
          
          $("#menu_top li.drop_top_menu").mouseleave(function(){
                $(this).children(".drop_menu_about").stop();
                $(".drop_menu_about").animate({ opacity: "0" }, 500, function(){
                    $(this).css('display', 'none');
                });
          });
                    
         // услуги 
          $(".sec_menu").mouseenter(function(){
            if($(this).children('div').children('div').children('ul').children('li').length > 0) {
                
                $(this).children(".drop_sec_menu").stop();
                $(this).children(".drop_sec_menu").css('display', 'block');
                li_width($(this));

				$(this).children(".drop_sec_menu").animate({ opacity: "1.0" }, 300); 
            }
          });
          
          $(".sec_menu").mouseleave(function(){
                $(this).children(".drop_sec_menu").stop();
                $(this).children(".drop_sec_menu").animate({ opacity: "0" }, 300, function() {
                    $(this).css('display', 'none');
                });
          });
          
          
          /////Картинкокрутилка
          //$('div.main_img *').css('z-index', '7');
          //$('.drop_sec_menu').css('z-index', '1100');
          //$('div.main_img').css('background-image', 'url('+base_url+'css/images/news.jpg)');
          $('div.main_img').append('<a id="img_rotator"></a>');
          //start_rotator();
          
          $('.theme_serv').css('cursor', 'pointer').click(function() {hide_related(); return false;});
          
        $('a[href$=".jpg"], a[href$=".jpeg"], a[href$=".JPG"], a[href$=".bmp"]').lightBox();
		
		
        
});

$(window).load(function() {
	if ($('#left_block').height() < $('.right_block').height() -13) $('#left_block').height($('.right_block').height() - 13);
	$('.t_email').each(function(index) {
		var tField = $(this);
		$.get(base_url+'welcome/ajax_get_email/'+tField.attr('alt'), function(data) {
			tField.html('<a class="mail_phone" href="mailto:'+data+'">'+data+'</a>');
		});
	});
});

function start_rotator() {
    $('#img_rotator').css('position', 'absolute').width(907).height(275).css('top', '0px').css('left', '0px').css('z-index', '1').css('display', 'block');
          $.getJSON(base_url+'welcome/ajax_get_index_images', function(data){
                     var iimages = data.iimages;
                     var numImg = iimages.length;
                     var inum = 0;
                     //$('#img_rotator').
                     change_img(0, iimages);
                     $("#img_rotator").everyTime(7000, function(i) {
                        //console.log('numImg '+ numImg +'|'+ inum);
                        if (inum + 1 == numImg) inum = -1;
                        change_img(inum + 1, iimages);
                        inum++;
                    });

                     
            });
}

function change_img(toNum, iimages) {
          $('#img_rotator').append('<div id="imrTmp"></div>');
          cimage = iimages[toNum].iimage;
          $('#imrTmp')
          .css('position', 'absolute')
          .width(0).height(275)
          .css('top', '0px')
          .css('left', '450px')
          .css('overflow', 'hidden');
          
          var timg = new Image();
  
            // wrap our new image in jQuery, then:
            $(timg)
                .attr('src', base_url+'drupal_admin/sites/default/files/index/'+cimage)
              // once the image has loaded, execute this code
              .load(function () {  
                    $('#imrTmp').append(this); //.css('background-image', 'url(./drupal_admin/sites/default/files/index/'+cimage+')');
                    $('div.left_info').animate({opacity: 0, }, 300);
					//$('div.left_info').fadeOut();
                    $('#imrTmp').animate({
                    width: '907px',
                    left: '0px'
                    }, 700, function() {
                        $('div.main_img').css('background-image', 'url('+base_url+'drupal_admin/sites/default/files/index/'+cimage+')');
                        $('#imrTmp').remove();
                        $('div.left_info').attr('href', iimages[toNum].ilink);
                        $('.title_left_info').html(iimages[toNum].ititle);
                        $('.text_left_info').html(iimages[toNum].itext);
						var IE='\v'=='v';
						if(IE) $('div.left_info').animate({opacity: 0.7}, 300);
						else $('div.left_info').animate({opacity: 1}, 300);
						
					   //$('div.left_info').fadeIn();
                    });
              })
              .error(function () {
                $('#imrTmp').remove();
              });
              
}
 
function li_width(uelem) {
   var i = 0;
   uelem.children('div').children('div').children('ul').children('li').each(function() {
    if(i % 2 == 0){
        $(this).width(260).css('overflow', 'hidden');
    }
    i++;
   });
    
}

function show_related(nid, type, category){
    prep_rel_cont();
    //console.log(base_url+'welcome/ajax_get_related/'+nid+'/'+type+'/'+category);
    $('#nav_left_block li a').css('color', '#ccc');
                            
    $.get(base_url+'welcome/ajax_get_related/'+nid+'/'+type+'/'+encodeURI(category.replace('/','_slash_').replace('(','').replace(')','')), function(data) {
        
        $('#related_container').append(data);
        if($('#related_container').children('div').height() < $('.right_block').height()) var newNheight = $('.right_block').height();
        else var newNheight = $('#related_container').children('div').height();
        $('#related_container').height(newNheight);
        $('.preloader').remove();
        $('#related_container').animate({
            opacity: 1
            }, 100);
    });
}

function hide_related() {
    $('#nav_left_block li a').css('color', '#ccc');
    $('#related_container').stop();
    $('#related_container').animate({
        opacity: 0
    }, 100, function() {
        $('#related_container').remove();
        $('#container > .right_block').animate({opacity: 1}, 100);
    });
    
}

function show_rel_node(nid, type) {
    prep_rel_cont();
    
    $.get(base_url+'welcome/ajax_get_node/'+nid+'/'+type, function(data) {
        $('#related_container').append(data);
        if($('#related_container').children('div').height() < $('#container').children('.right_block').height()) var newNheight = $('#container').children('.right_block').height();
        else var newNheight = $('#related_container').children('div').height();
        $('#related_container').height(newNheight);
        $('.preloader').remove();
        $('#related_container').animate({
            opacity: 1
            }, 100, function() {$('a[href$=".jpg"], a[href$=".jpeg"], a[href$=".JPG"], a[href$=".bmp"]').lightBox();});
    });
    
    return false;
}

function show_rel_fulltext(sname, type) {
   prep_rel_cont();
    $('#nav_left_block li a').css('color', '#ccc');
                            
    $.post(base_url+'welcome/ajax_get_fulltext_related', {sname: sname, type: type}, function(data) {
        
        $('#related_container').append(data);
        if($('#related_container').children('div').height() < $('.right_block').height()) var newNheight = $('.right_block').height();
        else var newNheight = $('#related_container').children('div').height();
        $('#related_container').height(newNheight);
        $('.preloader').remove();
        $('#related_container').animate({
            opacity: 1
            }, 100);
    });
    
    return false;
}

function prep_rel_cont() {
    $('#related_container').stop();
    $('#related_container').remove();
    $('#container').append('<div id="related_container"></div>');
    var rposition = $('#left_block').position();
    $('#related_container').append('<a href="#" onclick="hide_related(); return false;" style="z-index: 100;position: absolute; top: 5px; right: 0px; dislpay: block; padding: 0px; width: 10px; height: 10px; background-image: url('+base_url+'css/images/cross.png)"></a>');
    $('#left_block').append('<div class="preloader"></div>');
    $('#related_container')
                            .css('opacity', '0')
                            .css('left', rposition.left + 334)
                            .css('top', rposition.top)
                            .height(0)
                            .width($('.right_block').width() - 9)
                            .css('padding-top', '0px')
                            .css('padding-bottom', '30px');
    $('#container > .right_block').animate({opacity: 0}, 100);
}

