Cabinet = {
	editName: function(){
		var value = $("#consultant_name").val();
		oPopup.show({
			width: 440,
			items: [{type:'text', label:'Псевдоним консультанта:', name:'name'}],
			buttons: [{text:'Сохранить', handler: function(){
				var t = this.popup;
				var data = t.getData();
				if(data.name!=""){
					$("#consultant_name").html(data.name);
					$.post("/cabinet/form/save-name", data, function(data){
						
					}, "json");
				}
				t.close();
				return false;
			}}]
		});
	},
	orders: function(oid){
		$.post('/cabinet/index/ajax-neworders/',{id:oid},function(data){
			if(data.cnt > 0){
				$('.c_account').show();
				$('.neworder').attr('current', '0');
				$('.neworder').attr('countnew', data.cnt);
				$('.newordera').html(data.orders);
				$('.newcnt').html(data.cnt);
				$('.newcntdeclension').html(Kolizeo.declension(data.cnt, ['заказ', 'заказа', 'заказов']));
				if(data.cnt > 1){
					$('.nextnew').addClass('r_a').removeClass('r_t');
				}else{
					$('.nextnew').addClass('r_t').removeClass('r_a');
				}
				$('.prevnew').addClass('l_t').removeClass('l_a');
			}else{
				$('.c_account').hide();
			}
		},'json')
	}
}

Period = {
	dates: {1: null, 2: null},
	showed: false,
	monthNames: {1:"Январь", 2:"Февраль", 3:"Март", 4:"Апрель", 5:"Май", 6:"Июнь", 7:"Июль", 8:"Август", 9:"Сентябрь", 10:"Октябрь", 11:"Ноябрь", 12:"Декабрь"},
	monthLength: {1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31},
	buildMonth: function(n){
		var t = this;
		year = t.dates[n].year; month = t.dates[n].month; day = t.dates[n].day;
		var days = "";
		var d = new Date(year, month-1, 1);
		var wday = d.getDay();
		if(wday==0) wday = 7;
		for(var i=1; i< wday; i++) days += '<div class="day empty"> </div>';
		for(var i=1; i<=this.monthLength[month]; i++) days += '<div class="day'+(this.dates[n].day==i ? ' selected':'')+'" onclick="Period.setDay(this, '+n+', '+i+')">' + i + '</div>';
		return '<table><tr><td width="45" style="text-align:right"><img src="/images/calend_left.gif" onclick="Period.prevYear('+n+')" class="ctrl"></td><td width="80" style="text-align:center" class="choose">'+year+'</td><td width="45"><img src="/images/calend_right.gif" onclick="Period.nextYear('+n+')" class="ctrl"></td></tr></table><table><tr><td width="45" style="text-align:right"><img src="/images/calend_left.gif" onclick="Period.prev('+n+')" class="ctrl"></td><td width="80" style="text-align:center" class="choose">'+this.monthNames[month]+'</td><td width="45"><img src="/images/calend_right.gif" onclick="Period.next('+n+')" class="ctrl"></td></tr></table><div class="days">'+days+'</div>';
	},
	show: function(){
		if(this.showed) this.close();
		var t = this;
		/* Календарь*/
		var currentTime = new Date();
		var month = currentTime.getMonth() + 1;
		var day = currentTime.getDate();
		var year = currentTime.getFullYear();
		//alert ('хzzz==.year:'+year+', month:'+month+', day:'+day);
		if(!t.dates[1]) t.dates[1] = {year:year, month:month, day:day};
		if(!t.dates[2]) t.dates[2] = {year : 2010, month:month+1, day: day};
		var html =  '<div class="fcalend"><div class="calend_x" onclick="Period.close()"> </div><div class="fcalend_inner"><span style="font-size:12px;font-weight:bold">ВЫБЕРИТЕ НАЧАЛЬНЫЙ И КОНЕЧНЫЙ ПЕРИОД</span> <div class="dots f0 mp10" style="padding-bottom:4px"></div><table><tr><td width="170" id="period_1">' + t.buildMonth(1) + '</td><td width="170" id="period_2">' + t.buildMonth(2) + '</td></tr></table></div></div>';
		var p = $(".ajax-catalog").position();
		$(html).appendTo("body").css({left: p.left + 268, top: p.top - 100}).show();
		this.showed = true;
		this.clicked = {1: false, 2: false};
	},

        show_in_stat: function(){
		if(this.showed) this.close();
		var t = this;
                    current_date = new Date();
                    cur_y = current_date.getFullYear();
                    cur_m = current_date.getMonth() + 1;
                    cur_d = current_date.getDate();
                        if(!t.dates[1]) t.dates[1] = {year : cur_y, month: cur_m, day: 1};
                        if(!t.dates[2]) t.dates[2] = {year : cur_y, month: cur_m, day: cur_d};
                        var html =  '<div class="fcalend"><div class="calend_x" onclick="Period.close_in_stat()"> </div><div class="fcalend_inner"><span style="font-size:12px;font-weight:bold">ВЫБЕРИТЕ НАЧАЛЬНЫЙ И КОНЕЧНЫЙ ПЕРИОД</span> <div class="dots f0 mp10" style="padding-bottom:4px"></div><table><tr><td width="170" id="period_1">' + t.buildMonth(1) + '</td><td width="170" id="period_2">' + t.buildMonth(2) + '</td></tr></table></div></div>';
                        var p = $(".ajax-catalog").position();
                        $(html).appendTo("body").css({left: p.left + 268, top: p.top - 100}).show();
                        this.showed = true;
                        this.clicked = {1: false, 2: false};
	},

        	close_in_stat: function(){
		$(".fcalend").remove();
		this.showed = false;
		$("#filter_period_caption").html(this.formatPeriod());
		var d1 = this.dates[1]; var d2 = this.dates[2];
		Catalog.setPeriod_in_stat(Format.sqlDate(d1.year, d1.month, d1.day), Format.sqlDate(d2.year, d2.month, d2.day));
	},

	next: function(n){
		var d = this.dates[n];
		d.month = d.month + 1;
		if(d.month == 13){
			d.month = 1; d.year++;
		}
		this.dates[n] = d;
		$("#period_" + n).html(this.buildMonth(n));
	},
	prev: function(n){
		var d = this.dates[n];
		d.month = d.month - 1;
		if(d.month == 0){
			d.month = 12; d.year--;
		}
		this.dates[n] = d;
		$("#period_" + n).html(this.buildMonth(n));
	},
	nextYear: function(n){
		this.dates[n].year++;
		$("#period_" + n).html(this.buildMonth(n));
	},
	prevYear: function(n){
		this.dates[n].year--;
		$("#period_" + n).html(this.buildMonth(n));
	},
	setDay: function(obj, n, day){
		var j = $(obj);
		j.parents(".days").find("div.day").removeClass("selected");
		j.addClass("selected");
		this.dates[n].day = day;
		this.clicked[n] = true;
		if(this.clicked[1] && this.clicked[2]) this.close();
	},
	close: function(){
		$(".fcalend").remove();
		this.showed = false;
		$("#filter_period_caption").html(this.formatPeriod());
		var d1 = this.dates[1]; var d2 = this.dates[2];
		Catalog.setPeriod(Format.sqlDate(d1.year, d1.month, d1.day), Format.sqlDate(d2.year, d2.month, d2.day));
	},
	formatPeriod: function(){
		var d1 = this.dates[1]; var d2 = this.dates[2];
		var sFrom = Format.d2(d1.day) + "." + Format.d2(d1.month) + (d1.year != d2.year ? ("." + d1.year) : "");
		var sTo = Format.d2(d2.day) + "." + Format.d2(d2.month) + "." + d2.year;
		return sFrom + " - " + sTo;
	}
	
}
///////// Удалить заказ
function oDeleteOders(item)
{
	$.post('/profile/orders/ajax-orders/',{item:item}, function(data){});
	//return item;
}
/////////// Всплывающее окно для удаления заказа

function oPopOders(item,obj)
{
	
	oPopup.show({
		html:'<div class="f12" style="text-align:center">Вы, хотите отменить заказ? </div>',
		buttons:[ {text:'Да', handler:function(){
			oDeleteOders(item);
			$(obj).parents('tr').find('.unorders').html('<span class="pr">Отменен</span>');
		
			oPopup.closeAll();
			return false;
		}}, {text:'Нет', width:80, handler: function(){
			oPopup.closeAll();
			return false;
			}
			} ]
	});
}


Orders = {
	popup: null,
	setCount: function(i){
		$("#orders_count").html(i);
	},
	order: function(id){
		var t = this;
		t.popup = oPopup.show({ width: 534, html: '<div style="text-align:center;padding:50px;height:300px"><img src="/images/ajax64.gif" /></div>' });
		$.post("/cabinet/orders/order/id/" + id, {}, function(data){
			t.popup.jContainer.find("form").html(data);
		});
		return false;
	},
        history: function(order_type, order_id,item_id){
		var t = this;
                var url ='';

        switch (order_type)
        {
          case 'sms_chat':   url = "/cabinet/orders/history/id/" + order_id + "/sms_chat_session_id/" + item_id;
                            break;
          case 'chat': url = "/cabinet/orders/history/id/" + order_id + "/chat_session_id/" + item_id;
                            break;
           default: url ='';
        }
		t.popup = oPopup.show({ width: 534, html: '<div style="text-align:center;padding:50px;height:300px"><img src="/images/ajax64.gif" /></div>' });
		$.post(url, {}, function(data){
			t.popup.jContainer.find("form").html(data);
		});
		return false;
	},
	chat: function(id, item_id){
		var item_id = item_id||id;
		var t = this;
		t.popup = oPopup.show({ width: 534, html: '<div style="text-align:center;padding:50px;height:300px"><img src="/images/ajax64.gif" /></div>' });
		$.post("/cabinet/orders/order/id/"+ id +"/chat_session_id/" + item_id, {}, function(data){
			t.popup.jContainer.find("form").html(data);
		});
		return false;
	},
	sms_chat: function(id, item_id){
		var item_id = item_id||id;
		var t = this;
		t.popup = oPopup.show({ width: 534, html: '<div style="text-align:center;padding:50px;height:300px"><img src="/images/ajax64.gif" /></div>' });
		$.post("/cabinet/orders/order/id/"+ id +"/sms_chat_session_id/" + item_id, {}, function(data){
			t.popup.jContainer.find("form").html(data);
		});
		return false;
	},
	showClient: function(id){
		var t = this;
		t.popup = oPopup.show({ width: 532, html: '<div style="text-align:center;padding:50px;height:300px"><img src="/images/ajax64.gif" /></div>' });
		$.post("/cabinet/orders/ajaxshowclient/id/" + id, {}, function(data){
			t.popup.jContainer.find("form").html(data);
		});
		return false;
	},
	myorder: function(id){
		var t = this;
		t.popup = oPopup.show({ width: 752, html: '<div style="text-align:center;padding:50px;height:300px"><img src="/images/ajax64.gif" /></div>' });
		$.post("/profile/orders/order/id/" + id, {}, function(data){
			t.popup.jContainer.find("form").html(data);
		});
		return false;
	},
	saveComment: function(order_id){
		var comment = $("#order_comment").val();
		$.post("/cabinet/orders/save-comment", {order: order_id, comment: comment}, function(data){
			if(data.success){
				Catalog.reload();
			}
		}, "json");
		this.popup.close();
		return false;
	},
	closeOrder: function(order_id){
		$.post("/cabinet/orders/ajax_close_order", {order: order_id}, function(data){
			if(data.success){
				Catalog.reload();
			}
		}, "json");
		this.popup.close();
		return false;
	},
	filterClient: function(client_id){
		var url = Kolizeo.url({ profile_id: client_id }, Catalog.url);
		Catalog.load(url);
		return false;
	},
	
	press_enter: function(e)
	{
		if(!e){
			e = window.event;
			}
			if (e.keyCode == 13)
			{
				Orders.filterClient2();
				}
		},
	
	filterClient2: function(){
		var find_user_name = $("#find_client_orders").val();
                var url ='/cabinet/orders/ajax-handler/find_user_name/' + find_user_name;
                Catalog.load(url);
		return false;
	},
	
	resetFilterClient: function(){
	var url ='/cabinet/orders/ajax-handler/profile_id/0';
		Catalog.load(url);
		return false;
	},
	
	filterConsultant: function(consultant_id){
		var url = Kolizeo.url({'consultant_id': consultant_id}, Catalog.url);
		Catalog.load(url);
		return false;
	},
	resetFilterConsultant: function(){
		var url = Kolizeo.url({}, Catalog.url, "consultant_id");
		Catalog.load(url);
		return false;
	},
	showFace: function(img_url){
                oPopup.showNoCloseAll({
			html: '<table width="100%"><tr><td align="center"><img align="center" src='+img_url+' height="318" /></td></tr></table>',
			buttons:[ {text:'Закрыть окно', width: 122} ], 
			z_index: 10000,
			saveBlock: true
		});
                
		return false;
	}

}
Akts = {
		changeDate: function(){
		var month = $("#stat_month").val();
		var year = $("#stat_year").val();
		//alert(order);
		this.hide();
		$.post("/cabinet/acts/ajax-handler", {month: month, year: year}, function(data){
			$("#month_akts_block").html(data).css("height", "auto");
		});
	},
	submitAct: function(){
		
		$.post("/cabinet/acts/data", {
				consultant_id: $("#consultant_id").val(),
				tt_from: $("#tt_from").val(),
				tt_to: $("#tt_to").val(),
				fio: $("#fio").val(),
				passport: $("#pass_ser").val() + $("#pass_num").val(),
				pass_from: $("#pass_from").val(),
				pass_when: $("#pass_when").val()

			}, function(data){

			if (data.success)
			{
				$.post("/cabinet/acts/ajax-handler", {month: data.month, year: data.year}, function(data){
					$("#month_akts_block").html(data).css("height", "auto");
				});
			}
			else
			{
				alert(data.error)
			}
		}, "json");
	},

	changeFilter: function(field, input_id){
		var value = $("#" + input_id).val();
		hData = {};
		hData[field] = value;
		var url = Kolizeo.url(hData, this.url);
		//alert(url);
		this.load(url);
	},
	hide: function(){
		var j = $("#month_akts_block");
		var x = Math.round(j.width()/2)-32;
		var y = 200;
		var j2 = $('<img src="/images/ajax.gif" />').css("margin-left", x).css("margin-top", y);
		j.css("height", j.height()).html(j2);
	}
	}

Stat = {
	changeDate: function(){
		var month = $("#stat_month").val();
		var year = $("#stat_year").val();
		var order = $("#order_type").val();
		this.hide();
		$.post("/cabinet/stat/ajax-handler", {month: month, year: year, order: order}, function(data){
			$("#month_stat_block").html(data).css("height", "auto");
//		alert(data);
		});
	},
	changeFilter: function(field, input_id){
		var value = $("#" + input_id).val();
		hData = {};
		hData[field] = value;
		var url = Kolizeo.url(hData, this.url);
			
		//alert(url);
		this.load(url);
	},
	hide: function(){
		var j = $("#month_stat_block");
		var x = Math.round(j.width()/2)-32;
		var y = 200;
		
		var j2 = $('<img src="/images/ajax.gif" />').css("margin-left", x).css("margin-top", y);
		j.css("height", j.height()).html(j2);
	}
}

function consultant_gallery()
{
	oPopup.show({
		width:731,
		html:'<img src="/images/pixel.gif" width="663" height="444"><br><div class="f14" style="padding:10px 0px">Константина за работой. 2008 г.</div>'+
		'<div style="width:600px;margin:0px auto"><img src="/images/temp/temp21.jpg"><img src="/images/temp/temp22.jpg"><img src="/images/temp/temp21.jpg"><img src="/images/temp/temp21.jpg"><img src="/images/temp/temp22.jpg"><img src="/images/temp/temp21.jpg"></div>'
	});
}
