var testObj

$(document).ready(function(){
    $('#main-menu ul li').hover(
        function() {
            $(this).find('DIV.drop-menu').stop(true, true).slideDown()
            var btn = $(this).find('img').get(0).id.split('_')[0]
            if (btn) $('#'+btn+'_btn').attr('src','/pic/menu/'+btn+'_hover.png')
        },
        function() {
            $(this).find('DIV.drop-menu').slideUp('fast')
            var btn = $(this).find('img').get(0).id.split('_')[0]
            if (btn) $('#'+btn+'_btn').attr('src','/pic/menu/'+btn+(btn==section && btn!='front'?'_down':'')+'.png')
        }
    )
    if ($.browser.msie) $('DIV.drop-menu DIV').hover(
    	  function() {$(this).addClass("hover")},
    	  function() {$(this).removeClass("hover")}
    	)
    document.forms['searchForm'].q.onclick = function() {if (this.value=='Поиск') this.value = '' }
    document.forms['searchForm'].q.onblur  = function() {if (this.value=='') this.value = 'Поиск' }

    $('#browser DIV.left, #browser DIV.right, #top-preview DIV.left, #top-preview DIV.right').hover(
    	  function() {$(this).addClass("btn-hover")},
    	  function() {$(this).removeClass("btn-hover")}
    	)
    $('#browser DIV.left, #browser DIV.right, #top-preview DIV.left, #top-preview DIV.right, #greeting-close A').hover(
    	  function() {$(this).css('background-position','right bottom')},
    	  function() {$(this).css('background-position','right top')}
    	)
    $('#browser DIV.right').click(function() {
    	$('#browser .item:first').animate({width:'toggle'},350,null,function(){$('#browser .item:first').show().insertAfter($('#browser .item:last'))})
    	})
    $('#browser DIV.left').click(function() {
    	$('#browser .item:last').insertBefore($('#browser .item:first')).hide().animate({width:'toggle'},350)
    	})
    $('#top-preview DIV.right').click(function() {
	$('#top-preview .item:first').next().addClass('first')
	$('#top-preview .item:first').animate({width:'toggle'},350,null,function(){$('#top-preview .item:first').show().insertAfter($('#top-preview .item:last')).removeClass('first')})
    	})
    $('#top-preview DIV.left').click(function() {
    	var a = $('#top-preview .first')
    	$('#top-preview .item:last').insertBefore($('#top-preview .item:first')).hide().addClass('first').animate({width:'toggle'},350)
	a.removeClass('first')
    	})
    $('#top-preview DIV.item').hover(
    	  function() {
    	  	i = $(this).attr('id').split('-')[2]
    	  	if (top_preview[i]) $('#top-preview-info').css('left',($(this).position().left+4)+'px').width($(this).width()-1).slideDown('fast').html('<div class=\"title\">'+top_preview[i]['title']+'</div><div>'+top_preview[i]['text']+'</div>');
    	      	  	
    	  },
    	  function() {$('#top-preview-info').hide()}
    	)

    $('#banners .container').hover(
    	  function() { $(this).find('.cover').fadeOut(100) },
    	  function() { $(this).find('.cover').fadeIn(100, function(){ if ($.browser.msie) this.style.filter = 'alpha(opacity=80, style=0)' })}
    	)
    
    $('#searchForm INPUT[name=q]').autocomplete({
	serviceUrl: '/do/autocomplete.php',
	minChars: 2,
	delimiter: /(,|;)\s*/,
	maxHeight: 400,
	left:$('#main-menu LI.search').offset().left,
	width: $('#main-menu LI.search').width(),
	zIndex: 9999,
	deferRequestBy: 300,
	params: { 'part': 'search'},
	onSelect: function(data, value) { $('#searchForm INPUT[name=q]').val(data.substr(data.indexOf(': ')+2).replace(/&quot;/g,'"'))}
    }).fixPosition = function() {
		  var offset = this.el.offset()
		  $('#'+this.mainContainerId).css({top: (offset.top + this.el.innerHeight())+'px', left: (this.options.left?this.options.left:offset.left)+'px'})
		}
    
    /*
    // Fade in images to grayscale
    $("#top-links img").fadeIn(30);
    
    $('#top-links img').each(function(){
        var el = $(this)//.pixastic("desaturate");
        el.css({"position":"absolute","opacity":"0","z-index":"999"}).wrap("<div class='top_link_wrapper' style='display: inline-block; position: relative;'>")
          .clone().addClass('img_grayscale').css({"position":"absolute","z-index":"998","opacity":"1"}).pixastic("desaturate").insertBefore(el).queue(function(){
            var el = $(this);
            el.parent().css({"width":this.width,"height":this.height});
            el.dequeue();
        });
        
        //this.src = grayscale(this.src);
    })
    
    /*
    // Fade image
    $('#top-links img')
    .mouseout(function(){
        $(this).parent().find('img:last').stop().animate({opacity:0}, 300);
    })
    .mouseover(function(){
        $(this).parent().find('img:last').stop().animate({opacity:1}, 300);
    })
    /**/
})

function moduleShowTab(module_id,n)
{
  $('#'+module_id+' .module-tab').hide()
  $('#'+module_id+'-'+n).show()
  $('#'+module_id+' table').css('backgroundImage', 'url(/pic/audio-tab-'+n+'.png)')
}
function showSocial(social)
{
  $('#module-social DIV.social').hide()
  $('#module-social-'+social).show()
}
function sendFormCheck(form)
{
  form.field_name_first.value = form.field_name_first.value.trim()
  form.field_email.value = form.field_email.value.trim()
  if (!form.field_name_first.value.match("^[A-Za-zА-Яа-я0-9\. -]{2,}$"))
  {
    alert("Неверно заполнено поле 'Ваше имя'")
    form.field_name_first.focus()
    return false
  }
  if (!isValidEmail(form.field_email.value))
  {
    alert("Неверно заполнено поле 'Ваш e-mail'")
    form.field_email.focus()
    return false
  }
  form.submit()
}
function closeGreeting(no)
{
  var today = new Date()
  today.setFullYear(today.getFullYear()+1)
  setCookie('top-message-no',no,today.toUTCString(),'/')
  $('#top-message').slideUp()
}
