$(document).ready( function() {
$('body').addClass($.browser.msie ? "ie" : "");



$('.ie .button, .ie .ajax_button').css('display','inline-block');

var i=0;
var pth;
var uid;
pth = document.location.href;
uid = pth.substr(pth.indexOf('uid=') + 4);
//alert(uid);
if (uid.indexOf('&')>0) {
	uid = uid.substr(0,uid.indexOf('&'));
}
uid = uid.match(/\d+/);

$('div.item div.open_close').each( function() {
	$(this).attr('id',uid+'_'+i);
	if (!$.cookie(uid+'_'+i)) {
		if ($(this).parent('div.item').is('.close')) {
			$.cookie(uid+'_'+i,'true',{expires: 3});
		} else {
			$.cookie(uid+'_'+i,null);
		}
	} else {
		$(this).parent('div.item').addClass('close');
		$.cookie(uid+'_'+i,'true',{expires: 3});
	}
	i++;
});

var item;

	/* новая функция открытия/скрытия блоков лежит в шаблоне в main_header
	$('div.item div.open_close').click( function() {
		item = $(this).parents('div.item');
		if (item.is('.close')) {
			item.removeClass('close');
			if ($.browser.msie && $.browser.version == 6) {
			   item.find('.item_content').css('height','100%');
			} else {
				item.find('.item_content').animate({height:'100%'},100);
			}
			$cook =  $.cookie($(this).attr('id'));
			$cook = !$cook ? 'true' : null;
			$.cookie($(this).attr('id'), $cook,{expires: 3});
		} else {
			if ($.browser.msie && $.browser.version == 6) {
			   item.find('.item_content').css('height','0');
			   item.addClass('close');
			} else {
				item.find('.item_content').animate({height:'0'},100, function() {
					item.addClass('close');
				});
			}
			$cook =  $.cookie($(this).attr('id'));
			$cook = !$cook ? 'true' : null;
			$.cookie($(this).attr('id'), $cook,{expires: 3});
		}
	});*/

	$('ul.char_list_pid li.lvl1').addClass('close');
	$('ul.char_list_pid .open_close_chars').click( function() {
		item = $(this).parents('ul.char_list_pid li.lvl1');
		if (item.is('.close')) {
			item.removeClass('close');
			item.find('.unit.char').animate({height:'100%'},100);
		} else {
			item.find('.unit.char').animate({height:'0'},100, function() {
				item.addClass('close');
			});
		}
	return false;
	});
	$('a.closebutton').addClass('close');
	$('ul.closelist').addClass('close');
	$('a.closebutton').click( function() {
		item = $(this).next('ul.closelist');
		if ($(this).is('.close')) {
			$(this).removeClass('close');
			item.animate({height:'100%'},100, function() {
				item.removeClass('close');
			});
		} else {
			$(this).addClass('close');
			item.animate({height:'0'},100, function() {
				item.addClass('close');
			});
		}
	return false;
	});
	$('.avatar_img a.mini_edit').css('opacity','0');
	$('#userpic, .avatar_img .mini_edit').hover( function() {
		$('.avatar_img a.mini_edit').show().css({'opacity':'1'});
	}, function() {
		$('.avatar_img a.mini_edit').hide().css({'opacity':'0'});
	});

	function StatusMessageEditor () {
		this.value = '';
		this.edit_mode = false;

		this.startEdit = function() {
			$('#statusmessage_event').attr('checked', 'checked');
			$('.status_edit').hide();
			$('.statusmessage').show();
			$('.statusmessage input.statusmessage_input').focus();
			this.edit_mode = true;
		};
		this.stopEdit = function(success) {
			if (!success) {
				$('.statusmessage_input').val(this.value);
			} else {
				this.value = $('.statusmessage_input').val();
			}
			$('.statusmessage').hide();
			this.setPlaceHolder($('.statusmessage_input').val());
			$('.status_edit').show();
			this.edit_mode = false;
		};
		this.setPlaceHolder = function(html) {
			if (html == '') {
				html = 'изменить статус';
			} else {
				html = html.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
			}
			$('.status_edit').html(html);
		};
		this.recalcLength = function() {
			var count = 255 - $('.statusmessage_input').val().length;
			if (count < 0) {
				$('#statusmessage_count').html('<span class="toolong">Превышен лимит, пожалуйста сократите на '+Math.abs(count)+' символов</span>');
			} else {
				$('#statusmessage_count').html('Осталось '+count+' символов');
			}
		};
		this.isChanged = function() {
			return this.value != $('.statusmessage_input').val();
		};
		this.isEdit = function() {
			return this.edit_mode;
		};
		this.init = function() {
			this.value = $('.statusmessage input.statusmessage_input').val();
			this.setPlaceHolder(this.value);
			var _this = this;
			$('.status_edit').click(function(){_this.startEdit();});
			$('.statusmessage_input').bind("keypress keyup change focus", function(){_this.recalcLength();});
			$('.statusmessage_input').keypress(function (evt) {
				if (evt.keyCode == 13) {
					$('div.statusmessage .ajax_button').click();
					return false;
				}
			});
			$('#status_message .js_cancel').bind('click', function() {
				status_message_editor.stopEdit(false);
			});
			$(document).bind('click', function(e) {
				if (_this.isEdit() && !_this.isChanged() && ($(e.target).closest('#status_message').length == 0)) {
					if (!status_message_editor.isChanged()) {
						status_message_editor.stopEdit(false);
					}
				}
			});
		};
		this.init();
	}

	if ($('.statusmessage_input').length > 0) {
		window.status_message_editor = new StatusMessageEditor();
	}

	if ($('#shortprofile_form').length > 0 ) {
		$(document).keypress(function (evt) {
			if (evt.keyCode == 13 && ($('#shortprofile_form input.input_focus').length > 0)) {
				$('.shortprofile_edit').click();
			}
		});
	}


var flg1;
	$('select.select_to_anchor').each( function() {
		$(this).css('display','none');
		$(this).after('<a class="select_to_anchor" href="#">'+$(this).find('option:selected').html()+'</a>');
		flg1 = false;
	});
	$('a.select_to_anchor').click( function() {
		flg1 = true;
		$(this).css('display','none');
		$(this).parent().find('select.select_to_anchor').css('display','');
		return false;
	});
	$('.ajax_button.privacy_edit, .ajax_button.charslist_edit').click( function() {
		$('select.select_to_anchor').each( function() {
			$(this).parent().find('a.select_to_anchor').html($(this).find('option:selected').html());
		});
		$('select.select_to_anchor').css('display','none');
		$('a.select_to_anchor').css('display','');
	});

	//$('.frends_button .ajax_button').click();
	$('.decortable tr:not(.th):odd').addClass('odd');
	$('.decortable tr.decor_tr').hover( function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});


});


append_listener = function () {
	var mlflag
	var botton_position
	var button_id
	var cont
	var page
	var param
	var url
	var replace
	var scroll = function() {
		if (mlflag) {
			return true
		}
		if (!botton_position) {
			return true
		}
		if (page <= 1) {
			return true
		}
		h = parseInt($(window).height())
		t = parseInt($(window).scrollTop())
		if (h + t + 300 > botton_position.top) {
			mlflag = true
			$('#' + button_id).click()
		}
	}
	var click = function() {
		$(this).css({'background': 'url("/img/skin/loading.gif") no-repeat scroll center 30px transparent','height':'45px'})
		data = {}
		data['ajax'] = 'Y'
		data['page'] = page+1
		if (typeof param == 'object') {
			for (i in param) {
				data[i] = param[i]
			}
		}
		$.ajax({
			  url:url
			, data: data
			, success: function(data) {
				$('#' + button_id).css({'background':'none','height':'auto'})
				if (replace) {
					cont.html(data)
				} else {
					cont.append(data)
				}
				mlflag = false
			}
		})
	}
	this.update = function(opt) {
		if (opt['hide']) {
			botton_position = null
			$('#' + button_id).remove()
			$(window).unbind('scroll')
		} else if (opt['show']) {
			botton_position = getElementPosition(button_id)
		}
		page = opt['page']
	}
	this.init = function(opt) {
		button_id = opt['button_id']
		cont = opt['cont']
		url = opt['url'] || location.pathname
		replace = !!opt['replace']
		page = typeof opt['page'] == 'undefined' ? 1 : opt['page']
		if (typeof opt['scroll'] == 'undefined' || opt['scroll'] === true) {
			$(window).bind('scroll', scroll)
		}
		$('#' + button_id).bind('click', click)
		if (typeof opt['param'] == 'object') {
			param = opt['param']
		}
	}
}

/**
 * Постраничная навигация для блока в колонке
 * @param block_id
 * @param source
 * @param params
 * @returns {SidebarBlockPager}
 */
function SidebarBlockPager(block_id, source, params) {
	this.block_id = block_id;
	this.source = source;
	this.params = params;
	this.pages = [];
	this.pages_count = 1000000;
	this.page = 0;
	this.pages[this.page] = $('#' + this.block_id + '_sidebar .js_content').html();

	this.showPage = function(page) {
		var _this = this;
		if (this.pages[page] == null) {
			var params = this.params;
			params['page'] = page;
			gc.ajax.handle(this.source, params, function (result) {
				if (result['data']['page'] != '') {
					_this.pages[page] = result['data']['page'];
					$('#' + _this.block_id + '_sidebar .js_content').html(_this.pages[page]);
					_this.page = page;
					if (!result['data']['more']) {
						_this.pages_count = _this.pages.length;
					}
					_this.buttonState();
				}
			});
		} else {
			$('#' + this.block_id + '_sidebar .js_content').html(this.pages[page]);
			this.page = page;
			this.buttonState();
		}
	};
	this.next = function() {
		if (this.page < (this.pages_count - 1)) {
			this.showPage(this.page + 1);
		}
	};
	this.prev = function() {
		if (this.page > 0) {
			this.showPage(this.page - 1);
		}
	};
	this.buttonState = function() {
		if (this.page == (this.pages_count - 1)) {
			$('#next_button_' + this.block_id).addClass("disabled").removeClass("enabled");
		} else {
			$('#next_button_' + this.block_id).addClass("enabled").removeClass("disabled");
		}
		if (this.page == 0) {
			$('#back_button_' + this.block_id).addClass("disabled").removeClass("enabled");
		} else {
			$('#back_button_' + this.block_id).addClass("enabled").removeClass("disabled");
		}
	};
}

