
Yonder = {
    stopScroll : true



    , GeneratePersonDescription : function (person) {
        var res = [];
        res.push('<div class="develbio"><span class="bioname">');
        res.push(person.persName);
        res.push('</span></span>');
        res.push('<span class="biofunct"><span class="biolbl">');
        res.push(Yonder.PeopleList.locFunction);
        res.push(': </span><span class="bioval">');
        res.push(person.persFunction);
        res.push('</span></span>');
        res.push('<span class="biohobby"><span class="biolbl">');
        res.push(Yonder.PeopleList.locHobby);
        res.push(': </span><span class="bioval">');
        res.push(person.persHobby);
        res.push('</span></span></div>');
        return res.join('').replace(/"/g, "'");
    }

    , InsertInitialImages : function () {
        var initial=5
        , len = Yonder.PeopleList.people.length
        , start = Math.floor(Math.random() * (len-initial)) 
        , end = start + initial
        , person=null;
        
        for( start; start < end && start < len ; start ++ ) {
            person = Yonder.PeopleList.people[start];
            $('#frontpagegallery1').append('<img src="' + person.link + '" alt="' + Yonder.GeneratePersonDescription(person) + '"/>');
        }
    }

}


Yonder.SiteMenu = {
      
      showTID : null

    , InitMenu : function () {
	Yonder.SiteMenu.CheckDivers();
        var list =$('li.cat-item').has('ul.children'); it=0, itEnd =list.length;
        for(it; it < itEnd; it++) $(list[it]).hover(Yonder.SiteMenu.ShowMenu, Yonder.SiteMenu.HideMenu);        
    }

    , CheckDivers : function () {
	if( $('#sidebar').outerHeight() > 400 ) {
	    $('.sdb_bot:visible').fadeOut('slow');    
	} else {
	    $('.sdb_bot:hidden').fadeIn('slow');    
	}
	
    }

    , ShowMenu : function () {
        var actualShow = $(this);
        this.showTID=setTimeout(
            function() {		
                actualShow.find('ul.children').show('fast', Yonder.SiteMenu.CheckDivers);		
            }
            , 600
        );
    }

    , HideMenu : function () {
        if( this.showTID ) clearTimeout(this.showTID);
        this.showTID = null;

        var actualHide = $(this);

        setTimeout(
            function () {		
                if( actualHide.is('.current-cat-parent') || actualHide.is('.current-cat')) return;
                actualHide.find('ul.children').hide('slow', Yonder.SiteMenu.CheckDivers);
            }
            , 800
        );
    }

}






function checkScrollButtons() {
    var mouseDownButton = $('.mouseover_down');
    var scrollableContainer = $("#scrollableContainer");
    var mouseUpButton = $('.mouseover_up');

    if( scrollableContainer.scrollTop() <= 1 ) {
        mouseUpButton.addClass('disabled');        
    } else if( mouseUpButton.has('.disabled').length >= 0 ) mouseUpButton.removeClass('disabled')
    

    if( $('#scrollableContent').innerHeight() <= (scrollableContainer.scrollTop() + scrollableContainer.innerHeight())) {
        mouseDownButton.addClass('disabled');       
    } else if( mouseDownButton.has('.disabled').length >= 0 ) mouseDownButton.removeClass('disabled')
    

}

function scrollDownABit(amount, timeout) {
    checkScrollButtons();
    
    if( typeof(amount) == 'undefined') amount = "3";
    if( typeof(timeout) == 'undefined') timeout = 20;
    var scrollableContainer = $("#scrollableContainer");

     if( $('.mouseover_down').has('.disabled').length >= 1) {
        Yonder.StopScroll = true;
        console.log('is false');
        return;
     }
        
    scrollableContainer.scrollTo("+=6px", 0, {
        onAfter: function() {
            if( Yonder.StopScroll === false ) setTimeout(scrollDownABit, timeout);
        }
    });
}


function scrollUpABit(amount, timeout) {
    checkScrollButtons();

    if( typeof(amount) == 'undefined') amount = "3";
    if( typeof(timeout) == 'undefined') timeout = 20;
    var scrollableContainer = $("#scrollableContainer");

     if( $('.mouseover_up').has('.disabled').length >= 1) {
        Yonder.StopScroll = true;
        return;
     }

    scrollableContainer.scrollTo("-=6px", 0, {
        onAfter: function() {
            if( Yonder.StopScroll === false ) setTimeout(scrollUpABit, timeout);
        }
    });
}

function handle(delta) {
        if (delta < 0)
            scrollDownABit("6", 20);
        else
            scrollUpABit("6", 20);
}


/** Event handler for mouse wheel event.
 */
function wheel(event){
        var delta = 0;
        if (!event) /* For IE. */
                event = window.event;
        if (event.wheelDelta) { /* IE/Opera. */
                delta = event.wheelDelta/120;
                /** In Opera 9, delta differs in sign as compared to IE.
                 */
                if (window.opera)
                        delta = -delta;
        } else if (event.detail) { /** Mozilla case. */
                /** In Mozilla, sign of delta is different than in IE.
                 * Also, delta is multiple of 3.
                 */
                delta = -event.detail/3;
        }
        /** If delta is nonzero, handle it.
         * Basically, delta is now positive if wheel was scrolled up,
         * and negative, if wheel was scrolled down.
         */
        if (delta)
                handle(delta);
        /** Prevent default actions caused by mouse wheel.
         * That might be ugly, but we handle scrolls somehow
         * anyway, so don't bother here..
         */
        if (event.preventDefault)
                event.preventDefault();
	event.returnValue = false;
}

function init() {

    Yonder.SiteMenu.InitMenu();

    if( $('#scrollableContent').innerHeight() <= $('#scrollableContainer').innerHeight() )  {        
        $('#scrollLinks').css('display', 'none');
    } else {
        $('#scrollableContainer').width($('#scrollableContainer').outerWidth() - 20);
        $('#scrollLinks').width('20px');
        $('#scrollLinks').css('display', 'block');
    }



    $('.mouseover_up').hover (
          function() { Yonder.StopScroll = false; scrollUpABit();}
        , function() { Yonder.StopScroll = true;}
    )

    $('.mouseover_up').mousedown( function() {
        var step = $('#scrollableContent').innerHeight();
        var pos = $("#scrollableContainer").scrollTop() - step;
        if ( pos < 0 ) pos = 0;
        $("#scrollableContainer").scrollTo(pos, 800, {easing:'swing'});
    })


    $('.mouseover_down').hover (
          function() { Yonder.StopScroll = false; scrollDownABit();}
        , function() { Yonder.StopScroll = true;}
    )

    $('.mouseover_down').mousedown( function() {
        var step = $('#scrollableContent').innerHeight();
        var pos = $("#scrollableContainer").scrollTop() + step;
        $("#scrollableContainer").scrollTo(pos, 800, {easing:'swing'});
    })

    checkScrollButtons();

    if( typeof($("#frontpagegallery1")) != 'undefined') {
        $("#frontpagegallery1").jqFancyTransitions({width:760, height:380, delay:10000, stripDelay:1000, strips:1, titleOpacity: 1.0 });
        $('#frontpagegallery1').css('visibility', 'visible');
    }

    if (window.addEventListener)
        /** DOMMouseScroll is for mozilla. */
        window.addEventListener('DOMMouseScroll', wheel, false);
    /** IE/Opera. */
    window.onmousewheel = document.onmousewheel = wheel;

    if( $('.scrollWrapper')) {
        $('.scrollWrapper').css('visibility', 'hidden');
        setTimeout( function() {
            $('.scrollWrapper').scrollLeft(Math.round(Math.random()*25)*255);
            $('.scrollWrapper').css('visibility', 'visible');
            }
            , 300);
    }
}

