



	// 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;
	}

	function trim( str, charlist ) {	// Strip whitespace (or other characters) from the beginning and end of a string
		// 
		// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
		// +   improved by: mdsjack (http://www.mdsjack.bo.it)
		// +   improved by: Alexander Ermolaev (http://snippets.dzone.com/user/AlexanderErmolaev)
		// +	  input by: Erkekjetter
		// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	
		charlist = !charlist ? ' \s\xA0' : charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
		var re = new RegExp('^[' + charlist + ']+|[' + charlist + ']+$', 'g');
		return str.replace(re, '');
	}

	
	window.show_message_dialog = function(selector){
		
		var el = jQuery(selector); if (!el) return;
		var dialog = jQuery("#shared-dialog");
		
		dialog.html(el.html());
		
		dialog.dialog("option", {
			title: el.attr("title"),
			buttons: {
				OK: function(){
					jQuery(this).dialog("close");
				}
			},
			width: 300,
			height: 150
		});
		dialog.dialog("open");
		
	}

	window.get_free_dialog = function(){
		
		dialog_id = "shared-dialog";
		i = 2;
		while (jQuery("#"+dialog_id).dialog("isOpen") === true){
			//alert(jQuery("#"+dialog_id).dialog("isOpen"));
			dialog_id = "shared-dialog-"+i;
			if (!jQuery("#"+dialog_id).length){
				//alert(dialog_id);
				jQuery("#shared-dialog-container").append('<div id="'+dialog_id+'">123</div>');
				//alert(jQuery("#"+dialog_id).length);
			}
			i++;
		}
		dialog_selector = "#"+dialog_id;
		return jQuery(dialog_selector);
	}


	window.show_dialog = function(selector, options){
		

		var def_options = {
			buttons:{
				Отмена: function(){
					jQuery(this).dialog("close");
				}
			},
			width:600,
			height:250,
			draggable: true,
			resizable: false,
			modal: true,
			autoOpen: false
		}

		if (!selector || (selector=='')){
			selector = "#shared-dialog";
		}
		
		//alert(dialog_selector);
		
		var el = jQuery(selector);
		

		var el_options = {};
		if (el.attr("title")) el_options.title = el.attr("title");
		if (el.attr("width")) el_options.width = el.attr("width");
		if (el.attr("height")) el_options.height = el.attr("height");
		
		var res_options = {};
		jQuery.extend(true, res_options, def_options, el_options, options);
		
		if (options.buttons['Отмена'] == false) delete res_options.buttons['Отмена'];
		
		// alert(Dump(res_options));
		
		var free_dialog = window.get_free_dialog();
		
		
		
		//alert(free_dialog.html());
		
		//alert(jQuery("#shared-dialog").length);
		free_dialog.html(el.html());

		//free_dialog.dialog();
		var dia = free_dialog.dialog(res_options);
		
		free_dialog.dialog("open");
		
		return dia;
		
	}
	
	// RAZMERI KLIENTSKOY CHASTI OKNA BRAUZERA
	window.screenSize = function() {
				var w, h; // Объявляем переменные, w - длина, h - высота
				w = (window.innerWidth ? window.innerWidth : (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.offsetWidth));
				h = (window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.offsetHeight));
				return {w:w, h:h};
	};
	
	
	
	window.start_indication = function(){
		jQuery("body").css("cursor", "wait");		
	};
	
	window.stop_indication = function(){
		jQuery("body").css("cursor", "default");			
	};
	

	
	
	
	
	
	
	

	window.open_popup_admin_item = function(url, name, widthP, heightP){
		if (!widthP) widthP = 80;
		if (!heightP) heightP = 80;
		if (!name) name = "_blank";
	
		var w = jQuery(window).width();
		var h = jQuery(window).height();
	
		var new_w = parseInt(w*widthP/100);
		var new_h = parseInt(h*heightP/100);
	
		var l = parseInt((w - new_w) / 2);
		var t = parseInt((h - new_h) / 2);
		
	
		return window.open(url, name, "width="+new_w+",height="+new_h+",left="+l+",top="+t+",scrollbars=yes");
	
	}


	window.comments_open_form = function(parent_id){
			
		var form_block = jQuery("#block-form");
		if (!parent_id){
			form_block.show();
		} else {
			var ch = jQuery(".block[comment_id="+parent_id+"] > .children");
			var new_form_block = form_block.clone();
			new_form_block.prependTo(ch[0]);
			new_form_block.find("input[name=parent_id]").val(parent_id);
			new_form_block.find("textarea[name=text]").val("");
			new_form_block.attr("id", null);
			new_form_block.show();
		}
		
	}
	
	jQuery.ajaxSetup({
		type: "POST",
		beforeSend: function(){
    	if (window.start_indication) window.start_indication();
   	},
		complete: function(){
    	if (window.stop_indication) window.stop_indication();
   	},
		error: (function(){ return function(XMLHttpRequest, textStatus, errorThrown){
			if (XMLHttpRequest.status == '401'){
				jQuery("body").css("cursor", "default");
				jQuery("#login_dialog").dialog("open");
			} else if (XMLHttpRequest.status == '403'){
				jQuery("body").css("cursor", "default");
				jQuery("#norights_dialog").dialog("open");
			} else {
				jQuery(document.body).append(XMLHttpRequest.responseText);
			}
		}})()
	});
	
	jQuery(".post-comment-form").live("submit", function(){
		
		var formData = jQuery(this).serializeArray();
		var parent_id = jQuery("input[name=parent_id]", this).val();
		var text = jQuery("textarea[name=text]", this).val();
		var formBlock = jQuery(this).parent();
		var post_button = jQuery("input[name=post]", this);
		
		if (text == ""){
			alert("Введите текст сообщения");
			return false;
		}
		
		post_button.attr("disabled", "disabled");
		
		// loading start
		var new_ind = jQuery("#loading-indicator div").clone().css("display", "inline-block");
		formBlock.find("input[name=post]").after(new_ind);
		
		jQuery.ajax({
			url: "/comments/post", 
			data: formData, 
			success: (function(parent_id, formBlock){ return function(data, status, xhr){
				
				var post_button = formBlock.find("input[name=post]");
				post_button.attr("disabled", null);

				// loading end
				formBlock.find(".loading-indicator").remove();
				
				if (data.result == "ok"){
					if(parent_id != "0"){
						jQuery(".block[comment_id="+parent_id+"] > .children").prepend(data.text);
						formBlock.remove();	
					} else {
						jQuery("#new-comment-block").prepend(data.text);
						formBlock.find("textarea[name=text]").val("");
						formBlock.find("input[name=parent_id]").val("0");
						formBlock.hide();	
					}
				}
			}})(parent_id, formBlock),
			dataType: "json"
		});
		return false;
	});


	window.mark_del_comment = function(id, profile){
			
		var com_bl = jQuery(".block[comment_id="+id+"]");

		// loading start
		var new_ind = jQuery("#loading-indicator div").clone().css("display", "inline-block");
		new_ind.insertAfter(com_bl.find("> .ls"));
		
		var ajaxData = {id: id};
		if (profile) ajaxData.profile = profile;
		
		jQuery.ajax({
			url: "/comments/mark_del", 
			data: ajaxData, 
			success: (function(id){ return function(data, status, xhr){
				
				var com_bl = jQuery(".block[comment_id="+id+"]");
				
				// loading end
				com_bl.find(".loading-indicator").remove();
				
				if (data.result == "ok"){
					var ch = com_bl.find("> .children");
					ch.detach();
					com_bl.replaceWith(data.text);
					com_bl = jQuery(".block[comment_id="+id+"]");
					ch.appendTo(com_bl);
				}
			}})(id),
			dataType: "json"
		});
		
	}

	window.unmark_del_comment = function(id, profile){
			
		var com_bl = jQuery(".block[comment_id="+id+"]");

		// loading start
		var new_ind = jQuery("#loading-indicator div").clone().css("display", "inline-block");
		new_ind.insertAfter(com_bl.find("> .ls"));
		
		var ajaxData = {id: id};
		if (profile) ajaxData.profile = profile;
		
		jQuery.ajax({
			url: "/comments/unmark_del", 
			data: ajaxData, 
			success: (function(id){ return function(data, status, xhr){
				
				var com_bl = jQuery(".block[comment_id="+id+"]");
				
				// loading end
				com_bl.find(".loading-indicator").remove();
				
				if (data.result == "ok"){
					var ch = com_bl.find("> .children");
					ch.detach();
					com_bl.replaceWith(data.text);
					com_bl = jQuery(".block[comment_id="+id+"]");
					ch.appendTo(com_bl);
				}
			}})(id),
			dataType: "json"
		});
		
	}

	window.del_comment = function(id, profile){
			
		var com_bl = jQuery(".block[comment_id="+id+"]");

		// loading start
		var new_ind = jQuery("#loading-indicator div").clone().css("display", "inline-block");
		new_ind.insertAfter(com_bl.find("> .ls"));
		
		var ajaxData = {id: id};
		if (profile) ajaxData.profile = profile;
		
		jQuery.ajax({
			url: "/comments/del", 
			data: ajaxData, 
			success: (function(id){ return function(data, status, xhr){
				if (data.result == "ok"){
					jQuery(".block[comment_id="+id+"]").remove();
				}
			}})(id),
			dataType: "json"
		});
		
	}
	
	window.modfeed_mode_blocks = function(){
			
		var last_bl = jQuery(".comments .block").last();
		var last_id = last_bl.attr("comment_id");
		
		// loading start
		var new_ind = jQuery("#loading-indicator div").clone().addClass("in_use_modfeed_more_blocks").css("display", "inline-block");
		new_ind.insertAfter(jQuery(".comments"));
		
		var ajaxData = {id: last_id};
		//if (profile) ajaxData.profile = profile;
		
		jQuery.ajax({
			url: "/comments/modfeed_more_blocks", 
			data: ajaxData, 
			success: (function(last_id){ return function(data, status, xhr){
				
				// loading end
				jQuery(".in_use_modfeed_more_blocks").remove();
				
				if (data.result == "ok"){
					var last_bl = jQuery(".comments .block").last();
					last_bl.after(data.text);
				}
			}})(last_id),
			dataType: "json"
		});
		
		
	}




	// LOCATION SELECT

	window.location_select_second_list = function(id, loc_obj){
		
		// loading start
		var new_ind = jQuery("#loading-indicator-small div").clone().addClass("in_use_modfeed_more_blocks").css("display", "inline-block");
		new_ind.insertAfter(jQuery('.second-list',loc_obj));
		
		var ajaxData = {id: id};
		//if (profile) ajaxData.profile = profile;
		jQuery(".second-list", loc_obj).html("загрузка");
		
		jQuery.ajax({
			url: "/socnet/get_locations_opts", 
			data: ajaxData, 
			success: (function(id, loc_obj){ return function(data, status, xhr){
				
				//alert(status);
				
				// loading end
				jQuery(".in_use_modfeed_more_blocks").remove();
				
				jQuery(".second-list", loc_obj).html(data);
					
			}})(id, loc_obj),
			dataType: "text"
		});

	}	
	
	
	window.live_datepickers = function(){
		jQuery(".datepicker").datepicker();
	}
		
	window.live_buttons = function(){
		jQuery(".butt").button();
	}
		
	window.live_cluetips = function(){
		jQuery(".cluetip[title]").cluetip({
			cluezIndex: 10000,
			clickThrough: true,
			splitTitle: "|",
			showTitle: true,
			waitImage: false,
			width: 210,
			topOffset: -8,
			leftOffset: 8
		});
	}
		

	window.ajax_load_blocks = function(){
		
		var els = jQuery(".ajax-load-block");
		
		
		
		els.each(function(i, el){
			var jel = jQuery(el);
			var url = jQuery(el).attr("url");
			
			//alert(url);
			
			if (url) {
				jQuery.ajax({
					url: url, 
					success: (function(jel){ return function(data, status, xhr){
						//alert(data);
						jel.html(data);
						window.live_buttons();
						
						// update trustlink block position
						window.trustlink_block_position();
		


					}})(jel),
					dataType: "text",
					beforeSend: function(){
						
					},
					complete: function(){
						
					}
				});
			}
				//jQuery(el).load(url);
			
		});
		
	}
		
		
	// TRUSTLINK BLOCK
	
	/*window.trustlink_block_activate = function(){
		
		//jQuery('#main_template_table').resize((function(){ return function(){
		jQuery(document).resize((function(){ return function(){
			window.trustlink_block_position();
		};})());
		
		alert(111);
	}*/
	
	
	window.trustlink_block_position = function(){
		

		// define block
		var block = jQuery('.trustlink-block');
		
		
		// reset block
		block.css('top', '0px');
		
		
		// define offset
		var block_h = block.height();
		var block_y = block.position().top;
		var block_margin_top = parseInt(block.css('margin-top'));
		var block_margin_bottom =  parseInt(block.css('margin-bottom'));
		
		var bottom_bar = jQuery('#main_template_table_bottom_td');
		var bottom_bar_y = bottom_bar.position().top;
		
		var y_offset = bottom_bar_y - block_y - block_h - block_margin_top - block_margin_bottom;
		
		
		// set offset
		block.css('top', ''+y_offset+'px');
		
		//alert(block_margin_top);
		
	};	
		

	window.load_home_lenta = function(){
		
		var url = '/socnet/home_lenta';
		var jel = jQuery('#home-lenta-block');
		
		jQuery.ajax({
			url: url, 
			success: (function(jel){ return function(data, status, xhr){
				//alert(data);
				jel.html(data);
				window.live_buttons();
				
				jQuery("#forum-last-added-lenta").jScrollPane({
					dragMaxHeight:13,
					scrollbarWidth:13
				});
				
				// update trustlink block position
				window.trustlink_block_position();



			}})(jel),
			dataType: "text",
			beforeSend: function(){
				
			},
			complete: function(){
				
			}
		});
		
		
	}


	window.activate_cool_dialog = function(){
		
		var dialog = jQuery('.cool-dialog');
		var boxlay = dialog.find(' > .boxlay');
		var x = boxlay.find(' > div > .box > a.x');
		
      	var height = (window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.offsetHeight));
 		
		boxlay.css("height", height+"px");
		
		x.click(function(){
			var dialog = jQuery(this).parents('.cool-dialog');
			dialog.hide();
		});
		
		
		
	}
		
		

