$(document).ready(function(){
	$('.rating').not('.rating-disabled').addClass('clickable').click( zRatingClick ).mousemove( zRatingMouseMove ).mouseout( zRatingMouseOut );
	$('.z-check-auth').click( function( Event ){ ! zCheckAuth( Event, this ) && Event.preventDefault(); } );
	$('.toggle-window').click( zToggleWindow )
});

function zDebug( Text )
{
	if ( ! $( '#zDebug' ).length ) {
		$( '<div id="zDebug"></div>').appendTo( document.body );
	}
	$( '#zDebug' ).html( $( '#zDebug' ).html() + '<p>' + Text + '</p>' );
}


function zVote( Event )
{
	var $vote = $( Event.target ).is( '.vote' ) ? $( Event.target ) : $( Event.target ).parents( '.vote' );
	var vote = $vote.is ( '.vote-plus' ) ? 1 : -1;
	var $voting = $vote.parents( '.voting' );
	$.getJSON( '/post/vote/', {vote:vote, id:$voting.attr( 'oid' )}, function( json ) {
		$voting.find( '.score' ).text( json.score );
	} );

}

function zRatingClick( Event )
{
	if ( ! zCheckAuth( Event, $( this ) ) ) {
		return false;
	}
	var $rating = $( this );
	if ( $rating.is( '.rating1,.rating-1' ) ) {
		return;
	}
	var vote = 0;
	if ( $(this).is('.rating-hover-up') ) {
		vote = 1;
	}
	else if ( $(this).is('.rating-hover-down') ) {
		vote = -1;
	}
	$rating.addClass( 'rating'+vote );
	var newScore = Number( $rating.find( '.score' ).text() ) + vote;
	$rating.find( '.score' ).text( newScore );
	$rating.attr( 'score', newScore );
	if ( newScore >= 0 ) {
		$rating.removeClass( 'rating-negative' );
	}
	else {
		$rating.addClass( 'rating-negative' );
	}


	$.getJSON( '/post/vote/', {vote:vote, id:$rating.attr( 'oid' )}, function(json)
	{
		$rating.find( '.score' ).text( json.score );
		$rating.attr( 'score', json.score );
		if ( json.score >= 0 ) {
			$rating.removeClass( 'rating-negative' );
		}
		else {
			$rating.addClass( 'rating-negative' );
		}
	} );
}

function zRatingMouseMove( Event )
{
	var $rating = $( this );
	var $score = $rating.children( '.score' );
	var offset = $rating.offset();

	if ( $rating.is( '.rating1,.rating-1' ) ) {
		return;
	}

	if ( Event.pageX <= offset.left + $rating.width() / 2 )
	{
		if ( ! $rating.is( '.rating-hover-up' ) )
		{
			$rating.removeClass( 'rating-hover-down' ).addClass( 'rating-hover-up' );
			//$score.text( Number( $rating.attr( 'score' ) ) + 1 );
		}
	}
	else
	{
		if ( ! $rating.is( '.rating-hover-down' ) )
		{
			$rating.removeClass( 'rating-hover-up' ).addClass( 'rating-hover-down' );
			//$score.text( Number( $rating.attr( 'score' ) ) - 1 );
		}
	}
}

function zRatingMouseOut( Event )
{
	var $rating = $( this );
	var $score = $rating.children( '.score' );
	$rating.removeClass( 'rating-hover-up' ).removeClass( 'rating-hover-down' );
	$score.text( Number( $rating.attr( 'score' ) ) );
}

function zAppGet( ParamName )
{
	if ( typeof window.zApp == 'undefined' || typeof window.zApp[ParamName] == 'undefined' ) {
		return null;
	}
	return window.zApp[ParamName];
}

function zAppSet( ParamName, Value )
{
	if ( typeof window.zApp == 'undefined' ) {
		window.zApp = {};
	}
	window.zApp[ParamName] = Value;
}

function zCheckAuth( Event, $El )
{
	if ( zAppGet( 'username' ) ) {
		return true;
	}
	else
	{
		zLoginOrRegister( Event, $El );
		return false;
	}
}

function zLoginOrRegister( Event, El )
{
	var offset = $( El ).offset();
	zWindow.load( '/window/login_register/', {next:document.location.href.replace( /http:\/\/[^\/]+\//, '/' ) } );
	zWindow.show( offset.left, offset.top, 'bl' );
	zWindow.setAutoClose();
	Event.stopPropagation();
}

function zToggleWindow( Event )
{
	var $el = $( this );
	var offset = $el.offset();
	if ( zWindow.obj === this ) 
	{
		zWindow.hide();
		return false;
	}
	zWindow.load( $el.attr( 'href' ) );
	zWindow.show( offset.left, offset.top, 'bl' );
	zWindow.setAutoClose();
	zWindow.obj = this;
	Event.stopPropagation();
	Event.preventDefault();
	return false;
}