var menu_current_color = 0;
var menu_link_colors = new Array("#f8ef22", "#eb0a8d", "#28ace2");

function menu_highlight(n)
{
	document.getElementById('menu_item'+n).style.color = menu_link_colors[menu_current_color];
	$('#menu_item'+n+' a:first').css({'color':menu_link_colors[menu_current_color]});
	$('#menu_item'+n+' img').attr('src','_images/default/circle' + n + 'h.png');
	menu_current_color = (menu_current_color + 1) % 3;
}

function menu_downlight(n) {
	$('#menu_item'+n+' a:first').css({'color': '#000'});
	$('#menu_item'+n+' img').attr('src', '_images/default/circle' + n + '.png');
}

function open_submenu(n)
{
	$('.submenu').hide();
	$('#submenu_'+n).show();
}

function init_scrollpane()
{

	$('#content').jScrollPane({
		animateTo: true,
		scrollbarOnLeft: true,
		scrollbarWidth: 20,
		dragMinHeight: 20,
		dragMaxHeight: 20,
		scrollbarMargin: 20
	});
	// correct opera bug
	if(navigator.userAgent.indexOf('Opera') > -1) {
		$('body').css({'overflow-y':'hidden'});
	}
}

function init()
{
	$('.submenu').hide();
	$('a').focus(function() {this.blur();});
	init_ajax_links();
	fold_texts();
	tb_init('a.thickbox');
	$("a[rel*='lightbox']").lightBox();
	init_scrollpane();
}

function load_content(url)
{
	$("#submenu_0").hide();
	$("#submenu_1").hide();

	$('#content_loader').fadeIn('slow');
	$('#content').fadeOut('slow', function(){
		$('#content').load(url+'/?ajax=true', true, function(){
			$('#content_loader').fadeOut('slow');
			$('#content').fadeIn('slow', function() {
				if ($.browser.msie) this.style.removeAttribute('filter');
			});
			init();
		});
	});
}

function submit_comment()
{
	if($('#comment_sent').attr('value') == 1) return false;
	$('#comment_sent').attr('value', 1);
	var news_id = $('#news_id').attr('value');
	var nick = $('#comment_nick').attr('value');
	var email = $('#comment_email').attr('value');
	var text = $('#comment_text').attr('value');

	$.post("comments/add/?ajax=true", {
		'nick':nick,
		'email':email,
		'text':text,
		'news_id':news_id
	}, function(data) {
		$('#comment_sent').attr('value', 0);
		alert(data.substr(1));
		if(data.substr(0,1) == 1) {
			load_content('comments/'+news_id);
		}
	});
}

function init_ajax_links()
{
	$('a[rel*="ajax_link"]').attr('href', function(){
		return 'javascript:load_content("' + this + '/?ajax=true")';
	});
	$('a[rel*="ajax_link"]').removeAttr('rel');
	return true;
}

function fold_texts()
{
	var text_size = 400;
	$('.element .foldable').each(function() {
		var text = this;
		var full_text = text.innerHTML;
		if(full_text.length > text_size) {
			var hide_point = full_text.indexOf(' ', text_size);
			text.innerHTML = full_text.substr(0,hide_point) + '...';
			var folding = $(this).next();
			var read_more = folding.children('a[rel*="read_more"]');
			var read_less = folding.children('a[rel*="read_less"]');
				read_more.show();
				read_more.click(function() {
					read_more.hide();
					read_less.show();
					text.innerHTML = full_text;
					init_scrollpane();
				});

				read_less.click(function() {
					read_less.hide();
					read_more.show();
					text.innerHTML = full_text.substr(0,hide_point) + '...';
					init_scrollpane();
				});
		}
	});
}

function choose_stylesheet()
{
	var style = 1024;
	var screen_width = (screen.width) ? screen.width : 1024;
	var screen_height = (screen.height) ? screen.height : 1024;

	if(screen_width >= 1280 && screen_height >= 1024) style = 1280;
	else {
		if(screen_width < 1280 || screen_height < 1024) style = 1024;
		if(screen_width < 1024 || screen_height < 768) style = 800;
	}

	var to_show = null;
	$('link[rel*=stylesheet][title]').each(function() {
		if(this.title == style) to_show = this;
		this.disabled = true;
	});
	to_show.disabled = false;
}

$(function()
{
	choose_stylesheet();
	init();
});
