function Stream() {

	this.photos = [];
	this.newPhotos = [];
	this.container = false;
	this.id = false;
	this.isInitialized = false;
	this.expanded = false;
	this.page = 1;
	this.maxpage = 1;
	this.data = {
		'stream_id' : false,
		'stream_type' : false,
		'name' : "",
		'description' : "",
		'photos' : []
	};

	this.template = new Template(
			'<h3><a class="tag clickable tag_meta" href="' + '/streams/show/${data.stream_id}">${data.name}</a></h3> \
			<div class="streamcontent" id="${data.stream_id}">\
			<div class="photos_wrapper">\
				<ul class="photos clearfix"></ul>\
			</div>\
		</div>');

	this.initialize = function(data) {
		this.data = data;
		if (data && data.photos) {
			if (data.photos.length == 0) {
				this.tpl.unbind('update');
			}
			this.addPhotos(data.photos);
		}
	};

	this.addPhotos = function(photos) {
		for (index in photos) {
			this.addPhoto(photos[index]);
		}
	};

	this.addPhoto = function(photo) {
		for (index in this.photos) {
			if (this.photos[index] === photo.photo_id) {
				return;
			}
		}
		photo.stream_id = this.data.stream_id;
		var $photo = new Photo(photo);
		this.photos.push($photo.photo_id);
		this.newPhotos.push($photo);
	};

	this.toString = function() {
		this.template.set('data', this.data);
		var $output = jQuery(this.template.render());
		for (index in this.newPhotos) {
			// add spacer???
			if (Math.floor(Math.random() * 100) < 20) {
				var space = Math.floor(Math.random() * 200) + 50;
				jQuery('.photos', $output).prepend(
						'<li class="photo" style="height:' + space
								+ 'px"></li>');
			}
			var $photo = jQuery(this.newPhotos[index].toString());
			eyeem.initTooltips($photo);
			jQuery('.photos', $output).prepend($photo);
		}
		this.newPhotos = [];
		if (this.expanded) {
			jQuery('.photo', $output).css( {
				'width' : '100px'
			});
		}
		return $output;
	};

	this.setContainer = function($elm) {
		this.container = $elm;
	}

	this.setId = function($id) {
		this.id = $id;
	}

	this.update = function() {
		var container = jQuery(this);
		var tpl = jQuery(this).data('tpl');
		if (jQuery('.streamcontent', container).data('updating')) {
			return;
		}
		jQuery('.streamcontent', container).data('updating', true);
		jQuery
				.getJSON(
						($dev ? '/frontend_dev.php' : '') + '/api/streaminfo/'
								+ tpl.id + '/' + tpl.page,
						function(data) {
							if (data.result == "success") {
								tpl.initialize(data.data);
								if (!tpl.isInitialized) {
									tpl.container.empty()
											.append(tpl.toString());
									var start = Math
											.floor(Math.random() * 5000);
									var $elm = jQuery('.streamcontent',
											container);
									if ($elm.height() < jQuery(
											'.streamcontainer').height()) {
										$elm.css('bottom', 0);
									} else {
										$elm.css('bottom', $elm.height()
												- jQuery('.streamcontainer')
														.height());
									}
									tpl.container.oneTime(start, function() {
										tpl.initScrolling();
									});
									tpl.isInitialized = true;
								} else {
									if (tpl.newPhotos.length == 0) {
										container.unbind('update');
									}
									for (index in tpl.newPhotos) {
										var newPhoto = jQuery(tpl.newPhotos[index]
												.toString());
										jQuery('.photos', tpl.container)
												.prepend(newPhoto);
										eyeem.initTooltips(newPhoto);
										var $elm = jQuery('.streamcontent',
												container);
										var bottom = $elm.data('bottom')
												+ newPhoto.outerHeight(true);
										$elm.data('bottom', bottom);
										$elm.css('bottom', bottom);
										// add spacer???
										if (Math.floor(Math.random() * 100) < 20) {
											var space = Math.floor(Math
													.random() * 200) + 50;
/*
											if(Math.floor(Math.random() * 100) < 30){
												var space = 110;
												var mathfloor = Math.floor(Math.random()*100);
												if(mathfloor<20){	
													var $photo = jQuery('<li class="photo" style="height:'
													+ space + 'px"><a href="http://blog.eyeem.com/?p=2386" target="_blank"><img src="/images/eyeembeta.png" /></a></li>');
												}
												else if(mathfloor<50){
													var $photo = jQuery('<li class="photo" style="height:'
													+ space + 'px"><a href="http://blog.eyeem.com/?p=3243" target="_blank"><img src="/images/ilny.png" /></a></li>');
												}
												else{
													var $photo = jQuery('<li class="photo" style="height:'
													+ space + 'px"><a href="http://blog.eyeem.com/?p=3459" target="_blank"><img src="/images/book.png" /></a></li>');
												}																								
											}
											else{
*/
												var $photo = jQuery('<li class="photo" style="height:'
													+ space + 'px"></li>');	
/* 											} */

											jQuery('.photos', tpl.container)
													.prepend($photo);
											var bottom = $elm.data('bottom')
													+ $photo.outerHeight(true);
											$elm.data('bottom', bottom);
											$elm.css('bottom', bottom);
										}

									}
									tpl.newPhotos = [];
								}
								jQuery('.streamcontent', container).data(
										'updating', false);
							} else {
								jQuery('.streamcontent', container).data(
										'updating', false);
							}
						});
	}

	this.expand = function() {
		var $container = this.container;
		$container.css('zIndex', 3);
		jQuery('p', $container).show('blind', {}, 500);
		jQuery('.photo', $container).animate( {
			'width' : '100px'
		}, 1000);
		$container.addClass('stream_expanded', 1000);
	}

	this.shrink = function() {
		var $container = this.container;
		jQuery('.photo', $container).animate( {
			'width' : '210px'
		}, 1000);
		jQuery('p', $container).hide('blind', {}, 500);
		$container.removeClass('stream_expanded', 1000, function() {
			$container.css('zIndex', 2);
		});
	}

	this.setExpanded = function(isExpanded) {
		this.expanded = isExpanded;
	}

	this.initScrolling = function() {
		var $elm = jQuery('.streamcontent', this.container);
		$elm.hover(function() {
			jQuery(this).data('stop', true);
		}, function() {
			jQuery(this).data('stop', false);
		});
		$elm.data('tpl', this);
		var bottom = $elm.height() - jQuery('.streamcontainer').height();
		if ($elm.height() < jQuery('.streamcontainer').height()) {
			bottom = 0;
		}
		$elm.data('bottom', bottom);
		$elm.css('bottom', bottom);
		$elm.data('interval', 10);
		$elm.data('direction', -1);
		$elm.everyTime(30, this.doScroll);
	}

	this.doScroll = function() {
		var $elm = jQuery(this);
		var tpl = $elm.data('tpl');
		// im unteren scrollbereich, nachladen - false verfügbar
		var height = $elm.height();
		var $sHeight = jQuery('.streamcontainer').height();
		var $bHeight = jQuery('body').height();
		var containerheight = ($bHeight > $sHeight) ? $bHeight : $sHeight;
		var bottom = $elm.data('bottom');
		var direction = $elm.data('direction');

		if (tpl.photos.length < 4) {
			return;
		}
		if (bottom <= 200 && !$elm.data('updating')) {
			tpl.page = ++tpl.maxpage;
			tpl.container.trigger('update');
			tpl.page = 1;
		}
		// don't scroll
		if ($elm.data('stop') === true || $elm.data('fullstop') === true
				|| (height < containerheight && bottom >= 0)) {
			return;
		}
		if (bottom <= -height) {
			bottom = $elm.height();
		}
		_bottom = bottom + direction;
		jQuery(this).css('bottom', _bottom + 'px');
		$elm.data('bottom', _bottom);
		$elm.data('direction', direction);
	}

	this.initialize.apply(this, arguments);
}

