var ie  = !!document.uniqueID;
var ie6 = navigator.appVersion.match(/MSIE (5.5|6)/) && ie;
var audio;
var current;

var timeout = 0;

if (ie6) {
	XMLHttpRequest = function () {
		return (new ActiveXObject("Msxml2.XMLHTTP")) || (new ActiveXObject("Microsoft.XMLHTTP")) || false;
	};
}

var preloadImgs = function () {
	var i = arguments.length;
	while (i-- > 0) {
		(new Image()).src = '/images/' + arguments[i];
	}
};

var showNav = function () {
	var ul = this.parentNode.getElementsByTagName('UL')[0];
	var navs = this.parentNode.parentNode.getElementsByTagName('UL');
	var i = navs.length;
	while (i-- > 0) {
		if (navs[i] != ul) {
			navs[i].style.visibility = 'hidden';
		}
	}
	ul.style.visibility = 'visible';
	clearTimeout(timeout);
};

var hideNav = function () {
	var ul = this.parentNode.getElementsByTagName('UL')[0];
	timeout = setTimeout((function () {
		ul.style.visibility = 'hidden';
	}), 400);
};

var initNav = function () {
	var nav = document.getElementById('nav');
	if (nav) {
		var navs = nav.getElementsByTagName('A');
		var ul, i = navs.length;
		while (i-- > 0) {
			if (navs[i].parentNode.className && (ul = navs[i].parentNode.getElementsByTagName('UL')[0])) {
				navs[i].onmouseover = ul.onmouseover = showNav;
				navs[i].onmouseout  = ul.onmouseout  = hideNav;
			}
			if (ie && navs[i].parentNode.className) {
				navs[i].parentNode.style.backgroundPosition = '50% 11px';
				navs[i].innerText = '';
			}
		}
	}
};

var HttpRequest = function () {
	try {
		var link = new XMLHttpRequest();
	} catch (e) {
		alert('Ваш браузер не может корректно выполнить запрошенное действие.');
	}

	this.simple = function (path, userFunc) {
		link.onreadystatechange = function () {
			if (link.readyState == 4 && link.status == 200) {
				if (userFunc) {
					userFunc(link);
				}
			}
		};
		link.open("GET", path, true);
		if (ie6) {
			link.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
		}
		link.send(null);
	};
};

var getRingtones = function (direction) {
	var num = direction == 'next' ? 10 : 0;
	(new HttpRequest).simple('/scripts/httpreq.php?ringtones=' + num, analyzeRingtones);
	return false;
};

var analyzeRingtones = function (link) {
	var ul = document.getElementById('ringtones');
	ul.innerHTML = link.responseText;
	var a = ul.parentNode.getElementsByTagName('A');
	var i = a.length;
	while (i-- > 0) {
		if (a[i].onclick) {
			if (a[i].className.indexOf('next') != -1) {
				a[i].onclick = function () { return getRingtones('prev'); };
				a[i].className = a[i].className.replace('next', 'prev');
				a[i].innerHTML = 'Предыдущие 10';
			} else {
				a[i].onclick = function () { return getRingtones('next'); };
				a[i].className = a[i].className.replace('prev', 'next');
				a[i].innerHTML = 'Следующие 10';
			}
			break;
		}
	}
};

var openFull = function (image, width, height) {
	width += 18;
	height += 18;
	var left       = screen.width / 2 - width / 2;
	var top        = screen.height / 2 - height / 2;
	window.open('/display.php?' + image, '_blank', 'status=no,dependent=yes,modal=yes,height=' + height + ',width=' + width + ',left=' + left + ',top=' + top);
	return false;
};

var clearSearch = function (input) {
	if (input.value == input.defaultValue) {
		input.value = '';
	}
};

var checkSearch = function (input) {
	if (!input.value) {
		input.value = input.defaultValue;
	}
}; 

window.onload = function () {
	initNav();
};

function listen(){
		window.open('/mplayer.php?filename='+'http://striiming.trio.ee/narodnoe.m3u', 'new','width=408,height=181, toolbar=0, resizable=0, status=0, location=0, menubar=0,top=200,left=400');
	};

function OpenPopupStreamPlayer(itemId) {
if (current)
	current.close();    
audio = window.open('/streamplayer.php?id=' + itemId, 'audio', 'resizable=no,width=390,height=195');
 
    if (audio) {
        audio.focus();
current = audio;
    } else {
        alert('Для прослушивания Вам необходимо отключить блокирование всплывающих окон для этого сайта');
    }
    
    return false;
};


function OpenPopupWmPlayer(itemId) {
if (current)
	current.close();    
audio = window.open('/wmplayer.php?id=' + itemId, 'audio', 'resizable=no,width=390,height=195');
 
    if (audio) {
        audio.focus();
current = audio;
    } else {
        alert('Для прослушивания Вам необходимо отключить блокирование всплывающих окон для этого сайта');
    }
    
    return false;
};

function OpenPopupWmPlayerLow(itemId) {
if (current)
	current.close();    
audio = window.open('/wmplayer64.php?id=' + itemId, 'audio', 'resizable=no,width=390,height=195');
 
    if (audio) {
        audio.focus();
current = audio;
    } else {
        alert('Для прослушивания Вам необходимо отключить блокирование всплывающих окон для этого сайта');
    }
    
    return false;
};


function OpenPopupAudioPlayer(itemId) {
if (current)
	current.close();    
audio = window.open('/audioplayer.php?id=' + itemId, 'audio', 'resizable=no,width=390,height=195');
 
    if (audio) {
        audio.focus();
current = audio;
    } else {
        alert('Для прослушивания Вам необходимо отключить блокирование всплывающих окон для этого сайта');
    }
    
    return false;
};
