/*
 * TAGGYプレイヤーライブラリ
 * /js/Taggy/Player.js
 */

Taggy.Player = {
	List : {
		id : 'playList',
		template : Taggy.getContext() + '/ajp/video/playList.ajp'
	},
	View : {
		id : 'playView',
		template : Taggy.getContext() + '/ajp/video/playView.ajp'
	},
	location : {
		protocol : 'http:',
		hostname : 'rest.taggy.jp',
		pathname : '/rest/json/video'
	},
	//queries : {
	//	callback : 'Taggy.Player.response'
	//},
	pager : {
		total_entries : 0,
		per_page : 4,
		start_page : 0,
		previous_page : 0,
		current_page : 0,
		next_page : 1,
		last_page : -1
	},
	limit : 128,
	id_name : 'tpi',
	list_name : 'tpl',
	expires : '1d',
	path : '/',
	id : Taggy.Cookie.read('tpi'),
	list : eval(Taggy.Cookie.read('tpl')) || [],
	//failure : false,
	playing : false,
	requesting : false,
	Entries : [],
	Detail : {}
};
Taggy.Player.save = function (list) {
	var json = Taggy.array2json(list || Taggy.Player.list);
	Taggy.Cookie.write(this.id_name, this.id, this.expires, this.path);
	Taggy.Cookie.write(this.list_name, json, this.expires, this.path);
};
Taggy.Player.getList = function (page) {
	this.pager.total_entries  = this.list.length - 1;
	this.pager.current_page  = page || this.pager.start_page;
	//this.pager.last_page     = Math.ceil(this.list.length / this.pager.per_page);
	this.pager.last_page     = Math.floor(this.pager.total_entries / this.pager.per_page);
	this.pager.previous_page = Math.max(this.pager.current_page - 1, 0);
	this.pager.next_page     = Math.min(this.pager.current_page + 1, this.pager.last_page);

	var start = this.pager.current_page * this.pager.per_page;
	var end = start + this.pager.per_page;

	return this.list.slice(start, end);
};
Taggy.Player.viewer = function (res) {

	this.playing = false;
	this.Detail[this.id] = res;

	var tmpl = new Taggy.Templates(this.View.id);

	tmpl.load(
		this.View.template,
		{
			onSuccess : function (res) {
				Taggy.Element.show(Taggy.Player.View.id);
				swfobject.embedSWF(
					Taggy.Player.Detail[Taggy.Player.id].url,
					'player', 280, 230, '9.0.0');
			}
		}
	);
};
Taggy.Player.play = function (id) {
	if (this.playing) {
		var copy = this;
		var elem = document.getElementById(this.View.id);
		elem.innerHTML = 'Loading...';
		setTimeout(
			function () {
				copy.playing = false;
				copy.play(copy.id);
			},
			500
		);
		return;
	}

	var identifier = id || this.id;

	if (!identifier) return;
	if (identifier === null || identifier == 'null') return;
	if (identifier === undefined || identifier == 'undefined') return;

	var playList = document.getElementById(this.List.id);

	for (var i = 0; i < playList.childNodes.length; ++i) {
		var item = playList.childNodes[i];
		if (item.tagName === undefined) continue;
		if (item.id == '') continue;
		if (item.id != identifier) item.className = '';
	}

	var uri = this.location.protocol + '//' + this.location.hostname +
		this.location.pathname + '/' + identifier +
		'?callback=Taggy.Player.viewer&videoprev=true&ref=' + location.hostname;

	this.id = identifier;
	this.save();
	this.playing = this.id;

	Taggy.JSONP(uri);
	Taggy.Cancel();
};
Taggy.Player.request = function (page) {
	if (this.requesting) {
		var copy = this;
		var elem = document.getElementById(this.List.id);
		elem.innerHTML = 'Loading...';
		setTimeout(
			function () {
				copy.requesting = false;
				copy.request(copy.pager.current_page);
			},
			500
		);
		return;
	}

	var request_list = this.getList(page);
	
	if (0 >= request_list.length)
		request_list = this.getList(this.pager.previous_page);

	if (0 >= request_list.length) return;

	var uri = this.location.protocol + '//' + this.location.hostname +
		this.location.pathname + '/' + request_list.join(',') +
		'?callback=Taggy.Player.response&ref=' + location.hostname;

	this.requesting = this.pager.current_page;

	Taggy.JSONP(uri);
	Taggy.Cancel();
};
Taggy.Player.response = function (res) {
	this.requesting = false;
	this.Entries = res.Entry;

	var tmpl = new Taggy.Templates(this.List.id);
	tmpl.load(this.List.template);
};
Taggy.Player.exists = function () {
	return (this.list.length > 0) ? true : false;
};
Taggy.Player.add = function (id) {
	var neo = [id];
	for (var i = 0; i < Taggy.Player.list.length; ++i) {
		var value = Taggy.Player.list[i];
		if (id != value) neo[neo.length] = value;
	}

	if (neo.length > Taggy.Player.limit)
		neo.length = Taggy.Player.limit;

	if (this.list.length == 0) {
		this.play(id);
	}

	this.list = neo;
	this.save(neo);
	this.request();
};
Taggy.Player.del = function (id) {
	var neo = [];
	var current = null;

	for (var i = 0; i < this.list.length; ++i) {
		var value = this.list[i];
		if (id == value)
			current = i;
		else
			neo[neo.length] = value;
	}

	if (id == this.id) {
		this.id = null;
		Taggy.Element.remove('player');
		Taggy.Element.hide('playView');
		var next = (current < this.list.length - 1) ? current +  1 : current - 1;
		if (next >= 0) this.play(this.list[next]);
	}

	this.list = neo;
	this.save(neo);
	this.request(this.pager.current_page);
};
Taggy.Player.up = function (i) {
	var num = i + this.pager.current_page * this.pager.per_page;
	var id = this.list[num];
	var up = this.list[num - 1];

	if (up === undefined) return Taggy.Cancel();

	this.list.splice(num, 1, up);
	this.list.splice(num - 1, 1, id);
	this.request(this.pager.current_page);

	Taggy.Cancel();
};
Taggy.Player.down = function (i) {
	var num = i + this.pager.current_page * this.pager.per_page;
	var id = this.list[num];
	var down = this.list[num + 1];

	if (down === undefined) return Taggy.Cancel();

	this.list.splice(num, 1, down);
	this.list.splice(num + 1, 1, id);
	this.request(this.pager.current_page);

	Taggy.Cancel();
};
Taggy.Player.zoom = function (id) {
	alert('zoom', id);
	Taggy.Cancel();
};

window.addEventListener('load', function () {
		Taggy.Player.request();
		Taggy.Player.play(Taggy.Player.id);
		}, false);
