jQuery(document).ready(function($){

	$('a[rel="gallery"]').colorbox({photo:true});
	
	// Setup sidebar menus
	$('#sidebar li:not(:has(.current))').hoverIntent({
	   over: function() {
	           $('ul', this).show();
	       },
	   out: function() {
	           $('ul', this).hide();
	       },
	   timeout: 200
	});
	
	//Enable external links
	$('a[rel~=external]').attr('target','_blank');
	
	//Setup form field with default value
	function setFieldValue(id,value){
		$('#'+id).val(value).focus(function(){
			if( $(this).val() == value)
				$(this).val('');
		}).blur(function(){
			if( $(this).val() == '')
				$(this).val(value);
		});
	}
	
	//Setup quick links
	//$('#quick-links').width( ( $(window).width() / 2 ) - 218 );
	//$(window).resize(function(){
	//	$('#quick-links').width( ( $(window).width() / 2 ) - 218 );
	//});	
	
	//Add header shadow
	$('body').append('<div id="shadow"></div>');
	
	//Setup playlist buttons
	$('#player-playlist li a').each(function(index){
		$(this).click(function(){
			$f('player').play(index);
			return false;
		});
	});
	
	if( app == 'home' ){
		
		//Setup mini calendar color switching
		var colors = new Object();
		colors.white = '#FFF';
		colors.orange = '#ff9900';
		colors.blue = '#3399ff';
		colors.green = '#66cc00';
		colors.red = '#ff0000';
		colors.yellow = '#ffff00';
		colors.purple = '#cc00cc';	
		colors.gray = '#007B7B';		
		function showEvents(dayclass){
			$('a.white').css({
				color: '#bbb',
				fontWeight: 'normal'
			});
			$('a.' + dayclass).css({
				color: colors[dayclass],
				fontWeight: 'bold'
			});
		}
		$('#calendar-legend li').click(function(){
			showEvents($(this).attr('class'));
		});
		showEvents('white');
		
		//Setup mini calendar rollover lists
		$('#mini-calendar td a').each(function(){
			var remoteUrl = base_url+'events/eventsForDay?timestamp='+$(this).attr('id').replace(/event\-/,'');
			$(this).click(
				function(){
					$('#mini-calendar-events').slideUp('slow',function(){		
						$(this).load(remoteUrl,function(){
							$(this).slideDown('slow');
							tb_init('#mini-calendar-events a.thickbox');
						});
					});
					return false;
				}
			);
		});
		
			
    	// Rotate homepage images
    	var images = $('#homeRotation').children('img');
    	   images.css('position', 'absolute')
    	   .filter(':not(:first)').hide();
        var t = setTimeout('startFade()', 5000);
    
        startFade = function() {
            // Get the current and the next image
            var current = images.filter(':visible');
            var next = current.next()
            var next = (next.length) ? next : images.filter(':first');
            
            // Do the fade
            current.fadeOut(1200);
            next.fadeIn(1200);
            
            // Set a timer for the next fade
            var t = setTimeout('startFade()', 5000);
        }

	}
	
	//Setup sidebar border
	if( app != 'home' ){
		var sidebarHeight = $('#content-sidebar').height();
		var contentHeight = $('#content-body').height();
		if( sidebarHeight > contentHeight ){
			$('#content-body').css({
				border:'none'
			});
			$('#content-sidebar').css({
				borderLeft:'1px solid #6699cc',
				width:202
			});
		}
	}
	
	$('#content-body table')
	.removeAttr('border')
	.css({
		border: '1px solid #ddd',
		borderBottom: 'none',
		borderRight: 'none'
	}).find('td').css({
		borderBottom: '1px solid #ddd',
		borderRight: '1px solid #ddd',
		padding: '2px 5px'
	});
	
	
	$('#content-body table tr:even').children().css({ background: '#f5f5f5' });
	$('#content-body table tr:first').children().css({ background: '#ccc', fontWeight: 'bold' });

	
});


/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);