﻿$(function() {
	var EventPanel = $('#event-list');
	var EventRows = $('.event-row');
	var months = $('.m-digit');
	var ViewedId = 0;
	var eventPanelHeight = 30;
	var eventRowsCount = $('.event-row').length;
	var boxEventCount = 10;
	var viewedEventCount = eventRowsCount < boxEventCount ? eventRowsCount : boxEventCount;
	var startDates = GetDateFrame(ViewedId);
	var curMonth = new Date().getMonth() + 1;
	//var flag = true;

	/*for (var i = 0; i < eventRowsCount; i++) {
		var el = $(EventRows[i]);
		var m = parseInt(el.attr("mid"));
		if (m == curMonth) {
			monthOnClick(el);
			flag = false;
		}
	}
	if (flag)*/
		ChangeSelMonths(startDates.begin, startDates.end);
	/*else
		ChangeSelMonths(curMonth - 1, curMonth + 1);*/

	if (!$.cookie('gmap-options'))
		$.cookie('gmap-options', 'small');

	var gmapOptions = $.cookie('gmap-options');

	if (gmapOptions == 'big') {
		$('#gmap-small-panel').hide();
		$('#gmap-big-panel').show();
	} else if (gmapOptions == 'small') {
		$('#gmap-small-panel').show();
		$('#gmap-big-panel').hide();
	}

	$('#gmap-maximize').click(function() {
		$('#gmap-small-panel').hide();
		$('#gmap-big-panel').show();
		$.cookie('gmap-options', 'big');
	});

	$('#gmap-minimize').click(function() {
		$('#gmap-small-panel').show();
		$('#gmap-big-panel').hide();
		$.cookie('gmap-options', 'small');
	});

	// Click on month
	$('.month').click(function() { monthOnClick($(this)); });
	function monthOnClick(el) {
		if(viewedEventCount == 0) return;
		var mid = el.attr('mid');
		var bMonthEl, eMonthEl;
		var bPos, ePos = null;

		while (isNaN(bPos) || isNaN(ePos)) {
			bMonthEl = $('.event-row[mid=' + mid + ']:first');
			eMonthEl = $('.event-row[mid=' + mid + ']:last');
			bPos = parseInt(bMonthEl.attr('pid'));
			ePos = parseInt(eMonthEl.attr('pid'));

			if (mid < 12) mid++;
			else if (mid == 12) {
				var lastEventEl = $('.event-row:first');
				var curMid = parseInt(lastEventEl.attr('mid'));
				ePos = parseInt(lastEventEl.attr('pid'));
				bPos = parseInt($('.event-row[mid=' + curMid + ']:last').attr('pid'));
			}
		}

		var count = (ePos - bPos) + 1;
		var pos;

		if (count == 1)
			pos = bPos - 5;
		else
			pos = (bPos - (Math.floor((viewedEventCount - count) / 2))) - 1;

		//$(EventPanel[0]).scroll(pos * eventPanelHeight);
		if(typeof EventPanel[0].scrollTo == 'function')
			EventPanel[0].scrollTo(pos * eventPanelHeight, null);
	};

	// OnScroll event
	EventPanel.scroll(function() {
		var pos = EventPanel.position();
		var curId = Math.floor((pos.top * -1) / eventPanelHeight);
		var dates = GetDateFrame(curId);

		if (ViewedId != curId) {
			ViewedId = curId;
			ChangeSelMonths(dates.begin, dates.end);
		}
	});

	// Get begin and end months of viewed events
	function GetDateFrame(pos) {
		var res = {
			'begin': -1,
			'end': -1
		};
		res.begin = parseInt($('.event-row[pid=' + (pos + viewedEventCount) + ']').attr('mid'));
		res.end = parseInt($('.event-row[pid=' + (pos + 1) + ']').attr('mid'));

		if (isNaN(res.begin)) {
			res.begin = res.end;

			for (var i = pos; i < EventRows.length; i++) {
				curNode = $(EventRows[i]);
				res.begin = parseInt(curNode.attr('mid'));
				if (!isNaN(res.begin)) return res;
			}
		}

		return res;
	};

	// Change class of months in panel
	function ChangeSelMonths(bMonth, eMonth) {
		var curNode, curMid;
		var ex = true;

		if (bMonth == 1 && eMonth == 1) {
			eMonth = 2;
			ex = false;
		}

		if (bMonth == 12 && eMonth == 12) {
			bMonth = 11;
			ex = false;
		}

		var t = eMonth - bMonth;
		if (t < 2 && ex)
			eMonth = eMonth + (2 - t);

		for (var i = 0; i < months.length; i++) {
			curNode = $(months[i]);
			curMid = parseInt(curNode.attr('mid'));

			if (bMonth == curMid) {
				if (curMid == 1)
					curNode.attr('class', 'm-digit msel-ex-left');
				else
					curNode.attr('class', 'm-digit msel-left');
			} else if (eMonth == curMid) {
				if (curMid == 12)
					curNode.attr('class', 'm-digit msel-ex-right');
				else
					curNode.attr('class', 'm-digit msel-right');
			} else if (bMonth < curMid && curMid < eMonth)
				curNode.attr('class', 'm-digit msel-center');
			else
				curNode.attr('class', 'm-digit');
		}
	};

});
