var oMain = {};
oMain.profile = {};

$(document).ready(function () {

	oMain.ctrl.init();

});

oMain.ctrl = {
	authname : '',
	user_id : -1,
	city : 'msk',
	msgsNewCount : 0,
	msgsLastCheck : '',
	logined : false,
	init : function () {
		$("#isearch").focus(function () {
			_gaq.push(['_trackEvent', 'search', 'focus']);
			this.value='';
			});
		$("#acity").click(function () {
			_gaq.push(['_trackEvent', 'city', 'toggle']);
			$("#scity").toggle();
			return false;
			});
		$("#scity a").click(function () {
			_gaq.push(['_trackEvent', 'city', 'change', $(this).text()]);
			});
		if($.cookie('msgc')){ oMain.ctrl.msgsNewCount = $.cookie('msgc');} 
		oMain.ctrl.chkLogin();
		$('#logout').live('click',oMain.ctrl.logout);
		oMain.ctrl.chkMsg();
		setInterval(oMain.ctrl.chkMsg, 300000);
		setInterval(oMain.ctrl.animateMsg, 5000);
		
		$('.best_announce:first').addClass('active');
		$('.best_announce').mouseover(function () {
						$('.best_announce').removeClass('active');
						$(this).addClass('active');
						_gaq.push(['_trackEvent','best', 'mouseover', $(this).find('.big_ann_title a').text()]);
						});
		$('.best_announce a').click(function () {
			_gaq.push(['_trackEvent','best', 'go', $(this).closest('.best_announce').find('.big_ann_title a').text()]);
			});

		$('.best_announces_mini > dd').mouseover(function () {
						$('.best_announces_mini  > dd').removeClass('sactive');
						$(this).addClass('sactive');
						_gaq.push(['_trackEvent','best_mini', 'mouseover', $(this).find('a span').text()]);
			});
		$('.best_announces_mini > dd a').click(function () {
			_gaq.push(['_trackEvent', 'best_mini', 'go', $(this).find('span').text()]);
			});


			
		},
	chkMsg : function () {
			if(oMain.ctrl.logined){
				var mltc = $.cookie('msg'),
				now = new Date();
				if(mltc !== '') {oMain.ctrl.msgsLastCheck = new Date(mltc);}
				else {oMain.ctrl.msgsLastCheck = now - 11*60000;}
				if((now - oMain.ctrl.msgsLastCheck) > 10*60000) {
					oMain.ctrl.recheckMsg();}
			}
		},
	animateMsg : function () {
			if(oMain.ctrl.msgsNewCount > 0){
				$('#amess').animate({opacity: 0.1}, 'slow', function () {
					$('#amess').animate({opacity: 1}, 'slow');
				});
			}
		},
	recheckMsg : function () {
			$.ajax({
				type: "GET",
				url: "/ajax/msg.html",
				data: { 'mode' : 'check' },
				dataType: "text",
				success: function(msgs){
					if(msgs != 'error'){
						oMain.ctrl.msgsNewCount = msgs;
						var hTxt = '<a id="aprofile" href="/user/profile/">'+ oMain.ctrl.authname + '</a><a id="amess" ';
						if(oMain.ctrl.msgsNewCount > 0 ){
							hTxt += 'class="new_mail" ';
							oMain.ctrl.animateMsg();
						}
						hTxt += 'href="/user/profile/messages/">Сообщений: ' + oMain.ctrl.msgsNewCount + '</a><a id="logout" href="/?auth.logout=do">Выйти</a>';
						$("#login").html(hTxt);
						$.cookie('msg', new Date(),{path: '/'});
						$.cookie('msgc', oMain.ctrl.msgsNewCount,{path: '/'});
					}
				}
			});
		},
	chkLogin : function () {
		var cookie = $.cookie('cookie');
		if(cookie == 'on'){
			oMain.ctrl.authname = $.cookie('auth.name');
			oMain.ctrl.user_id = $.cookie('user_id');
			var city = $.cookie('city');
			if(city){ oMain.ctrl.city = city; }
			if(window.location.pathname == "/" && oMain.ctrl.city != "msk"){
				window.location = "/" + oMain.ctrl.city + "/"; 
			}
			if(oMain.ctrl.user_id && oMain.ctrl.user_id > 0) {
				_gaq.push(['_setVar','registered']);
				oMain.ctrl.logined = true;
				var hTxt = '<a id="aprofile" href="/user/profile/">'+ oMain.ctrl.authname + '</a><a id="amess" ';
				if(oMain.ctrl.msgsNewCount > 0 ){
					hTxt += 'class="new_mail" ';
					oMain.ctrl.animateMsg();
				}
				hTxt += 'href="/user/profile/messages/">Сообщений: ' + oMain.ctrl.msgsNewCount + '</a><a id="logout" href="/?auth.logout=do">Выйти</a>';
				$("#login").html(hTxt);
			}
			else { oMain.ctrl.doForm(oMain.ctrl.authname); }
			}
		else { oMain.ctrl.doForm(oMain.ctrl.authname); }
		},
	doForm : function(authname){
		if(!authname){ authname = ''; }
			$("#login").html('<a id="alogin" href="/login/">Войти</a><a href="/registration/">Зарегистрироваться</a>' +
						'<div id="logon"><form id="lform" method="post" action="."><input type="hidden" name="auth.logon" value="do" />' +
						'Логин:<br /><input type="text" name="auth.name" value="' + authname +'" /><br />' +
						'Пароль:<br /><input type="password" name="auth.passwd" description="Пароль" /><br />' +
						'<button name="action">Войти</button></form><a href="/login/forgot/">Напомнить пароль</a><p class="error"></p></div>');
			$("#lform input").keydown(function(event){
						if(event.keyCode == 13){ $("#lform").trigger("submit"); }
						});
			$("#alogin").click(function () {
						$("#logon").toggle();
						return false;
						});
			$("#logon > form").submit(function () {
						var loginData = $("#logon > form").serialize();
						oMain.ctrl.logon(loginData);
						return false;
						});	
		},
	logout : function(event){
		event.preventDefault();
		_gaq.push(['_trackEvent','auth', 'logout']);
		$.ajax({
			type: "GET",
			url: "/ajax/logon.html",
			data: { 'auth.logout' : 'do' },
			dataType: "json",
			error: function(errObj, errMsg, errTh){
				_gaq.push(['_trackEvent','auth', 'error']);
				$("#logon > p").html(errMsg);
				},
			success: function(userdata){
				if(!userdata || userdata[0].user_id == -1){
					oMain.ctrl.doForm(oMain.ctrl.authname);
					oMain.ctrl.logined = false;
					oMain.ctrl.user_id = -1;
					window.location = "/" + oMain.ctrl.city + "/";
					}
				}
			});
		},
	logon : function(loginData){
		_gaq.push(['_trackEvent','auth', 'login']);
		$.ajax({
			type: "GET",
			url: "/ajax/logon.html",
			data: loginData,
			dataType: "json",
			error: function(errObj, errMsg, errTh){
				_gaq.push(['_trackEvent','auth', 'error']);
				$("#logon > p").html(errMsg);
				},
			success: function(userdata){
				if(!userdata || userdata[0].user_id == -1){
					_gaq.push(['_trackEvent','auth', 'wrong']);
					$("#logon > p").html('Ошибка авторизации!');
					}
				else {
					_gaq.push(['_trackEvent','auth', 'success']);
					oMain.ctrl.logined = true;
					oMain.ctrl.authname = userdata[0].username;
					oMain.ctrl.user_id = userdata[0].user_id;
					window.location = '/user/profile/';
					}
				}
			});	
	}
	
	};

