function Querystring(qs) {
	this.params = new Object()
	this.get=Querystring_get

	if (qs == null)
		qs=location.search.substring(1,location.search.length)

	if (qs.length == 0) return

	qs = qs.replace(/\+/g, ' ')
	var args = qs.split('&')

	for (var i=0;i<args.length;i++) {
		var value;
		var pair = args[i].split('=')
		var name = unescape(pair[0])

		if (pair.length == 2)
			value = unescape(pair[1])
		else
			value = name

		this.params[name] = value
	}
}

function Querystring_get(key, default_) {
	if (default_ == null) default_ = null;

	var value=this.params[key]
	if (value==null) value=default_;

	return value
}

/* выбор картинок */
(function() {
	var selImage;
	var selVideo;
	var selAudio;
	var links1;
	var links2;
	var links3;

	try {
		selImage = document.getElementById('im'+document.getElementById('selImage').value);
		selVideo = document.getElementById('im'+document.getElementById('selVideo').value);
		selAudio = document.getElementById('im'+document.getElementById('selAudio').value);
		links1   = document.getElementById('dir-image').getElementsByTagName('a');
		links2   = document.getElementById('dir-video').getElementsByTagName('a');
		links3   = document.getElementById('dir-audio').getElementsByTagName('a');
	} catch (e) {
		return;
	}

	// adding onclick for images
	for (var i = 0; i < links1.length; i++) {
		var link = links1[i];
		link.onclick = function() {
			if (selImage != this) {
				document.getElementById('selImage').value = this.id.substr(2);
				this.className = "choice";
				this.blur();
				if (selImage) {
					selImage.className = "";
				}
				selImage = this;
			}
			return false;
		}
	}

	// adding onclick for videos
	for (var i = 0; i < links2.length; i++) {
		var link = links2[i];
		link.onclick = function() {
			if (selVideo != this) {
				document.getElementById('selVideo').value = this.id.substr(2);
				this.className = "choice";
				this.blur();
				if (selVideo) {
					selVideo.className = "";
				}
				selVideo = this;
			}
			return false;
		}
	}

	// adding onclick for videos
	for (var i = 0; i < links3.length; i++) {
		var link = links3[i];
		link.onclick = function() {
			if (selAudio != this) {
				document.getElementById('selAudio').value = this.id.substr(2);
				this.className = "choice";
				this.blur();
				if (selAudio) {
					selAudio.className = "";
				}
				selAudio = this;
			}
			return false;
		}
	}

})();

/* переключалка табов */
(function() {
	var qs   = new Querystring();
	var scr  = document.getElementById('tabs');
	if (!scr) return;
	var h2s = scr.getElementsByTagName('li');
	if (!h2s.length) return;
	for (var i = 0; i < h2s.length; i++) {
		var link = h2s[i].getElementsByTagName('a')[0];
		link.onclick = function() {
			var clickName = this.hash.substr(1);
			for (var j = 0; j < h2s.length; j++) {
				var linkT = h2s[j].getElementsByTagName('a')[0].hash.substr(1);
				var imgT  = h2s[j].getElementsByTagName('a')[0].getElementsByTagName('img')[0];
				var divT = null;
				try {
					divT = document.getElementById(linkT);
				} catch(e) {
				}
				if (linkT == clickName) {
					imgT.src = "/fedmms/i/"+linkT+"-a.gif";
					h2s[j].className = "on";
					if (divT) {
						divT.style.display = "block";
					}
				} else {
					imgT.src = "/fedmms/i/"+linkT+".gif";
					h2s[j].className = "off";
					if (divT) {
						divT.style.display = "none";
					}
				}
			}
			if (clickName == "dir-image") {
				document.getElementById("imagetext").style.visibility = "visible";
			} else {
				document.getElementById("imagetext").style.visibility = "hidden";
			}
			document.getElementById('selectedTab').value = clickName;
			this.blur();
			return false;
		}
	}
	for (var i = 0; i < h2s.length; i++) {
		var link = h2s[i].getElementsByTagName('a')[0];
		if (link.hash == '#'+qs.get('tab')) {
			link.onclick();
			break;
		}
	}
})();

/* кнопка preview */
(function() {
	var scr  = document.getElementById('previewButton');
	if (!scr) return;
	scr.onclick = function() {
		document.getElementById('preview').value = '1';
		this.form.submit();
		return true;
	}
})();

/* кнопка send */
(function() {
	var scr  = document.getElementById('sendButton');
	if (!scr) return;
	scr.onclick = function() {
		document.mainsend.submit();
		return false;
	}
})();

/* чекбокс удалить */
(function() {
	var scr  = document.getElementById('deleteLink');
	if (!scr) return;
	scr.onclick = function() {
		document.getElementById('deleteImg').value = '1';
		document.mainsend.submit();
		return false;
	}
})();


function createBaloon(name, type) {
	var baloon = document.createElement('div');
	baloon.setAttribute('id', 'baloonN'+name);
	if (type == true) {
		baloon.className = 'baloon another';
	} else {
		baloon.className = 'baloon';
	}

	var baloonHeader = document.createElement('DIV');
	baloonHeader.className = 'baloon-top';

	var baloonBody   = document.createElement('DIV');
	baloonBody.className = 'baloon-middle';

	var baloonFooter = document.createElement('DIV');
	baloonFooter.className = 'baloon-bottom';

	baloonBody.innerText = 'baloon';

	baloon.appendChild(baloonHeader);
	baloon.appendChild(baloonBody);
	baloon.appendChild(baloonFooter);

	baloon.onselectstart = function(e) { return false; }
	baloon.onclick       = function(e) {
		this.style.display = 'none';
		document.getElementById('w'+name).focus();
	}

	document.body.insertBefore(baloon, document.body.firstChild);
}

function showBaloon(name, text, type) {
	var element;
	var baloon;
	try {
		element = document.getElementById('w'+name);
		baloon  = document.getElementById('baloonN'+name);
	} catch (e) {
		return;
	}
	if (!element) return;
	baloon.childNodes.item(1).innerHTML = "<img src=\"/fedmms/i/baloon/alert.gif\"/>" + text;
	baloon.style.display = 'block';

	var xleft=0;
	var xtop=0;

	i = element;
	o = i;
	do {
		xleft += o.offsetLeft;
		xtop  += o.offsetTop;
	} while (o=o.offsetParent);

	xwidth  = i.clientWidth  ? i.clientWidth  : i.style.pixelWidth;
	xheight = i.clientHeight ? i.clientHeight : i.style.pixelHeight;

	w = window;

	xbody   = document.compatMode=='CSS1Compat' ? w.document.documentElement : w.document.body;
	dwidth  = xbody.clientWidth   ? xbody.clientWidth   : w.innerWidth;
	bwidth  = baloon.clientWidth;
	bheight = baloon.clientHeight;

	if (type == true) {
		baloon.style.top  = xtop + 5 + 'px';
	} else {
		baloon.style.top  = xtop - bheight + 20 + 'px';
	}
	baloon.style.left = xleft + xwidth - 60 + 'px';

	i.onfocus = function(e) { baloon.style.display = 'none'; }
}

/* создание балунов с ошибками */
(function() {
	if (window.noticeE == undefined) return;
	for (var k in noticeE) {
		var html = noticeE[k].join("<br />");
		createBaloon(k, (k == 'captcha'));
		showBaloon(k, html, (k == 'captcha'));
	}
})();

/* валидатор ввода телефона */
function testKey(e) {
	var key = (typeof e.charCode == 'undefined' ? e.keyCode : e.charCode);

//	if (e.ctrlKey || e.altKey || key < 32 || key > 255 || (key >= 48 && key <= 57))
	if (key < 32 || (key >= 48 && key <= 57) || key > 60000)
		return true;
	else
		return false;
}

/* убивалка veil */
(function() {
	var scr  = document.getElementById('close');
	if (!scr) return;

	var body = document.getElementsByTagName("body")[0];
	scr.onclick = function() {
		body.className = "";
		return false;
	}

})();

(function() {
	var sendAnother = document.getElementById('send-another-popup');
	if(sendAnother == null) {
		return;
	}
	var complete = sendAnother.getAttribute('complete');
 
	if(complete !== 'true') {
		window.setTimeout('window.location.reload(true);', 60000);
	}
 
	sendAnother.onclick = function() {
		try {
			window.opener.location = '/';
			window.opener.focus()
		} catch (e) {
			window.open(this.href).focus()
		}
 
		if (this.getAttribute('complete') === 'true') {
			window.close()
		}
 
		return false
	}
})();

// mp3 player
$(function() {
window.getNodeTop = function(node){
	var coords = {top: 0, left: 0}
	while(node.tagName.toLowerCase() != 'body' && node.tagName.toLowerCase() != 'html') {
	coords.top  += node.offsetTop  - node.scrollTop;
	coords.left += node.offsetLeft - node.scrollLeft;

	node = node.offsetParent;
	}

	return coords;
}

window.fnClosePlayer = function() {
	$('#mp3').hide().empty();
};

$('<div id="mp3">mp3</div>').appendTo('body');
$('body #mp3').css('margin-left', '-190px');
$('body #mp3').css('margin-top',  '-20px');

$('div.play').click(function(){
	var mp3 = this;

	$('#mp3').hide().empty().each(function() {
	var so = new SWFObject('/fedmms/i/swf/mp3.swf', 'mp3flash', 194, 167, 6, '#ffffff');
		so.addParam('wmode', 'transparent');
		so.addParam('menu',   'false');

		so.addVariable('sPath',   $(mp3).attr('mp3:link') + ($.browser.msie ? '?' + Math.random() : ''));

		so.addVariable('sTitle',  $(mp3).attr('mp3:title'));
		so.addVariable('sArtist', $(mp3).attr('mp3:author') || '');
		so.addVariable('sType',   $(mp3).attr('mp3:format') || '');
		so.addVariable('sDllnk',  $(mp3).attr('mp3:path'));

		var isWork = so.write(this);

		if(!isWork) {
			return true;
		}

		var coords = getNodeTop(mp3);

		$(this).css({top: coords.top - 40, left: coords.left + 30});
		}).show();

		return false;
	});
});

