thumbMargin = new Array(0, 5, 10, 15, 20);

function slides() {

	this.getSlides = function() {
		elArray = $('#slideshow div.slide');
		return elArray;
	}

	this._prev = function() {
		$(this.getSlides()[this.position-1]).fadeOut('slow');
		if (this.position <= 1) {
			this.position = this.amount;
		} else {
			this.position--;
		}
		$(this.getSlides()[this.position-1]).fadeIn('fast');
	}

	this._next = function() {
		$(this.getSlides()[this.position-1]).fadeOut('slow');
		if (this.position >= this.amount) {
			this.position = 1;
		} else {
			this.position++;
		}
		$(this.getSlides()[this.position-1]).fadeIn('fast');
	}

	this.next = function() {
		this.autoplay = false;
		this._next();
	}

	this.prev = function() {
		this.autoplay = false;
		this._prev();
	}

	/* constructor */
	this.amount = this.getSlides().length;
	this.autoplay = true;

	if (this.amount > 0) {
		this.position = 1;
		$(this.getSlides()[this.position-1]).show();
	} else {
		this.position = 0;
	}

}

function addNavigation() {
	var navPrev = $('<a></a>').addClass('prev').click(function() { mySlide.prev() });
	var navNext = $('<a></a>').addClass('next').click(function() { mySlide.next() });
	$('#slideshow').append(navPrev).append(navNext);
}

function doTimer() {
	if (mySlide.autoplay)
		mySlide._next();
}

function GetRandom( min, max ) {
	if( min > max ) {
		return( -1 );
	}
	if( min == max ) {
		return( min );
	}
	return( min + parseInt( Math.random() * ( max-min+1 ) ) );
}









function EmbedStartFilm() {
	var so = new SWFObject('/files/start-player.swf','','100%','100%','8');
	so.addParam('allowscriptaccess','always');
	so.addParam('allowfullscreen','true');
	so.addParam('scale','noborder');
	so.addVariable('salign','LT');
	so.addVariable('bgcolor','#000000');
	so.addVariable('loop','true');
	so.addParam('flashvars','&amp;film=startfilm-' + GetRandom(151,151) + '.flv&amp;link=/programm');
	so.write('hintergrund_film');
}












function EmbedLogoFilm() {
	var so4 = new SWFObject('/files/logo-player.swf','','400','100','8');
	so4.addParam('allowscriptaccess','always');
	so4.addParam('allowfullscreen','false');
	so4.addParam('scale','noscale');
	so4.addVariable('salign','LT');
	so4.addVariable('bgcolor','#000000');
	so4.addVariable('loop','true');
	so4.addParam('flashvars','&amp;film=logofilm-' + GetRandom(1,3) + '.flv&amp;link=/');
	so4.write('logo_frei');
}

function EmbedErrorFilm() {
	var so3 = new SWFObject('/files/error-film-' + GetRandom(1,3) + '.swf','','0','0','8');
	so3.addParam('allowscriptaccess','always');
	so3.addParam('allowfullscreen','false');
	so3.addParam('scale','noscale');
	so3.addVariable('salign','LT');
	so3.addVariable('bgcolor','#ffffff');
	so3.addVariable('loop','true');
	so3.write('error-film');
}


$(document).ready(function() {
	
	$('div.programm_text div.article_thumbnail').each(function() {
		$(this).css("margin-right", thumbMargin[GetRandom(0,4)]+"px");
	});
	
	if ($('div#slideshow').length > 0) {
		mySlide = new slides();
		if (mySlide.amount > 1) {
			addNavigation();
			timer = window.setInterval(doTimer, 8000);
			return true;
		} else {
			return false;
		}
	}
	
	if ($('body#default').length > 0) {
		if ($.browser.mac()) {
			$('div#hintergrund_film').css({
				'top' : '0px',
				'left' : '0px',
				'height' : '100%',
				'width' : '100%'
			});
			$('p.datum_quick').css('color','#eee');
		}
		EmbedStartFilm();
	}
	
	if ($('body#programm').length > 0) {
		if ( GetRandom(1,4) < 4 ) {
			EmbedLogoFilm();
		}
	}
	
	if ($('body#error').length > 0) {
		EmbedErrorFilm();
	}
	
});

$(window).unload(function() {
	clearInterval(timer);
});