function CityAutoComplete (url, s_name, s_id, start) {
	this.url     = url
	this.e_name  = $(s_name);
	this.e_id    = $(s_id);
	this.ready   = false;
	this.start   = start;
}

CityAutoComplete.prototype.init = function () {
	var controller = this;
	
	var onItemSelect = function (el) {
		controller.e_id.val(el.extra);
		controller.draw();
	}
	
	var formatItem = function (row, i, num) {
		var cur = controller.e_name.val();
		return "<b>" + cur + "</b>" + row[0].substring(cur.length);
	}
	
	this.e_name.focus(function () {
		controller.draw('input');
	});
	
	this.e_name.blur(function () {
		setTimeout(function () {
	//		controller.draw();
		}, 0)
	});

	$(document).ready(function() {
		controller.e_name.autocomplete(controller.url, {
			'onItemSelect'   : onItemSelect,
			'selectFirst'    : true,
			'formatItem'     : formatItem,
			'maxItemsToShow' : 10,
			'delay'          : 1});
	});
	
	this.draw(null, this.start);
}

CityAutoComplete.prototype.draw = function (mode, start) {
	if (mode == "input") {
		this.e_name.css("font-style", "normal");
		this.e_name.css("color", "#000");
		
		this.e_name.val('');
		this.e_id.val('');
		
		this.ready = false;
		this.onReadyChange();
	} else {
		if (this.e_id.val() && !start) {
			this.e_name.css("font-style", "normal");
			this.e_name.css("color", "#000");
		
			this.ready = true;
			this.onReadyChange();
		} else {
			this.e_name.css("font-style", "italic");
			this.e_name.css("color", "#666");
			if (start) {
				this.e_name.val(start);
			} else {
				this.e_name.val('Выберите город');
			}
			
			this.ready = false;
			this.onReadyChange();
		}
	}
}

CityAutoComplete.prototype.onReadyChange = function () {}

function Calendar (container, now, start, months, special) {
	this.container = container;
	
	this.now     = now;
	this.start   = start;
	this.months  = months;
	this.special = special;
	
	this.init();
}

Calendar.prototype.init = function () {
	this.draw();
}

Calendar.prototype.draw = function () {
	var parent = this;
	
	var monthnames = {
		1  : "Январь",
		2  : "Февраль",
		3  : "Март",
		4  : "Апрель",
		5  : "Май",
		6  : "Июнь",
		7  : "Июль",
		8  : "Август",
		9  : "Сентябрь",
		10 : "Октябрь",
		11 : "Ноябрь",
		12 : "Декабрь" 
	}
	
	var weeknums  = { Mon : 1, Tue : 2, Wed : 3, Thu : 4, Fri : 5, Sat : 6, Sun : 7 }
	
	this.container.html('<table class="calendar"></table>');
	var etable = $(".calendar", this.container);
	
	var addHeader = function (label, first) {
		first = first | false;
		
		if (first) {
			etable.append('<tr class="header"><td colspan="2" class="prev">← <span class="js-link">Предидущие</span></td>' +
				'<td colspan="3" class="label">' + label +'</td>' + 
				'<td colspan="2" class="next"><span class="js-link">Следующие</span> →</td></tr>');
		} else {
			etable.append('<tr class="header"><td colspan="7" class="label">' + label +'</td></tr>');
		}
		
		etable.append(
			'<tr class="wdays">' + 
				'<td>Пн</td>' +
				'<td>Вт</td>' +
				'<td>Ср</td>' +
				'<td>Чт</td>' +
				'<td>Пт</td>' +
				'<td class="holiday">Сб</td>' +
				'<td class="holiday">Вс</td>' +
			'</tr>'
		);
	}
	
	var today = Date.today();
	
	for (var i = 0; i < this.months; i++) {
		var d    = this.start.clone().addMonths(i).moveToFirstDayOfMonth();
		var wnum = weeknums[d.toString("ddd")];
		
		addHeader(monthnames[d.toString('M')] + d.toString(', yyyy'), i == 0);
		
		var contents = '<tr class="row">';
		for (var y = 1; y < wnum; y++) {
			contents += '<td class="empty"></td>';
		}
		
		while (true) {
			var dnum = d.toString("dd");
			var wnum = weeknums[d.toString("ddd")];
			var skey = d.toString("yyyy-MM-dd");
			
			var addclass = "day-" + skey;
			
			if (wnum == 6 || wnum == 7) {
				addclass += " holiday";
			}
			
			if (today.toString("yyyy-MM-dd") == skey) {
				addclass += " today";
			}
			
			if (typeof(this.special[skey]) != "undefined") {
				addclass += " " + this.special[skey];
			}
			
			contents += '<td class="day ' + addclass + '"><div>' + dnum + '</div></td>';
			
			d.addDays(1);
			
			if (d.toString("dd") == 1) {
				for (var y = wnum; y < 7; y++) {
					contents += '<td class="empty"></td>';
				}
				contents += '</tr>';
				break;
			} else if (weeknums[d.toString("ddd")] == 1) {
				contents += '</tr><tr class="row">';
			}
		}
		
		etable.append(contents);
	}
	
	$(".prev", etable).click(
		function () { parent.prev() }
	);
	
	$(".next", etable).click(
		function () { parent.next() }
	);
	
	for (var skey in this.special) {
		var t = function (date) {
			$(".day-" + skey, etable).click(function () {
				parent.onclick(date);
			});
		};
		
		t(skey);
	}
}

Calendar.prototype.prev = function () {
	this.start.addMonths(this.months * -1)
	this.draw();
}

Calendar.prototype.next = function () {
	this.start.addMonths(this.months)
	this.draw();
}

Calendar.prototype.onclick = function (date) {}

function DailyPrognosis (eparent, url) {
	this.eparent = eparent
	this.url     = url;
	this.day     = 0;
	this.zodiac  = 0;
}

DailyPrognosis.prototype.init = function () {
	var parent = this;
	
	$(".switch", this.eparent).click(function () { parent.switch_() });
	
	for (var i = 1; i <= 12; i++) {
		var t = function (zodiac) {
			var callback = function () {
				if (parent.zodiac == zodiac) {
					parent.zodiac = 0;
				} else {
					parent.zodiac = zodiac;
				}
				
				parent.title();
				parent.load()
			}
			
			$(".zodiac-" + zodiac + " img", parent.eparent).click(callback);
			$(".zodiac-" + zodiac + " span", parent.eparent).click(callback);
		}
		
		t(i);
	}
}

DailyPrognosis.prototype.switch_ = function () {
	if (this.day == 0) {
		$(".switch", this.eparent).html("← На сегодня");
		this.day = 1;
		this.title();
	} else {
		$(".switch", this.eparent).html("На завтра →");
		this.day = 0;
		this.title();
	}
	
	this.load();
}

DailyPrognosis.prototype.load = function () {
	var parent = this;
	var zodiac = this.zodiac;
	
	$(".zodiac span", this.eparent).removeClass("zodiac-selected");
	$(".zodiac-" + this.zodiac + " span").addClass("zodiac-selected");
	
	var callback = function (data) {
		if (!zodiac) {
			$(".zodiac-img", parent.eparent).fadeOut(100);
		}
		
		$(".content", parent.eparent).fadeOut(100, function () {
			if (zodiac) {
				$(".zodiac-img", parent.eparent).attr("src", "/html/images/zodiac-index/big/" + zodiac + ".png");
				$(".zodiac-img", parent.eparent).fadeIn(100);
			}
			
			$(".content", parent.eparent).html(data);
			$(".content", parent.eparent).fadeIn(100);
		});
	}
	
	jQuery.get(this.url + zodiac + '/' + this.day + '/', null, callback);
}

DailyPrognosis.prototype.title = function () {
	if (this.zodiac == 0) {
		title = "Для всех знаков ";
	} else {
		name = $(".zodiac-" + this.zodiac + " span", parent.eparent).text();
		title = "Для знака " + name + " ";
	}
	
	if (this.day == 0) {
		title += "на сегодня.";
	} else {
		title += "на завтра.";
	}
	
	$(".title", this.eparent).html(title);
}

MultiFileWidget = function (name) {
	this.name   = name;
	this.parent = $(".widget-files-" + this.name);
	this.init();
}

MultiFileWidget.prototype.init = function () {
	this.addEvents();
	this.setDefaultLabel();
}

MultiFileWidget.prototype.addEvents = function () {
	var controller = this;
	
	$(".add-file", this.parent).click(function () { controller.onAddFile(); });
	$(".default", this.parent).click(function () { controller.setDefaultLabel(); });
	$(".js-delete", this.parent).click(function () { $(this).parent().remove() });
}

MultiFileWidget.prototype.onAddFile = function () {
	var html = '<div class="file-c"><input type="file" name="' + this.name + '" class="file" /><span class="js-link js-delete">Удалить</span></div>';
	
	$(".new", this.parent).append(html);
	$(".js-delete", this.parent).click(function () { $(this).parent().remove() });
}

MultiFileWidget.prototype.setDefaultLabel = function () {
	$(".default", this.parent).next().text('');
	$(".default[checked]", this.parent).next().text('По умолчанию');
}