$(window).load(function() { $(document).pngFix(); }); 

$(document).ready(function() { 
    init_cluetip(); 

    if((typeof browsing_status_url != 'undefined') && (!$.browser.msie || $.browser.version>6))
        tb_show(false, browsing_status_url, false);

    if((typeof TagToTip == 'function') && (typeof UnTip == 'function')) {
        $("img.tip, span.tip, div.tip").mouseover(function () {
            TagToTip(
                $(this).attr("rel"), 
                CLICKCLOSE, true, 
                CLICKSTICKY, true, 
                FADEIN, 500, 
                FADEOUT, 500, 
                FOLLOWMOUSE, false, 
                STICKY, ($('#'+$(this).attr("rel")).html().indexOf("href") != -1), 
                DURATION, 0, 
                DELAY, 500
            );
        });

        $("img.tip, span.tip, div.tip").mouseout(function () {
            UnTip();
        });
    }

    $('button').each(function() {
        $(this).html('<table cellspacing="0" cellpadding="0" border="0"><tr><td class="lbtn"></td><td class="btn">'+$(this).html()+'</td><td class="rbtn"></td></tr></table>');
        if($.browser.msie) 
            $(this).css('width', $(this).children().get(0).scrollWidth+8+'px');
    });

    // init area ON/OFF mechanizm
    $('a.switch_on').click(function() {
        var this_ptr = $(this);

        this_ptr.css('display', 'none');
        this_ptr.next().css('display', 'block');

        this_ptr.parent().parent().parent().removeClass('area_closed');
        this_ptr.parent().parent().parent().addClass('area_open area_open_special');
        this_ptr.parent().parent().next().slideDown('normal');
    });

    $('a.switch_off').click(function() {
        var this_ptr = $(this);

        this_ptr.css('display', 'none');
        this_ptr.prev().css('display', 'block');

        this_ptr.parent().parent().next().slideUp('normal', function() {
            this_ptr.parent().parent().parent().removeClass('area_open area_open_special');
            this_ptr.parent().parent().parent().addClass('area_closed');
        });
    });

    // initialize icons highlighting on mouse over
    $('img.m_over').mouseover(function() {
        var prev = $(this).attr('src');
        $(this).attr('src', $(this).attr('rel'));
        $(this).attr('rel', prev);
    }).mouseout(function() {
        var prev = $(this).attr('src');
        $(this).attr('src', $(this).attr('rel'));
        $(this).attr('rel', prev);
    });

});

function init_cluetip() {
    
    if($.isFunction($.fn.cluetip)) {
      
      $('a.fmenu').each(function() {
        if($(this).attr('rel').indexOf('fmenu') == -1)
            $(this).attr('rel', '/details/fmenu/index.php?m_id='+$(this).attr('rel'));

      });

      $('a.fmenu').cluetip({
        width : 266, 
        height : 'auto', /*245*/
        local : false, 
        attribute : 'rel', 
        showTitle : false, 
        titleAttribute : '', 
        cluetipClass : 'v3', 
        waitImage : true, 
        arrows : false, 
        cursor : 'hand', 
        positionBy : 'auto', 
        //topOffset: -10,
        //leftOffset: 30, 
        dropShadow : false, 
        sticky : true, 
        ajaxCache : false, 
        mouseOutClose : true, 
        closeText : '', 
        onShow : function() {
            if(tb_init != 'undefined')
                tb_init('a.thickbox');
        }
      }).mouseout(function() {
        var timer = window.setTimeout(function() { 
            $('#cluetip-outer').parent().hide().removeClass().end().children().empty(); 
        }, 200);
        $('#cluetip-outer').parent().mouseover(function() {
            window.clearInterval(timer);
        });
      });

      $('#cluetip-outer').hover(function() {}, function() {
          if($("#TB_overlay").css('display') == 'block') return false;

          $('#cluetip-outer').parent().hide().removeClass().end().children().empty(); 
      });

    }
}

function invert_action_icon(is_on, target) { 
    $('#add_'+target+'_icon').css('display', is_on ? 'none' : 'block'); 
    $('#del_'+target+'_icon').css('display', is_on ? 'block' : 'none'); 

    if(target.indexOf('real_friend') != -1) {
        $('#add_real_friend').css('display', is_on ? 'none' : 'block'); 
        
        if(!is_on)
            $('#tab_body_content_real_friends').load(
                $('#tab_body_content_real_friends').attr('url'), {}, init_inline_content
            );
    
    } else if(target.indexOf('friend') != -1) {
        $('#add_friend').css('display', is_on ? 'none' : 'block'); 
        
        if(!is_on)
            $('#tab_body_content_friends').load(
                $('#tab_body_content_friends').attr('url'), {}, init_inline_content
            );
    }
}

function init_inline_content() {
    $('div > div.numbering > a.numbering').click(page_number_click);
    $('img.mtbn').bind('contextmenu', function () { return false; });
    
    if(tb_init != 'undefined')
        tb_init('div.tab_body_content a.thickbox, td a.thickbox, div.tab_body_content area.thickbox, div.tab_body_content input.thickbox');
    
    tb_remove();

    init_cluetip();
}
