// JavaScript Document
var Player = {
	ID: null,
	css_selector_string: "#web-anthony div div a, #tv-list li a",
	currently_playing : {},
	is_ready: false,
	first_added_item: null,
	popout: null,
	popReady: false,
	popStatus: false,
	FO: { 
			movie:"/videogaiden/media_player/player.swf", 
			id:"ExternalInterface",  
			width:"522px", 
			height:"323px", 
			majorversion:"8", 
			build:"15", 
			allowfullscreen: "true" ,
			bgcolor : "#000000",
			allowScriptAccess : "always",
			swLiveConnect : "true"
		},
	add: function(mySelector){
		Player.ID = mySelector;
		UFO.create(Player.FO, Player.ID);
		// for popup items
		if($("body").hasClass("popup")){
			$("#non_flash_player").before('<a href="http://www.bbc.co.uk/webwise/categories/plug/flash/flash.shtml?intro">Flash Help</a>&nbsp;');
		// for calender items
		}else if($("body").hasClass("loader")){
			$("#non_flash_player").before('<a href="http://www.bbc.co.uk/webwise/categories/plug/flash/flash.shtml?intro">Flash Help</a>&nbsp;');
		// main items
		} else {
			$("#non_flash_player").before('<a href="http://www.bbc.co.uk/webwise/categories/plug/flash/flash.shtml?intro">Flash Help</a>&nbsp;');
			if(!jQuery.browser.msie){
				$("#non_flash_player").after('&nbsp;<a id="popout_link" href="/videogaiden/popout.shtml">Popout</a>');
			}
			//$("#player").after('<div class="player_buttons"><a href="http://www.bbc.co.uk/webwise/categories/plug/flash/flash.shtml?intro">Flash Help</a>&nbsp;<a id="non_flash_player" href="http://www.bbc.co.uk/scotland/player/?item=' + Player.ID + '" title="Open Scotland Player in Popup Window">Alternative Version</a></div>');
		}
		if(UFO.hasFlashVersion(Player.FO.majorversion, Player.FO.build)){
			$("#player").addClass("flash");
			$(Player.css_selector_string).bind("click", function(e){
				// hijack the anchor event
				e.preventDefault();
				var href = $(this).attr("href");
				id = href.split("/").reverse()[0];
				// get rid of ?item= starting bit
				id = id.substring(6);
				Player.playItem(id, $(this).attr("title"));
				
				//
				$("#non_flash_player").attr("href", href).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();
				});
				$("#popout_link").bind("click", function(e){
					// open the popout player
					e.preventDefault();
					Player.popOut();
					r = setInterval(function(){
						if(Player.popReady){
							if(Player.popout.play){
								Player.popout.play(Player.currently_playing.item_id, Player.currently_playing.item_name);
							}
							clearInterval(r);
						}
					}, 1000);
				});
				
			});
			$("#popout_link").bind("click", function(e){
				// open the popout player
				e.preventDefault();
				if(Player.popout == null || Player.popout.closed){
					Player.popOut();
				}
			});
			if(Player.first_added_item == null){
					Player.first_added_item = $(Player.css_selector_string);
			}
		} else {
			$(Player.css_selector_string).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 = "videoGaiden AV item";
		}else{
			some_obj.name = item_name;
		}
		if(Player.popout != null && !Player.popout.closed){
			Player.popout.play(some_obj.ID, some_obj.name);
		} else {
			Player.popReady = false;
			var a = $("#ExternalInterface").get(0);
			a.as___play(some_obj);
		}
	},
	popIn: function(){
		$("#player_popped").remove();
		$("#" + Player.ID).css("display", "block");
		var t=setInterval("showImage()",1000);
		showImage = function(){
			if(Player.is_ready){
				Player.showImage("/videogaiden/media_player/play_video.gif");
				clearInterval(t);
			}
		}
		Player.popStatus = false
	},
	popOut: function(){
		if(Player.popStatus == false){
			Player.popStatus = true
			//var a = $("#ExternalInterface").get(0);
			//
			//a.as___pause ();
			//
			$("#" + Player.ID).css("display", "none");
			$("#player").prepend('<div id="player_popped"><img src="/videogaiden/images/video_popped_out_image.jpg" /></div>');
			Player.popout = window.open($("#popout_link").attr("href"),"popout","status=no,scrollbars=no,resizable=yes,width=520,height=420,top=30,left=15");
			if (window.focus) {Player.popout.focus();}
		}
	}, 
	currentlyPlaying: function(item_id){
		$("#" + Player.currently_playing.item_id).parent().toggleClass("current");
		if(item_id != Player.currently_playing.item_id){
			Player.currently_playing = {"item_id": item_id, "item_name": $("#"+item_id).attr("title")};
		}
		// do show video
		$(".gallery_list li").addClass("current");
		$(".gallery_list li").toggleClass("current");
		$("#" + item_id).parent().addClass("current");
	},
	getNext: function(){
		if(Player.currently_playing.item_id != undefined){
			
			//$(Player.css_selector_string)
			
			current_item = $("#" + Player.currently_playing.item_id);
			current_item_type = current_item.attr("class");
			next_item_li = current_item.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(){
		// trigger the click event on the first element
		$(Player.first_added_item).slice(0,1).trigger("click");
	},
	ready: function(){
		// 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(){
		var url = window.location.href;
		if(Player.currently_playing.item_id != undefined){
			vid_link = "http://www.bbc.co.uk/videogaiden/video/" + Player.currently_playing.item_id+"/";
			return vid_link;
		} else {
			return "http://www.bbc.co.uk/videogaiden/";
		}
	}
};

/* 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;