$( document ).ready (
    function () {
		
        refrClock();
		tooltip ();
        
        $('div.news-head').click(
            function () {
                $(this).toggleClass('unfolded').next('div.news-body').slideToggle('slow');
            }
        );
        
        $( 'img' ).error (
            function () {
                $(this).attr (
                    {
                        'src' : 'pic/imgmiss.gif'
                    }
                );
            }
        );

        $( window ).scroll (
            function () {
                $( '#user_bar' ).css(
                    {
                        'bottom' : '-' + $( window ).scrollTop ( ) + 'px'
                    }
                );
            }
        );
	
        if ( window.location.pathname == '/browse.php' ) {
            hash_global = window.location.hash;
            $.history.init( pageload );
        }
    }
);

function refrClock() {
    var d = new Date();
    var s = d.getSeconds();
    var m = d.getMinutes();
    var h = d.getHours();
    var day = d.getDay();
    var date = d.getDate();
    var month = d.getMonth();
    var year = d.getFullYear();
    var am_pm;
    if (s < 10) {
        s = "0" + s
    }
    if (m < 10) {
        m = "0" + m
    }
    if (h > 12) {
        h -= 12;
        am_pm = "PM"
    }
    else {
        am_pm = "AM"
    }
    if (h < 10) {
        h = "0" + h
    }
    
	$('#clock').empty().append(h + ":" + m + ":" + s + " " + am_pm);
	$('#clock2').empty().append(h + ":" + m + ":" + s + " " + am_pm);
	
    setTimeout("refrClock()", 1000);
}

function pageload ( hash_global ) {
	if ( hash_global != '' ) {
        $( '#releases-table' ).load ( hash_global );
	}
}


function rateit ( rid, rtype, rate ) {

    field = '#ratearea-' + rid + '-' + rtype;

    $( field ).empty ().append ( '<div align="center"><img src="pic/loading.gif" border="0"/></div>' );
    $.get( 'rate.php',
        {
            'ajax' : 1,
            'id'   : rid,
            'type' : rtype,
            'act'  : rate
        },
        function ( data ) {
            $(field).empty ().append ( data );
        }
    );

    return false;
}

function reclama (id) {
    var __rt = Math.round ( Math.random () * 100000 );
    document.write ( '<script language="javascript" type="text/javascript" src="http://id15631.luxup.ru/show/' + id + '/?div=lx_' + id + '&rt=' + __rt + '&r=' + escape(document.referrer) + '"></script>' );
}

function google () {
    var gaJsHost = ( ( 'https:' == document.location.protocol ) ? 'https://ssl.' : 'http://www.' );
    document.write ( unescape ( '%3Cscript src=\'' + gaJsHost + 'google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E' ) );
    try {
	var pageTracker = _gat._getTracker("UA-9225099-2");
	pageTracker._trackPageview();
    }
    catch ( err ) { }
}

function marketGidVerx ( )
{
    var MGCD = new Date ( );
    document.write ( '<script type="text/javascript" src="http://jsc.dt00.net/b/i/bigfangroup.org.10608.js?t=' + MGCD.getYear ( ) + MGCD.getMonth() + MGCD.getDate() + MGCD.getHours() + '" charset="windows-1251"></script>');
}

function liveinternet () {
	document.write('<a href="http://www.liveinternet.ru/click" target="_blank"><img src="//counter.yadro.ru/hit?t57.10;r' + escape(document.referrer) +
	( ( typeof ( screen ) == 'undefined') ? '' : ';s' + screen.width + '*' + screen.height + '*' + (screen.colorDepth ? screen.colorDepth : 
	screen.pixelDepth ) ) + ';u' + escape(document.URL) + ';' + Math.random() + '" alt="" title="LiveInternet" ' + 'border="0" width="88" height="31" /></a>')
}

function new_year () {
	now = new Date ();
	ex  = new Date ( 2011, 0, 1, 0, 0, 0 );
	hours = now.getHours ();
	minutes = now.getMinutes ();
	seconds = now.getSeconds ();
	timeStr = "" + hours;
	timeStr += ( ( minutes < 10 ) ? ":0" : ":" ) + minutes;
	date = now.getDate ();
	month = now.getMonth () + 1;
	year = now.getYear();
	dateStr = "" + date;
	dateStr += ( ( month < 10 ) ? "/0" : "/" ) + month;
	dateStr += "/" + year;
	ostStr = "";
	x = y = ( ex.getTime() - now.getTime () ) / 1000;
	s_ch = Math.floor ( x / 60 / 60 );
	ostStr = ( ( s_ch < 10 ) ? "0" : "" ) + s_ch + ' час. ';
	s_min = Math.floor ( ( x / 60 / 60 - Math.floor ( x / 60 / 60 ) ) * 60 );
	ostStr = ostStr + ((s_min < 10) ? "0" : "") + s_min + ' мин. ';
	x = (((x/60/60 - Math.floor(x/60/60))*60) - Math.floor((x/60/60 - Math.floor(x/60/60))*60))*60;
	s_sec = Math.floor(x);
	ostStr = ostStr + ((s_sec < 10) ? "0" : "") + s_sec + ' сек. ';
	if ( y >= 0 ) {
		document.getElementById('new_year').innerHTML = ostStr;
		Timer= setTimeout("new_year()", 500);
	}
	else {
		alert('Поздравляем с Новым Годом!!!');
	}
}

function tooltip (location) {
    if ( ! location )
    {
        location = 'body';
    }
    xOffset = 6;
    yOffset = 16;
    $(location).find('[title]').hover(
        function ( e ) {
            this.t = this.title;
            this.title = "";
            $(location).append("<p id='tooltip'>"+ this.t +"</p>");
            $("#tooltip").css("top", ( e.pageY - xOffset ) + "px" ).css( "left", ( e.pageX + yOffset ) + "px" ).show ();
        },
        function () {
            this.title = this.t;
            this.t = '';
            $("#tooltip").remove ();
        }
    ).
    click (
        function () {
            this.title = this.t;
            this.t = '';
            $("#tooltip").remove ();
        }
    );
    $(location).find('[title]').mousemove(
        function ( e ) {
            $("#tooltip").css( "top", ( e.pageY - xOffset ) + "px" ).css( "left", ( e.pageX + yOffset ) + "px" );
        }
    );
}
