$(document).ready(function() {
	if ($.browser.msie && navigator.appVersion.match(/ MSIE [345]/)) return;

	$('#program-genre').change(function() {
		this.form.submit();
	});
	$('#program-type').change(function() {
		try {
			this.form.page.disabled = true;
		} catch(e) {
		}
		try {
			this.form.cols.disabled = true;
		} catch(e) {
		}
		this.form.submit();
	});
	$('#syn-show').click(function() {
		this.form.submit();
	});
	$('.theme select').change(function() {
		this.form.submit();
	});

	function closeDropDown() {
		$('.pushed').removeClass('pushed');
		$('.ddl').hide();
		//$('.channels-wrap').get(0).style.height = 'auto';

		/*try {
			hide_balloon();
		} catch(e) {
		}*/
	}

	$(document).click(closeDropDown);

	$('div.control a.dd-push').click(function(event) {
		if (!$(this).is('.pushed')) {
			closeDropDown();
			$(this).addClass('pushed');
			var pl = $('#programs-list').get(0);
			var pn = this.parentNode;
			$('.channels-wrap').height($(pl).height() - 28);
			/*if ($(this.parentNode.parentNode.previousSibling).is('.totd')) {*/
			if ($(this.parentNode.parentNode.previousSibling).is('.channels-wrap')) {
				pl.style.top = 'auto';
				pl.style.bottom = pn.parentNode.offsetParent.offsetHeight - pn.parentNode.offsetTop - pn.offsetTop - 2 + 'px';
			} else {
				pl.style.top = pn.parentNode.offsetTop + pn.offsetTop + pn.offsetHeight + 'px';
				pl.style.bottom = 'auto';
			}
			if ($(this.parentNode).is('.rev')) {
				pl.style.left = '10px';
				pl.style.right = 'auto';
			} else {
				pl.style.left = 'auto';
				pl.style.right = '7px';
			}
			$(pl).show();
		}
		else {
			closeDropDown();
		}
		return false;
	});

	$('div.channels a.dd-push').click(function(event) {
		if (!$(this).is('.pushed')) {
			closeDropDown();
			$(this).addClass('pushed');

			var ch = $(this).parents('.channel:first');
			var div = ch.next().get(0);
			var lft = ch.get(0).offsetLeft;
			if (lft > 0) lft += 15;
			if (lft > 350) lft -= 317;
			div.style.top = ch.height() + 'px';
			div.style.left = lft + 'px';
			$(div).show();
			$('.channels-wrap').height($(div).height() + 60);
		}
		else {
			closeDropDown();
		}
		return false;
	});

	$('b.larr').add('b.rarr').each(function() {
		this.style.top = (this.parentNode.parentNode.offsetHeight - 10) / 2 + 'px';
	});

	$('#timesheet a.dd-push').click(function(event) {
		if ($(this).is('.pushed')) return;
		closeDropDown();
		$(this).addClass('pushed');
		if ($(this.parentNode.parentNode).is('.rev')) {
			var pl = $('#time-list-rev').get(0);
			pl.style.left = '0';
			pl.style.right = 'auto';
		} else {
			var pl = $('#time-list-ff').get(0);
			pl.style.left = 'auto';
			pl.style.right = '-2px';
		}
		var y = 0;
		var pn = this.parentNode;
		while (pn && pn.id != 'timesheet') {
			y += pn.offsetTop;
			pn = pn.offsetParent;
		}
		if (this.parentNode.parentNode.parentNode.parentNode.tagName.toLowerCase() == 'tfoot') {
			pl.style.top = 'auto';
			pl.style.bottom = $('#timesheet').get(0).offsetHeight - y - 4 + 'px';
		} else {
			pl.style.top = y + this.parentNode.offsetHeight + 'px';
			pl.style.bottom = 'auto';
		}
		$(pl).show();
		return false;
	});

	if ($('#timesheet').length) {
		$('<div id="timeline"></div>').prependTo('#timesheet').get(0).$time = $('#timesheet').get(0).$time;
		moveTimeLine();
		window.setInterval(moveTimeLine, 10000);
	}
	if ($('#program h3 span').length) {
		window.setInterval(updateDateTime, 1000);
	}

	/* Settings on/off */
	$('#settings').click(function() {
		document.cookie = 'settings=' + (this.checked ? 'on' : 'off') + '; path=/';
		this.form.submit();
		return null;
	});
});

function moveTimeLine() {
	var $tl = $('#timeline');
	var tl = $tl.get(0);
	var delta = Math.floor((new Date() - tl.$time) / 6000);
	var tdiv = $('#timesheet thead td div').get(Math.floor(delta / 150));
	if(!tdiv) {
		$tl.hide();
		return;
	}
	var tbl = $('#timesheet table').get(0);
	$tl.css({
		top: tbl.offsetTop - 10 + 'px',
		height: tbl.offsetHeight + 10 + 'px',
		left: tdiv.offsetLeft + Math.floor(tdiv.offsetWidth / 150 * (delta % 150)) + 'px'
	}).attr('title', Date().match(/\d+:\d+/)).show();
}

function updateDateTime() {
	$('#program h3 span').html('' + Date().match(/\d+:\d+:\d+/));
}
