// <![CDATA[
var opera = Boolean(window["opera"]);
var ie = (navigator.appName.indexOf("Microsoft") != -1) && !opera;

function mju_play_track(num) {
	if (num <= 0) return false;
	var mc = ie ? window.mju : window.document.mju;
	mc.SetVariable("play_track",num);
}

function mju_play_file(chars) {
	if (!chars.length) return false;
	var mc = ie ? window.mju : window.document.mju;
	mc.SetVariable("play_file",chars);
}

function mju_do(cmd) {
	if (!cmd.length) return false;
	var mc = ie ? window.mju : window.document.mju;
	mc.SetVariable("do_"+cmd," ");
}

function gebi(id) { return document.getElementById(id); }

var DOM = (typeof(document.getElementById) != 'undefined');

function calc_cham() {
	var cham = '';
	var name = 'color';
	if (DOM) {
		elems = document.getElementsByTagName('input');
		for (i = 0; i < elems.length; i++) {
			if (elems[i].className == name){
				if (elems[i].value != '') {
					if (elems[i].id == 'back') { // если фон сайта
						gebi('mju_object').style.background = '#'+gebi(elems[i].id).value;
					} else {
						a = parseInt(gebi(elems[i].id+'_a').value);
						as = '';
						if (a < 0) a = 0; else if (a > 255) a = 255;
						if (a < 255) as = a.toString(16);
						if (as.length == 1) as = '0'+as;
						cham += '&'+(elems[i].id)+'=';
						cham += gebi(elems[i].id).value;
						cham += as;
					}
				}
			}
		}
	}
	if (gebi('invert').checked) cham += '&invert=1';
	return cham;
}

function refresh_colors() {
	var cham = calc_cham();
	mju_swf(cham);
}

function trim( str, charlist ) {   
    charlist = !charlist ? ' \s\xA0' : charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');   
    var re = new RegExp('^[' + charlist + ']+|[' + charlist + ']+$', 'g');   
    return str.replace(re, '');   
} 

function mju_html () {
	var cham = calc_cham();
	var pars = '';
	if (gebi('auto_run').checked) pars += '&auto_run=1'; else pars += '&auto_run=0';
	if (gebi('repeat0').checked) pars += '&repeat=0';
	else if (gebi('repeat1').checked) pars += '&repeat=1';
	else if (gebi('repeat2').checked) pars += '&repeat=2';
	if (gebi('shuffle').checked) pars += '&shuffle=1'; else pars += '&shuffle=0';
	if (gebi('anti_cache').checked) pars += '&anti_cache=1'; else pars += '&anti_cache=0';
	if (gebi('unicode').checked) pars += '&unicode=1'; else pars += '' /*'&unicode=0'*/;
	var width = parseInt(gebi('width').value);
	var height = parseInt(gebi('height').value);
	var url = trim(gebi('url').value);
	if (url == 'http://') url = '';
	while (url.sub(0,1) == '/' && url.length > 1) url = substr(1,url.length-1); // удаляем слеши с начала
	while (url.sub(url.length-1,1) == '/' && url.length > 1) url = substr(0,url.length-1); // удаляем слеши с конца
	var mpath = trim(gebi('mpath').value);
	while (mpath.sub(0,1) == '/' && mpath.length > 1) mpath = substr(1,mpath.length-1); // удаляем слеши с начала
	while (mpath.sub(mpath.length-1,1) == '/' && mpath.length > 1) mpath = substr(0,mpath.length-1); // удаляем слеши с конца
	var mname = trim(gebi('mname').value);
	while (mname.sub(0,1) == '/') mname = substr(1,mname.length-1); // удаляем слеши с начала
	var mfile = mname;
	if (mpath != '') mfile = mpath+'/'+mfile;
	mfile = url+'/'+mfile;
	var ppath = trim(gebi('ppath').value);
	while (ppath.sub(0,1) == '/' && ppath.length > 1) ppath = substr(1,ppath.length-1); // удаляем слеши с начала
	while (ppath.sub(ppath.length-1,1) == '/' && ppath.length > 1) ppath = substr(0,ppath.length-1); // удаляем слеши с конца
	var pname = trim(gebi('pname').value);
	while (pname.sub(0,1) == '/') pname = substr(1,pname.length-1); // удаляем слеши с начала
	var pfile = pname;
	if (ppath != '') pfile = ppath+'/'+pfile;
	pfile = url+'/'+pfile;
	var html = '';
	if (mfile == '') {
		html = 'ВНИМАНИЕ! Неверно заданы параметры файла плеера';
	} else if (pfile == '') {
		html = 'ВНИМАНИЕ! Неверно заданы параметры файла плейлиста';
	} else if (width <= 0 || height <= 0) {
		html = 'ВНИМАНИЕ! Неверно заданы размеры плеера';
	} else html = 
		'<object width="'+width+'" height="'+height+'" id="mju">'+
		'<param name="allowScriptAccess" value="sameDomain" />'+
		'<param name="swLiveConnect" value="true" />'+
		'<param name="movie" value="'+mfile+'" />'+
		'<param name="flashvars" value="playlist='+pfile+pars+cham+'" />'+
		'<param name="loop" value="false" />'+
		'<param name="menu" value="false" />'+
		'<param name="quality" value="high" />'+
		'<param name="wmode" value="transparent" />'+
		'<embed src="'+mfile+'" flashvars="playlist='+pfile+pars+cham+'" loop="false" menu="false" quality="high" wmode="transparent" bgcolor="#000000" width="'+width+'" height="'+height+'" name="mju" allowScriptAccess="sameDomain" swLiveConnect="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />'+
		'</object>';
	return html;
}

function mju_code() {
	var code = gebi('html_code');
	code.value = mju_html();
}

function mju_swf (cham) {
	var so = new SWFObject('/_swf/mju.swf', 'mju', '150', '150', '6', '#000000');
	so.addParam('allowScriptAccess', 'sameDomain');
	so.addParam('swLiveConnect', 'true');
	so.addParam('loop', 'false');
	so.addParam('menu', 'false');
	so.addParam('quality', 'high');
	so.addParam('wmode', 'transparent');
	so.addVariable('playlist', '/_swf/playlist.mpl'+cham);
	so.write('mju_object');
}

function decol (id) {
	if (gebi(id)) {
		gebi(id).value = '';
		gebi(id).style.background = 'none';
	}
	if (gebi(id+'_a')) gebi(id+'_a').value = '';
	return false;
}

var cur_step = 1;
function step(st) {
	if (cur_step == st) return;
	gebi('step'+cur_step).style.display = 'none';
	gebi('steps'+cur_step).className = '';
	cur_step = st;
	gebi('step'+cur_step).style.display = 'block';
	gebi('steps'+cur_step).className = 's';
	if (cur_step == 4) gebi('forcol').style.display = 'block';
	else gebi('forcol').style.display = 'none';
	if (cur_step == 5) {
		gebi('next').style.display = 'none';
	} else {
		switch (cur_step) {
			case 1: gebi('nextt').innerHTML = 'к параметрам'; break;
			case 2: gebi('nextt').innerHTML = 'к настройкам'; break;
			case 3: gebi('nextt').innerHTML = 'разукрашивать плеер'; break;
			case 4: gebi('nextt').innerHTML = 'получить код'; break;
			default: gebi('nextt').innerHTML = '';
		}
		gebi('next').style.display = 'block';
	}
	if (cur_step == 5) {
		mju_code();
		var html = gebi('html_code').value;
		if (html.indexOf('ВНИМАНИЕ!') != -1) { 
			gebi('html_code').style.display = 'none';
			gebi('html_res').innerHTML = html;
		} else { // все нормально
			gebi('html_code').style.display = 'block';
			gebi('html_res').innerHTML = 'Теперь всё готово для прослушивания музыки и подкастов на вашем сайте =)';
		}
	}
}

function steps() {
	for (i = 2;i <= 5;i++) gebi('step'+i).style.display = 'none';
	gebi('steps').style.display = 'block';
	gebi('next').style.display = 'block';
}
// ]]>

