$(document).ready(function(){

	if ($.fn.corner) {
		$('.o-content').corner('tl');
	}

	if ($.fn.cycle) {
		var $slideshow = $('.gallery-slideshow');
		if ($slideshow.length) {
			$slideshow.cycle({
				fx: 'fade'
			});
		}
	}

	$('.calendar .prev a').live('click', function(e){
		e.preventDefault();
		var cal = $(this).parents('.calendar')[0];
		var url = $(cal).find('input[name=url]').val();
		var y = parseInt($(cal).find('input[name=y]').val());
		var m = parseInt($(cal).find('input[name=m]').val()) - 1;
		if (m == 0) {
			m = 12;
			y = y - 1;
		}
		$(this).parents('.calendar').load($.param.querystring(url, 'y=' + y + '&m=' + m));
		return false;
	});

	$('.calendar .next a').live('click', function(e){
		e.preventDefault();
		var cal = $(this).parents('.calendar')[0];
		var url = $(cal).find('input[name=url]').val();
		var y = parseInt($(cal).find('input[name=y]').val());
		var m = parseInt($(cal).find('input[name=m]').val()) + 1;
		if (m > 12) {
			m = 1;
			y = y + 1;
		}
		$(this).parents('.calendar').load($.param.querystring(url, 'y=' + y + '&m=' + m));
		return false;
	});
	
	$('#arch-cal tbody a').live('click', function(e){
		e.preventDefault();
		var qs = 'd=' + $(this).attr('rel');
		var view = $("#arch-view").val();
		if (view == 'week') {
			qs += '&v=week';
		}
		window.location = $.param.querystring(window.location.href, qs);
		return false;
	});

	if ($.fn.tipsy) {
		var $imgs = $('.o-cat-img');
		if ($imgs.length) {
			$imgs.tipsy({title: 'alt', gravity: 'n'});
		};
	}
	
	$('input.search-option').click(function(){
		var id = $(this).attr('id');
		if ($(this).attr('checked')) {
			$('#' + id + '-content').show();
		} else {
			$('#' + id + '-content').hide();
		}
	});
	
	// object categories
	$('div.o-view-category h2').click(function(){
		var id = $(this).attr('id');
		if ($(this).attr('status') == 1) {
			$('#'+id+'-content').slideUp();
			$(this).attr('status', 0);
		} else {
			$('#'+id+'-content').slideDown();
			$(this).attr('status', 1);
		}
	});
	
	if ($.fn.colorbox) {
		var colorbox_title = function(){
		    var title = $(this).attr('title');
			var text = $(this).attr('data-text');
			var html = '';
			if (title != undefined) {
				html += title;
			}
			if (text != undefined) {
				html += text;
			}
		    return html;
		};

		$('a[rel=lightbox]').colorbox({
			rel: "nofollow",
			title: colorbox_title,
			maxWidth: "800px"
		});

		$('a[rel=lightbox\[album\]]').colorbox({
			current: "{current}/{total}",
			loop: false,
			title: colorbox_title,
			maxWidth: "800px"
		});

		$('a.show-map').colorbox({
			rel: "nofollow",
			width: "680px",
			height: "680px",
			iframe: true
		});
	}
});
