/*
PRINCIP: kazdemu tag-u, pres ktere chceme skakat se nastavi trida scroll.

init uz si je sam vyhleda, prip. pokud obsahuje nejake obrazky, tak se
prubezne po jejich nahrani zaktualizuje jejich poloha.

kd - korekce dolu
*/

//------------------------------------------------------------------------------
var scroll = {
    list: []
    , curr: -1
    , conf: { axis: 'y', duration: 250 }
    , kd: 20
    , form: false
    , key2event: {
	    37: scroll_event_prev
	    , 75: scroll_event_prev
	    , 39: scroll_event_next
	    , 74: scroll_event_next
	    , 67: scroll_event_c
    }
};

//------------------------------------------------------------------------------
function scroll_init()
{
    scroll.list = [];
    $('.scroll').each(function(){
	var i = $(this).attr('id');
	if ( ! i )
	{
	    i = "scroll-_" + scroll.list.length;
	    $(this).attr( 'id', i );
	}
	var y = $(this).offset().top;
	scroll.list[scroll.list.length] = { id: '#' + i, y: y };
    });
}

//------------------------------------------------------------------------------
function scroll_event_prev()
{
    var y = $(window).scrollTop();
    for ( var i = scroll.list.length - 1; i >= 0; -- i )
    {
	if ( scroll.list[i].y >= y ) continue;
	$(window).scrollTo( scroll.list[i].id, scroll.conf );
	scroll_init();
	break;
    }
}

//------------------------------------------------------------------------------
function scroll_event_next()
{
    var y = $(window).scrollTop();
    for ( var i = 0; i < scroll.list.length; ++ i )
    {
	if ( scroll.list[i].y <= ( y + scroll.kd ) ) continue;
	$(window).scrollTo( scroll.list[i].id, scroll.conf );
	scroll_init();
	break;
    }
}

//------------------------------------------------------------------------------
function scroll_event_c()
{
    $(window).scrollTo( "#comments", scroll.conf );
}

//------------------------------------------------------------------------------
function scroll_form( id )
{
	if ( id )
	{
		$("#"+id+" textarea"        ).focus( function(){ scroll.form = true;  });
		$("#"+id+" input[type=text]").focus( function(){ scroll.form = true;  });
		$("#"+id+" textarea"        ).blur(  function(){ scroll.form = false; });
		$("#"+id+" input[type=text]").blur(  function(){ scroll.form = false; });
	}
	else
	{
		$("textarea"        ).focus( function(){ scroll.form = true;  });
		$("input[type=text]").focus( function(){ scroll.form = true;  });
		$("textarea"        ).blur(  function(){ scroll.form = false; });
		$("input[type=text]").blur(  function(){ scroll.form = false; });
	}
}

//------------------------------------------------------------------------------
$(document).ready(function(){
    scroll_init();
    $('.scroll img').load( scroll_init() );
    scroll_form();

    $(document).keydown(
	    function(event)
	    {
		    if ( event.shiftKey || event.ctrlKey ) return;
		    if ( scroll.form ) return;
		    var f = scroll.key2event[event.which];
		    if ( f ) f();
	    }
    );
});

//------------------------------------------------------------------------------

