
function VideoPlayer(id, playlist_id, configuation)
{
	this.id = id;
	
	if(!VideoPlayer.instances[id])
	{
		if(this.getType() != "single")
		{
			var playlist = false;
			
			// get playlist from url
			if(configuation.noUrls == false)
				 playlist = this.getPlaylistFromURL();

			// if we dont have a playlist get it from data in html
			if(!playlist)
				playlist = $('#'+this.id).attr('data-playlist');
			
			// if we still have no playlist get it from the default playlist
			if(!playlist)
				playlist = playlist_id;

			this.playlistId = playlist;
		}
		
		if(typeof(configuation) == "object")
		{
			if(typeof(configuation.animationSpeed) != "undefined")
				this.animationSpeed = configuation.animationSpeed;
				
			if(typeof(configuation.playlistElementWidth) != "undefined")
				this.playlistElementWidth = configuation.playlistElementWidth;
				
			if(typeof(configuation.playlistPosition) != "undefined")
			{	
				this.playlistPosition = configuation.playlistPosition;
				
				if(this.playlistPosition == "after")
					this.hidePlaylistOnPlay = false;
			}

			if(typeof(configuation.showInfoOnHover) != "undefined")
				this.showInfoOnHover = configuation.showInfoOnHover;
				
			if(typeof(configuation.basePath) != "undefined")
				this.basePath = configuation.basePath;
				
			if(typeof(configuation.googleAnalytics) != "undefined")
				this.googleAnalytics = configuation.googleAnalytics;

		   	if(typeof(configuation.debug) != "undefined")
		   		this.debug = configuation.debug;
		
			if(typeof(configuation.noUrls) != "undefined")
		   		this.noUrls = configuation.noUrls;
		
			if(typeof(configuation.hideScrubberWhilePlaying) != "undefined")
			{
				if(!this.isAppleTouchDevice() && this.getContentType() != "audio")
		   			this.hideScrubberWhilePlaying = configuation.hideScrubberWhilePlaying;
			}
		}

		VideoPlayer.instances[id] = this;

		this.init();
	}
}

VideoPlayer.instances = [];

VideoPlayer.handleFlashEvent = function(player_id, my_event)
{
	var player = VideoPlayer.instances[player_id];
	player.log("Got event "+my_event+" for player "+player_id+".");
	
	if(my_event == "loadstart")
	{
		player.videoLoading();
	}
	else if(my_event == "play")
	{
		player.videoPlay();
	}
	else if(my_event == "pause")
	{
		player.videoPause();
	}
	else if(my_event == "playing")
	{
		player.videoPlaying();
	}
	else if(my_event == "ended")
	{
		player.videoEnd();
	}
	else if(my_event == "canplaythrough")
	{
		player.videoCanPlayThrough();
	}
	
	return true;
}

VideoPlayer.prototype.startScreenVisible = false;
VideoPlayer.prototype.basePath = "";
VideoPlayer.prototype.width = 925;
VideoPlayer.prototype.height = 520;
VideoPlayer.prototype.oldWidth = 925;
VideoPlayer.prototype.oldHeight = 520;
VideoPlayer.prototype.contentType = false;
VideoPlayer.prototype.originalDocumentTitle = document.title;
VideoPlayer.prototype.videoInformationTimeout = false;
VideoPlayer.prototype.navigationTimer = false;
VideoPlayer.prototype.scrubberTimer = false;
VideoPlayer.prototype.html5 = null;
VideoPlayer.prototype.showInfoOnHover = false;
VideoPlayer.prototype.hideScrubberWhilePlaying = false;
VideoPlayer.prototype.playlistInformation = false;
VideoPlayer.prototype.playlists = false;
VideoPlayer.prototype.currentVideo = false;
VideoPlayer.prototype.animationSpeed = 500;
VideoPlayer.prototype.videoLoaded = false;
VideoPlayer.prototype.playlistElementWidth = 192;
VideoPlayer.prototype.locationHash = false;
VideoPlayer.prototype.playlistListVisible = false;
VideoPlayer.prototype.playerGenerated = false;
VideoPlayer.prototype.reloadingPlayer = false;
VideoPlayer.prototype.debug = false;
VideoPlayer.prototype.clickEvent = 'click';
VideoPlayer.prototype.flashPlayer = false;
VideoPlayer.prototype.playerType = false;
VideoPlayer.prototype.preventPauseAction = false;
VideoPlayer.prototype.flashAccessCounter = 0;
VideoPlayer.prototype.flashNoticeVisible = false;
VideoPlayer.prototype.googleAnalytics = false;
VideoPlayer.prototype.playsFullScreen = false;
VideoPlayer.prototype.isFullWindow = false;
VideoPlayer.prototype.playlistPosition = "inside";
VideoPlayer.prototype.hidePlaylistOnPlay = true;
VideoPlayer.prototype.playlistSlideshowPosition = false;
VideoPlayer.prototype.playlistSlideshowTimeout = false;
VideoPlayer.prototype.playlistSlideshowDuration = 5000;
VideoPlayer.prototype.callbacks = {};

VideoPlayer.prototype.init = function()
{
	// get size from container
	this.width = $('#'+this.id).width();
	this.height = $('#'+this.id).height();
	
	// load playlists
	this.getPlaylists();
	this.getPlaylistInformation();

	// set click event to touchend for apple touch devices
	if(this.isAppleTouchDevice())
	{
		this.clickEvent = 'touchend';
	}
}

VideoPlayer.prototype.bindEvent = function(event_name, methode)
{
	if(typeof(methode) == "function")
	{
		this.callbacks[event_name] = methode;
	}
}

VideoPlayer.prototype.callEvent = function(event_name)
{
	if(typeof(this.callbacks[event_name]) == "function")
	{
		this.callbacks[event_name](this);
	}
}

VideoPlayer.prototype.getPlaylists = function()
{
	if(this.getType() == "full")
	{
		var player = this;

		$.getJSON(this.basePath+'/videos/get_playlists/', function(data)
		{
			player.gotPlaylists(data);
		});
	}
	else
	{
		this.gotPlaylists([]);
	}
}

VideoPlayer.prototype.gotPlaylists = function(data)
{
	this.playlists = data;
	if(this.playerGenerated == false)
	{
		this.generatePlayer();
	}	
}

VideoPlayer.prototype.getPlaylistInformation = function()
{
	if(this.getType() != "single")
	{
		var player = this;
	
		if(this.playlistId)
		{
			$.getJSON(this.basePath+'/videos/get_info/'+this.playlistId+"/", function(data)
			{
				player.gotPlaylistInformation(data);
			});
		}
		else
		{
			this.hideLoading();
		}
	}
	else
	{
		// build dummy data
		var data = {
			barContent: false,
			barPosition: "hidden",
			endAction: "start",
			id: "video",
			name: "",
			poster: $('#'+this.id+"_default_poster").get(0).src,
			teaser: [],
			autoPlay: false,
			videos: [
				{
					description: "",
					id: "standalone_player",
					poster: $('#'+this.id+"_default_poster").get(0).src,
					posterSmall: $('#'+this.id+"_default_poster").get(0).src,
					showTitle: false,
					teaser: [],
					title: "",
					video: $('#'+this.id+"_default_video").get(0).href
				}
			
			]
		};
	
		this.gotPlaylistInformation(data);
	}
}

VideoPlayer.prototype.gotPlaylistInformation = function(data)
{
	this.playlistInformation = data;
	
	this.trackEvent("playlist", this.playlistInformation.name);
	
	if(this.playerGenerated == false)
	{	
		this.generatePlayer();
	}
	else
	{
		this.updatePlayer();
	}
}

VideoPlayer.prototype.updatePlayer = function()
{
	var player = this;
	this.log("updatePlayer");
	
	this.reloadingPlayer = true;
	this.pause();
	this.hideVideo();
	
	this.hideNavigation();
	this.hideTeasers();
	this.hideVideoInformation();
	this.hidePlaylist(true);
	this.showReLoading();
	this.updatePlaylist();
	
	$('.video_playlists.playlist_'+this.id+' li').removeClass('active');
	$('.video_playlists.playlist_'+this.id+' .playlist_'+this.playlistId).addClass('active');
	
	player.showStartScreen(true);
	
	if(this.playlistInformation.autoPlay)
	{
		// start first video
		this.play();
	}
	
	
	// this.hidePoster(function()
	// {
	// 		player.showPoster();
	// 		
	// });
	player.reloadingPlayer = false;
}

VideoPlayer.prototype.playPauseClicked = function()
{
	this.playPause();
}

VideoPlayer.prototype.videoClicked = function()
{
	this.playPause();
}

VideoPlayer.prototype.scrollLeftClicked = function(e)
{
	if(!$('#'+this.id+"_playlist_scroll_left").hasClass("inactive"))
	{
		var player = this;

		$('#'+this.id+"_playlist").animate({scrollLeft: "-="+this.playlistElementWidth*2 }, this.animationSpeed, "easeInOutQuad", function()
		{
			player.checkPlaylistScrollingStatus();
		});
	}
	
	e.stopPropagation();
}

VideoPlayer.prototype.scrollRightClicked = function(e)
{
	if(!$('#'+this.id+"_playlist_scroll_right").hasClass("inactive"))
	{
		var player = this;
	
		$('#'+this.id+"_playlist").animate({scrollLeft: "+="+this.playlistElementWidth*2 }, this.animationSpeed, "easeInOutQuad", function()
		{
			player.checkPlaylistScrollingStatus();
		});
	}
	
	e.stopPropagation();
}

VideoPlayer.prototype.checkPlaylistScrollingStatus = function()
{
	if($('#'+this.id+"_playlist").scrollLeft() == 0)
	{
		$('#'+this.id+"_playlist_scroll_left").addClass("inactive");
	}
	else
	{
		$('#'+this.id+"_playlist_scroll_left").removeClass("inactive");
	}
	
	if($('#'+this.id+"_playlist ul").width() - $('#'+this.id+"_playlist").width() <= $('#'+this.id+"_playlist").scrollLeft())
	{
		$('#'+this.id+"_playlist_scroll_right").addClass("inactive");
	}
	else
	{
		$('#'+this.id+"_playlist_scroll_right").removeClass("inactive");
	}

}

VideoPlayer.prototype.playlistItemClicked = function(e)
{
	var id = parseInt(e.currentTarget.id.replace(this.id+'_play_video_', ""));
	this.hidePauseScreen();
	this.loadVideo(id);
}

VideoPlayer.prototype.scrubberClicked = function(e)
{
	var element = $('#'+this.id+'_navigation .video_scrubber');
	
	// only update when we have an event
	if(typeof(e) != "undefined")
	{
		var click_pos = (e.pageX-element.offset().left) / element.width();

		this.seekTo(click_pos); 
		this.updatePosition(true);
	}
	
	// remove events
	$(document).unbind('mousemove');
	$(document).unbind('mouseup');
	
	// reenable text selection
	document.onselectstart = function () { return true; };
}

VideoPlayer.prototype.scrubberClickedStart = function(e) 
{
	// disable text-selection
	document.body.focus();
	document.onselectstart = function () { return false; };
	
	var player = this;
	
	$(document).bind('mousemove', function(e)
	{
		player.scrubberMoved(e);
	});
	
	$(document).bind('mouseup', function(e)
	{
		player.scrubberClicked();
	});
}

VideoPlayer.prototype.scrubberMoved = function(e)
{
	var element = $('#'+this.id+'_navigation .video_scrubber');
	var click_pos = e.pageX - element.offset().left;
	
	if(click_pos > element.width())
	{
		// don’t jump to end to prevent loading the next video
		click_pos = .9999;
	}
	else if(click_pos < 0)
	{
		click_pos = 0;
	}
	else
	{
		click_pos = click_pos / element.width();
	}

	this.seekTo(click_pos); 
	this.updatePosition(true);
}

VideoPlayer.prototype.scrubberTouched = function(event)
{
	var e = event.originalEvent;
	e.preventDefault();
	
	var element = $('#'+this.id+'_navigation .video_scrubber');
	
	if(typeof(e.targetTouches[0]) != 'undefined')
		var page_x = e.targetTouches[0].pageX;
	else
		var page_x = e.pageX;
		

	var click_pos = (page_x-element.offset().left) / element.width();
	
	this.seekTo(click_pos); 
	this.updatePosition(true);
}

VideoPlayer.prototype.playerHover = function(event)
{
	if(event.type == "mouseenter")
	{
		if(this.showInfoOnHover)
		{
			this.showVideoInformation(false);
		}
		
		if(this.hideScrubberWhilePlaying)
		{
			this.showNavigation();
		}
	}
	else if(event.type == "mouseleave")
	{
		if(this.showInfoOnHover)
		{
			this.hideVideoInformation();
		}
		
		if(this.hideScrubberWhilePlaying && !this.isPaused())
		{
			this.hideNavigation();
		}
	}
}

VideoPlayer.prototype.startHover = function(mouseout)
{
	if(mouseout)
	{
		$('#'+this.id+"_start").removeClass("hover");
	}
	else
	{
		$('#'+this.id+"_start").addClass("hover");
	}
}

VideoPlayer.prototype.startClicked = function()
{
	if(this.startScreenVisible)
	{
		this.hideStartScreen();
		this.play();
	}
	else
	{
		this.playPause();
	}
}

VideoPlayer.prototype.playlistGroupClicked = function(e)
{
	var playlist_group = e.currentTarget.id.split("_")[3];
	
	var current_playlist_group = this.playlistListVisible;
	var player = this;
	
	$('#'+this.id+'_playlists_groups li').removeClass("active");
	$(e.currentTarget).addClass("active");
	
	
	var show_playlist = function()
	{
		player.playlistListVisible = playlist_group;
		
		var height = $('#'+player.id+"_playlists_"+playlist_group).height();
		$('#'+player.id+"_playlists_"+playlist_group).height(0);
		$('#'+player.id+"_playlists_"+playlist_group).animate({height: height}, player.player, function()
		{
			$('#'+player.id+"_playlists_"+playlist_group).height('auto');
		});
	}

	
	if(current_playlist_group)
	{
		$('#'+this.id+"_playlists_"+current_playlist_group).animate({height: 0}, this.animationSpeed, function()
		{
			$('#'+player.id+"_playlists_"+current_playlist_group).hide();
			$('#'+player.id+"_playlists_"+current_playlist_group).height('auto');
			
			if((player.playlistListVisible != playlist_group)) 
			{	
				show_playlist();
			}
			else
			{	
				player.playlistListVisible = false;
				$('#'+player.id+'_playlists_groups li').removeClass("active");
			}
		});
	}
	else
	{
		show_playlist();
	}
}


VideoPlayer.prototype.playlistClicked = function(e)
{
	var info = e.currentTarget.id.split("_");
	
	var playlistGroup = info[2];
	var playlistId = info[3];
	var player = this;
	
	if(this.playlists[playlistGroup] && this.playlists[playlistGroup].playlists[playlistId])
	{
		this.loadPlaylist(this.playlists[playlistGroup].playlists[playlistId].id);
		
		$('#'+this.id+"_playlists_"+this.playlists[playlistGroup].id).animate({height: 0}, this.animationSpeed, function()
		{
			$('#'+player.id+"_playlists_"+player.playlists[playlistGroup].id).hide();
			$('#'+player.id+"_playlists_"+player.playlists[playlistGroup].id).height('auto');
			player.playlistListVisible = false;
		});
	}
}

VideoPlayer.prototype.loadPlaylist = function(playlist_id, play_video)
{
	this.log("Switching Playlist from "+this.playlistId+" to "+playlist_id+".");
	
	if(playlist_id && this.playlistId != playlist_id)
	{
		this.playlistId = playlist_id;
		this.currentVideo = false;
		this.changeUrl();
		this.getPlaylistInformation();
	}
	else
	{
		if(play_video)
			this.loadVideo(play_video);
		else
			this.playlistEnd(true);
	}
}

VideoPlayer.prototype.getType = function()
{
	if(this.playerType === false)
	{
		if($('#'+this.id).hasClass("single"))
		{
			this.playerType = "single";
		}
		else if($('#'+this.id).hasClass("standalone"))
		{
			this.playerType = "standalone";
		}
		else
		{
			this.playerType = "full";
		}
	}
	
	return this.playerType;
}

VideoPlayer.prototype.getContentType = function()
{
	if(this.contentType === false)
	{
		if($('#'+this.id).attr("data-type") && $('#'+this.id).attr("data-type") == "audio")
		{
			this.contentType = "audio";
		}
		else
		{
			this.contentType = "video";
		}
	}
	
	return this.contentType;
}

VideoPlayer.prototype.generatePlayer = function()
{
	if(this.playlistInformation && this.playlists)
	{
		var player = this;
		
		// init playlist html
		var playlist_html = "";
		var playlist_html_sub = "";
	
		$('#'+this.id).addClass("paused");
		
		if(this.playlists.length)
		{
			playlist_html += '<ul id="'+this.id+'_playlists_groups" class="video_playlist_navi" style="display:none;width:'+this.width+'px">';
		
			for(var i in this.playlists)
			{
				if(this.playlists[i].playlists.length > 0)
				{
					playlist_html += '<li id="'+this.id+'_playlists_group_'+this.playlists[i].id+'">'+this.playlists[i].name+'</li>';

					playlist_html_sub += '<div style="display:none;width:'+this.width+'px"" id="'+this.id+'_playlists_'+this.playlists[i].id+'" class="video_playlists playlist_'+this.id+'"><h4>'+this.playlists[i].title+'</h4><ul>';

					for(var k in this.playlists[i].playlists)
					{
						playlist_html_sub += '<li id="'+this.id+'_playlists_'+i+'_'+k+'" class="playlist_'+this.playlists[i].playlists[k].id+'">'+this.playlists[i].playlists[k].name+'</li>';
					}

					playlist_html_sub += '</ul></div>';
				}
			}
		
			playlist_html += '</ul>';
		}
	
		// add to DOM
		$('#'+this.id).before(playlist_html+playlist_html_sub);
		$('#'+this.id+'_playlists_groups li').click(function(e)
		{
			player.playlistGroupClicked(e); 
			return false;
		});
	
		$('.video_playlists.playlist_'+this.id+" ul li").click(function(e)
		{
			player.playlistClicked(e);
			return false;
		});
	
		$('.video_playlists.playlist_'+this.id+' .playlist_'+this.playlistId).addClass('active');

		// let playlists appear
		var playlist_height = $('#'+this.id+'_playlists_groups').height();
		$('#'+this.id+'_playlists_groups').height(0);
		$('#'+this.id+'_playlists_groups').animate({'height': playlist_height}, this.animationSpeed);
		
		var html = "";
		
		// init player html
		if(this.supportsHTML5())
		{	
			html += '<video id="'+this.id+'_video" style="display:none" poster="/custom/files/videos/poster.jpg"></video>';
			this.log("Using HTML5");
		}
		else
		{
			html += '<div id="'+this.id+'_video" class="flash_video"><div id="'+this.id+'_flash"><img class="video_poster" src="'+this.playlistInformation.poster+'" width="100%" height="100%" /></div></div>';
			this.log("Using Flash");
		}
		
		var audio_title = "";
		var playlist_html = "";
		
		if(this.getContentType() == "video")
		{
			html += '\
				<img id="'+this.id+'_poster" class="video_poster" src="'+this.playlistInformation.poster+'" />\
				\
				<div id="'+this.id+'_bar" style="display:none" class="video_sidebar '+this.playlistInformation.barPosition+'">\
					'+this.playlistInformation.barContent+'\
				</div>\
				\
				<ul id="'+this.id+'_teaser" class="video_teaser" style="display:none"></ul>\
				\
				<div id="'+this.id+'_loading" class="video_loading"></div>\
				\
				<div id="'+this.id+'_start" style="display:none" class="video_start '+this.playlistInformation.barPosition+'"></div>\
				\
				<div id="'+this.id+'_info" class="video_title" style="display:none"><h2 id="'+this.id+'_title">Title</h2><p id="'+this.id+'_desc">desc</p></div>';
				
			playlist_html = '\
				<div id="'+this.id+'_playlist_container" class="video_playlist_container video_playlist_container_'+this.playlistPosition+'" style="display:none;width:'+this.width+'px;">\
					<div id="'+this.id+'_playlist_scroll_left" class="video_playlist_scroll_left"></div>\
					<div id="'+this.id+'_playlist_scroll_right" class="video_playlist_scroll_right"></div>\
					<div id="'+this.id+'_playlist" class="video_playlist"></div>\
				</div>';
				
			if(this.playlistPosition == "inside")
				html += playlist_html;
		}
		else
		{
			audio_title = '<div id="'+this.id+'_audiotitle" class="audio_title">'+$('#'+this.id).attr("data-title")+'</div>';
			
			/*if(this.supportsHTML5())
			{
				audio_title += '<div id="'+this.id+'_video" class="flash_video"><div id="'+this.id+'_flash"></div></div>';
			}*/
		}
		
			
		html += '\
			<div id="'+this.id+'_navigation" class="video_navigation">\
				<div class="play"></div>\
				'+audio_title+'\
				<div id="'+this.id+'_time_current" class="video_time_current"></div>\
				<div class="video_scrubber">\
					<div class="video_played"></div>\
				</div>\
				<div id="'+this.id+'_time_remaining" class="video_time_remaining"></div>\
			</div>\
		';
		
		// insert element
		$('#'+this.id).html(html);
		
		if(this.playlistPosition == "after")
		{
			$('#'+this.id).after(playlist_html);
			this.showPlaylist();
		}
		
		if(this.getContentType() == "audio")
			this.showNavigation(true, true);
		
		// move elements to the right positions
		this.resizePlayerElements();
		
		if(!this.supportsHTML5())
		{
			var flashvars = {
				playerID: this.id,
				contentType: this.getContentType()
			}
			
			var params = {
				allowScriptAccess: "always",
				wmode: "transparent"
			}

			var attributes = {
				
			}
			
			var additional = (this.getContentType() == "audio") ? 1 : 0;
			
			swfobject.embedSWF("/modules/videos/media/player.swf", this.id+'_flash', this.width, this.height+additional, "9.0.98", "/custom/js/expressInstall.swf", flashvars, params, attributes);
		}
		
		this.updatePlaylist();
		this.playerGenerated = true;

		// bind video element events
		$('#'+this.id+"_video").bind('ended', function(e) { player.videoEnd(e)});
		$('#'+this.id+"_video").bind('play', function(e) { player.videoPlay(e)});
		$('#'+this.id+"_video").bind('playing', function(e) { player.videoPlaying(e)});
		$('#'+this.id+"_video").bind('pause', function(e) { player.videoPause(e)});
		$('#'+this.id+"_video").bind('loadstart', function(e) { player.videoLoading(e)});
		$('#'+this.id+"_video").bind('canplaythrough', function(e) { player.videoCanPlayThrough(e)});
		$('#'+this.id+"_video").bind('waiting', function(e) { player.videoWaiting(e)});
		$('#'+this.id+"_video").bind('loadeddata', function(e) { player.videoLoadedData(e)});
		$('#'+this.id+"_video").bind('stalled', function(e) { player.videoStalled(e)});
		$('#'+this.id+"_video").bind('error', function(e) { player.videoError(e)});
		$('#'+this.id+"_video").bind('loadeddata', function(e) { player.videoLoadedData(e)});
		$('#'+this.id+"_video").bind('loadedmetadata', function(e) { player.videoLoadedMetaData(e)});
		
		// bind ui events
		$('#'+this.id+"_start").bind(this.clickEvent, function(e) { player.startClicked(e); return false; });
		$('#'+this.id+"_audiotitle").bind(this.clickEvent, function(e) { player.startClicked(e); return false; });
		
		$('#'+this.id+"_bar").bind(this.clickEvent, function(e) { player.startClicked(e); return false; });
		$('#'+this.id+"_poster").bind(this.clickEvent, function(e) { player.startClicked(e); return false; });
		
		//$('#'+this.id+"_time_remaining").bind(this.clickEvent, function(e) { player.fullWindow(); return false; });
		
		// Special iOS-Events
		if(this.isAppleTouchDevice())
		{
			$('#'+this.id+"_navigation .video_scrubber").bind('touchmove touchstart', function(e) { player.scrubberTouched(e); });
			
			$('#'+this.id).addClass("touchdevice");
			$('#'+this.id+"_playlist_container").addClass("touchdevice");
		}
		else
		{
			$('#'+this.id).bind('mouseenter mouseleave', function(e) { player.playerHover(e)});
			
			$('#'+this.id+"_navigation .video_scrubber").bind('mouseup mousedown', function(e) { player.scrubberClicked(e); });
			
			$('#'+this.id+"_video").bind(this.clickEvent, function(e) { player.videoClicked(e); });
			
			//$('#'+this.id+"_bar").bind('mouseenter', function(e) { player.startHover(); });
			$('#'+this.id+"_poster").bind('mouseenter', function(e) { player.startHover(); });
			//$('#'+this.id+"_bar").bind('mouseleave', function(e) { player.startHover(true); });
			$('#'+this.id+"_poster").bind('mouseleave', function(e) { player.startHover(true); });
		}


		$('#'+this.id+"_navigation .play").bind(this.clickEvent, function(e) { player.playPauseClicked(e); return false; });
		$('#'+this.id+"_playlist_scroll_left").bind(this.clickEvent, function(e) { player.scrollLeftClicked(e); return false; });
		$('#'+this.id+"_playlist_scroll_right").bind(this.clickEvent, function(e) { player.scrollRightClicked(e); return false; });

		// show elements
		if(document.location.hash == "" || document.location.hash == "#")
		{
			this.showStartScreen();
		}

		// check if url has changed
		this.urlChanged();
	}
	else
	{
		this.log("Not Building Player... waiting for data to be loaded.");
	}
}

VideoPlayer.prototype.resizePlayerElements = function()
{
	// container
	$('#'+this.id).width(this.width);
	
	var additional = (this.getContentType() == "audio") ? parseInt($('#'+this.id+"_navigation").innerHeight()) : 0;
		
	$('#'+this.id).height(this.height+additional);
	
	// video
	$('#'+this.id+"_video").width(this.width);
	$('#'+this.id+"_video").height(this.height);
	
	$('#'+this.id+"_flash").width(this.width);
	$('#'+this.id+"_flash").height(this.height);
	
	// poster 
	$('#'+this.id+"_poster").width(this.width);
	$('#'+this.id+"_poster").height(this.height);
	
	// teaser 
	$('#'+this.id+"_teaser").width(this.width);
	$('#'+this.id+"_teaser").height(this.height);
	
	// playlists
	
	if(this.playlistPosition == "inside")
		$('#'+this.id+"_playlist_container").css("margin-top", this.height-120);
		
	$('#'+this.id+"_playlist_scroll_right").css("margin-left", this.width-25);
	$('#'+this.id+"_playlist").css("width", this.width-54);
	
	// navigation
	$('#'+this.id+"_navigation").css("width", this.width);
	$('#'+this.id+"_navigation").css("margin-top", this.height);
	$('#'+this.id+"_navigation .video_scrubber").css("width", this.width-150);
	
	// video information
	var video_info_padding_hor = $('#'+this.id+"_info").outerWidth()-$('#'+this.id+"_info").width();
	var video_info_height = $('#'+this.id+"_info").outerHeight();
	
	$('#'+this.id+"_info").css("width", this.width-video_info_padding_hor);
	$('#'+this.id+"_info").css("margin-top", this.height-video_info_height);
	
	// sidebar
	this.setSidebarSize();
	
	// start button
	if($('#'+this.id+"_start.left").length || $('#'+this.id+"_start.bottom").length)
	{
		var start_button_width = $('#'+this.id+"_start").outerWidth();
		var start_button_height = $('#'+this.id+"_start").outerHeight();
		
		$('#'+this.id+"_start").css("margin-left", this.width - start_button_width - 20);
		$('#'+this.id+"_start").css("margin-top", this.height - start_button_height - 20);
	}
	else
	{
		this.moveElementToCenter('#'+this.id+"_start");
	}
	
	// loading bar
	this.moveElementToCenter('#'+this.id+"_loading");
}

VideoPlayer.prototype.setSidebarSize = function()
{
	// reset properties
	$('#'+this.id+"_bar").css("width", "");
	$('#'+this.id+"_bar").css("height", "");
	$('#'+this.id+"_bar").css("margin", "");
	
	// get padding values
	var sidebar_padding_hor = $('#'+this.id+"_bar").outerHeight()-$('#'+this.id+"_bar").height();
	var sidebar_padding_vert = $('#'+this.id+"_bar").outerWidth()-$('#'+this.id+"_bar").width();
	
	if($('#'+this.id+"_bar.left").length)
	{
		$('#'+this.id+"_bar").height(this.height - sidebar_padding_hor);
	}
	else if($('#'+this.id+"_bar.bottom").length)
	{
		$('#'+this.id+"_bar").width(this.width-sidebar_padding_vert);
		$('#'+this.id+"_bar").css("margin", (this.height - $('#'+this.id+"_bar").outerHeight())+"px 0 0 0");
	}
}

VideoPlayer.prototype.moveElementToCenter = function(element)
{
	var element_width = $(element).outerWidth();
	var element_height = $(element).outerHeight();
	
	$(element).css("margin-left", this.width / 2 - element_width / 2);
	$(element).css("margin-top", this.height / 2 - element_height / 2);
}

VideoPlayer.prototype.enterFullScreen = function()
{
	this.getVideo().webkitEnterFullscreen();
	this.playsFullScreen = true;
}

VideoPlayer.prototype.exitFullScreen = function()
{
	if(this.playsFullScreen == true)
		this.getVideo().webkitExitFullscreen();
		
	this.playsFullScreen = false;
}

VideoPlayer.prototype.callActionOnFlash = function(action, parameter1, parameter2)
{
	if(typeof(this.getVideo().loadVideo) != "undefined")
	{
		if(typeof(this.getVideo()[action]) == "function")
		{
			if(action == "loadVideo")
			{
				this.getVideo().loadVideo(parameter1, parameter2);
			}
			else
			{
				this.getVideo()[action]();
			}

			this.log("Called action “"+action+"” on flash.");
		}
		else
		{
			this.log("Action “"+action+"” is no function: "+typeof(this.getVideo()[action]));
		}
	}
	else
	{
		var player = this;

		this.log("FlashPlayer not loaded, waiting.");
		
		
		if(this.flashAccessCounter < 50)
		{
			setTimeout(function()
			{
				player.callActionOnFlash(action, parameter1, parameter2);
			}, 100);
			
			this.flashAccessCounter++;
		}
		else
		{
			this.displayNoFlashMessage();
		}
	}
}

VideoPlayer.prototype.displayNoFlashMessage = function()
{
	if(!this.flashNoticeVisible)
	{
		$('#'+this.id+'_playlists_groups').hide();
		$('#'+this.id+'_start').hide();
		$('#'+this.id+'_start').hide();
		$('#'+this.id+'_bar').hide();
		
		var hinweis = '<div class="notice" id="'+this.id+'_notice">Leider benötigt der Video Player Flash oder einen Browser, wie <a href="http://www.apple.com/safari/">Safari oder <a href="http://www.google.com/chrome/">Chrome</a><br />Instalieren Sie bitte <a href="http://get.adobe.com/flash">Flash</a> oder Installieren Sie einen der unterstützten Browser.</div>';
		
		$('#'+this.id).html(hinweis+$('#'+this.id).html());
		
		this.flashNoticeVisible = true;
	}
	
}

VideoPlayer.prototype.showLoading = function()
{
	$('#'+this.id+"_navigation .video_scrubber").addClass("loading");
}

VideoPlayer.prototype.showReLoading = function()
{
	$('#'+this.id+'_loading').fadeIn(this.animationSpeed);
}

VideoPlayer.prototype.hideLoading = function()
{
	$('#'+this.id+"_navigation .video_scrubber").removeClass("loading");
	$('#'+this.id+'_loading').fadeOut(this.animationSpeed);
}

VideoPlayer.prototype.loadVideo = function(video_id)
{
	video_id = parseInt(video_id);

	this.log("loadVideo("+video_id+")");
	
	this.reloadingPlayer = false;
	
	// check if we have another video
	if(this.currentVideo !== video_id)
	{
		this.currentVideo = video_id;
		this.videoLoaded = false;
	}
	
	// change video if video isn't loaded
	if(!this.videoLoaded)
	{
		// get video information
		if(this.playlistInformation.videos[this.currentVideo])
		{
			// mark current video in playlist
			$('#'+this.id+'_playlist li.active').removeClass("active");
			$('#'+this.id+"_play_video_"+this.currentVideo).addClass("active");
			
			var current_video = this.playlistInformation.videos[this.currentVideo].video;
			var current_poster = this.playlistInformation.videos[this.currentVideo].poster;
			
			document.title = this.originalDocumentTitle+" – "+this.playlistInformation.videos[this.currentVideo].title;
			
			this.trackEvent("play", this.playlistInformation.videos[this.currentVideo].title);
			
			$('#og_title').attr('content', this.playlistInformation.videos[this.currentVideo].title);
			
			// set poster and source
			this.showPoster(current_poster);
			
			if(this.supportsHTML5())
			{
				$('#'+this.id+"_video").attr("poster", current_poster);
				$('#'+this.id+"_video").attr("src", current_video);
				
				this.getVideo().load();
			
				var player = this;
				if(this.isAppleTouchDevice())
				{	
					setTimeout(function()
					{
						player.forcePlay();
					}, 1000);
				}
			}
			else
			{
				this.callActionOnFlash("loadVideo", current_video, current_poster);
			}
			
			this.hideStartScreen();
			
			if(this.getContentType() == "audio")
				this.audioStart();
			else
				this.showNavigation();
			
			// reset scrubber position
			$('#'+this.id+" .video_played").width(0);

			// video has been loaded
			this.videoLoaded = true;
			this.changeUrl();
		}
	}
	else
	{
		this.showVideo();
		this.hidePoster();
		this.showNavigation();
		
		this.seekTo(0);
		this.play();
	}
	
	this.showVideoInformation();
}

VideoPlayer.prototype.changeUrl = function()
{
	if(this.getType() != "single" && this.noUrls == false)
	{
		var hash = "";
		
		if(this.getType() == "full")
		{
			hash = this.playlistId;

			if(this.currentVideo !== false)
				hash += "/"+this.currentVideo;
		}
		else
		{
			if(this.currentVideo !== false)
				hash += this.currentVideo;
		}

		document.location.hash = hash;
		this.locationHash = document.location.hash;
	}
}

VideoPlayer.prototype.getPlaylistFromURL = function()
{
	var information = document.location.hash.replace("#", "").split("/");
	
	if(this.getType() == "full" && information[0] && this.noUrls == false)
	{
		return information[0];
	}
	else
	{
		return false;
	}
}

VideoPlayer.prototype.getVideoFromURL = function()
{
	var information = document.location.hash.replace("#", "").split("/");
	
	if(this.getType() == "full")
	{
		if(information[1])
		{
			return information[1];
		}
	}
	else if(this.getType() == "standalone")
	{
		if(information[0])
		{
			return information[0];
		}

	}
	
	return false;
}

VideoPlayer.prototype.urlChanged = function()
{
	if(this.locationHash != document.location.hash)
	{
		this.locationHash = document.location.hash;
		var playlist = this.getPlaylistFromURL();
		
		if(playlist || this.getType() == "standalone")
		{
			var video = this.getVideoFromURL();
			
			if(video)
			{
				this.loadPlaylist(playlist, video);
			}
			else
			{
				this.loadPlaylist(playlist);
			}
		}
		else
		{
			if(this.locationHash == "" || this.locationHash == "#")
			{
				this.playlistEnd(true);
			}
		}
		
		this.log(this.locationHash);
	}
	
	var player = this;
	setTimeout(function()
	{
		player.urlChanged();
	}, 200);

}

VideoPlayer.prototype.hidePoster = function(callback)
{
	this.log("hidePoster");
	
	if(typeof(callback) != "undefined")
		callback = function() {};

	$('#'+this.id+"_poster").fadeOut(this.animationSpeed, callback);
}

VideoPlayer.prototype.showPoster = function(src)
{
	if(typeof(src) == "undefined")
	{
		src = this.playlistInformation.poster;
	}
	
	this.log("showPoster: "+src);
	
	$('#'+this.id+"_poster").attr("src", src);
	$('#'+this.id+"_poster").fadeIn(this.animationSpeed);
}

VideoPlayer.prototype.getVideo = function()
{
	if(this.supportsHTML5())
		return $('#'+this.id+"_video").get(0);
	else
		return $('#'+this.id+"_flash").get(0);
}

VideoPlayer.prototype.play = function()
{
	this.log("play");
	
	if(!this.videoLoaded)
	{
		this.log("loading & play");
		this.loadVideo(0);
	}
	else
	{
		if(this.supportsHTML5())
		{
			this.getVideo().play();
		}
		else
		{
			this.callActionOnFlash("playVideo");
		}
	}
}


VideoPlayer.prototype.pause = function(prevent_pause_action)
{
	this.log("pause");
	
	if(prevent_pause_action)
		this.preventPauseAction = true;
	
	if(this.supportsHTML5())
	{
		this.getVideo().pause();
	}
	else
	{
		this.callActionOnFlash("pauseVideo");
	}
}

VideoPlayer.prototype.isPaused = function()
{
	if(this.supportsHTML5())
	{
		return this.getVideo().paused;
	}
	else
	{
		var paused = this.getVideo().paused();
		
		if(typeof(paused) == "undefined")
			paused = true;
		
		return paused;
	}
}

VideoPlayer.prototype.playPause = function()
{
	if(this.isPaused())
	{
		this.play();
	}
	else
	{
		this.pause();
	}
}

VideoPlayer.prototype.seekTo = function(position)
{
	var time = this.getDuration() * position;
	
	if(this.supportsHTML5())
		this.getVideo().currentTime = time;
	else
		this.getVideo().seekTo(time);
}

VideoPlayer.prototype.getCurrentTime = function()
{
	if(this.supportsHTML5())
	{
		return this.getVideo().currentTime;
	}
	else
	{
		if(typeof(this.getVideo().getCurrentTime) != "undefined")
		{
			return this.getVideo().getCurrentTime();
		}
		else
		{
			return 0;
		}
	}
}

VideoPlayer.prototype.getDuration = function()
{
	if(this.supportsHTML5())
	{
		return this.getVideo().duration;
	}
	else
	{
		if(typeof(this.getVideo().getDuration) == "undefined")
			return 0;
		else
			return this.getVideo().getDuration();
	}
}

VideoPlayer.prototype.updatePosition = function(once)
{
	var player = this;
	
	var width = (player.getCurrentTime()/player.getDuration());
	if(isNaN(width)) width = 0;
	
	var full_width = $('#'+this.id+" .video_scrubber").width();
	$('#'+this.id+" .video_played").width(width * full_width);
	
	this.updateTime();
	
	this.log("Timecode Update");
	
	if(once !== true && !this.scrubberTimer)
	{
		this.scrubberTimer = setInterval(function()
		{
			player.updatePosition()
		}, 100);
	}
}

VideoPlayer.prototype.stopUpdatingPosition = function()
{
	if(this.scrubberTimer)
	{
		clearInterval(this.scrubberTimer);
		this.scrubberTimer = false;
	}
	
}

VideoPlayer.prototype.updateTime = function(time, remaining)
{
	var time = this.getCurrentTime();
	var remaining = this.getDuration()-time;
	
	if(!isNaN(time) && !isNaN(remaining))
	{
		$('#'+this.id+'_time_current').text(this.formatTime(time));
		
		var my_time = false;
		
		$('#'+this.id+'_time_remaining').text("-"+this.formatTime(remaining));
	}
	else
	{
		$('#'+this.id+'_time_current').text("");
		$('#'+this.id+'_time_remaining').text("");
	}
}

VideoPlayer.prototype.updatePlaylist = function()
{
	html = '<ul style="width:'+(this.playlistInformation.videos.length*this.playlistElementWidth+10)+'px">';

	for(var i in this.playlistInformation.videos)
	{
		var playListItem = this.playlistInformation.videos[i];

		html += '		<li id="'+this.id+'_play_video_'+i+'" style="background-image:url(\''+playListItem.posterSmall+'\')"><h4>'+playListItem.title+'</h4></li>';
	}

	html += '		</ul>';

	$('#'+this.id+'_playlist').html(html);
	this.checkPlaylistScrollingStatus();
	
	var player = this;
	$('#'+this.id+"_playlist ul li").click(function(e) { player.playlistItemClicked(e); return false; });
}

VideoPlayer.prototype.showNavigation = function(no_animation, force)
{
	if(!this.isAppleTouchDevice("iPhone") && this.currentVideo !== false || force)
	{
		this.updatePosition(true);

		var height = (parseInt(this.height) + parseInt($('#'+this.id+"_navigation").innerHeight()));

		if(no_animation)
			$('#'+this.id).height(height);
		else
			$('#'+this.id).animate({'height': height}, this.animationSpeed);
			
		this.log("showNavigationEnd");
	}
}

VideoPlayer.prototype.overNavigation = function()
{
	clearTimeout(this.navigationTimer);
}

VideoPlayer.prototype.hideNavigation = function()
{
	this.log("hideNavigation");
	var height = parseInt(this.height);
	$('#'+this.id).animate({'height': height}, this.animationSpeed);
}

VideoPlayer.prototype.showVideoInformation = function(fade_out)
{
	var fade_out = (typeof(fade_out) == "undefined") ? true: fade_out;
	
	if(this.currentVideo !== false && this.playlistInformation.videos[this.currentVideo].showTitle)
	{
		clearTimeout(this.videoInformationTimeout);
		
		$('#'+this.id+"_title").text(this.playlistInformation.videos[this.currentVideo].title);
		$('#'+this.id+"_desc").text(this.playlistInformation.videos[this.currentVideo].description);
		$('#'+this.id+"_info").fadeIn(this.animationSpeed);
		
		var player = this;
		
		// fade out after 5 secons
		if(fade_out)
		{
			this.videoInformationTimeout = setTimeout(function()
			{
				player.hideVideoInformation();
			}, 5000);
		}
	}
}

VideoPlayer.prototype.hideVideoInformation = function()
{
	$('#'+this.id+"_info").fadeOut(this.animationSpeed);
	clearTimeout(this.videoInformationTimeout);
}

VideoPlayer.prototype.showPauseScreen = function()
{
	if(this.startScreenVisible == false)
	{
		this.log("showPauseScreen");
		this.showTeasers();
		this.showPlaylist();
	}
}

VideoPlayer.prototype.hidePauseScreen = function()
{
	this.log("hidePauseScreen");
	
	this.hidePlaylist();
	this.hideTeasers();
}

VideoPlayer.prototype.log = function(string)
{
	if(this.debug && typeof(console.log) != "undefined")
		console.log(string);
}

VideoPlayer.prototype.fullWindow = function()
{
	if(this.isFullWindow)
	{
		this.isFullWindow = false;
		
		this.width = this.oldWidth;
		this.height = this.oldHeight;
		
		$('#'+this.id).css("position", "relative");
		$('#'+this.id+'_fullwindow_placeholder').hide();
		$(window).resize(function(){});
	}
	else
	{
		this.isFullWindow = true;

		// save old data
		this.oldWidth = this.width;
		this.oldHeight = this.width;
		
		this.width = $(window).width();
		this.height = $(window).height();
		
		if($('#'+this.id+'_fullwindow_placeholder').length == 0);
			$('#'+this.id).before('<div id="'+this.id+'_fullwindow_placeholder" style="background-color:#000;width:'+this.oldWidth+'px;height:'+this.oldHeight+'px;"></div>');

		$('#'+this.id).css("position", "absolute");
		$('#'+this.id).css("left", 0);
		$('#'+this.id).css("top", 0);
		
		var player = this;
		$(window).resize(function()
		{
			player.resizeFullWindowPlayer();
		});
	}
	
	this.resizePlayerElements();
}

VideoPlayer.prototype.resizeFullWindowPlayer = function()
{
	if(this.isFullWindow)
	{
		this.width = $(window).width();
		this.height = $(window).height();
		
		this.resizePlayerElements();
	}
}

VideoPlayer.prototype.showTeasers = function()
{
	// not implemented yet 
	
	/*this.log("showTeasers");
	
	var teaser_html = '';

	if(this.currentVideo && this.playlistInformation.teaser || this.playlistInformation.videos[this.currentVideo] && this.playlistInformation.videos[this.currentVideo].teaser)
	{
		var i = 0;
		while(i < 3)
		{
			var	teaser = false;
			
			if(this.playlistInformation.videos[this.currentVideo].teaser && this.playlistInformation.videos[this.currentVideo].teaser[i])
			{
				teaser = this.playlistInformation.videos[this.currentVideo].teaser[i];
			}
			else if(this.playlistInformation.teaser && this.playlistInformation.teaser[i])
			{
				teaser = this.playlistInformation.teaser[i];
			}
			
			if(teaser)
			{
				teaser_html += '<li style="background-image:url('+teaser.image+')"><a href="'+teaser.url+'"><h3>'+teaser.title+'</h3></a></li>';
			}
			
			i++;
		}

		$('#'+this.id+"_teaser").html(teaser_html);
		$('#'+this.id+"_teaser").fadeIn(this.animationSpeed);
	}*/
}

VideoPlayer.prototype.hideTeasers = function()
{
	$('#'+this.id+"_teaser").fadeOut(this.animationSpeed);
}
	
VideoPlayer.prototype.showPlaylist = function()
{
	this.log("showPlaylist");
	
	// show playlist only if we have more than one video
	if(this.playlistInformation.videos.length > 1)
	{
		$('#'+this.id+"_playlist_container").fadeIn(this.animationSpeed);
		this.scrollPlaylistToCurrentVideo(!this.hidePlaylistOnPlay);
	}
}

VideoPlayer.prototype.scrollPlaylistToCurrentVideo = function(animate)
{
	if(this.currentVideo > 1)
	{	
		if(animate)
		{
			var player = this;
			
			$('#'+this.id+"_playlist").animate({
				scrollLeft: this.playlistElementWidth*(this.currentVideo-1)
			}, this.animationSpeed*2, "easeInOutQuad", function ()
			{
				player.checkPlaylistScrollingStatus();
			});
			
		}
		else
		{
		
			$('#'+this.id+"_playlist").scrollLeft(this.playlistElementWidth*(this.currentVideo-1));
		}
	}
}

VideoPlayer.prototype.hidePlaylist = function(force)
{
	this.log("hidePlaylist");
	
	if(this.hidePlaylistOnPlay)
	{
		// show playlist only if we have more than one video
		if(this.playlistInformation.videos.length > 1 || force)
		{
			$('#'+this.id+"_playlist_container").fadeOut(this.animationSpeed);
		}
	}
}

VideoPlayer.prototype.formatTime = function(seconds)
{
	// make calculations only if we have more than one second
	if(seconds > 1)
	{
		seconds = Math.round(seconds);
		var minutes = Math.floor(seconds / 60);
		minutes = (minutes >= 10) ? minutes : "0" + minutes;
		seconds = Math.floor(seconds %  60);
		seconds = (seconds >= 10) ? seconds : "0" + seconds;
	}
	else
	{
		var minutes = "00";
		seconds = "00";
	}
	
	return minutes + ":" + seconds;
}

VideoPlayer.prototype.videoPlay = function()
{
	this.callEvent("videoPlay");
	
	this.showVideo();
	this.hidePauseScreen();
	this.hidePoster();
	
	if(this.hideScrubberWhilePlaying)
	{
		this.hideNavigation();
	}
	else
	{
		this.showNavigation();
	}
	
	this.log("videoPlay");
}

VideoPlayer.prototype.showVideo = function(animate)
{
	if(this.supportsHTML5())
	{
		if(animate)
			$('#'+this.id+"_video").fadeIn(this.animationSpeed);
		else
			$('#'+this.id+"_video").show();
	}
}

VideoPlayer.prototype.hideVideo = function(animate)
{
	if(this.supportsHTML5())
	{
		if(animate)
			$('#'+this.id+"_video").fadeOut(this.animationSpeed);
		else
			$('#'+this.id+"_video").hide();
	}
}

VideoPlayer.prototype.videoPlaying = function()
{
	this.callEvent("videoPlaying");
	
	this.hideLoading();
	this.setPlayPauseStatus(false);
	this.updatePosition();
	
	this.log("videoPlaying");
}

VideoPlayer.prototype.setPlayPauseStatus = function(status)
{
	if(typeof(status) == "undefined")
		status = this.isPaused();
	
	if(status)
	{
		this.log("setPlayPauseStatus: paused");
		$('#'+this.id).addClass("paused");
	}
	else
	{
		this.log("setPlayPauseStatus: not paused");
		$('#'+this.id).removeClass("paused");
	}
}

VideoPlayer.prototype.videoPause = function()
{
	this.callEvent("videoPause");
	
	// clear interval for moving the scrubber
	this.stopUpdatingPosition();
	
	if(!this.reloadingPlayer && !this.startScreenVisible && !this.preventPauseAction)
	{
		if(!this.showInfoOnHover)
			this.hideVideoInformation();
			
		this.showPauseScreen();
		
		if(this.isAppleTouchDevice('iPhone'))
		{
			this.hideVideo();
			this.showPoster();
		}
	}
	
	if(this.hideScrubberWhilePlaying)
	{
		this.showNavigation();
	}
	
	this.preventPauseAction = false;
	this.setPlayPauseStatus(true);
	
	this.log("videoPause");
}

VideoPlayer.prototype.videoEnd = function()
{
	this.callEvent("videoEnd");
	
	if(this.currentVideo < (this.playlistInformation.videos.length - 1))
	{
		this.loadVideo(this.currentVideo+1);
		
		if(this.hidePlaylistOnPlay == false)
			this.scrollPlaylistToCurrentVideo(true);
			
		this.log("videoEnd");
	}
	else
	{
		this.pause();
		this.playlistEnd();
	}
	
	this.setPlayPauseStatus(true);
}

VideoPlayer.prototype.hideStartScreen = function(callback)
{
	this.log("hideStartScreen: "+this.startScreenVisible);
	this.callEvent("hideStartScreen");
	
	if(this.startScreenVisible === true)
	{
		this.startScreenVisible = false;
		
		this.stopPlaylistSlideshow();
		
		if(this.getContentType() == "audio")
		{
			$('#'+this.id+"_audiotitle").fadeOut(this.animationSpeed, callback);
		}
		else
		{
			$('#'+this.id+"_bar").fadeOut(this.animationSpeed, callback);
		}
		
		$('#'+this.id+"_start").hide();
	}
	else
	{
		if(typeof(callback) != "undefined")
		{
			callback();
		}
	}
}

VideoPlayer.prototype.showStartScreen = function(force)
{
	this.log("showStartScreen:"+this.startScreenVisible);
	
	if(!this.startScreenVisible || force)
	{
		if(this.playlistInformation.barPosition != "hidden")
		{
			$('#'+this.id+"_bar").removeClass("left bottom");
			$('#'+this.id+"_bar").addClass(this.playlistInformation.barPosition);
			$('#'+this.id+"_bar").html(this.playlistInformation.barContent);
			$('#'+this.id+"_bar").fadeIn(this.animationSpeed);
		}
		else
		{
			$('#'+this.id+"_bar").fadeOut(this.animationSpeed);
		}
		
		$('#'+this.id+"_start").removeClass("hidden left bottom");
		$('#'+this.id+"_start").addClass(this.playlistInformation.barPosition);
		
		this.setSidebarSize();
		
		var self = this;
		
		if(this.playlistInformation.videoPostersSlideshow)
		{
			self.startPlaylistSlideshow();
		}

		$('#'+this.id+"_start").fadeIn(this.animationSpeed);
		
		this.pause();
		this.hideVideo();
		this.showPoster();
		this.hideLoading();
		this.hidePlaylist(true);
		this.currentVideo = false;
		this.videoLoaded = false;
		this.changeUrl();
		
		this.startScreenVisible = true;
	}
}

VideoPlayer.prototype.startPlaylistSlideshow = function()
{
	var self = this;
	
	$('#'+self.id+"_poster").bind('load', function()
	{
		$(this).fadeIn(self.animationSpeed);
	});
	
	function startPlaying()
	{
		self.playlistSlideshowTimeout = setTimeout(function()
		{
			self.startPlaylistSlideshow();
		}, self.playlistSlideshowDuration);
	}
	
	if(self.playlistSlideshowPosition !== false)
	{
		$('#'+self.id+"_poster").fadeOut(self.animationSpeed, function()
		{
			var poster = self.playlistInformation.videos[self.playlistSlideshowPosition].poster;
			$('#'+self.id+"_poster").attr('src', poster);
			
			if(self.playlistSlideshowPosition == self.playlistInformation.videos.length - 1)
				self.playlistSlideshowPosition = 0;
			else
				self.playlistSlideshowPosition++;
				
			startPlaying();
		});
	}
	else
	{
		self.playlistSlideshowPosition = 0;
		startPlaying();
	}
	
	
}

VideoPlayer.prototype.stopPlaylistSlideshow = function()
{
	$('#'+this.id+"_poster").unbind('load');
	clearTimeout(this.playlistSlideshowTimeout);
	this.playlistSlideshowPosition = 0;
}

VideoPlayer.prototype.audioEnd = function()
{
	$('#'+this.id+'_time_current').fadeOut(this.animationSpeed);
	$('#'+this.id+'_time_remaining').fadeOut(this.animationSpeed);
	
	var player = this;
	$('#'+this.id+"_navigation .video_scrubber").fadeOut(this.animationSpeed, function()
	{
		$('#'+player.id+"_audiotitle").fadeIn(player.animationSpeed);
	});
	
}

VideoPlayer.prototype.audioStart = function()
{
	$('#'+this.id+'_time_current').show();
	$('#'+this.id+'_time_remaining').show();
	$('#'+this.id+"_navigation .video_scrubber").show();
}

VideoPlayer.prototype.playlistEnd = function(force_start)
{
	this.callEvent("playlistEnd");
	
	force_start = ((typeof(force_start) == "undefined") ? false : force_start);
	
	if(this.getContentType() == "video")
	{
		this.hideVideo(true);
		this.showPoster();
		this.hideNavigation();
		
		$('#'+this.id+"_playlist ul li.active").removeClass("active");
	}
	else
	{
		this.audioEnd();
	}
	
	if(force_start)
	{
		this.reloadingPlayer = true;
		this.pause();
	}
	
	if(this.playlistInformation.endAction == "start" || force_start)
	{
		this.hidePauseScreen();
		this.hideVideoInformation();

		this.showStartScreen();
	}
	else
	{
		this.showPauseScreen();
	}
	
	this.log("playlistEnd");
}

VideoPlayer.prototype.videoLoading = function()
{
	this.showLoading();
	this.showVideo(true);
	this.callEvent("videoLoading");
	
	$('#'+this.id+"_navigation .video_scrubber").addClass("loading");
}

VideoPlayer.prototype.videoCanPlayThrough = function()
{
	this.log("videoCanPlayThrough");
	this.callEvent("videoCanPlayThrough");
	
	if(this.getCurrentTime() == 0 || !this.supportsHTML5())
	{
		$('#'+this.id+"_navigation .video_scrubber").removeClass("loading");
		this.play();
	}
}

VideoPlayer.prototype.forcePlay = function()
{
	this.log("forcePlay");
	
	// if video is still paused try to start it with a fake click
	// this hack is neccessary because ipad does no autoplay
	if(this.isPaused())
	{
		this.log("fake_click");
		
		var player = this;
		var a = $('<a href="#" id="fakeClick"></a>');
		a.bind("click", function(e)
		{
			e.preventDefault();
			player.play();
		});

		$("body").append(a);
						
		var fake_click;
		var play_button = a.get(0);
		
		if(document.createEvent) 
		{
			fake_click = document.createEvent("MouseEvents");
		}

		if (fake_click.initMouseEvent) 
		{
			fake_click.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
			play_button.dispatchEvent(fake_click);
		}
		
		a.remove();
	}
}

VideoPlayer.prototype.videoLoadedData = function()
{
	//this.seekTo(0);
	this.callEvent("videoLoadedData");
	this.log("videoLoadedData");
}

VideoPlayer.prototype.videoLoadedMetaData = function()
{
	this.updateTime();
	this.callEvent("videoLoadedMetaData");
	
	this.log("videoLoadedMetaData");
	
}

VideoPlayer.prototype.videoWaiting = function()
{
	this.showLoading();
	this.callEvent("videoWaiting");
	
	this.log("videoWaiting");
}

VideoPlayer.prototype.videoStalled = function()
{
	//this.showLoading();
	this.log("videoStalled");
	this.callEvent("videoStalled");
	
}

VideoPlayer.prototype.videoError = function()
{
	this.log("videoError");
	this.callEvent("videoError");
	
	this.playlistEnd();
}

VideoPlayer.prototype.supportsHTML5 = function()
{
	if(this.html5 === null)
	{
		if("HTMLVideoElement" in window)
		{
			var myVideo = document.createElement("video");
			
			// get content type
			var content_type = (this.getContentType() == "audio") ? "audio/mpeg" : "video/mp4";
			
			// check if browser supports this
			var support = myVideo.canPlayType(content_type);
			
			// return result
			this.html5 = (support == "maybe" || support == "probably");			
		}
		else
		{
			this.html5 = false;
		}
	}

	return this.html5;
}

VideoPlayer.prototype.isAppleTouchDevice = function(type)
{
	if(type == undefined)
	{
		var type = "iPad|iPhone";
	}
	
	var reg_exp = new RegExp(type, 'i');
	
	return navigator.userAgent.match(reg_exp) != null;
}

VideoPlayer.prototype.isIE6 = function()
{
	var is_ie = /MSIE 6/i.test(navigator.userAgent);
	return is_ie;
}

VideoPlayer.prototype.trackEvent = function(action, label, value)
{
	if(this.googleAnalytics && typeof(_gaq) != "undefined")
	{
		_gaq.push(['_trackEvent', 'Video Player', action, label]);
	}
}
