// Create new script element and start loading.
function ajaxBegin(href)
{
	showAjaxClock();
	ajaxExecute(href);
}

function ajaxEnd() { 
	ajaxCleanup();
	hideAjaxClock(); 
}

function showAjaxClock() {
	if(obj = get("ajax_clock")) obj.style.display="block";
}

function hideAjaxClock() {
	if(obj = get("ajax_clock")) obj.style.display="none";
}

function isset(varname){
//  return(typeof(window[varname])!='undefined');
	return typeof(varname) != 'undefined';
}
function getChildById(parent, child_id) {
	var nodes = parent.getElementsByTagName('div');
	for(var i = 0; i < nodes.length; i++) {
		if (nodes[i].id == child_id)
			return nodes[i];
	}
	return null;	
}

function getFormElement(form, element_id) {
	var els = form.elements;
	for(var i = 0; i < els.length; i++) {
		if (els[i].id == element_id)
			return els[i];
	}
	return null;	
}

/*----------------------------------------
//	POPUP					
----------------------------------------*/
modal_view = false;
modal_up = 0; // Move popup upper on the screen
function scrollRip() {
	if(modal_view) {
		var w = 0;
		var h = 0;    
		var obj = get('create_await_wnd');
		if (!obj) obj = get('complaint_wnd');
		if (!obj) obj = get('move_track_wnd');
		if (!obj) obj = get('rename_wnd');
	       	var client_w = obj.clientWidth;
		var client_h = obj.clientHeight;

		if (window.innerWidth) {
			//Provided by most browsers, but importantly, not Internet Explorer.
			w = window.innerWidth;
			h = window.innerHeight;
			s = pageYOffset;
		} else 	if (document.body.clientWidth) {
			//Provided by many browsers, including Internet Explorer.
			w = document.body.clientWidth;
			h = document.body.clientHeight;
			s = document.body.scrollTop;
		} else if (document.documentElement.clientWidth) {
			//Provided by most DOM browsers, including Internet Explorer.
			w = documentElement.clientWidth;
			h = documentElement.clientHeight;
			s = documentElement.scrollTop;		
		}
		obj.style.left = w/2 - client_w/2;
		obj.style.top = h/2 - client_h/2 + s - modal_up;
	}
}

function wnd_input(name) {
	this.obj = get(name);
	this.obj_title = getChildById(this.obj, "title");
	this.obj_form = this.obj.getElementsByTagName("form")[0];
	var tmp = this.obj.getElementsByTagName("select");
	if ( tmp.length == 0 ) tmp = this.obj.getElementsByTagName("textarea");
	if ( tmp.length == 0 ) tmp = this.obj.getElementsByTagName("input");
	this.obj_text = tmp[0];	
/*
	this.obj = get(name);
	this.obj_title = getChildById(this.obj, "title");
	this.obj_form = this.obj.getElementsByTagName("form")[0];
	var tmp = this.obj.getElementsByTagName("select");
	if ( tmp.length == 0 ) tmp = this.obj.getElementsByTagName("input");
	this.obj_text = tmp[0];	
*/
}

wnd_input.prototype.Title = function(title) {
	if (isset(title) ) { this.obj_title.innerHTML = title; }
	return this.obj_title.innerHTML;
}

wnd_input.prototype.Text = function(text) {
	if (isset(text) ) { this.obj_text.value = text; }
	return this.obj_text.value;
}

wnd_input.prototype.Show = function(title, text) {
	if (isset(title) ) this.Title(title);
	if (isset(text) ) this.Text(text);
	
	var w = 0;
	var h = 0;    
	var client_w = this.obj.clientWidth;
	var client_h = this.obj.clientHeight;

	if (window.innerWidth) {
		//Provided by most browsers, but importantly, not Internet Explorer.
		w = window.innerWidth;
		h = window.innerHeight;
		s = pageYOffset;
	} else 	if (document.body.clientWidth) {
		//Provided by many browsers, including Internet Explorer.
		w = document.body.clientWidth;
		h = document.body.clientHeight;
		s = document.body.scrollTop;
	} else if (document.documentElement.clientWidth) {
		//Provided by most DOM browsers, including Internet Explorer.
		w = documentElement.clientWidth;
		h = documentElement.clientHeight;
		s = documentElement.scrollTop;		
	}
	this.obj.style.left = w/2 - client_w/2;
	this.obj.style.top = h/2 - client_h/2 + s - modal_up;

	screen_disable(true);
	modal_view = true;
	this.obj.style.visibility = 'visible';
	this.obj.focus();
}
	
wnd_input.prototype.Hide = function() {
	screen_disable(false);
	modal_view = false;
	this.obj.style.visibility = 'hidden';
}

wnd_input.prototype.OnSubmit = function(func) {
	var fm = this.obj.getElementsByTagName("form");
	this.obj_form.onsubmit = func;
}

function screen_disable(flag) {
	el = get("disable_full");
	if (1) { 
		el.style.width = document.width ? document.width : document.body.scrollWidth;
		el.style.height = document.height ? document.height : document.body.scrollHeight;
	}

	if(flag) 
		el.style.visibility = 'visible';
	else
		el.style.visibility = 'hidden';

}


/*function TabActivatebyIndex(div_name, ind) {
	var dv = get(div_name);
	if (!dv) return;
	var nodes = dv.getElementsByTagName('table');
	for(var i = 0; i < nodes.length; i++) {
		var cname = "";
		var tdnodes = nodes[i].getElementsByTagName('td');
		if (i == ind) { 
			cname = "tab_active";
		}
		if (i != nodes.length-2) {tdnodes[0].innerHTML+=' |'}
		var anodes = nodes[i].getElementsByTagName('a');
		anodes[0].className = cname;
	}
}*/

function TabActivatebyIndex(div_name, ind) {
	var dv = get(div_name);
	if (!dv) return;
	var anodes = dv.getElementsByTagName('a');
	for(var i = 0; i < anodes.length; i++) {
		var cname = "";
		if (i == ind) { 
			cname = "tab_active";
		}
		anodes[i].className = cname;
	}
}

/*----------------------------------------
//	AJAX FORM POST
//	DON'T USE WITH FILE UPLOAD FORMS					
----------------------------------------*/
function FormPost(fm, add_params) {
	if (!fm) return;
	var els = fm.elements;
	var params = "";
	for(var i = 0; i < els.length; i++) {
		var p_name = "";
		var p_value = "";
		if (els[i].type == "checkbox") {
			if (els[i].checked) {
				p_name = els[i].name;
				p_value = els[i].value;
			}
		}
		else {
			p_name = els[i].name;
			p_value = els[i].value;
		}

		if (p_name.length) {
			if( params.length ) params += "&";
			params += p_name + "=" + ruescape( p_value );
		}
		els[i].disabled = true;
	}
	if (add_params) {
		if(params.length) params += "&";
		params += add_params;
	}
	var act = fm.attributes.getNamedItem("action").nodeValue;
	if (! makePOSTRequest(act, params, FormPostCallback) );
	showAjaxClock();
}

function FormPostCallback() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			eval(http_request.responseText);
		}
	}
}

/*----------------------------------------
//	FILTER
----------------------------------------*/
function tag_filter(str) {
	str = str.replace(/&/g, "&amp;");
	str = str.replace(/</g, "&lt;");
	str = str.replace(/>/g, "&gt;");
	str = str.replace(/#/g, "&#35;");
	str = str.replace(/"/g, "&quot;");
	str = str.replace(/'/g, "&#39;");
	str = str.replace(/%/g, "&#37;");
	return str;
}

function untag_filter(str) {
	str = str.replace(/&#37;/g, "%");
	str = str.replace(/&#39;/g, "'");
	str = str.replace(/&quot;/g, "\"");
	str = str.replace(/&#35;/g, "#");
	str = str.replace(/&gt;/g, ">");
	str = str.replace(/&lt;/g, "<");
	str = str.replace(/&amp;/g, "&");
	return str;
}

/*----------------------------------------
//	SHOW HELP TIPS
----------------------------------------*/
// If NS -- that is, !IE -- then set up for mouse capture
//if (!) document.captureEvents(Event.MOUSEMOVE)
// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;
// Temporary variables to hold mouse x-y pos.s
var MouseX = 0
var MouseY = 0
// Main function to retrieve mouse x-y pos.s
function getMouseXY(e) {
  if (document.all) { // grab the x-y pos.s if browser is IE
    MouseX = event.clientX + document.body.scrollLeft
    MouseY = event.clientY + document.body.scrollTop
  } else {  
    MouseX = e.pageX
    MouseY = e.pageY
  }  
  // catch possible negative values in NS4
  if (MouseX < 0){tempX = 0}
  if (MouseY < 0){tempY = 0}  
}


function IsMouseIn(elem, x, y) {
	if(! isset(x)) x = MouseX;
	if(! isset(y)) y = MouseY;
//	alert (elem.style.position.left + " " + elem.style.left + elem.style.width + " " +
//		elem.style.top + " " + elem.style.top + elem.style.height );
	if (x < elem.offsetLeft ) return false;
	if (x > elem.offsetLeft + elem.clientWidth ) return false;
	if (y < elem.offsetTop ) return false;
	if (y > elem.offsetTop + elem.clientHeight ) return false;
	return true;
}

function TipObject(Tobj) {
	this.obj = Tobj;
	this.help_obj = getChildById(this.obj, "help_tip");
	this.x = MouseX;
	this.y = MouseY;
}

TipObject.prototype.Show = function() {
	if(this.help_obj) {
		this.help_obj.style.left = this.x;
		this.help_obj.style.top = this.y;
		this.help_obj.style.display = "inline";
	}
}

TipObject.prototype.Hide = function() {
	if (IsMouseIn(this.obj)) return false;
	if (IsMouseIn(this.help_obj)) return false;
	this.help_obj.style.display = "none";
	return true;
}

var CurrHelpObj = null;

function ShowHelp(this_el) {
	if (CurrHelpObj) return;
	CurrHelpObj = new TipObject( this_el );
	setTimeout("if(CurrHelpObj) CurrHelpObj.Show()",400);
}

function HideHelp() 
{
	if (CurrHelpObj == null) return;
	setTimeout("if ( CurrHelpObj && CurrHelpObj.Hide() ) CurrHelpObj = null;", 10);
}


/*********************************************
* Friends operations
**********************************************/
var friend_url = "/audio_page/";

function invite_friend( user_id )
{
	var friend_id = user_id;
	friend_invite_wnd.OnSubmit( function() {
		var imsg = ruescape(friend_invite_wnd.Text());
	 	var q = "action" + "=invite_friend";
	 	q += "&page=friends&u=" + user_id;
	 	q += "&user_id=" + user_id;
		q += "&msg=" + imsg;
		q += "&rand=" + Math.random() ;
		if (imsg == "" )
		{
			alert("Напишите текст сообщения");
			return false;
		}else
		{
		ajaxBegin(friend_url + "?ajax=friend_action&" + q );
		friend_invite_wnd.Hide();
		}
		return false;
	});
	friend_invite_wnd.Text( "" );
	friend_invite_wnd.Show();
}

function invite_confirm( user_id )
{
 	var q = "action=invite_confirm";
 	q += "&page=friends&u=" + user_id;
 	q += "&user_id=" + user_id;
	q += "&rand=" + Math.random() ;
	ajaxBegin(friend_url + "?ajax=friend_action&" + q );
}

function invite_decline( user_id )
{
 	var q = "action=invite_decline";
 	q += "&page=friends&u=" + user_id;
 	q += "&user_id=" + user_id;
	q += "&rand=" + Math.random() ;
	ajaxBegin(friend_url + "?ajax=friend_action&" + q );
}

function remove_friend( user_id )
{
	if ( confirm("Вы уверены, что хотите удалить этого пользователя из списка друзей?") )
	{
	 	var q = "action=remove_friend";
	 	q += "&page=friends&u=" + user_id;
 		q += "&user_id=" + user_id;
		q += "&rand=" + Math.random() ;
		ajaxBegin(friend_url + "?ajax=friend_action&" + q );
	}
}

function ArrowHover(obj,hover)
{
	obj.className = (hover) ? 'step_active' : 'step';
	obj.style.cursor = 'pointer' || 'hand';
}
function LArrowHover(obj,hover)
{
	obj.className = (hover) ? 'Tstep_active' : 'Lstep';
	obj.style.cursor = 'pointer' || 'hand';
}
function RArrowHover(obj,hover)
{
	obj.className = (hover) ? 'Tstep_active' : 'Rstep';
	obj.style.cursor = 'pointer' || 'hand';
}
//----------------------------------------------------------
function rank_download_track(trackid)
{
	ajaxExecute("/cat/cat.fexec?mode=ajaxdownloadtrack&type=simple&track=" + trackid);
	return true;
}

function rank_read_text(trackid)
{
	ajaxExecute("/cat/cat.fexec?mode=ajaxlistentrack&type=simple&track=" + trackid);
	return true;
}

function zero_rank_download_track(trackid)
{
	ajaxExecute("/cat/cat.fexec?mode=ajaxdownloadtrack&type=simple&zero=1&track=" + trackid);
	return true;
}

function zero_rank_read_text(trackid)
{
	ajaxExecute("/cat/cat.fexec?mode=ajaxlistentrack&type=simple&zero=1&&track=" + trackid);
	return true;
}
