/* work around msie braindead hover pseudoclass support */

var activeMenu = null ;
var hideMenu = false ;

function maybe_hide_menu() {
    if (hideMenu) {
        hideMenu = false ;
	if (activeMenu) {
	    activeMenu.css('display', 'none') ;
	    activeMenu = null ;
	}
    }
}

function init_menu(i, e) {
      var smenu = $(e).find('.smenu') ;
      if (smenu.get(0)) {
        $(e).hover(function () {
	    if (activeMenu) {
	        activeMenu.css('display', 'none') ;
		hideMenu = false ;
	    }
	    activeMenu = smenu ;
            var scale = $('#bmenu').offset().top - $('#tmenu').offset().top ;
	    smenu.css({
                display: 'block',
                top: ($(e).offset().top / scale * 125 + $(e).height()),
                left: ($(e).offset().left / scale * 125) - $('#footer').offset().left
            }) ;
	}, function () {
	    hideMenu = true ;
	    window.setTimeout(maybe_hide_menu, 800) ;
	}) ;

	$('#footer').after(smenu.remove().css({
	    display: 'none', position: 'absolute',
	    top: ($(e).offset().top + $(e).height()),
            left: $(e).offset().left
	})) ;

	smenu.hover(function () {
	        hideMenu = false ;
	     }, function () {
	        hideMenu = true ;
		window.setTimeout(maybe_hide_menu, 800) ;
	     }
	) ;
    
      }
}
$(document).ready(function(){

    $('div.mp3 a').click(function () {
      var url = this.href ;
      $(this).flash(
        {
          src: '/img/player_mp3_maxi.swf', height: 20, width: 120,
          flashvars: {
            mp3: url,
            showinfo: '0', showstop: '1', autoplay: '1', showinfo: '1',
            textcolor: '666666', buttoncolor: '666666',
            buttonovercolor: '000000', bgcolor1: 'f5f5f5', bgcolor2: 'cccccc',
            sliderovercolor: '333333'
          }
        },
        {version: 6},
        function (opts) {
          $(this).parent().replaceWith($.fn.flash.transform(opts)) ;
        }
      ) ;
      return false ;
    }) ;

    $('.search a').click(function () { $('.search form input.submit').click() ; return false }) ;

    $("#add-to-bookmarks").click(function () {
        if (jQuery.browser.msie) {
            window.external.AddFavorite(location.href, document.title) ;
        } else if (window.sidebar) {
            window.sidebar.addPanel(document.title, location.href, '') ;
        }
        return false ;
    }) ;
    if (jQuery.browser.msie) {
        /* [insert a traditional "damn you, msie" rant here] */
        setTimeout(function () {
            $('.topmenu').each(init_menu) ;
            $('.botmenu').each(init_menu) ;
        }, 1000) ;
    } else {
        $('.hover-fix').mouseover(function (ev){ $(this).addClass('hover') }) ;
        $('.hover-fix').mouseout(function (ev){ $(this).removeClass('hover') });
    }

    var ids = new Array ;

    $('.raiting').each(function (i, e){
        if (eid = e.className.match(/element-(\d+)/)) { ids.push(eid[1]) }
    }) ;

    if (main_element_id && main_element_id != '') {
        $.ajax({
            url: '/counters',
            async: true,
            cache: false,
            data: ('main=' + main_element_id + '&ids=' + ids)
        }) ;
    }
}) ;

function echo_comments_moderate() {
        div = $(this).parents('.comment').get(0) ;
        divid = div.id
        saved = $(this).children().text()
        $(this).children().text('Минутку...') ;
        $.ajax({
            url: $(this).attr('href'),
            cache: false,
            success: function(html) {
                $(div).replaceWith(html) ;
                $('#' + divid + ' a.remote').click(echo_comments_moderate) ;
            }}) ;
        return false ;
}

function echo_replace() {
        div = $(this).parents('.replaced').get(0) ;
        divid = div.id
        saved = $(this).children().text()
        $(this).text('Минутку...') ;
        $.ajax({
            url: $(this).attr('href'),
            cache: false,
            success: function(html) {
                $(div).replaceWith(html) ;
                $('#' + divid + ' a.replace').click(echo_replace) ;
            }}) ;
        return false ;
}

function echo_comments() {
    $('#comments .comment a.remote').click(echo_comments_moderate) ;
    $('a.replace').click(echo_replace) ;
}

