function gotojoin() {
	window.location.href = "index.php?action=join";
}

function videostart() {
	try {
		$.get("?action=ajax/trailer_view_num&guimode=ajax&scene_id=" + $("#scene_id").val() + "&rand=" + Math.random());
	} catch(e) {}
}

function playTrailer(opts) {
	$("#player").attr("scenedata", "scene_" + val)
							.html("")
							.videoplayer("init", {
								player_key: $("#flowplayer_key").val(),
								label: "Play Trailer",
								autoplay: true,
								error_url: "?action=join"
						  }).videoplayer("render");
}

function activateScene(scene_id) {
	//alert(scene_id.toSource());
	var el;
	if (isNaN(scene_id)) {
		try {
			scene_id = $(this).parents('li').filter(':first').find('input[name=scene_id]').val();
		} catch(e) { throw e; }
		if (isNaN(scene_id)) {
			throw 'activateScene(scene_id was not a number)';
			return false;
		}
	}

	$('#scene_list>li.active_scene').each(function() {
		animateSceneList($(this), 'off');
	});

	el = $('#scene_list>li.sceneid_' + scene_id);
	animateSceneList(el, 'on');

	el.find('input[type=hidden]').each(function() {
		var blah = $(this);
		switch (blah.attr('name')) {
			case 'scene_id':
				$('#scene_id').val(blah.val());
				break;
			case 'scene_title':
				$('#player_scene_title').html(blah.val());
				$('#scene_title').val(blah.val());
				break;
			case 'scene_pornstar':
				$('#player_scene_pornstar').find('a').html(blah.val());
				$('#scene_pornstar').val(blah.val());
				break;
		}
	});
	/* New player */
	$("#player").attr("scenedata", "scene_" + scene_id)
							.html("")
							.videoplayer("init", {
								player_key: $("#flowplayer_key").val(),
								label: "Play Trailer",
								autoplay: true,
								error_url: "?action=join"
						  }).videoplayer("render");
	return false;
}

function animateSceneList(el, dir) {
	var h5 = el.find('h5').filter(':first'), playing_icon = el.find('.scene_playing').filter(':first');
	switch (dir) {
		case 'on':
			el.addClass('active_scene');
			h5.stop().animate({ bottom: 0 }, 'fast');
			playing_icon.stop().delay('fast').animate({ top: 0 }, 'fast');
			el.find('.scene_title').filter(':first').animate({ opacity: 0.75 }, 'fast', 'swing').delay('fast').animate({ opacity: 1 }, 'fast', 'swing');
			break;
		case 'off':
			el.removeClass('active_scene');
			h5.stop().animate({ bottom: 0-h5.height() }, 'fast');
			playing_icon.stop().animate({ top: playing_icon.height() }, 'fast');
			break;
		case 'in':
			h5.stop().animate({ bottom: 0-h5.height()/3.5 }, 'fast');
			break;
		case 'out':
			if (!el.hasClass('active_scene'))
				h5.stop().animate({ bottom: 0-h5.height() }, 'fast');
			break;
	}
}

$(function() {
	$('#scene_list a.scene_list_coverlink').mouseenter(function() {
		var el = $(this).parents('li').filter(':first');
		animateSceneList(el, 'in');
	}).mouseleave(function() {
		var el = $(this).parents('li').filter(':first');
		animateSceneList(el, 'out');

	}).click(function(){
		var temp = $(this).parent().parent().attr('class').split('_');
		$("#player").attr("scenedata", "scene_" + temp[2])
							.html("")
							.videoplayer("init", {
								player_key: $("#flowplayer_key").val(),
								label: "Play Trailer",
								autoplay: true,
								error_url: "?action=join"
						  }).videoplayer("render");
		return false;

	});

	var activeId = document.location.href.match(/scene_id=([0-9]+)/), myEl;
	if (activeId && activeId[1])
		myEl = $('#scene_list>li').filter('.scene_'+activeId[1]);
	if (!myEl || !myEl[0])
		myEl = $('#scene_list>li').filter(':eq(1)');

	//("#player").attr("scenedata", "scene_" + val(myEl.find('input[name=scene_id]').val()));
	//alert($("#player").attr('scene_data'));
	activateScene(myEl.find('input[name=scene_id]').val());
});
