var moving = false;

function finished() {
	moving = false;
}

function starting() {
	moving = true;
}

function LoadCategories(className) {
	var a = [];
	a = document.getElementsByClassName(className);
	for (var i = 0, j = a.length; i < j; i++) {
		$(a[i]).firstDescendant().next().hide();
		$(a[i]).firstDescendant().isclosed = true;
		attach_pos_funcs($(a[i]).firstDescendant(), $(a[i]).firstDescendant().next());
	}
}

function attach_pos_funcs(title, content) {
	title.onclick = function() {
		if (!title.isclosed && content.visible() && !moving) {
			title.isclosed = true;
			//title.className = "title closed";
			content.visualEffect('blind_up',{duration:.5, beforeStart:function() {
				starting();
				// hide flash
				title.next().childElements().each(function(s) {
					s.firstDescendant().next().className = 'flash_hidden';
				});
			}, afterFinish:function() {
				finished();
			}});
		} else if (!(content.visible()) && !moving){
			title.isclosed = false;
			//title.className = "title open";
			content.visualEffect('blind_down',{duration:.5, beforeStart:function() {
				starting();
			}, afterFinish:function() {
				finished();
				// show flash
				title.next().childElements().each(function(s) {
					s.firstDescendant().next().className = '';
				});
			}});
		}
	};
}

var stopping_others = false;

function stop_others(obj) {
	if (!stopping_others) {
		stopping_others = true;
	    if (obj.state != 'false') {
		    $$('div.talent_category').each(function(s) {
		        s.childElements()[1].childElements().each(function(t) {
		            p = t.childElements()[1].childElements()[0];
		            if (p.sendEvent) {
						if (p.id != obj.id) {
							if (p.getConfig().state == 'PLAYING') {
								p.sendEvent('PLAY');
							}
		                }
		            }
		        });
		    });
	    }
		stopping_others = false;
	}
}

function playerReady(obj) {
	$(obj['id']).addControllerListener('ITEM', 'stop_others');
	$(obj['id']).addControllerListener('PLAY', 'stop_others');
}
