var support_dom = document.createElement;
var is_mac = (navigator.userAgent.toLowerCase().indexOf("mac")!=-1);

function gebi(id){
	return document.getElementById(id)
}

function select_clear(obj) {
	if (support_dom && !is_mac) {
		while (obj.lastChild) obj.removeChild(obj.lastChild);
	} else {
		for (var i=obj.options.length-1; i>=0; i--) obj.options[i]=null;
	}
}

function select_hide(obj, hide) {
	obj.style.display = ''; // IE hack
	if (hide) obj.style.display = 'none';
}

function select_fill(obj, arr) {
	len = arr.length;
	for(var i = 0; i < len; i++) {
		if (!arr[i].id) continue;
		obj.options[i] = new Option(arr[i].t, arr[i].id);
	}
}

function get_idx(arr, needle_id) {
	len = arr.length;
	for (i=0; i<len; i++) {
		if (arr[i].id == needle_id) return i;
	}
	return -1;
}

function tree_expand(obj_id) {
	var obj = gebi(obj_id);
	if (obj.style.display == 'none' || obj.style.display == '') {
		obj.style.display = 'block';
	} else {
		obj.style.display = 'none';
	}
}

function music_toggle_subtr(eid, display) {
	var obj = gebi(eid);
	var inps = obj.getElementsByTagName('TR')
	for(var i = 0; i < inps.length; i++) {
		var obj = inps[i];
		if (obj.getAttribute('eid') == eid) {
			obj.style.display = display ? 'none' : '';
		}
	}
}

function music_toggle_track(obj_ids, $obj) {
	if (!Array.prototype.isPrototypeOf(obj_ids)) {
		 obj_ids = [obj_ids];
	}
	for (var obj_id in obj_ids) {
		var obj = gebi(obj_ids[obj_id]);
		if (obj.style.display == 'none') {
			obj.style.display = '';
			$obj.innerHTML = '<img src="/images/icons/cd_minus.gif" alt="скрыть композиции альбома">';
		} else {
			obj.style.display = 'none';
			$obj.innerHTML = '<img src="/images/icons/cd_plus.gif" alt="показать все композиции альбома">';
		}
	}
}

function music_toggle_track_e(obj_ids, $obj) {
	if (!Array.prototype.isPrototypeOf(obj_ids)) {
		 obj_ids = [obj_ids];
	}
	for (var obj_id in obj_ids) {
		var obj = gebi(obj_ids[obj_id]);
		if ($obj.getAttribute('hi') == 1) {
			$obj.innerHTML = '<img src="/images/icons/cd_plus.gif" alt="показать все композиции альбома">';
			$obj.setAttribute('hi', 0) ;
			music_toggle_subtr(obj_ids[obj_id], true);
		} else {
			$obj.innerHTML = '<img src="/images/icons/cd_minus.gif" alt="скрыть композиции альбома">';
			$obj.setAttribute('hi', 1) ;
			music_toggle_subtr(obj_ids[obj_id], false);
		}
	}
}

function genre_tree_expand(obj_id, e) {
	var obj = gebi(obj_id);
	var has_childs = e.getAttribute('cid');
	if (obj.style.display == 'none' || obj.style.display == '') {
		obj.style.display = 'block';
		e.src = '/images/icons/cd_minus.gif';
	} else {
		obj.style.display = 'none';
		e.src = '/images/icons/cd_plus.gif';
	}
}



var played_track = '';
function preview_track_switch_icons(track) {
	var inps = document.getElementsByTagName('A')
	for(var i = 0; i < inps.length; i++) {
		var obj = inps[i];
		var aid = obj.getAttribute('tid');
		if (aid) {
			if (aid == track) {
				obj.innerHTML = '<img src="/images/swf/player_stop.gif" width=17 height=17>';
			} else {
				obj.innerHTML = '<img src="/images/swf/player_play.gif" width=17 height=17>';
			}
		}
	}
}

function preview_track(el) {
	var obj = gebi('player');
	if (!obj) return;
	
	track = el.getAttribute('tid');
	var html = '<img src="images/d.gif" width=1>';
	if (played_track == track) { // stop
		played_track = '';
	} else { // play
		var html = AC_FL_GetContent(
			'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0',
			'width','1',
			'height','1',
			'src','/images/swf/player.swf?song_url='+track+'&autoplay=true',
			'quality', 'high',
			'pluginspage','http://www.macromedia.com/go/getflashplayer',
			'movie','/images/swf/player.swf?song_url='+track+'&autoplay=true'
		);
		played_track = track;
	}
	obj.innerHTML = html;
	preview_track_switch_icons(played_track);
}
