var cur_dish_th = '';

function AlphaInit ()
{
    $( '#alpha_toggler' ).toggle (
        function () {
            $( '.alpha' ).animate ( { height: 'show' }, 300 );
        },
        function () {
            $( '.alpha' ).animate ( { height: 'hide' }, 300 );
        }
    );
}

function ShowDishMid ( id, obj )
{
    $( '#dish_r_' + cur_dish_th ).hide ();
    cur_dish_th = id;
    
    var offset = $( obj ).offset ();
    var bwidth = $( 'body' ).width ();
    
    if ( bwidth < offset.left + 323 )
    {
        $( '#dish_r_' + id )
            .find ( 'div' )
            .css ( 'left', '-229px' );
    }
    
    $( '#dish_r_' + id )
        .css ( { top: ( parseInt ( offset.top ) - 25 ) + 'px', left: ( parseInt ( offset.left ) - 25 ) + 'px' } )
        .show ();
}

function HideDishMid ( obj )
{
    $( obj ).hide ();
}

function ShowDishBox ( path, place, place_name, cat, name, desc, price )
{
    jQuery.facebox ( function () {
        var img = new Image();

        img.onload = function () {
            jQuery.facebox ( '<img src="' + path + '" onclick="$(document).trigger(\'close.facebox\')" /><div class="dish_box_name">' + name + '</div><div class="dish_box_desc">' + desc + '</div><div class="dish_box_price">' + price + '</div><div class="dish_box_place"><a href="/site/all/main/' + place + '/">' + place_name + '</a> / ' + cat + '</div>' );
        }
        
        img.src = path;
    } );
}

function VoteForPost ( id, type, place )
{
    var data = {
		id: id,
		place: place,
		type: type
	};
	
	client_request ( 'places', 'avote_for_post', '', 'GET', data, 'AfterVoteForPost', id + ', "' + type + '"' );
}

function AfterVoteForPost ( id, type, data )
{
	$( '#post_' + id )
		.find ( 'div.post_message' )
		.remove();
	
	if ( data.status == 'unauth' )
	{
        window.location = '/slogin/?type=vote_for_post';
	}
	
	if ( data.status == 'limit' )
	{
		$( '#post_' + id )
			.find ( 'div.post_votes_table' )
			.hide();

		$( 'div.post_message' ).remove();
			
		$( '#post_' + id )
			.append ( '<div class="post_message"><b>За одного пользователя нельзя голосовать больше 2-х раз в сутки</b></div>' )
			.find ( 'div.post_message' )
			.animate ( { height: 'show', opacity: 'show' }, 200 );
			
		setTimeout ( function () {
			$( '#post_' + id )
    				.find ( 'div.post_message' )
    				.animate ( { height: 'hide', opacity: 'hide' }, 200 );
			setTimeout ( function () {
				$( '#post_' + id )
    				.find ( 'div.post_message' )
    				.remove();
			}, 200 );
		}, 5000 );
	}
	
	if ( data.status == 'myrow' )
	{
		$( '#post_' + id )
			.find ( 'div.post_votes_table' )
			.hide();

		$( 'div.post_message' ).remove();
			
		$( '#post_' + id )
			.append ( '<div class="post_message"><b>Вы не можете голосовать за свой пост</b></div>' )
			.find ( 'div.post_message' )
			.animate ( { height: 'show', opacity: 'show' }, 200 );
			
		setTimeout ( function () {
			$( '#post_' + id )
    				.find ( 'div.post_message' )
    				.animate ( { height: 'hide', opacity: 'hide' }, 200 );
			setTimeout ( function () {
				$( '#post_' + id )
    				.find ( 'div.post_message' )
    				.remove();
			}, 200 );
		}, 5000 );
	}
	
	if ( data.status == 'voted' )
	{
		$( '#post_' + id )
			.find ( 'div.post_votes_table' )
			.hide();
		
		$( '#post_' + id )
			.append ( '<div class="post_message"><b>Вы уже голосовали за этот пост</b></div>' )
			.find ( 'div.post_message' )
			.animate ( { height: 'show', opacity: 'show' }, 200 );
			
		setTimeout ( function () {
			$( '#post_' + id )
    				.find ( 'div.post_message' )
    				.animate ( { height: 'hide', opacity: 'hide' }, 200 );
			setTimeout ( function () {
				$( '#post_' + id )
    				.find ( 'div.post_message' )
    				.remove();
			}, 200 );
		}, 5000 )
	}
	
	if ( data.status == 'ok' )
	{
		var offset = $( '#post_' + id ).find( '.post_ud' ).offset();
		
		$( 'body' ).append ( '<div class="voted_op">Спасибо, ваш голос учтен.</div>' );
		$( '.voted_op' )
			.css ( {top: ( offset.top - 13 ) + 'px', left: ( offset.left + 80 ) + 'px'} )
			.animate ( {opacity: 'show'}, 200 );
			
		setTimeout ( function () {
			$( '.voted_op' ).animate ( {opacity: 'hide'}, 200 );
			setTimeout ( function () {
				$( '.voted_op' ).remove ();
			}, 300 );
		}, 2000 );
		
		var up   = parseInt ( $( '#post_' + id ).find( 'span.vote_link_yes > span' ).text() );
		var down = parseInt ( $( '#post_' + id ).find( 'span.vote_link_no > span' ).text() );
		
		if ( type == 'u' )
		{
			up++;
		} else {
			down++;
		}
		
		$( '#post_' + id )
			.find( 'span.vote_link_yes > span' )
			.text ( up );
		$( '#post_' + id )
			.find( 'span.vote_link_no > span'  )
			.text ( down );
		
		$( '#post_' + id )
    		.find ( 'span.votes_rows' )
    		.replaceWith ( '<div class="op_ajax_indicator">Идет загрузка...</div>' );
			
		ShowPostVotes ( id );
	}
}

function ShowPostVotes ( id )
{
	$( '#post_' + id )
		.find ( 'div.post_message' )
		.remove();
	
	if ( $( '#post_' + id ).find ( 'div.op_ajax_indicator' ).length == 1 )
	{
		var data = {
			id: id
		}
		
		client_request ( 'places', 'get_votes_list', '', 'GET', data, 'AfterShowPostVotes', id );
	}
	
	if ( $( '#post_' + id ).find ( 'div.post_votes_table:hidden' ).length > 0 )
	{
		$( '#post_' + id )
			.find ( 'div.post_votes_table' )
			.animate ( { height: 'show', opacity: 'show' }, 200 );
	} else {
		$( '#post_' + id )
			.find ( 'div.post_votes_table' )
			.animate ( { height: 'hide', opacity: 'hide' }, 200 );
	}
}

function AfterShowPostVotes ( id, data )
{
	var html = '<span class="votes_rows">';
	var type = '';
	var summ = 0;

	for ( var i = 0; i < data.rows.length; i++ )
	{
		type = data.rows[i].type == 'u' ? '<img src="/images/small_arr_u.gif" alt="" height="9" width="9"> <span class="votes_list_up">ЗА</span>' : '<img src="/images/small_arr_d.gif" alt="" height="9" width="9"> <span class="votes_list_down">ПРОТИВ</span>';
		
		if ( data.rows[i].type == 'u' )
		{
		    summ += parseInt ( data.rows[i].points );
		} else {
		    summ -= parseInt ( data.rows[i].points );
		}
		
		html += '<div><a href="/user/' + data.rows[i].user_id + '/"><img src="' + data.rows[i].avatar + '" class="avatar" width="25" height="25" alt="" /></a><a href="/user/' + data.rows[i].user_id + '/">' + data.rows[i].user + '</a>&nbsp;&nbsp;&nbsp;&mdash;&nbsp;&nbsp;&nbsp;' + type + '</div>';
	}
	
	html += '</span>';

	$( '#post_' + id )
		.find ( 'div.op_ajax_indicator' )
		.remove();
	
	$( '#post_' + id )
		.find ( 'div.post_votes_table' )
		.append ( html )
		.find ( 'span.votes_rows > div' )
		.css ( { backgroundColor: '#fdfdf2' } );
		
	$( '#post_' + id )
		.find ( 'span.votes_rows > div:nth-child(even)' )
		.css ( { backgroundColor: '#f8f8d5' } );
}

function ShowHiddenOpinion ( id )
{
    $( '#post_' + id )
        .find ( 'span.hidden_post_message' )
        .hide ()
        .end ()
        .find ( 'span.hidden_post_text' )
        .show ()
        .end ()
        .find ( 'td.opinion_marks > span' )
        .show ();
}

var suggest_timeout = null;
var suggest_sended = 0;

function SearchSuggestInit ()
{
    $( '#keyword_field' ).bind ( 'keydown', function ( event ) {
        if ( ( event.keyCode >= 96 && event.keyCode <= 105 ) || ( event.keyCode >= 65 && event.keyCode <= 90 ) || ( event.keyCode >= 48 && event.keyCode <= 57 ) || ( event.keyCode >= 188 && event.keyCode <= 192 ) || ( event.keyCode >= 219 && event.keyCode <= 222 ) || event.keyCode == 226 || event.keyCode == 59 || event.keyCode == 111 || event.keyCode == 32 || event.keyCode == 8 || event.keyCode == 46 )
        {
            clearTimeout ( suggest_timeout );
            
            if ( $( '#keyword_field' ).val().length > 0 )
            {
                suggest_timeout = setTimeout ( function () {
                    var q = $( '#keyword_field' ).val();
                    suggest_sended++;
                    
                    $.getJSON ( '/ajax/places/suggest/', { q: q, s: suggest_sended }, function ( data ) {
                        if ( suggest_sended == data.s )
                        {
                            if ( data.rows.length > 0 )
                            {
                                $( '#suggest_results' ).empty().show();
                                
                                for ( var i in data.rows )
                                {
                                    $( '#suggest_results' ).append ( '<div onclick="ClickSuggestN(this)" onmouseover="HoverSuggestN(this)" class="suggest_n">' + data.rows[i] + '</div>' );
                                }
                            } else {
                                $( '#suggest_results' ).empty().hide();
                            }
                        }
                    } );
                }, 150 );
            } else {
                $( '#suggest_results' ).empty().hide();
            }
        }
        
        switch ( event.keyCode )
    	{
    		case 38:
    			SuggestMS ( 'up' );
    			break;
    			
    		case 40:
    			SuggestMS ( 'down' );
    			break;
    	}
    } )
    .bind ( 'blur', function () {
        clearTimeout ( suggest_timeout );
        $( '#suggest_results' ).empty().hide();
    } );
}

function HoverSuggestN ( obj )
{
    $( '#keyword_field' ).val ( $( obj ).text() );
    $( 'div.suggest_n' ).removeAttr ( 'id' );
    $( obj ).attr ( 'id', 'suggest_n_selected' );
}

function ClickSuggestN ( obj )
{
    $( '#keyword_field' ).val ( $( obj ).text() );
    $( '#suggest_results' ).empty().hide();
}

function SuggestMS ( action )
{
	var num = null;
	
	if ( $( 'div.suggest_n' ).length > 0 )
	{
		$( 'div.suggest_n' ).each ( function ( index ) {
			if ( $( this ).attr ( 'id' ) == 'suggest_n_selected' )
			{
				num = index;
				$( this ).removeAttr ( 'id' );
			}
		} );
		
		if ( action == 'down' )
		{
			if ( num == $( 'div.suggest_n' ).length - 1 || num == null )
			{
				num = 0;
			} else {
				num++;
			}
		} else {
			if ( num == 0 || num == null )
			{
				num = $( 'div.suggest_n' ).length - 1;
			} else {
				num--;
			}
		}
		
		$( 'div.suggest_n:eq(' + num + ')' ).attr ( 'id', 'suggest_n_selected' );
			
		$( '#keyword_field' ).val ( $( '#suggest_n_selected' ).text() );
	}
}