// Vsyako razno
open_close = function(id) {
	var el = document.getElementById(id);
	if (el) if(el.style.display == 'none'){
		el.style.display = 'block';
	} else {
		el.style.display = 'none';
	}
}
open_tag = function(id) {
	var el = document.getElementById(id);
	if (el) el.style.display = 'block';
}
close_tag = function(id) {
	var el = document.getElementById(id);
	if (el) el.style.display = 'none';
}


// Thanks for Dmitri koterov!
function Dump(d,l) {
    if (l == null) l = 1;
    var s = '';
    if (typeof(d) == "object") {
        s += typeof(d) + " {\n";
        for (var k in d) {
            for (var i=0; i<l; i++) s += "  ";
            s += k+": " + Dump(d[k],l+1);
        }
        for (var i=0; i<l-1; i++) s += "  ";
        s += "}\n"
    } else {
        s += "" + d + "\n";
    }
    return s;
}

mygetPosition = function(o){
	var y;
	var x=y=0;
	while(o){
		x += o.offsetLeft;
		y += o.offsetTop;
		o = o.offsetParent;
	}
	return{x:x, y:y};
};

// Menedjer zagruzok
orange_loading_manager = new cl_loading_manager(1000 * 60 * 10);   // 10 min

// Ispolnenie command servera
run_commands = function(command_list){
	
	//alert(Dump(command_list));
	
	len = command_list.length;
	for (i = 0; i < len; i++){
	
		switch (command_list[i].name){
			
			case 'update_infoblock':
			
				document.getElementById('infoblock_' + command_list[i].data.infoblock).innerHTML = command_list[i].data.content;
			
			break;

			case 'update_block':
			
				document.getElementById(command_list[i].data.block_id).innerHTML = command_list[i].data.content;
			
			break;

			case 'do':
			
				eval(command_list[i].data.script);
			
			break;
			
			case 'apply_css':
			
				var content = command_list[i].data.css;
				
				// Got from IREN :)
				if(document.implementation && document.implementation.createDocument){
					// Mozilla and Opera
					var text_node = document.createTextNode(content);               
					var st_node = document.createElement('style');
					//st_node.id = 'iren_ss_' + name.trim();
					st_node.type = 'text/css';
					st_node.appendChild(text_node);
					document.getElementsByTagName('head')[0].appendChild(st_node);
				} else if(window.ActiveXObject) {
					// IE (this code working in Opera and Mozilla also)
					// from MSDN http://msdn2.microsoft.com/en-us/library/ms533698.aspx
					var ss = document.createElement('style');
					document.documentElement.firstChild.appendChild(ss);
					ss.styleSheet.cssText = content;
				}
			
			break;
			
			case 'print_data':
				alert(Dump(command_list[i].data.data));
			break;
		}//sw
	
	}//for

}// fn run_commands


// AJAX zapros
ajax_request = function(url, indication, caching_time, callback_fn){
	
	if (indication) {
		open_tag('orange_loading');
	}
	
	if(typeof caching_time == 'undefined') {
		caching_time = 0;
	}

	url = url + "&orange_mode=ajax";
	
	// Podgotavlivaem loader
	var loader = new cl_dispatcher(orange_loading_manager, 'post', true, caching_time);
	
	var ors_create = function(callback_fn) { return function(){
		
		if(loader.get_state() == 4){
			
			var resp = loader.get_response()
			var retx = loader.get_response_text()
			
			var error = resp.orange_error;
			if (error != '') {
				if (typeof error != 'undefined'){
					alert(error);
					return false;
				}
			}

			if (indication) {
				close_tag('orange_loading');
			}
			
			if (resp.commands) run_commands(resp.commands);
			
			if (callback_fn) callback_fn();
			
		}
		
	}}
	
	loader.send_request(url, { q: 'ajaxyahhoo' }, ors_create(callback_fn));
}

// old function: replace!
ajax_request_preloading = function(url, caching_time){
	url = url + "&orange_mode=ajax";
	if(typeof caching_time == 'undefined') {
		caching_time = 0/*10 min*/;
	}
	var loader = new cl_dispatcher(orange_loading_manager, 'post', true, caching_time);
	var ors_create = function() { return function(){
		if(loader.get_state() == 4){
			var resp = loader.get_response()
			var retx = loader.get_response_text()
			var error = resp.orange_error;
			if (error != '') {
				if (typeof error != 'undefined'){
					alert(error);
					return false;
				}
			}
		}
	}}
	loader.send_request(url, { q: 'ajaxyahhoo' }, ors_create());
}


// Klassicheskiy zapros
classic_request = function(url){
	document.location = url;
}

request = function(url, indication, caching_time){
	if(config["using_ajax"] == "yes"){
		ajax_request(url, indication, caching_time);
	} else {
		classic_request(url);
	}
}


classic_submit = function(url, form_id, name, value){
  form = document.getElementById(form_id);
	form.action = url;
	form.submit();
}

ajax_submit = function(url, form_id, indication, callback_fn){
	if (indication) {
		open_tag('orange_loading');
	}

	form = document.getElementById(form_id);
	
	url = url + "&orange_mode=ajax";
	
	// Podgotavlivaem loader
	var loader = new cl_dispatcher(orange_loading_manager, 'post', false, 0);
	
	var ors_create = function() { return function(){
		
		if(loader.get_state() == 4){
    
			var resp = loader.get_response()
			var retx = loader.get_response_text()
			
			var error = resp.orange_error;
			if (error != '') {
				if (typeof error != 'undefined'){
					alert(error);
					return false;
				}
			}

			if (indication) {
				close_tag('orange_loading');
			}
			
			if (resp.commands) run_commands(resp.commands);

			if (callback_fn) callback_fn();

		}
		
	}}
	
	loader.send_request(url, form, ors_create());
	
}

fixPNG = function(element)
{
  //Если браузер IE версии 5.5-6
  if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent))
  {
	var src;
	
	if (element.tagName=='IMG') //Если текущий элемент картинка (тэг IMG)
	{
	  if (/\.png$/.test(element.src)) //Если файл картинки имеет расширение PNG
	  {
		src = element.src;
		element.src = "images/blank.gif"; //заменяем изображение прозрачным gif-ом
	  }
	}
	else //иначе, если это не картинка а другой элемент
	{
	  //если у элемента задана фоновая картинка, то присваеваем значение свойства background-шmage переменной src
	  src = element.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i);
	  if (src)
	  {
		src = src[1]; //берем из значения свойства background-шmage только адрес картинки
		element.runtimeStyle.backgroundImage="none"; //убираем фоновое изображение
	  }
	}
	//если, src не пуст, то нужно загрузить изображение с помощью фильтра AlphaImageLoader
	if (src) element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
  }
}//fn


orange_initialization = function(e){
	

	// Yarlichek zagruzka
	document.onmousemove = function(e){
			if (!e) e = event;
			el = document.getElementById('orange_loading');
			if (el){
				el.style.left = e.clientX + 15 + document.documentElement.scrollLeft;
				el.style.top = e.clientY + document.documentElement.scrollTop;
			}
	}

	// vizov funkcii inicializacii infoblockov
	components_initialization();

}