
/* ** jScroll Facebook-Plugin ** */
function FacebookScroll(){
	$('.scroll-pane-facebook').each(
		function()
		{
			$(this).jScrollPane(
				{
					showArrows: true,
					verticalDragMinHeight: 25
				}
			);
			var api = $(this).data('jsp');
			var throttleTimeout;
			$('#fbpinnwand').bind(
				"resize",
				function()
				{
					if ($.browser.msie) {
						// IE fires multiple resize events while you are dragging the browser window which
						// causes it to crash if you try to update the scrollpane on every one. So we need
						// to throttle it to fire a maximum of once every 50 milliseconds...
						if (!throttleTimeout) {
							throttleTimeout = setTimeout(
								function()
								{
									api.reinitialise();
									throttleTimeout = null;
								},
								50
							);
						}
					} else {
						api.reinitialise();
					}
				}
			);
		}
	)
}

/* ** Browser Scrollbar Syncronize ** */
jQuery.fn.synchronizeScroll = function() {

        var elements = this;
        if (elements.length <= 1) return;

		elements.scroll(
		function() {
			var left = $(this).scrollLeft();
			var top = $(this).scrollTop();
			elements.each(
				function() {
					if ($(this).scrollLeft() != left) $(this).scrollLeft(left);
					if ($(this).scrollTop() != top) $(this).scrollTop(top);
				}
			);
		});		
}

/* ** Set RowIDs ** */
function SetRowIds (tableSelector) {
	var rowItems = tableSelector;
	
	for (var i = 0; i < rowItems.length; i++){
		node = $(rowItems[i]);
		number = i+1;
		node.addClass("rowItem_" + number);
	}
}

function SyncroScrollPane (tableSelector){
	
	//console.log(tableSelector);
	
	var scrollArea1 = $(tableSelector + ' .tableDummy').jScrollPane({
			showArrows: true,
			clickOnTrack: true
		});
		
	var scrollArea2 = $(tableSelector + ' .scrollTableContent').jScrollPane({
		showArrows: true,
		clickOnTrack: true
	});
	
	$(tableSelector + ' .tableDummy').bind(
        'jsp-scroll-x',
        function(event, scrollPositionX, isAtLeft, isAtRight)
        {
            if(	$(this).find('.jspDrag').hasClass('jspActive') || 
				$(this).find('.jspTrack').hasClass('jspActiveClick') || 
				$(this).find('.jspArrow').hasClass('jspActive'))
			{
            scrollArea2.data('jsp').scrollToX(scrollPositionX)
            }
        }
    )

	$(tableSelector + ' .scrollTableContent').bind( 
		'jsp-scroll-x', 
		function(event, scrollPositionX, isAtLeft, isAtRight) 
		{
			if(	$(this).find('.jspDrag').hasClass('jspActive') || 
				$(this).find('.jspTrack').hasClass('jspActiveClick') || 
				$(this).find('.jspArrow').hasClass('jspActive') || 
				$(this).find('.jspDrag').hasClass('jspActiveKey'))
			{
			scrollArea1.data('jsp').scrollToX(scrollPositionX) 
			} 
		} 
	)
}

function SyncroTable(tableSelector){
	
	// Set tableDummyWidth
	$(tableSelector + ' .tableDummy div').width($(tableSelector + ' .scrollTableContent table tr').width());
	
	SyncroTableHeight(tableSelector);
	
	
}

function SyncroTableHeight(tableSelector) {
	
	// Set tableRowHeight from scrollTableHead to scrollTableContent
	SetRowIds($(tableSelector + ' .scrollTableContent table tr'));
	
	var rowId = 0;
	
	$(tableSelector + ' .scrollTableHead table tr').each(function(i){
		rowId = $(tableSelector + ' .scrollTableHead table tr').index(this) + 1;
		var superZahl = $(this).height()+1;
		//console.log(superZahl);
		$(tableSelector + ' .scrollTableContent table tr.rowItem_' + rowId).height($(this).height()+1);
	});
	
	// Set tableRowHeight from scrollTableContent to scrollTableHead
	SetRowIds($(tableSelector + ' .scrollTableHead table tr'));
	
	$(tableSelector + ' .scrollTableContent table tr').each(function(i){
		rowId = $(tableSelector + ' .scrollTableContent table tr').index(this) + 1;
		$(tableSelector + ' .scrollTableHead table tr.rowItem_' + rowId).height($(this).height());
	});
}

/* ** Alternative Layout for TableScrolling ** */
/*	
  	timer = 1;
	
	if ($('.row').size() > 5){

		var  size = $('.row').size();
		
		$('td[class*="row_"]').hide();
		$('th[class*="row_"]').hide();
		
		for( i=1; i <= 5; i++){
  			$('.row_'+i).show();
		}	
	}

	$('.scrollbar').click( function() {
		var mytimer = timer;
		var  size = $('.row').size();
		
		$('td[class*="row_"]').hide();
		$('th[class*="row_"]').hide();
		alert(mytimer);
		for (i = mytimer+1; i <= mytimer+5; i++) {
			$('.row_' + i).show();
			
		}
		
		timer++;
	});
*/

$(document).ready(function() {
	
	/* ** General jScroll ** */
		$('.scroll-pane').jScrollPane({
			showArrows: true, 
			verticalDragMinHeight: 25 
		});
		
		$(".scrollTableWrapper").each(function (i) {
			/* ** Syncronize Table ** */
			SyncroTable('#'+ $(this).attr('id'));
			
			/* ** Syncronize BrowserScrollBar ** */
			$('#'+ $(this).attr('id')).synchronizeScroll();
			
			/* ** Syncronize jScrollPane ** */
			//console.log($(this +'.tableContent table th').length);
			
			if ($(this +'.tableContent table th').length < 8) {
				$(this +'.tableDummy').remove();
				$(this).css({'padding-top': '12px'});
				$(this +'.jspContainer').css({'padding-bottom': '5px'});
			}else {
				
				SyncroScrollPane('#'+ $(this).attr('id'));
			}
		
		});
		
	/* ** Homepage Facebook-Box-Scroll ** */
		FacebookScroll();
	
});
