// $Id: events_scroll.js 117 2011-03-10 10:01:37Z jon $
/*
 * js to control events scrolling. 
 * The set of event items are scrolled behind a 'viewport' by making the items' enclosing div (eventlist) 
 * position:relative and changing the top attribute when the up/down buttons are activated 
 * 
 */
var now=0;
var ctop=0;
var inc=150;

$(document).ready(function() {
	// time 'now' is stored in title of .eventlist
	var now=$('.eventlist').attr('title');
	
	// nid of next event chronologically from now is stored in title of .eventlistcontainer
	var nexteventnid=$('.eventlistcontainer').attr('title');
	
	if (jQuery.browser.msie && jQuery.browser.version < 8) {
		//alert(jQuery.browser.msie+'--'+jQuery.browser.version); // ie: true--7.0
		do_ie7(nexteventnid);
		return
	}

	// get eventlist 'depth' and use below to stop scrolling off bottom  
	var eldepth=$('.eventlist').height()*-1;
	
	// set onclick handlers to move events by a set number of pixels
	 $('#scrollbuttonD').click(function() {
		 if (ctop < 0){
			 ctop=ctop+inc;
			 //$('.eventlist').css('top',ctop+'px');
			 //console.log("ctop="+ctop);
			$('.eventlist').animate({
				top: ctop
				},
				750,
				'swing'
		 	);
		 }
	 });

	 $('#scrollbuttonU').click(function() {
		 //console.log('eventlist depth='+eldepth+' ctop='+ctop);
		 if (ctop > eldepth){
			 ctop=ctop-inc;
			 
			//$('.eventlist').css('top',ctop+'px');
			$('.eventlist').animate({
				top: ctop
				},
				750,
				'swing'
		 	);	
		 }
	 });	
	

	//nexteventnid='281';
	nexteventsel='.eventlist #'+nexteventnid;
	
	// get position of next event object
	var pos=$(nexteventsel).position();
	
	// set scroll reference point (allowing for 15px top padding in eventitems) 
	ctop= -pos.top - 15;

	//$('.eventlistcontainer').css('border','solid 1px blue');

	// set spacial class on nex event item so we can theme that entry specially
	var ei=$(nexteventsel);
	ei.addClass('nexteventitem');
	/**
	//console.log('ei.height='+ei.height());
	// item div height is not full height of eventitems - for some reason
	// so work out ht of each line within item and set this on eventitem
	var th=0;
	ei.children().each(function(){th+=$(this).height();});
	//console.log('th='+th);
	ei.height(th);
	 **/
	
	// move pane div so current event is at top of enclosing frame   
	var newpos=ctop+'px';
	$('.eventlist').css('top',newpos);

	//var fdate=$(nexteventsel+' .eventfdate').html(); // Thursday 29 Oct 2009
	//alert('now='+now+' next='+nexteventsel+' cureltop='+$('.eventlist').css('top')+' newpos='+newpos+' eventfdate='+fdate);
		
});

/* IE7 shows overflowed text - so do display none - but then position of next event becomes 0
 * and scrolling requires event divs to be hidden/displayed
 * so just display future events - no scrolling
 */
function do_ie7(nexteventnid){
	$('#scrollbuttonD').css('display','none');
	$('#scrollbuttonU').css('display','none');
	
	// adjust top margin
	$('#block-events_list-0').css('margin-top','10px');
	
	
	//nexteventnid=172;
	var d=0;
	var nidfound=false;
	$('.eventlist .eventitem').each(function (i) {
        //if (this.title < now) 
    	//	this.style.display="none";
	
		// title used to hold event secs
		// id used to hold event nid
		if (!nidfound && $(this).attr('id') == nexteventnid)
			nidfound=true;

		if (!nidfound){
        	$(this).css('display','none');
        } else {
        	// tweak top padding
        	$(this).css('padding-top','0');
        	
          	// only display a few  items otherwise will overflow bottom...
        	d++;
        	if (d > 3)
        		$(this).css('display','none');
        }
      });
}

