function ajax(url,params,target,cache,no_loading_info,loading_img,onload_function)
{
    if(!no_loading_info) {
      loading_started(loading_img);
    }
	// 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(target) target.innerHTML = ajax_obj.responseJS.content;
            if(ajax_obj.responseJS.script) {
               if(tt_ie) {
    // IE sux
                  window.setTimeout(ajax_obj.responseJS.script, 0);
               }
               else {
                  eval(ajax_obj.responseJS.script);
               }
               if(onload_function) {
			eval(onload_function);
	       }
            }
            if(!no_loading_info) {
              loading_finished();
            }
        }
    }
    // Prepare request object (automatically choose GET or POST).
    ajax_obj.open('POST', url, true);
    // Send data to backend.
    if(!params)
    	ajax_obj.send( null );
    else
    	ajax_obj.send( params );    
};

function lang_admin(url)
{
	this.url = url;
	
	this._do_action = function(action,module,constant_name,language,value,target)
	{
		var a = new Array();
		if(module) a['module']=module;
		if(action) a['action']=action;
		if(constant_name) a['constant_name']=constant_name;
		if(language) a['lang']=language;
		if(value) a['value']=value;
		ajax(this.url,a,target);
	}
	
	this.edit_constant = function(module,constant_name,language,value,target)
	{
		this._do_action('edit',module,constant_name,language,value,target);
	}
	
	this.add_constant = function(module,constant_name,language,value,target)
	{
		this._do_action('add',module,constant_name,language,value,target);		
	}
	
	this.del_constant = function(module,constant_name,language,target)
	{
		this._do_action('del',module,constant_name,language,null,target);				
	}
	
};

function ch_admin(url,type)
{
    this.url = url;
    this.type = type;

    this.get = function(offset,filter,target)
    {
         var a = new Array();
         a['o']=offset;
         a['filter']=filter;
         a['ch_type']=this.type;
         ajax(this.url,a,target);
    }
};

function admin_by_id(url)
{
    this.url = url;

    this.get = function(id,action,target)
    {
         var a = new Array();
         a['id']=id;
         a['action']=action;
         ajax(this.url,a,target);
    }
};

function loading_started(img_src)
{
	if(!document.getElementById('ajax_loader_div')) return;
	var css = document.getElementById('ajax_loader_div').style;
	if(!img_src) {
		img_src='ajax_loader.gif';
	}
	document.getElementById('ajax_loader_img').src='/media/des/'+img_src;
	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';
};

HIDDEN_SELECTS = new Array();

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};
}

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;
		if(slider.parentNode.lastChild.id && 0 > pos.curleft - tt_GetDivW(slider.parentNode.lastChild) + tt_GetDivW(slider))
		{
			slider.parentNode.lastChild.style.left = pos.curleft - tt_GetDivW(slider.parentNode.lastChild) + tt_GetDivW(slider);
		}
		if(tt_GetClientH()+tt_GetScrollY()<pos.curtop+tt_GetDivH(slider.parentNode.lastChild))
		{
			slider.parentNode.lastChild.style.top = pos.curtop - tt_GetDivH(slider.parentNode.lastChild);
		}
		if(tt_GetClientW()+tt_GetScrollX()<pos.curleft+tt_GetDivW(slider.parentNode.lastChild))
		{
			slider.parentNode.lastChild.style.left = pos.curleft - tt_GetDivW(slider.parentNode.lastChild) + tt_GetDivW(slider);
		}
		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 admin_panel_switch(panel)
{
	var t=document.getElementById('admin_main_menu');
	if(t.style.display=="none")
	{
		t.style.display="block";
		panel.innerHTML='<img src="'+SRVROOT+'media/des/application_side_contract.gif">';
		panel.className="slider_btn";
	}
	else
	{
		t.style.display="none";
		panel.innerHTML='<img src="'+SRVROOT+'media/des/application_side_expand.gif">';
		panel.className="slider_btn_pressed";
	}
	save_in_session({admin_panel_state:t.style.display+''});
};

function save_in_session(params)
{	
	var ajax_obj = new JsHttpRequest();
    ajax_obj.open('GET', '/ajax/session_storage', true);
    ajax_obj.send( params );    
}

function switch_previews(obj,mode,pic_id) {
	if(pic_id==active_pic) {
		return;
	}
	obj.childNodes[1-mode].className='gallery_preview_hidden';
	obj.childNodes[mode].className='gallery_preview_shown';
}

function set_active_pic() {
	old_obj=document.getElementById('pic_div_'+active_pic);
	if(old_obj) {
		old_obj.childNodes[0].className='gallery_preview_hidden';
		old_obj.childNodes[1].className='gallery_preview_shown';
	}
}

function reset_active_pic() {
	if(active_pic) {
		old_obj=document.getElementById('pic_div_'+active_pic);
		if(old_obj) {
			old_obj.childNodes[0].className='gallery_preview_shown';
			old_obj.childNodes[1].className='gallery_preview_hidden';
		}
		active_pic=0;
	}
}

function switch_ig_previews(obj,mode,pic_id) {
	if(pic_id==document.getElementById('ig_active_pic').value) {
		return;
	}
	obj.childNodes[1-mode].className='gallery_ig_preview_hidden';
	obj.childNodes[mode].className='gallery_ig_preview_shown';
}

function set_ig_active_pic() {
	old_obj=document.getElementById('ig_pic_div_'+document.getElementById('ig_active_pic').value);
	if(old_obj) {
		old_obj.childNodes[0].className='gallery_ig_preview_hidden';
		old_obj.childNodes[1].className='gallery_ig_preview_shown';
	}
}

function open_ig_image(url,image_id,obj) {
	if(fade_in_progress) {
		return;
	}
	if(image_id!=document.getElementById('ig_active_pic').value) {
		fade_layer(new Array('inner_gallery_image'),function(){open_ig_image_2(url,image_id,obj)});
	}
}

function open_ig_image_2(url,image_id,obj) {
	loading_started();
	document.getElementById('inner_gallery_outer').style.width=tt_GetClientW();
	document.getElementById('inner_gallery_content').style.width=tt_GetClientW();
	scrollTo(0,0);
	ajax(url,null,document.getElementById('inner_gallery_image'),true,false,'ajax_loaderb.gif');
	if(document.getElementById('ig_active_pic').value) {
		old_obj=document.getElementById('ig_pic_div_'+document.getElementById('ig_active_pic').value);
		if(old_obj) {
			old_obj.childNodes[0].className='gallery_ig_preview_shown';
			old_obj.childNodes[1].className='gallery_ig_preview_hidden';
		}
	}
	document.getElementById('ig_active_pic').value=image_id;
	obj.childNodes[0].className='gallery_ig_preview_hidden';
	obj.childNodes[1].className='gallery_ig_preview_shown';
}
function open_ig_image_3() {
	loading_finished();
	resize_body();
	scrollTo((get_real_page_width()-tt_GetClientW())/2,0);
	real_length=120+parseInt(document.getElementById('inner_gallery_image').offsetHeight);
	vis_height=tt_GetClientH();
	if(real_length>vis_height) {
		document.getElementById('inner_gallery_content').style.height=real_length+'px';
	}
	else {
		document.getElementById('inner_gallery_content').style.height=(document.getElementById('inner_gallery_outer').offsetHeight-120)+'px';
	}
	unfade_layer(new Array('inner_gallery_image'),null);
}

var move_div_obj=null;
var is_moving=false;
function ig_previews_right() {
	if(is_moving) {
		return;
	}
	obj=document.getElementById('inner_gallery_previews');
	n=obj.childNodes.length;
	i=parseInt(document.getElementById('ig_i').value);
	if((i+4)<=n) {
		document.getElementById('inner_gallery_prev').style.cursor='pointer';
		if((i+4)==n) {
			document.getElementById('inner_gallery_next').style.cursor='default';
		}
		i++;
		document.getElementById('ig_i').value=i;
		move_div_obj=obj;
		setTimeout(function() { move_div(-(i-2)*90,-(i-1)*90,true) },5);
	}
}

function ig_previews_right_over() {
	if(is_moving) {
		return;
	}
	obj=document.getElementById('inner_gallery_previews');
	n=obj.childNodes.length;
	i=parseInt(document.getElementById('ig_i').value);
	if((i+4)<=n) {
		document.getElementById('inner_gallery_next').className='prev_next_active';
	}
}

function ig_previews_right_out() {
	document.getElementById('inner_gallery_next').className='prev_next_inactive';
}

function ig_previews_left() {
	if(is_moving) {
		return;
	}
	obj=document.getElementById('inner_gallery_previews');
	n=obj.childNodes.length;
	i=parseInt(document.getElementById('ig_i').value);
	if(i>1) {
		document.getElementById('inner_gallery_next').style.cursor='pointer';
		if(i==2) {
			document.getElementById('inner_gallery_prev').style.cursor='default';
		}
		i--;
		document.getElementById('ig_i').value=i;
		move_div_obj=obj;
		setTimeout(function() { move_div(-i*90,-(i-1)*90,false) },5);
	}
}

function ig_previews_left_over() {
	if(is_moving) {
		return;
	}
	obj=document.getElementById('inner_gallery_previews');
	n=obj.childNodes.length;
	i=parseInt(document.getElementById('ig_i').value);
	if(i>1) {
		document.getElementById('inner_gallery_prev').className='prev_next_active';
	}
}

function ig_previews_left_out() {
	document.getElementById('inner_gallery_prev').className='prev_next_inactive';
}

function move_div(position,limit,to_right) {
	is_moving=true;
	if(to_right) {
		step=-10;
	}
	else {
		step=10;
	}
	if((to_right&&position>limit)||(!to_right&&position<limit)) {
		position+=step;
		move_div_obj.style.left=position;
		setTimeout(function() { move_div(position,limit,to_right) },50);
	}
	else {
		move_div_obj.style.left=limit;
		is_moving=false;
	}
}

function load_page(page_url,a_obj) {
	for(i=0;i<a_obj.parentNode.parentNode.childNodes.length;i++) {
		if(a_obj.parentNode.parentNode.childNodes[i].className=='top_menu_item_right') {
			a_obj.parentNode.parentNode.childNodes[i].childNodes[0].className='top_menu_link';
		}
	}
	a_obj.className='top_menu_link_active';
	reset_active_pic();
	ajax(page_url,null,document.getElementById('content'),true);
}

function submit_contact_form(url,form_obj,caption) {
	document.getElementById('form_send_status').innerHTML=caption;
	a=new Array();
	for(i=0;i<form_obj.length;i++) {
		a[form_obj[i].name]=form_obj[i].value;
	}
	ajax(url,a,null,false);
	return false;
}

function get_real_page_width() {
	return (document.body.scrollWidth > document.body.offsetWidth)?document.body.scrollWidth:document.body.offsetWidth;
}

function get_real_page_height() {
	return (document.body.scrollHeight > document.body.offsetHeight)?document.body.scrollHeight:document.body.offsetHeight;
}

var project_gallery_url;
function open_project_gallery(url) {
	real_width=get_real_page_width();
	real_height=get_real_page_height();
	if(real_height<800)
		real_height=800;
	window.scroll(0,0);
	project_gallery_url=url;
	obj=document.createElement('DIV');
	obj.id='inner_gallery_outer';
	obj.className='inner_gallery_outer';
	obj.style.width=real_width+'px';
	obj.style.height=real_height+'px';
	obj.style.filter='progid:DXImageTransform.Microsoft.Alpha(opacity=75)';
	if(tt_ie) {
		obj.attachEvent('onclick',close_inner_gallery);
	}
	else {
		obj.addEventListener('click',close_inner_gallery,false);
	}
	document.body.insertBefore(obj,document.getElementById('first_div'));
	fade_objs=new Array(document.getElementById('inner_gallery_outer'));
	make_fade(0,false,function(){open_project_gallery_2()},75);
}

function open_project_gallery_2() {
	client_width=get_real_page_width();
	client_height=get_real_page_height();
	obj=document.createElement('DIV');
	obj.id='inner_gallery_content';
	obj.className='inner_gallery_content';
	obj.style.width=client_width+'px';
	obj.style.height=(client_height-120)+'px';
	document.body.insertBefore(obj,document.getElementById('first_div'));
	fade_objs=new Array(document.getElementById('inner_gallery_content'));
	make_fade(0,false,function(){open_project_gallery_done()});
}

var shrink_div_width=0;
var shrink_div_height=0;
var shrink_div_aspect=0;
var limit_x=0;
var limit_y=0;
function shrink_div() {
	if(!limit_x||!limit_y) {
		limit_x=get_real_page_width();
		limit_y=get_real_page_height();
	}
	client_width=limit_x;
	client_height=limit_y;
	if(shrink_div_aspect==0) {
		shrink_div_aspect=client_width/client_height;
	}
	obj=document.getElementById('inner_gallery_outer');
	need_iteration=false;
	if(shrink_div_width<client_width) {
		shrink_div_width+=1+shrink_div_width/11;
		if(shrink_div_width>client_width) {
			shrink_div_width=client_width;
		}
		need_iteration=true;
	}
	if(shrink_div_height<client_height) {
		shrink_div_height+=1+(shrink_div_width/shrink_div_aspect)/11;
		if(shrink_div_height>client_height) {
			shrink_div_height=client_height;
		}
		need_iteration=true;
	}
	if(need_iteration) {
		setTimeout('shrink_div()',5);
		obj.style.left=(client_width-shrink_div_width)/2+'px';
		obj.style.top=(client_height-shrink_div_height)/2+'px';
		obj.style.width=shrink_div_width+'px';
		obj.style.height=shrink_div_height+'px';
	}
	else {
		obj=document.createElement('DIV');
		obj.id='inner_gallery_content';
		obj.className='inner_gallery_content';
		obj.style.width=client_width+'px';
		obj.style.height=(client_height-120)+'px';
		document.body.insertBefore(obj,document.getElementById('first_div'));
		fade_objs=new Array(document.getElementById('inner_gallery_content'));
		make_fade(0,false,function(){open_project_gallery_done()});
	}
}

function open_project_gallery_done() {
	if(project_gallery_url.length) {
		ajax(project_gallery_url,null,document.getElementById('inner_gallery_content'),true,false,'ajax_loaderb.gif');
	}
}

var fade_objs=new Array();
function fade_layer(div_ids,return_func) {
	fade_objs=new Array();
	for(i=0;i<div_ids.length;i++) {
		if(document.getElementById(div_ids[i])) {
			fade_objs.push(document.getElementById(div_ids[i]));
		}
	}
	make_fade(100,true,return_func);
}

function unfade_layer(div_ids,return_func) {
	fade_objs=new Array();
	for(i=0;i<div_ids.length;i++) {
		if(document.getElementById(div_ids[i])) {
			fade_objs.push(document.getElementById(div_ids[i]));
		}
	}
	make_fade(0,false,return_func);
}

var fade_in_progress=false;
function make_fade(transp,is_out,return_func,limit) {
	fade_in_progress=true;
	if(limit==null) {
		if(is_out) {
			limit=0;
		}
		else {
			limit=100;
		}
	}
	for(i=0;i<fade_objs.length;i++) {
		fade_objs[i].style.opacity=transp/100;
		fade_objs[i].style.filter='progid:DXImageTransform.Microsoft.Alpha(opacity='+transp+')';
	}
	if(is_out) {
		step_sign=-1;
	}
	else {
		step_sign=1;
	}
	if(transp<80) {
		transp+=10*step_sign;
	}
	else {
		transp+=5*step_sign;
	}
	if((transp>limit&&is_out)||(transp<=limit&&!is_out)) {
		setTimeout(function() { make_fade(transp,is_out,return_func,limit); },5);
	}
	else {
		if(return_func) {
			setTimeout(return_func);
		}
		if(transp>=100) {
			for(i=0;i<fade_objs.length;i++) {
				fade_objs[i].style.filter='';
			}
		}
		fade_in_progress=false;
	}
}

function close_inner_gallery() {
	if(fade_in_progress) {
		return;
	}
	scrollTo(0,0);
	fade_objs=new Array(document.getElementById('inner_gallery_outer'),document.getElementById('inner_gallery_content'));
	make_fade(75,true,function(){remove_inner_gallery_divs()});
}

function remove_inner_gallery_divs() {
	document.body.removeChild(document.getElementById('inner_gallery_content'));
	document.body.removeChild(document.getElementById('inner_gallery_outer'));
	shrink_div_width=0;
	shrink_div_height=0;
	shrink_div_aspect=0;
	limit_x=0;
	limit_y=0;
}

function resize_body() {
	obj=document.getElementById('inner_gallery_outer');
	if(obj) {
		obj.style.width=get_real_page_width()+'px';
	}
	obj=document.getElementById('inner_gallery_content');
	if(obj) {
		obj.style.width=get_real_page_width()+'px';
		obj.style.height=get_real_page_height()+'px';
	}
}

function add_to_bookmark(a) {
	if(window.opera&&window.print) {
		var mbm=a;
		mbm.setAttribute("rel","sidebar");
		mbm.setAttribute("title",document.title);
		mbm.setAttribute("href",window.location.href);
		mbm.click();
	}
	else if(window.sidebar) {
		window.sidebar.addPanel(document.title,window.location.href,"");
	}
	else if(window.external) {
		window.external.AddFavorite(window.location.href, document.title);
	}
}
