﻿var playlist = {}
var cPlaylistItem = 0;
var spot_clip = "/player2/bufo_clip.wmv";
var spot_duration = 4000;
var spot_playing = false;

function media_stop(sender, args) {
	var media = document.getElementById("mp").content.findName("MPAVideo");
	media.Position = "00:00:00";
	media.stop();
	media.findName("ply").visibility = "Visible";
	media.findName("pas").visibility = "Collapsed";
}


function media_playpause(sender, args) {
	var plugin = document.getElementById("mp");
	var media = plugin.content.findName("MPAVideo");
	var animation_textarea = plugin.content.findName("animatedTextArea");
	switch (media.CurrentState) {
		case "Stopped":
			media.play();
			media.findName("ply").visibility = "Collapsed";
			media.findName("pas").visibility = "Visible";
			animation_textarea.begin();
			break;
		case "Paused":
			media.play();
			media.findName("ply").visibility = "Collapsed";
			media.findName("pas").visibility = "Visible";
			break;
		default:
			media.pause();
			media.findName("ply").visibility = "Visible";
			media.findName("pas").visibility = "Collapsed";
	}
}

function media_prev() {
	var media = document.getElementById("mp").content
	if (cPlaylistItem > 0) {
		cPlaylistItem += -1;
		media_play_item(media, cPlaylistItem)
	}
}

function media_next() {
	var media = document.getElementById("mp").content
	if (cPlaylistItem < playlist.length-1) {
		cPlaylistItem += 1;
		media_play_item(media, cPlaylistItem)
	}
}


function media_ended(sender, args) {
	if (spot_playing) {
		media_play_item(this, cPlaylistItem)
	} else {
		media_next();
	}
}


function media_fullscreen(sender, args) {
	var media = document.getElementById("mp").content;
	media.fullScreen = !media.fullScreen;
}


function onFullScreenChanged(sender, eventArgs) {
	var plugin = sender.getHost();
	var media = sender.findName("MPAVideo");
	var logo = sender.findName("MPLogo");
	var textarea = sender.findName("MPTextArea");
	var scaleTransform = sender.findName("MPLogoTransform");
	if (plugin.content.fullscreen) {
		media.width = plugin.content.actualWidth;
		media.height = media.width*3/4;
		media["Canvas.Top"] = (plugin.content.actualHeight - media.height)/2
		sender.findName("MPControlBar").opacity = 0;
		if (scaleTransform) {
			scaleTransform.ScaleX = 2;
			scaleTransform.ScaleY = 2;
			logo["Canvas.Top"] = media.height-20;
			logo["Canvas.Left"] = media.width-logo.width-20;
		}
		if (textarea) {
			textarea.opacity = 0;
		}
	} else {
		media.width = 400;
		media.height = 300;
		media["Canvas.Top"] = 4;
		sender.findName("MPControlBar").opacity = 1;
		if (scaleTransform) {
			scaleTransform.ScaleX = 1;
			scaleTransform.ScaleY = 1;
			logo["Canvas.Top"] = 270;
			logo["Canvas.Left"] = 300;
		}
	}
}

function media_play_spot(sender, item_num) {
	spot_playing = true;
	var plugin = document.getElementById("mp");
	var animation_textarea = plugin.content.findName("animatedTextArea");
	animation_textarea.stop();
	var media = plugin.content.findName("MPAVideo");
	media.stop();
	media.Source = spot_clip;
	//media.play();
}


function media_play_item(sender, item_num) {
	var plugin = document.getElementById("mp");
	var media = plugin.content.findName("MPAVideo");
	media.findName("ply").visibility = "Collapsed";
	media.findName("pas").visibility = "Visible";
	if (item_num == null) {
			item_num = 0;
		}
	cPlaylistItem = item_num;
	if (spot_playing) {
		spot_playing = false;
		
		var animation_textarea = plugin.content.findName("animatedTextArea");
		var bandname = plugin.content.findName("bandname");
		var videotitle = plugin.content.findName("videotitle")
		animation_textarea.stop()
		
		media.stop();
		if (playlist.length > 1) {
			media.Source = playlist[item_num].URL;
			plugin.content.findName("menuTitle").text = playlist[item_num].TITLE;
			bandname.text = playlist[item_num].BANDNAME;
			videotitle.text = playlist[item_num].TITLE;
		} else {
			media.Source = playlist.URL;
			plugin.content.findName("menuTitle").text = playlist.TITLE;
			bandname.text = playlist.BANDNAME;
			videotitle.text = playlist.TITLE;
		}
	
		media.play();
		animation_textarea.begin();
		//playlist_setcurrent("#videos", item_num);
		//playlist_currentinfo(item_num);
	} else {
		spot_playing = true;
		media_play_spot(this, null)
	}
	
}


function media_loaded(sender, args) {
	sender.getHost().content.onFullScreenChange = onFullScreenChanged;
	sender.findName("MPAVideo").addEventListener("MediaFailed","media_error");
	media_play_item(this, 0);
}

function media_error(sender, args) {
	media_next();
}


function media_btnover(sender, args) {
	sender.cursor = "Hand";
	sender.opacity = 1;
}

function media_btnout(sender, args) {
	sender.cursor = "Default";
	sender.opacity = 0.5;
}

function media_menuon(sender, args) {
	var menu = sender.getHost().content.findName("MPMenu");
	menu["Canvas.ZIndex"] = 10;
}


function media_menuoff(sender, args) {
	var menu = sender.getHost().content.findName("MPMenu");
	menu["Canvas.ZIndex"] = 0;
}


function playlist_setcurrent(el, item) {
	var element = el + " li"
	var pl = $(element);
	$(pl).removeClass("current_video");
	$($(pl)[item]).addClass("current_video");
}


function playlist_currentinfo(item) {
	$("#clip_title").html(playlist[item].TITLE);
	$("#clip_abstract").html(playlist[item].ABSTRACT);
}
