Cufon.replace('#topmenu a.top', { fontFamily: 'Myriad Pro', hover: { color: '#ffffff' } });
Cufon.replace('h1[class!=main_h1]', { fontFamily: 'Myriad Pro' });
Cufon.replace('h2', { fontFamily: 'Myriad Pro' });
Cufon.replace('#about_title', { fontFamily: 'Myriad Pro' });
Cufon.replace('.Myriad', { fontFamily: 'Myriad Pro' });
Cufon.replace('a.more_red_r', { fontFamily: 'Myriad Pro' });
Cufon.replace('a.more2_red_r', { fontFamily: 'Myriad Pro' });

function menu_on(b,m)
{
  if (!m.attr('id')) return;
  
  var width = b.width() - 32;
  if (width < 170)
	 width = 170;
  
  $('.submenu').css('display', 'none');
  $('.submenu a').css('width', width + 'px');
  
  m.css('display', 'block');
  $('#' + b.attr('id') + ' a.top').each(function(){ if (!$(this).hasClass('white')) $(this).addClass('white') });
  b.addClass('opened');
}

function menu_off(b,m)
{
  if (!m.attr('id')) return;
	
  m.css('display', 'none');
  $('a.top').each(function(){ if ($(this).hasClass('white')) $(this).removeClass('white') });
  b.removeClass('opened');
}

$(function($){

  if ($('body').width() <= 1000)
  	$('#header').css('background', '#fff');
  
  if ($(window).width() <= 1024)
  {
	$('.more_red_out.dis').show();
	$('.collection_title h2').css('width', '380px');
  }
  
  $('a[ur]').each(function(){
	var href = $(this).attr('ur');
	$(this).attr('href', href);
  });
  
  $('.centered .text').each(function(){
	var text = '';
	var id = $(this).attr('rel');
	$('#' + id).each(function(){
	  text = $(this).html();
	});
	$(this).html(text);
  });
	
  $('.submenu_owner').hover(
    function() {
      var id = '#' + $(this).attr('rel');
      var par = $(this);
      $(id).each(function(){
    	menu_on(par, $(this));  
      });
    },
    function() {
      var id = '#' + $(this).attr('rel');
      var par = $(this);
      $(id).each(function(){
    	menu_off(par, $(this));
      });
    }
  );

  $('a.partition').each(function(){
    if ($(this).attr('big'))
    {
      $(this).attr('href', $(this).attr('big'));
      $(this).facebox();
    }
    if ($(this).attr('biglightbox'))
    {
      $(this).attr('href', $(this).attr('biglightbox'));
    }
  });

  var options = { opacity: 0.2, height: 7/12 };
  if ($('.reflect')[0])
    $('.reflect').reflect(options);

  var top = 0;
  var first = true;

  $('.list_door').unbind('click').click(function(){
  
	var door_id = $(this).attr('door_id');
	if (!(door_id))
	  door_id = $(this).attr('rel');
    if (!$('#' + door_id)[0] || $(this).hasClass('partition'))
      return;
    
    if ($('#banner_work_left')[0])
      $('#banner_work_left').css('display', 'none');
  
    $('#door_left').show();
  
    var to_move_door = true;
  
    $('ul.level1').each(function(){
      if (!$(this).hasClass('closed'))
      {
          $('#doors_menu_arrow').css('visibility', 'visible');
          $('#doors_menu_arrow').addClass('active');
          to_move_door = false;
          $(this).animate(
            {
              height: 0
            },
            ($.browser.msie && ($.browser.version == '7.0' || $.browser.version == '6.0')) ? 0 : 600,
            function()
            {
              top = Math.round($("#marker").offset().top);
              move_door();
            }
          );
          $(this).addClass('closed');
          $(this).children('li').each(function(){ $(this).css('display', 'none'); });
      }
    });
  
    $('.list_door').each(function(){
      if ($(this).hasClass('current'))
        $(this).removeClass('current'); 
    });
    $(this).addClass('current');

    // show red frame
    $('.frame').each(function(){ $(this).css('visibility', 'hidden') });
    $(this).children('.frame').each(function(){
    	$(this).css('display', 'block');
    	$(this).css('visibility', 'visible');
    });
  
    // show left door
    var id = door_id;
    
    var door_url = '', url_text = '';
    $('#'+id + ' .door_url').each(function(){
      door_url = $(this).attr('href');
      url_text = $(this).text();
    }); 
    $('#door_left .info .more_gray').attr('href', door_url);
    $('#door_left .info .more_buy').attr('href', door_url + '#feedback');
    
    
    var door_what = '';
    $('#'+id + ' .door_what').each(function(){
      door_what = $(this).text();
    });
    door_what += ' ';
    $('#door_left .info .more_buy').text(door_what);
    
    var door_price = '';
    $('#'+id + ' .door_price').each(function(){
      door_price = $(this).html();
    }); 
    $('#door_left .info .door_price').html(door_price);
    
    var door_price_note = '';
    $('#'+id + ' .door_price_note').each(function(){
      door_price_note = $(this).html();
    }); 
    $('#door_left .info .door_price_note').html(door_price_note);    
    
    $('#'+id + ' .door_image_full').each(function(){
      $('#door_left .image_full').html('<a href="' + door_url + '"><img src="' + $(this).attr('src') + '" width="' + $(this).attr('imgwidth') + '" height="' + $(this).attr('imgheight') + '" /></a>');
    });
    
    $('#'+id + ' .door_title').each(function(){
      $('#door_left .door_title').html($(this).html());
    });
    
    var door_gallery_count = 0;
    $('#'+id + ' .door_gallery_count').each(function(){
      door_gallery_count = parseInt($(this).text());
    }); 
    
    var door_text = '';
    var have_more = 0;
    door_text = $('#collection_default_text_' + $('#'+id).attr('collection_id')).html();
    $('#'+id + ' .door_text').each(function(){
       have_more = parseInt($(this).attr('have_more'));
       if ($(this).text())
         door_text = $(this).html();
    });
    if (door_text == null)
      door_text = '';
    var door_link_text = ''
    if (door_text && have_more)
    {
      var link_text = 'Посмореть описание';
      if (door_gallery_count > 0)
        link_text = 'Посмотреть описание и еще фотографии';
        
      door_text = door_text + '<a class="more_gray" href="' + door_url + '">' + link_text + '</a>';
    }
    else
    {
      if (door_gallery_count > 0)
      {
        // если нет описания то ссылку вывести под фото
        door_link_text = '<a href="' + door_url + '#item_gallery" class="more_front">Еще фотографии</a> <b>('  + door_gallery_count + ')</b>'; 
      }
    }
    $('#door_left .door_text').html('<br>' + door_text);
    $('#more_foto').each(function(){
      $(this).html(door_link_text);
    });
    
    if (to_move_door)
      move_door();
    
    return false;
  });
  
  function move_door()
  {
    //постараться переместить дверь максимально вверх, но под меню (за элементом marker)
    
    var door_top = 0;
    
    //var html_top = Math.round(document.documentElement.scrollTop);
    var html_top = Math.round($(window).scrollTop());
    
    if (html_top <= top)
      door_top = top;
    else
      door_top = html_top + 20;
      
    var left = $("#door_left").offset().left; // левая координата нужна для ie
    if (first && ($.browser.opera && parseInt($.browser.version) < 11)) // при первом перемещении offset считается не от всего документа, а от родительского div
    // в 11 опере это исправили
    {
      left = 0;
      door_top -= top;
    }
    first = false;
      
    //$("#door_left").offset({ top: door_top, left: left });
    $("#door_left").offset({ left: left });
    $("#door_left").animate({'marginTop': Math.round(door_top - top)}, 200);
  }
  
  $('#more_btn').bind('click', function(){
    if ($('#more_open').css('display') == 'none')
    {
      $(this).addClass('clicked');
      $('#more_open').show();
    }
    else
    {
      $(this).removeClass('clicked');
      $('#more_open').hide();
    }
  });
  
  if ($('#open_text_btn')[0])
  {
	var open = $('#text-open .text').html();
	var closed = $('#text-closed .text').html();
	$('#text-closed .text').html(closed);
	  
    $('#open_text_btn').bind('click', function(){
      if (!$(this).hasClass('pressed'))
      {
    	var text = open + closed;
    	$('#text-open .text').html(text);
    	$(this).addClass('pressed');
    	$('#open_text').addClass('pressed');
    	$(this).text('Свернуть');
      }
      else
      {
    	$('#text-open .text').html(open);
    	$(this).removeClass('pressed');
    	$('#open_text').removeClass('pressed');
    	$(this).text('Подробнее');
      }
    });
  }
  
  $('.partition_chapter_foto').bind('mouseenter', function() { chapter_foto_mouseenter($(this)); });
  $('.chapter_foto').bind('mouseenter', function() { chapter_foto_mouseenter($(this)); });
  $('.furniture_chapter').bind('mouseenter', function() { chapter_foto_mouseenter($(this)); });
    
  $('.partition_chapter_foto').bind('mouseleave', function() { chapter_foto_mouseleave($(this)); });
  $('.chapter_foto').bind('mouseleave', function() { chapter_foto_mouseleave($(this)); });
  $('.furniture_chapter').bind('mouseleave', function() { chapter_foto_mouseleave($(this)); });
    
  function chapter_foto_mouseenter(obj)
  {
    obj.children('a.arrow').each(function(){ $(this).addClass('hover') });
  }
  
  function chapter_foto_mouseleave(obj)
  {
    obj.children('a.arrow').each(function(){ $(this).removeClass('hover') });
  }
  
  $('#doors_menu_arrow').click(function(){
    if ($(this).hasClass('active'))
    {
      open_menu();
      $(this).removeClass('active');
      $(this).css('visibility', 'hidden');
    }
  });  
  
  function open_menu()
  {
    $('ul.level1').each(function(){
      if ($(this).hasClass('closed'))
      {
        $('#door_left').hide();      
        $('#banner_work_left').css('display', 'block');
        $(this).animate(
          {
            height: '100%'
          },
          ($.browser.msie && ($.browser.version == '7.0' || $.browser.version == '6.0')) ? 0 : 900,
          function()
          {
            top = Math.round($("#marker").offset().top);
          }
        );
        $(this).children('li').each(function(){
        	if ($.browser.msie && ($.browser.version == '7.0' || $.browser.version == '6.0'))
        		$(this).css('display', 'block');
        	else
        		$(this).slideDown(200);
        });
        $(this).removeClass('closed');
      }
    });
  }
});
