var AllSizesToForce = new Array(94,98,85,84,80,82,80,94,111,98,108,107);

var StartMonth = 0;

var SizesToAdjust = new Array();
var SizesInPixel = new Array();
var SizesToForce = new Array();

var DifferenceToForce = 0;
var Restart = true;

var MousePosition = new Object();
var MouseDifference = 0;
var MonthSizes = new Array();

var HTML = new Array();
var HTMLData = new Object();

var Stamp = '';
var Locked = false;
var ShareURL = '';

var ClickTimer;
var ShareTimer;
var ShareOutTimer;

var SetToToday;
var TodayBox;

function LaunchStream(StampToSet){
	$('#Categories LI').each(function(){ $(this).click(Filter); });
	$('#ScrollMonths').click(function(event){ FadeStream('Absolute-' + event.pageX); });
	$('#ScrollThumb').draggable({containment:'DIV#ScrollArea', axis:'x', drag:DragStream, start:DragStart});
	$('#StreamContent').bind('mousedown', ScrubStart);
	
	Stamp = StampToSet;	
	StartMonth = parseInt(Stamp.substr(5, Stamp.length));

	LoadStream();
	$(document).bind('mousemove', Arrows);
}

function FilterLoadStream(StampToSet){
	$('#ScrollMonths').click(function(event){ FadeStream('Absolute-' + event.pageX); });
	$('#ScrollThumb').draggable({containment:'DIV#ScrollArea', axis:'x', drag:DragStream, start:DragStart});
	$('#StreamContent').bind('mousedown', ScrubStart);
	
	Stamp = StampToSet;	
	StartMonth = parseInt(Stamp.substr(5, Stamp.length));
	
	LoadStream();	
	$(document).bind('mousemove', Arrows);
}

function SizeStream(){
	$('#ScrollArea').css('width', ($(window).width()-66) + 'px');
	$('#StreamArrowRight').css('left', ($(window).width()-68) + 'px');

	if($('#StreamContent').width() <= $(window).width()) { 
		$('#ScrollThumb').hide();
		//Locked = true;
	} else {
		$('#ScrollThumb').show();
		//Locked = false;
	}
}


function SizeMonths() {
	
	//$('#ScrollThumb').css('left', (parseInt($(window).width()) / 2) - (parseInt($('#ScrollThumb').width()) / 2) + 'px');
	$('#ScrollThumb').css('left', (parseInt($(window).width()) - 200) + 'px');
	
	MonthSizesPercent = new Array();
	
	StreamContentWidth = $('#StreamContent').width();
	MonthCount = 0;
	MonthTotal = 100;
	
	$('#ScrollMonths').children('.Month').each(function() {
		MonthSize = (((MonthSizes[MonthCount]) / StreamContentWidth)*100);	
		MonthCount++;
			
		if(MonthCount == $('#ScrollMonths').children('.Month').size()) MonthSize = MonthTotal;		
		MonthTotal -= MonthSize;
		MonthSizesPercent.push(MonthSize);	
	});
	
	
	// VARIABLEN-RESET
	SizesToAdjust = new Array();
	SizesInPixel = new Array();
	SizesToForce = new Array();
	
	DifferenceToForce = 0;
	TotalForce = 0;
	MonthCount = 0;
	
	// MONATSGRÖSSEN AUS AllSizesToForce ERSTELLEN, ABHÄNGIG VON DARGESTELLTER ZEITSPANNE
	for(M=StartMonth; M<StartMonth+5; M++) { 
		MonthToPickFrom = M;
		if(MonthToPickFrom >= 12) MonthToPickFrom -= 12;
		
		//alert('ID: ' + MonthToPickFrom + ' - ' + AllSizesToForce[MonthToPickFrom]);
		SizesToForce.push(AllSizesToForce[MonthToPickFrom]);
	}
	
	// GESAMTGRÖSSE
	for(F=0; F<SizesToForce.length; F++) TotalForce += SizesToForce[F];

	Restart = true;
	
	// WENN GESAMTGRÖSSE UNTERSCHRITTEN > ENDE
	//alert(($(window).width() - 64) + ' < ' + TotalForce);
	
	if(($(window).width() - 64) < TotalForce){
		$('#ScrollMonths .Month').each(function(){
			$(this).css('width', (SizesToForce[MonthCount]) + 'px');
			MonthCount++;
		});
		
		return;
	}
		
	// GRÖSSE DER ELEMENTE IN PIXEL	
	for(S=0; S<MonthSizesPercent.length; S++) SizesInPixel[S] = ($(window).width() - 64)/100*MonthSizesPercent[S];
	//for(S=0; S<MonthSizesPercent.length; S++) alert(($(window).width() - 64)/100*MonthSizesPercent[S]);
	
	// PRÜFUNG, OB ELEMENT DIE MONATS-MINDEST-GRÖSSE UNTERSCHREITET					
	for(S=0; S<SizesInPixel.length; S++){		
		if(SizesInPixel[S] <= SizesToForce[S]){
			DifferenceToForce += SizesToForce[S] - SizesInPixel[S];
			SizesInPixel[S] = SizesToForce[S];
		} else {
			SizesToAdjust.push(S);
		}
	}
	
	// WENN UNTERSCHREITUNGEN, DANN ANPASSUNG DER ANDEREN ELEMENTE		
	if(DifferenceToForce > 0) { 
		while(Restart) {
			Adjust();
		}
	}
							
	// PER CSS SETZEN
	MonthCount = 0;
	$('#ScrollMonths .Month').each(function(){
		$(this).css('width', (parseInt(SizesInPixel[MonthCount])-1) + 'px');
		MonthCount++;
	});
}

function Adjust(){
	DifferenceThatHasBeenSubtracted = 0;
	TotalSizeOfAdjustables = 0;
	
	// GESAMT VERFÜGBARE ANPASSBARE ELEMENTE
	for(S=0; S<SizesToAdjust.length; S++) TotalSizeOfAdjustables += SizesInPixel[SizesToAdjust[S]];
					
	Restart = false;
									
	for(S=0; S<SizesToAdjust.length; S++) {
		// ANTEIL, DER AN DIESEM ELEMENT ANZUPASSEN IST
		DifferenceToLoose = DifferenceToForce*(SizesInPixel[SizesToAdjust[S]]/TotalSizeOfAdjustables);
		
		// WENN DAS NICHT ZUR GÄNZE GEHT ...
		if(SizesInPixel[SizesToAdjust[S]] - DifferenceToLoose <= SizesToForce[SizesToAdjust[S]]){					
			DifferenceToForce -= SizesInPixel[SizesToAdjust[S]] - SizesToForce[SizesToAdjust[S]];
			DifferenceToForce -= DifferenceThatHasBeenSubtracted;
			
			SizesInPixel[SizesToAdjust[S]] = SizesToForce[SizesToAdjust[S]];
			
			NewSizesToAdjust = new Array();
			for(N=0; N<SizesToAdjust.length; N++) if(N != S) NewSizesToAdjust.push(SizesToAdjust[N]);
			SizesToAdjust = NewSizesToAdjust;
			
			if(SizesToAdjust.length == 0) return;
			
			Restart = true;
			
			return;
		// WENN MÖGLICH ...	
		} else {
			SizesInPixel[SizesToAdjust[S]] -= DifferenceToLoose;
			DifferenceThatHasBeenSubtracted += DifferenceToLoose;
		}
	}
	
	// PRÜFSUMME > MUSS NACH DIESER FUNKTION IN Align() WIEDER NULL SEIN.
	DifferenceToForce -= DifferenceThatHasBeenSubtracted;
}

function Arrows(event){
	if(event.pageX < 100) $('#StreamArrowLeft').css('visibility', 'visible');
	else $('#StreamArrowLeft').css('visibility', 'hidden');

	if(event.pageX > $(window).width()-100) $('#StreamArrowRight').css('visibility', 'visible');
	else $('#StreamArrowRight').css('visibility', 'hidden');
}

function DragStart(){
	$('#StreamContent').stop();
	$('#ScrollThumb').stop();
}

function DragStream(){
	Percent = ($('#ScrollThumb').position().left-33) / ($(window).width()-66 - $('#ScrollThumb').width());
	Percent = Math.min(Percent, 1);	
	StreamPosition = Percent;
	$('#StreamContent').css('left', parseInt((0-($('#StreamContent').width() - $(window).width()))*Percent) + 'px');
}

function FadeStream(Direction){	
	Position = $('#StreamContent').position();
	if(String(Direction).indexOf('Absolute-') != -1){
		Type = 'Absolute';
		
		Direction = parseInt(String(Direction).substr(9, String(Direction).length));
		Direction -= 33;
		
		Percent = (Direction / ($(window).width()-66));
		Target = 0 - Math.abs(($('#StreamContent').width() - $(window).width())) * Percent;
	
		Distance = Target - Position.left;
		
	} else {
		Type = 'Relative';
		Distance = ($(window).width() - 100)*-1*Direction;
	}
		
	Percent = Math.abs(Position.left)/Math.abs($('#StreamContent').width() - $(window).width());
	
	if(Percent == 0 && Direction == -1 && Type == 'Relative') SetStream(-1);
	else if(Percent == 1 && Direction == 1 && Type == 'Relative') SetStream(1);
	else {
	
		Target = Math.min(Position.left + Distance, 0);
		Target = Math.max(($('#StreamContent').width() - $(window).width())*-1, Target);
		
		Percent = Math.abs(Target)/Math.abs($('#StreamContent').width() - $(window).width());
		
		$('#StreamContent').stop();
		$('#StreamContent').animate({left: Target}, {duration: 1000});
		
		$('#ScrollThumb').stop();
		$('#ScrollThumb').animate({left: parseInt(33 + ($(window).width()-66 - $('#ScrollThumb').width())*Percent)}, {duration: 1000});
		
	}
}



function SetStream(Offset){
	Percent = ($('#ScrollThumb').position().left-33) / ($(window).width()-66 - $('#ScrollThumb').width());
	Percent = Math.min(Percent, 1);
	
	if(Offset < 0 && Percent > 0 && Percent != 0) {
		$('#ScrollThumb').stop();
		$('#ScrollThumb').animate({left: 33 + 'px'}, {duration: 250 + 750*Percent});
		
		$('#StreamContent').stop();
		$('#StreamContent').animate({left: 0 + 'px'}, {duration: 250 + 750*Percent});
		return;
	} else if(Offset > 0 && Percent >= 0 && Percent != 1){
		$('#ScrollThumb').stop();
		$('#ScrollThumb').animate({left: ($(window).width() - 33 - $('#ScrollThumb').width()) + 'px'}, {duration: 250 + 750*(1-Percent)});
		
		$('#StreamContent').stop();
		$('#StreamContent').animate({left: (0-($('#StreamContent').width() - $(window).width())) + 'px'}, {duration: 250 + 750*(1-Percent)});
		return;
	}
			
	Stamp = String(Stamp).split('-');
	Stamp[0] = Number(Stamp[0]);
	Stamp[1] = Number(Stamp[1]);
	
	Stamp[1] += Offset;
		
	if(Stamp[1] < 1){ 
		Stamp[1] = 12 + Stamp[1];
		Stamp[0]--;
	}
	
	if(Stamp[1] > 12){ 
		Stamp[1] = Stamp[1]-12;
		Stamp[0]++;
	}
	
	Stamp = Stamp.join('-');
	
	LoadStream();
}

function LoadStream(){
	Loader(true);
	
	$.ajax({
	  url: 'backend/action.php?Action=Stream-Tom&Stamp=' + Stamp,
	  dataType: 'json',
	  success: CreateStream,
		cache: true
	});
}

function CreateStream(Data){
	
	HTML = new Array();
	HTMLData = Data;
	
	CompareToToday = new Date();
	CompareMonth = CompareToToday.getMonth()+1;
	CompareDate = CompareToToday.getDate();
	
	CompareToToday = Number(String(CompareToToday.getFullYear()) + (String(CompareMonth).length == 1 ? '0' : '') + String(CompareMonth) + (String(CompareDate).length == 1 ? '0' : '') + String(CompareDate));
		
	for(M=0; M<Data.Months.length; M++){
		var Boxes = Data['Month-' + M];
		var HTMLMonth = new Array();
		
		for(B=1; B<=parseInt(Boxes.Amount); B++){
			
			var Entry = Boxes['Entry-' + B];
			
			if(typeof(Entry.Date) != "undefined"){
				if(Number(String(Entry.Date).split('.').reverse().join('')) >= CompareToToday && TodayBox == null) {
					TodayBox = M + '-' + B;
				}
			}
			
			var EntryHTML 	= '<div id="' + M + '-' + B + '" class="Box ' + Entry.Type + ' ' + Entry.Category + '"' + ((String(Entry.Link).length > 0) ? ' style="cursor:pointer;" onmouseover="javascript:OverStart(this, \'' + Entry.Link + '\', \'' + Entry.Category + '\');" onmousedown="javascript:ClickStart(this, \'' + Entry.Link + '\', \'' + Entry.Category + '\');"' : '') + '>';
			EntryHTML += '<div class="Category"></div>';
			
			var Hidetext = String(Entry.Hidetext);
			if (Hidetext != '1') {
			if(String(Entry.Title).length > 0 || String(Entry.Teaser).length > 0) { 
				EntryHTML += '<div class="Content">';
				EntryHTML += '<span class="Date">' + Entry.Date + '</span>';
				if(String(Entry.Title).length > 0) EntryHTML += '<h1 class="Title">' + Entry.Title + '</h1>';  
				if(String(Entry.Teaser).length > 0) EntryHTML += '<div class="Text">' + Entry.Teaser + '</div>'; 
				EntryHTML += '</div>';
			} }
			
			var HalfClass = 'HalfImage';
			if (Hidetext == '1') { HalfClass = '';} 
			if(String(Entry.Image).length > 0) EntryHTML += '<div class="Image' + ((String(Entry.Title).length > 0 || String(Entry.Teaser).length > 0) ? ' ' + HalfClass : '') + '"><img src="' + Entry.Image + '" class="Image" /></div><div class="Clear"></div>';
			
			LinkType = '';
			if(Entry.Link.length > 0 && Entry.Link.indexOf(BasePath) == -1) { 
				LinkType = Entry.Link.substr(7, Entry.Link.length);
				LinkType = LinkType.substr(0, LinkType.indexOf('/'));
				LinkType = str_replace('.feedsportal.com', '', LinkType);
			}
			
			if (LinkType.length > 0) {
				EntryHTML += '<div class="Meta"><div class="Left"><img src="images/rss-stream-' + Entry.Category.toLowerCase() + '.png" align="absmiddle" />&nbsp;</div><div class="Left"';
				if (Entry.Type == 'Small')
				{
					 EntryHTML += 'style="width:108px; overflow:hidden;"';
				}
				EntryHTML += '>' + LinkType + '</div><div class="Clear"></div></div></div>';
			} else if(Entry.Category != 'Grey') {
				EntryHTML += '<div class="Meta"><div class="Left">' + Entry.Views + ' Aufrufe</div><div class="Right">' + (Entry.Comments > 0 ? Entry.Comments + ' Kommentare' : '') + '</div><div class="Clear"></div></div></div>';
			} else {
				EntryHTML += '<div class="Meta"><div class="Left">Kommen Sie später zurück!</div><div class="Clear"></div></div></div>';
			}
			
			HTMLMonth.push(EntryHTML);
		}
				
		HTML.push(HTMLMonth);
	}
	
	HTMLString = "";
	HTMLMonths = "";
	
	for(M=0; M<Data.Months.length; M++){
		HTMLMonths += '<div class="Month' + (M+1 == Data.Months.length ? ' Last' : '') + '">' + Data.Months[M] + '</div>';
				
		HTMLString += '<div class="Month"' + (M == 0 ? ' style="background:none;"' : '') + '><h1 class="Month" id="Month-' + Data.Dates[M] + '">' + Data.Months[M].split(' ').join('<br />') + '</h1>';
		for(B=0; B<HTML[M].length; B++) HTMLString += HTML[M][B];
		HTMLString += '<div class="Clear"></div></div>';
	}
	
	HTMLString += '<div class="Clear"></div>';
	HTMLMonths += '<div class="Clear"></div>';
	
	$('#StreamContent').html(HTMLString);
	$('#ScrollMonths').html(HTMLMonths);
	
	PlaceStream(false);
}

function str_replace(search, replace, subject) {
	return subject.split(search).join(replace);
}

function PlaceStream(FilterPosition) {
	Size = 0;
	MonthSizes = [];
	
	$('#StreamContent').children('.Month').each(function() {
				
		Total = 0;
		TotalHeight = 1;
		
		Position = {x:0, xOversize:false, y:1};
		Storage = [];

		$(this).children('.Box').each(function() {
			
			ID = $(this).attr('id');
			ID = ID.split('-');
			
			Entry = HTMLData['Month-' + ID[0]]['Entry-' + ID[1]];
			
			BoxHeight = 1;
			BoxWidth = 1;
			
			if(Entry.Type == "High") BoxHeight = 2;
			else if(Entry.Type == "Wide") BoxWidth = 2;
			else if(Entry.Type == "Large") { 
				BoxWidth = 2;
				BoxHeight = 2;
			}
						
			if(!FilterStream(Entry.Category)) PlaceStreamBox(ID.join('-'), 0, 0 - TotalHeight - 5);
			else {
				
				if(BoxHeight > 1 && Position.y == 2){
					Storage.push({x:Position.x, y:Position.y, id:Total});
					if(BoxWidth == 2) Storage.push({x:Position.x+1, y:Position.y, id:Total});
					
					Position.x += 1;
					Position.y = 0;
				}
				
				if(BoxHeight == 1 && BoxWidth == 1 && Storage.length > 0){
					StoragePosition = Storage.shift();
					
					while((Total - StoragePosition.id) > 3 && Storage.length > 0) StoragePosition = Storage.shift();
					
					if((Total - StoragePosition.id) <= 3){
						KeepPosition = Position;
						
						Position = StoragePosition;
						Position.reset = true;
					}
				}
		
				PlaceStreamBox(ID.join('-'), Position.x, Position.y - TotalHeight);
				
				if(BoxWidth > 1){
					if(Position.y == 0){
						if(BoxHeight == 1) Storage.push({x:Position.x, y:1, id:Total}, {x:Position.x, y:2, id:Total}, {x:Position.x+1, y:1, id:Total}, {x:Position.x+1, y:2, id:Total});
						else Storage.push({x:Position.x, y:2, id:Total}, {x:Position.x+1, y:2, id:Total});
					} else if(Position.y == 1){
						if(BoxHeight == 1) Storage.push({x:Position.x, y:2, id:Total}, {x:Position.x+1, y:2, id:Total}, {x:Position.x+1, y:0, id:Total});
						else Storage.push({x:Position.x+1, y:0, id:Total});
					} else if(Position.y == 2){
						Storage.push({x:Position.x+1, y:0, id:Total}, {x:Position.x+1, y:1, id:Total});
					}
					
					Position.x += 2;
					Position.y = 0;
					
				} else if(typeof(Position.reset) != 'undefined' && Position.reset == true) Position = KeepPosition;
				else {				
					if(Position.y + BoxHeight > 2) {
						Position.x++;
						Position.y = 0;
					} else Position.y += BoxHeight;
				}
			}
			
			Total++;
			TotalHeight += BoxHeight;
			
		});	
		
		if(Position.y == 0) Position.x -= 1;
		
		MonthSize = ((Position.xOversize ? 2 : 1) + Position.x)*160;
		MonthSizes.push(MonthSize);
		
		$(this).css('width', MonthSize + 'px');
				
		Size += MonthSize+50;
	});
			
	$('#StreamContent').css('left', '0px');
	$('#StreamContent').css('width', Size + 'px');
		
	PlaceHelp();									  
	SizeStream();
	SizeMonths();
	
	if (FilterPosition == true) {
		Offset = $('#Month-2010-10').position().left - $(window).width()/2;
		$('#StreamContent').css('left', (0-Offset) + 'px');

		Position = $('#StreamContent').position();
		Target = Math.min(Position.left, 0);
		Target = Math.max(($('#StreamContent').width() - $(window).width())*-1, Target);
		
		Percent = Math.abs(Target)/Math.abs($('#StreamContent').width() - $(window).width());
		
		$('#ScrollThumb').css('left', parseInt(($(window).width()-32 - $('#ScrollThumb').width())*Percent) + 'px');		
	}
	else if((SetToToday && TodayBox != null)) { 
		Offset = $('#' + TodayBox).position().left - $(window).width()/2 + ($('#' + TodayBox).width() / 2);
		$('#StreamContent').css('left', (0-Offset) + 'px');
		
		Position = $('#StreamContent').position();
		Target = Math.min(Position.left, 0);
		Target = Math.max(($('#StreamContent').width() - $(window).width())*-1, Target);
		
		Percent = Math.abs(Target)/Math.abs($('#StreamContent').width() - $(window).width());
		
		$('#ScrollThumb').css('left', parseInt(($(window).width()-32 - $('#ScrollThumb').width())*Percent) + 'px');	
	} else {
		$('#ScrollThumb').css('left', '33px');
	}
	
	SetToToday = false;
	TodayBox = null;
	
	Cufon.refresh('h1.Month');
	
	Loader(false);
}

function PlaceStreamBox(ID, X, Y){
	$('#' + ID).css('left', X*160 + 'px');
	$('#' + ID).css('top', Y*185 + 'px');
}

function FilterStream(Filter){
	if(Filter == 'Grey') return true;
	
	Result = false;
	
	$('#Categories LI').each(function(){
		Status = String($(this).find('IMG').attr('src'));
		if($(this).hasClass(Filter) && Status.indexOf('-active') != -1) Result = true;
	});
	return Result;
}

function Filter(){
	Status = String($(this).find('IMG').attr('src'));
		
	AllActive = true;
	$('UL#Categories li').each(function(){ if($(this).find('IMG').attr('src') == 'images/legend-inactive.png') AllActive = false; });
	
	if(AllActive) {
		$('UL#Categories li').each(function(){ $(this).find('IMG').attr('src', 'images/legend-inactive.png'); });
		$(this).find('IMG').attr('src', 'images/legend-active.png');
	} else {
	
		if(Status.indexOf('-active') != -1) Status = 'Active';
		else Status = 'Inactive';
	
		switch(Status){
			case 'Active':
				$(this).find('IMG').attr('src', 'images/legend-inactive.png');
			break;
			case 'Inactive':
				$(this).find('IMG').attr('src', 'images/legend-active.png');
			break;
		}
	}
	
	PlaceStream(true);
}

function Loader(Visible){
	if(Visible) $('#Loader').fadeIn();
	else $('#Loader').fadeOut();
}

function HideEnd(){
	$('#End').fadeOut();
}




/* Share */
function OverStart(Trigger, Link, Category){
	if(Link.length == 0 || $('#' + Trigger.id + 'Share').size() > 0) return;	
	$('#' + Trigger.id).bind('mouseleave', {Trigger: Trigger}, OverEnd);	
	clearTimeout(ShareTimer);	
	ShareTimer = setTimeout(function(){ OverStartShow(Trigger, Link, Category); }, 200);
}

function OverStartShow(Trigger, Link, Category){
	$('#' + Trigger.id).prepend('<div class="Share ' + Category + '" id="' + Trigger.id + 'Share"><img src="images/stream-share.png" align="absmiddle" /><h4>DRAG TO SHARE</h4><div class="Clear"></div></div>');
	$('#' + Trigger.id + 'Share').css('width', ($('#' + Trigger.id).width() + 2) + 'px');
}

function OverEnd(event){
	clearTimeout(ShareTimer);
	
	$('#' + event.data.Trigger.id + 'Share').remove();
}

function ClickStart(Trigger, Link, Type){
	if(Link.length == 0) return;	
	ClickDate = new Date();	
	clearTimeout(ClickTimer);	
	ClickTimer = setTimeout(function(){ ClickShare(Trigger, Link, Type); }, 200);	
	$('#' + Trigger.id).bind('mouseup', {Link: Link, Time: ClickDate.getTime()}, ClickEnd);
}


function ClickEnd(event){
	ClickDate = new Date();
	
	clearTimeout(ClickTimer);
	
	if((ClickDate.getTime() - event.data.Time) < 200 && MouseDifference < 10) {
		Link = String(event.data.Link);
		if(Link.indexOf('http://') != -1 && Link.indexOf(BasePath) == -1) window.open(Link);
		else location.href = event.data.Link;
	}
}

function ClickShare(Trigger, Link, Category){
	if(MouseDifference < 10) {
		MouseDifference = 0;
		
		ShareURL = Link;
				
		$(document).unbind('mousemove', Scrub);
		$(document).unbind('mouseup');
		
		$('#' + Trigger.id + 'Share').remove();
		
		if(Category == 'Blue') $('#CalendarShareIcon').removeClass('Hide');
		else $('#CalendarShareIcon').addClass('Hide');
		
		$('#ShareIcons').css({width:($('#ShareIcons').children('img:not(.Hide)').size()*76) + 'px'});
			
		$('#Share').fadeIn();
		$('#ShareAble').append('<div id="ShareBox" class="' + Trigger.className + '"><div class="Share ' + Category + '" id="' + Trigger.id + 'Share-Hover"><img src="images/stream-share.png" align="absmiddle" /><h4>DRAG TO SHARE</h4><div class="Clear"></div></div>' + $('#' + Trigger.id).html() + '</div>');
		$('#ShareAble').bind('mousemove', ClickShareMove);
		
		$('#' + Trigger.id + 'Share-Hover').css('width', ($('#ShareBox').width() + 2) + 'px');
		
		$('#ShareBox').hide();
		
		$(document).bind('mouseup', ClickShareEnd);
	}
}

function ClickShareMove(event){
	$('#ShareBox').show();
	
	Y = event.pageY - 195;
	
	$('#ShareBox').css('left', (event.pageX - 75) + 'px');
	$('#ShareBox').css('top', Y + 'px');
}

function ClickShareEnd(event){
	if(event.target.src) {
		Status = String(event.target.src);
		Status = Status.substr(Status.indexOf('share-') + 6, Status.length);
		Status = Status.substr(0, Status.indexOf('.'));
		
		ShareTitle = $('#ShareBox').find('H1').text();
		ShareDate = $('#ShareBox').find('SPAN.Date').text();
		
		switch(Status.toUpperCase()){
			case "FACEBOOK":
				window.open("http://www.facebook.com/sharer.php?u=" + escape(ShareURL) + "&t=" + escape(ShareTitle));
			break;
			case "TWITTER":
				window.open("http://twitter.com/home?status=" + escape(ShareURL));
			break;
			case "GOOGLE":
				window.open("http://www.google.com/reader/link?url=" + escape(ShareURL) + "&title=" + escape(ShareTitle) + "&srcTitle=" + escape("Fachgruppe für Werbung und Marktkommunikation") + "&srcUrl=" + escape(location.href));
			break;
			case "ICAL":
				document.location.href = BasePath + "backend/action.php?Action=ICal&Title=" + escape(ShareTitle) + "&URL=" + escape(ShareURL) + "&Date=" + escape(ShareDate);
			break;
			case "MAIL":
				document.location.href = "mailto:?subject=Betreff&body=" + escape(ShareURL);
			break;
		}
		
		ShareURL = "";
	}
	
	$('#Share').fadeOut();
	$('#ShareBox').remove();
	
	$(document).unbind('mousemove');
	$(document).unbind('mouseup');
	
	$(document).bind('mousemove', Arrows);
}

function ScrubStart(event){	
	MousePosition.x = event.pageX;
	MouseDifference = 0;
	
	$('#StreamContent').stop();
	$('#ScrollThumb').stop();
		
	$(document).bind('mousemove', Scrub);
	$(document).bind('mouseup', ScrubEnd)
};

function ScrubEnd(event){
	clearTimeout(ClickTimer);
	
	MouseDifference = 0;
	
	$(document).unbind('mousemove', Scrub);
	$(document).unbind('mouseup')
}

function Scrub(event){
	Difference = event.pageX - MousePosition.x;
	MouseDifference += Math.abs(Difference);
	
	Position = $('#StreamContent').position();
	Target = Math.min(Position.left + Difference, 0);
	Target = Math.max(($('#StreamContent').width() - $(window).width())*-1, Target);
	
	Percent = Math.abs(Target)/Math.abs($('#StreamContent').width() - $(window).width());
	
	$('#ScrollThumb').css('left', parseInt(33 + ($(window).width()-66 - $('#ScrollThumb').width())*Percent) + 'px');
	$('#StreamContent').css('left', Target + 'px');
				
	MousePosition.x = event.pageX;
}
