/* ==========================================
	Media Page Javascript
	www.southwickchurch.org.uk
	media.js, v1.9
	Nathanael Woodbridge (Soli Deo Gloria)
============================================= */

var base_path = '/media/item/data/';

$(document).ready(function() {
	//set up
	$(".playing").hide();
	$(".loading").hide();
	
	//load item on click. Title is in 'rel' attr
	$('.play-item').click(function() {
		var item_id = $(this).attr("id");
		var item_title = $(this).attr("rel");
		
		load_item(item_id, item_title);
		
		return false;
	});
	
	if ($('.playing').attr('rel') == 'single') {
		//single item, so item does not need to be loaded
		$(".loading").hide();
		$(".playing").show();
		
		media_url = $('.playing div.link a').attr('href');
		media_type = $('.playing div.link a').attr('rel');
		
		loadPlayer(media_url, media_type);
	}
});

function load_item(id, title) {
	$(".playing").hide();
	$(".loading").show();
	
	var load_url = base_path + id + '/' + title + '/';
	
	$.get(load_url, function(data, status) {
		$('.playing .now').html(data);
		
		media_url = $('.playing div.link a').attr('href');
		media_type = $('.playing div.link a').attr('rel');
		
		loadPlayer(media_url, media_type);
		
		$(".loading").hide();
		$(".playing").show();
	});
	
	$(".playing").ajaxError(function(e, xhr, settings, exception) {
		$(".loading").hide();
		$(".playing").show();
		$(".playing").html('<h2>Item Not Found</h2> <p>Please try again. If the problem persists, please contact us.</p>');
	});
}

function loadPlayer(url, type) {
	if (type == 'video') {
		var height = 360;
	} else if (type == 'audio') {
		var height = 24;
	} else {
		var height = 360;
	}
	var so = new SWFObject('/workspace/js/player.swf','player','540',height,'9','#000000');
	so.addParam('allowfullscreen','true');
	so.addParam('allowscriptaccess','always');
	so.addParam('wmode','opaque');
	so.addVariable('autostart','true');
	so.addVariable('file', url);
	so.write('player');
}
