// JavaScript Document
$(document).ready(function(){
	Player.add();
});
var Player = {
	currently_playing : {},
	is_ready: false,
	first_added_item: null,
	FO: { 
			movie:"/scotland/music/tv/media/media_player/player.swf", 
			id:"ExternalInterface",  
			width:"410px", 
			height:"258px", 
			majorversion:"8", 
			build:"15", 
			allowfullscreen: "true" ,
			bgcolor : "#101010",
			allowScriptAccess : "always",
			swLiveConnect : "true"
		},
	add: function(){
		UFO.create(Player.FO, "player");
		if(UFO.hasFlashVersion(Player.FO.majorversion, Player.FO.build)){
			$("#player").addClass("flash");
			$(".media/a").bind("click", function(e){
				// hijack the anchor event
				id = $(this).attr("href").split("/").reverse()[0];
				// get rid of ?item= starting bit
				id = id.substring(6);
				Player.playItem(id, $(this).attr("title"));
				e.preventDefault();
				//
				
			});
			if(Player.first_added_item == null){
					Player.first_added_item = $(".media/a");
			}
		} else {
			$(".media/a").bind("click", function(e){
				// open the broadband console
				popup_window = window.open($(this).attr("href"),"player","status=no,scrollbars=no,resizable=yes,width=671,height=407,top=30,left=15");
				if (window.focus) {popup_window.focus();}
				e.preventDefault();
			});
		}
	},
	playItem: function(item_id, item_name){
		Player.currently_playing = {"item_id" : item_id, "item_name": item_name};
		var some_obj = new Object();
		if(item_id == undefined){
			some_obj.ID = "video1"
		}else{
			some_obj.ID = item_id
		}
		if(item_name == undefined){
			some_obj.name = "Scotland Music AV item"
		}else{
			some_obj.name = item_name
		}
		var a = $("#ExternalInterface").get(0);
		a.as___play(some_obj);
	},
	currentlyPlaying: function(item_id){
		if(item_id != Player.currently_playing.item_id){
			Player.currently_playing = {"item_id": item_id, "item_name": $("#"+item_id).attr("title")};
		}
		$(".playing").toggleClass("playing");
		$("#" + item_id).parent().parent().addClass("playing");
	},
	getNext: function(){
		if(Player.currently_playing.item_id != undefined){
			current_item = $("#" + Player.currently_playing.item_id);
			current_item_type = current_item.attr("class");
			next_item_li = current_item.parent().parent().next();
			if(next_item_li){
				var a = $("." + current_item_type, next_item_li).attr("href").split("/").reverse()[0]
				// get rid of ?item= starting bit
				a = a.substring(6);
				return a;
			} else {
				return 0;
			}
		} else {
			return -1;
		}
	},
	playFirst: function(){
		if(video_starting_id_str == null){
			var some_id = $(Player.first_added_item).attr("href").split("/").reverse()[0];
			// get rid of ?item= starting bit
			some_id = some_id.substring(6);
			var some_title = $(Player.first_added_item).attr("title");
			//
			Player.playItem(some_id, some_title);
		}else{
			Player.playItem(video_starting_id_str, "");
		}
	},
	ready: function(){
		// console.log("flash is ready")
		// runs when the the flash player is ready to play something
		Player.is_ready = true;
	},
	showImage: function(img_path){
		var a = $("#ExternalInterface").get(0);
		if(a.as___setMediaImage){
			a.as___setMediaImage(img_path);
		}
	},
	getLink: function(){
		return location.href;//"http://www.bbc.co.uk/scotland/music/tv/index.shtml";
	},
	goFullScreen: function(){
		// not implemented
	},
	stopFullScreen: function(){
		// not implemented
	}
}

/* FLASH - JAVASCRIPT INTEGRATION */
/* This maps the functions in the Player object to the functions expected by the flash object */
js___now_playing = Player.currentlyPlaying;
js___full_screen_mode = Player.goFullScreen;
js___minimized_screen_mode = Player.stopFullScreen;
js___get_next = Player.getNext;
js___flash_started = Player.ready;
js___now_playing = Player.currentlyPlaying;
js___get_link = Player.getLink;
js___play_first = Player.playFirst;