function myErrorHandler() { return true; }
window.onerror = myErrorHandler;
function getPos(obj) {
	var x = 0, y = 0;
	while ( obj ) {
		x += obj.offsetLeft;
		y += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return {
		x: x,
		y: y
	};
}
var voted = false;
var curWidth, color;
var first = true;
function RateEvt(e) {
	if ( voted ) return false;
	var ind = document.getElementById('grade');
	if ( first ) {
		curWidth = $(ind).width();
		color = $(ind).css('background-color');
		first = false;
	}
	if ( e.type == 'click' || e.type == 'mousemove' ) {
		var pos = getPos(ind);
		var newWidth = (Math.round((e.clientX - pos.x)/10) * 10); // - 130;
		if ( e.type == 'click' && !voted ) {
			$(ind).width(newWidth);
			voted = true;
			curWidth = newWidth;
			var form = $(ind).parents('form');
			$(form).find('input[name="GRADE"]').val(newWidth/10);
			$.ajax({
				type: $(form).attr('method'),
				url: $(form).attr('action'),
				data: $(form).serializeArray(),
				success: function(resp) {
					$('#center').html(resp);
				},
				dataType: ''
			});
			return false;
		} else if ( e.type == 'mousemove' ) {
			if ( !voted ) {
				$(ind).css('background-color', '#8F2033');
				$(ind).width(newWidth);
			}
		}
	} else if ( e.type == 'mouseout' ) {
		e.cancelBubble = true;
		$(ind).css('background-color', color);
		$(ind).width(curWidth);
	}
}
$(document).ready(function() {
	$('#top-stories').easySlider({
		auto: false,
		nextId: 'next-story',
		prevId: 'prev-story'
	});
	$('#carousel').easySlider({
		auto: false,
		nextId: 'next-slide',
		prevId: 'prev-slide'
	});
	$('#latest-albums').easySlider({
		auto: false,
		nextId: 'next-album',
		prevId: 'prev-album'
	});
	var fsStep = 2;
	$('#fs-minus').live('click', function() {
		var cur = $('#fs-area').css('font-size');
		cur = parseInt(cur);
		var fs = (cur - fsStep);
		if ( fs < 12 ) { fs = 12; }
		$('#fs-area').css('font-size', fs + 'px');
		return false;
	});
	$('#fs-plus').live('click', function() {
		var cur = $('#fs-area').css('font-size');
		cur = parseInt(cur);
		var fs = (cur + fsStep);
		if ( fs > 17 ) { fs = 17; }
		$('#fs-area').css('font-size', fs + 'px');
		return false;
	});
	$('div.gray-zone a.e-mail').click(function() {
		if ( $(this).hasClass('fix-send') ) {
			$.ajax({
				type: 'GET',
				url: window.location + '?EMAILED=Y',
				success: function() {},
				dataType: ''
			});
		}
	});
	$('#comments-toggler').live('click', function() {
		var area = $('#comments-area');
		var show = $(area).css('display') == 'none';
		if ( show ) {
			$(this).text('Hide');
			$(area).show();
		} else {
			$(this).text('Show');
			$(area).hide();
		}
		return false;
	});
	var show = $('#add-comment').css('display') == 'none';
	$('span.add-comment').live('click', function() {
		var form = $('#add-comment');
		if ( show ) {
			$(form).show().insertAfter($(this).parent());
			$(form).find('input[name="PARENT_ID"]').val('');
			show = false;
		} else {
			$(form).hide();
			show = true;
		}
		return false;
	});
	$('#add-comment input.cancel').live('click', function() {
		$('#add-comment').hide();
		show = true;
	});
	$('#comments-area a.answer').live('click', function() {
		var form = $('#add-comment');
		$(form).find('input[name="PARENT_ID"]').val($(this).attr('id'));
		$(form).show().insertAfter($(this).parent());
		show = true;
	});
	$('#comments-area a.delete').live('click', function() {
		var comment = $(this).parents('div.block4');
		if ( confirm('Delete this comment?') ) {
			$.ajax({
				type: 'GET',
				url: $(this).attr('href'),
				success: function () {
					window.location.reload();
				},
				dataType: ''
			});
		}
		return false;
	});
	$('#comments-area a.complain').live('click', function() {
		var user = $(this).attr('name');
		var btn = $(this);
		if ( confirm('Are you sure you want to notifiy site administrator about a violation in the comments policy by user: "' + user + '" ? ')) {
			$.ajax({
				type: 'GET',
				url: window.location + '?MARK=' + $(this).attr('id'),
				success: function () {
					alert('Thank you for your help!'+"\n"+'Your message was received and will be evaluated soon.');
					$(btn).remove();
				},
				dataType: ''
			});
		}
		return false;
	});
	$('div.file input[type="radio"]').click(function() {
		var size = $('input[name="PDF_' + $(this).val() + '_SIZE"]').val();
		$('#pdf-size').show().text(size);
	});
	var prevSQuery = '';
	$('form#quick-search').submit(function() {
		var searchVal = $(this).find('input#search-query').val();
		if ( searchVal == 'Search free archives' || searchVal == '' ) {
			alert('Empty search query');
			return false;
		}
	});
	var prevSQuery = '';
	$('#quick-search input#search-query').focus(function() {
		prevSQuery = $(this).val();
		if ( prevSQuery == 'Search free archives' ) {
			$(this).val('');
		}
	})
	.blur(function() {
		var curSQuery = $(this).val();
		if ( curSQuery == '' ) {
			if ( prevSQuery != '' ) {
				$(this).val(prevSQuery);
			} else {
				$(this).val('Search free archives');
			}
		}
	})
	.keypress(function() {
		prevSQuery = '';
	});
	/* Search */
	$('#search input[name="PERIOD"]').click(function() {
		var fields = $('#search div.custom');
		if ( $(this).val() != 'CUSTOM' ) {
			$(fields).hide();
			$(fields).find(':input').attr('disabled', 'disabled');
		} else {
			$(fields).show();
			$(fields).find(':input').removeAttr('disabled');
		}
	});
	/* Hot keys */
	$(document).keypress(function(event) {
		if ( !event ) {
			event = window.event;
		}
		var key = event.keyCode;
		if ( event.ctrlKey ) {
			var el;
			if ( key == 37 ) {
				el = $('.prev a');
			}
			if ( key == 39 ) {
				el = $('.next a');
			}
			if ( $(el).is('A') ) {
				var url = $(el).attr('href');
				window.location = url;
			}
		}
	});
});
