n = navigator.userAgent.toLowerCase(),
tt_u = "undefined";
tt_op = (document.defaultView && typeof(eval("w" + "indow" + "." + "o" + "p" + "er" + "a")) != tt_u);
tt_ie = n.indexOf("msie") != -1 && document.all && !tt_op;

function ajax(url,params,target,cache,onload_function)
{
    loading_started();
	// Create new JsHttpRequest object.
	var ajax_obj = new JsHttpRequest();
	if(!cache) cache=false;
	else cache = true;
	ajax_obj.caching = cache;

    // Code automatically called on load finishing.
    ajax_obj.onreadystatechange = function() {
        if (ajax_obj.readyState == 4) {
            // Write result to page element (_RESULT becomes responseJS). 
            if(ajax_obj.responseJS.script) {
               if(tt_ie) {
    // IE sux
                  window.setTimeout(ajax_obj.responseJS.script, 0);
               }
               else {
                  eval(ajax_obj.responseJS.script);
               }
            }
            if(target&&ajax_obj.responseJS.no_load==0) target.innerHTML = ajax_obj.responseJS.content;
            loading_finished();
	    if(onload_function)
	    	eval(onload_function);
        }
    }
    if(!params) {
        params=new Array();
    }
    params['ajax']=1;
    // Prepare request object (automatically choose GET or POST).
    ajax_obj.open('POST', url, true);
    // Send data to backend.
    ajax_obj.send( params );    
}

function skip_loading() {
	skip_loading=1;
}

function loading_started()
{
	if(!document.getElementById('ajax_loader_div')) return;
	var css = document.getElementById('ajax_loader_div').style;
	var x = tt_GetScrollX();
	var y = tt_GetScrollY();
	x = tt_Int(tt_GetClientW() - 34) / 2 + x;
	y = tt_Int(tt_GetClientH() - 22) / 2 + y;
	css.left = x + "px";
	css.top = y + "px";
	css.display='block';
};

function loading_finished()
{
	if(!document.getElementById('ajax_loader_div')) return;
	var css = document.getElementById('ajax_loader_div').style;
	css.display='none';
};

function evaluate_slider(slider)
{
	var tmp=slider.parentNode.lastChild.style.display;
	ff.forElement('div','slider',function(elm){
		elm.style.display="none";
	});
	ff.forElement('div','slider_btn_pressed',function(elm){
		elm.className="slider_btn";
	});
	show_hidden_selects();
	if(tmp!='block') 
	{
		slider.parentNode.lastChild.style.display='block'; 
		var pos=findPos(slider);
		slider.parentNode.lastChild.style.left = slider.parentNode.lastChild.style.top = null;
		slider.className="slider_btn_pressed";
		hide_cross_selects(slider.parentNode.lastChild);
	}
	return false;
};

function hide_cross_selects(obj)
{
	if(!tt_ie56) return;
	var elements = document.getElementsByTagName('select');
	for(i=0;i<elements.length;i++)
	{
		elements[i].style.visibility='hidden';
	}
};

function show_hidden_selects()
{
	if(!tt_ie56) return;
	var elements = document.getElementsByTagName('select');
	for(i=0;i<elements.length;i++)
	{
		elements[i].style.visibility='visible';
	}
};

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return {'curleft':curleft,'curtop':curtop};
}

var cursor_position_in_text=0;
var first_open=true;

function smiles_slider_open(url_start,obj) {
	first_open=true;
	cursor_position_in_text=getCaretPos(document.getElementById('message_text'));
	ajax(url_start+'/35/',null,obj,true);
}

function insert_smile(smile) {
	if(!first_open)
		cursor_position_in_text=getCaretPos(document.getElementById('message_text'));
	else
		first_open=false;
	document.getElementById('message_text').value=document.getElementById('message_text').value.substring(0,cursor_position_in_text)+smile+' '+document.getElementById('message_text').value.substring(cursor_position_in_text);
	setCaretPos(document.getElementById('message_text'),cursor_position_in_text+smile.length+1);
}

function getCaretPos(obj)
{
	obj.focus();   
	if(obj.selectionStart||tt_op) return obj.selectionStart;//Gecko   
	else if (document.selection)//IE   
	{   
		var sel = document.selection.createRange();   
		var clone = sel.duplicate();   
		sel.collapse(true);   
		clone.moveToElementText(obj);   
		clone.setEndPoint('EndToEnd', sel);   
		return clone.text.length;   
	}   
	return 0;   
}

function setCaretPos(obj,position) { 
	if(obj.setSelectionRange) {
		obj.focus();
		obj.setSelectionRange(selStart,selEnd);
	}
	else if(obj.createTextRange) {
		var range=obj.createTextRange();
		range.move("character", position);
		range.select();  
	}
}

function switch_visibility(object_id) {
	if(obj=document.getElementById(object_id)) {
		if(obj.style.display=='none')
			obj.style.display='block';
		else if(obj.style.display=='block')
			obj.style.display='none';
	}
}

function cancel_mouse(ev) {
	if (ev.stopPropagation)
		ev.stopPropagation();
	else
		ev.cancelBubble=true;
}

var photo_opened=0;

function photo_over(obj,has_big,title) {
	if(photo_opened) {
		photo_big_hide(document.getElementById(photo_opened+'_div'));
	}
	if(tmp_obj=document.getElementById(obj.id+'_div')) {
		tmp_obj.style.display='block';
		tmp_obj.style.zIndex=1000;
		photo_opened=obj.id;
		return;
	}
	tmp_obj=document.createElement('DIV');
	tmp_obj.className='photo_big';
	tmp_obj.style.left=getLeft(obj)+'px';
	tmp_obj.style.top=getTop(obj)+'px';
	if(has_big==0) {
		src=obj.src;
		tmp_obj.style.width='140px';
	}
	else {
		src=obj.src.replace(/\.[a-z]{3}$/,'_a.'+obj.src.substring(obj.src.search(/\.[a-z]{3}$/)+1));
		tmp_obj.style.width='340px';
	}
	tmp_obj.innerHTML='<div style="margin: 10px 10px 10px 10px;"><img src="'+src+'" alt="'+obj.alt+'" onLoad="document.getElementById(\''+obj.id+'_div\').style.cursor=\'default\'"/><br/>'+title+'</div>';
	tmp_obj.id=obj.id+'_div';
	tmp_obj.onmouseout=function(){photo_big_hide(tmp_obj);}
	tmp_obj.style.cursor='wait';
	document.body.insertBefore(tmp_obj,document.getElementById('main'));
	photo_opened=obj.id;
}


function photo_out(obj) {
}

function photo_big_hide(obj) {
	obj.style.display='none';
	obj.style.zIndex=-1;
	photo_opened=0;
}

function getLeft(o)
{
	var r=o.offsetLeft;
	while(o=o.offsetParent)
		r+=o.offsetLeft;
	return r;
}

function getTop(o)
{
	var r=o.offsetTop;
	while(o=o.offsetParent)
		r+=o.offsetTop;
	return r;
}

function is_key_to_send(e) {
	if(e.ctrlKey) {
		if(e.keyCode==13||e.keyCode==10)
			return 2;
	}
	return 1;
}

var popups_timeouts=new Array();

function show_popup(obj,url,width,tmout) {
	if(url!=null) {
		div_id='popup_'+url.replace('/','0x47');
	}
	else {
		do {
			div_id='popup_'+Math.round(10000*Math.random());
		}
		while(document.getElementById(div_id));
	}
	if(popups_timeouts[div_id]) {
		clearTimeout(popups_timeouts[div_id]);
		popups_timeouts[div_id]=null;
	}
	if(tmp_obj=document.getElementById(div_id)) {
		tmp_obj.style.left=getLeft(obj)+'px';
		tmp_obj.style.top=getTop(obj)+obj.offsetHeight+5+'px';
		tmp_obj.style.display='block';
		tmp_obj.style.zIndex=1000;
		return;
	}
	tmp_obj=document.createElement('DIV');
	tmp_obj.className='photo_big';
	tmp_obj.style.left=getLeft(obj)+'px';
	tmp_obj.style.top=getTop(obj)+obj.offsetHeight+5+'px';
	tmp_obj.style.width=width+'px';
	tmp_obj.innerHTML='';
	tmp_obj.id=div_id;
	obj.onmouseout=function(){hide_popup(tmp_obj,tmout);}
	tmp_obj.style.cursor='wait';
	document.body.insertBefore(tmp_obj,document.getElementById('main'));
	if(url!=null) {
		ajax(url,null,tmp_obj,true,'end_wait("'+tmp_obj.id+'")');
	}
	return tmp_obj;
}

function hide_popup(obj,tmout) {
	if(popups_timeouts[obj.id]==null) {
		popups_timeouts[obj.id]=window.setTimeout(function() { hide_popup_end(obj,false); },tmout);
	}
}

function hide_popup_end(obj,no_check_timeouts) {
	if(popups_timeouts[obj.id]||no_check_timeouts) {
		obj.style.display='none';
		obj.style.zIndex=-1;
		popups_timeouts[obj.id]=null;
	}
}

function end_wait(id) {
	document.getElementById(id).style.cursor='default';
}

function show_photo(obj,url,width,tmout) {
	tmp_obj=show_popup(obj,null,width,tmout);
	tmp_obj.innerHTML='<img src="'+url+'" alt="" onLoad="end_wait(\''+tmp_obj.id+'\')"/>';
}

var delayed_starts=Array();

function delayed_start(func,handle,tmout) {
	if(!delayed_starts[handle]) {
		delayed_starts[handle]=window.setTimeout(func,tmout);
		delayed_starts[handle]=null;
	}
}

function delayed_start_cancel(handle) {
	clearTimeout(delayed_starts[handle]);
	delayed_starts[handle]=null;
}

function set_photo_rating(url,photo_id,rating) {
	ajax(url+"/81/i"+rating+"?ui="+photo_id,null,document.getElementById('photo_rating_links_'+photo_id),false);
}

