$(document).ready(function(){
    $("ul.dropdown li").hover(function(){
		if($.browser.opera) return;
        $('ul:first',this).css('visibility', 'visible');
        $(this).addClass("hover");
    }, function(){
		if($.browser.opera) return;
        $('ul:first',this).css('visibility', 'hidden');
        $(this).removeClass("hover");
    });
	if($.browser.opera) return;
    $("ul.dropdown li ul li:has(ul)").find("a:first").append(" &raquo; ");
});

function displayEventtext(id, name, elem) {
	$.getJSON(webroot+'?mini=1&mode=evtt&evt='+id+'&name='+name, {}, function(data, textStatus) {
		$('#eventtext').html(data.html);
		$('a.ebtn').removeClass('ebtn-active');
		$(elem).addClass('ebtn-active');
		afterUpdate();
	});
	return false;
}

function displayContent(id) {
	hideMenu();
	unHideNews();
	$.getJSON(webroot+'?mini=1&mode=cnt&cnt='+id, {}, function(data) {
		$('#maincontent').html(data.html);
		bindAjaxForms();
		afterUpdate();
	});
	return false;
}

function displayEvent(id, evttime) {
	unHideNews();
	$.getJSON(webroot+'?mini=1&mode=evt&evt='+id+'&evtt='+evttime, {}, function(data) {
//		setMainContent(data.html);
		$('#maincontent').html(data.html);
		afterUpdate();
	});
	return false;
}

function displayProgram(searchstr) {
	hideMenu();
	unHideNews();
	if(typeof searchstr == 'undefined') searchstr = '';
	$.getJSON(webroot+'?mini=1&mode=prog'+(searchstr != '' ? '&q='+encodeURI(searchstr).replace('#','%23') : ''), {}, function(data) {
		$('#maincontent').html(data.html);
		afterUpdate();
	});
	return false;
}

function displayAllNews(searchstr) {
	hideMenu();
	unHideNews();
	$.getJSON(webroot+'?mini=1&mode=allnews', {}, function(data){
		$('#maincontent').html(data.html);
		afterUpdate();
	});
	return false;
}

function afterUpdate() {
//	try{document.getElementById('maincontent').scrollIntoView()}catch(Ex){}
	cufonApply();
}

function cufonApply() {
	try{
		Cufon.replace('.myriad, .title, .blocktitle-right, .ebtninner a');
		Cufon.now();
	}catch(ex){}
}

function setMainContent(html) {
	var cc = $('#maincontent');
	cc.html(html);
	var cc = $('#eventtext');
	cc.jScrollPane({scrollbarWidth:20,reinitialiseOnImageLoad:true});
}

var hiddenNews = -1;
function unHideNews() {
	if(hiddenNews != -1){
		$('#newsrow_'+hiddenNews).show(100);
	}
	hiddenNews = -1;
}
function displayNews(id) {
	$.getJSON(webroot+'?mini=1&mode=news&nid='+id, {}, function(data) {
		$('#maincontent').html(data.html);
		afterUpdate();
		unHideNews();
		hiddenNews = id;
		$('#newsrow_'+id).hide(100);
	});
	return false;
}

function displayWholeCalendar(month) {
	$.getJSON(webroot+'?mini=1&mode=wcal&mnth='+month, {}, function(data) {
		$('#wholecalendar').html(data.html);
		bindScrollbars();
	});
	return false;
}

function displaySubCalendar(month) {
	$.getJSON(webroot+'?mini=1&mode=cal&mnth='+month, {}, function(data) {
		var cc=$('#calendar_container');
		cc.jScrollPaneRemove();
		cc.html(data.html);
//		cc.css('height','120px');
//		cc.css('max-height','120px');
//		cc.css('overflow','hidden');
//		cc.css('line-height','11px');
//		cc.jScrollPane({scrollbarWidth:20});
		bindScrollbars();
	});
	return false;
}

function hideMenu() {
	$('ul.dropdown ul').each(function(e){
		this.style.visibility='hidden';
	});
}

var rotates;
var rotate_idx = -1;
var rotate_max = -1;
var can_rotate = false;
var rlinks;
function startRotate() {
	rotates = $("#rotatecontainer div.rotate");
	rotate_max = rotates.length;
	if(rotate_max > 1) {
		can_rotate = true;
		var idx = 0;
		var str = '';
		for(var i = 0; i < rotate_max; i++) {
			str += '<a href="javascript:;">'+(i+1)+'</a> ';
		}
		$('#speciallinks').html(str);
		rlinks = $("#speciallinks a");
		rlinks.each(function() {
			var myidx = idx++;
			$(this).click(function() {
				doRotate(myidx);
			});
		});
	} else {
		$('#speciallinks').hide();
	}
	if(can_rotate) {
		doRotate(-1);
	}
}

function doAutoRotate() {
	doRotate(-1);
}

function doRotate(to_idx) {
	if(!can_rotate) return;
	if(rot_time)clearTimeout(rot_time);
	rot_time = null;
	if(rotate_idx != -1 && rotates[rotate_idx]) {
		$(rlinks[rotate_idx]).removeClass('active');
		$(rotates[rotate_idx]).fadeOut(200);
	}
	if(to_idx != -1) {
		rotate_idx = to_idx % rotate_max;
	} else {
		rotate_idx = (++rotate_idx) % rotate_max;
	}
	$(rlinks[rotate_idx]).addClass('active');
	$(rotates[rotate_idx]).fadeIn(1500);
	rot_time = setTimeout(doAutoRotate, 8000);
}
var rot_time;
$(document).ready(function() {
	window.setTimeout(startRotate, 10);
});

