$(document).ready(function() {

// Temporary - do this with php later
	$('#listings table tr:odd, ul#ride_list li:odd').addClass('alt');


// Show Fancybox when a.fancybox is clicked
	$('a.fancybox').fancybox({
		'hideOnContentClick': false,
		'overlayShow': true,
		'overlayOpacity': 0.5,
		'frameWidth': 700,
		'frameHeight': 400
	}); 
	
	
// Details Gallery - configures the thumbnail scrolling, etc
	$(".photo ul.slideshow").cycle({
        fx:			'fade',
        timeout:	0,
        speed:		850,
        pager:		'ul.pager',
        nowrap:		1,
        prev:		'ul#controls li.prev a',
        next:		'ul#controls li.next a',
        after:		controlToggle,
        pagerAnchorBuilder: function(idx, slide) { return 'ul.pager li:eq(' + idx + ') a'; }
    });
    
    function controlToggle(curr, next, opts) {
	    var index = opts.currSlide;
	    var curImgHeight = $('ul.slideshow li:visible').height();
	    $('li.prev')[index == 0 ? 'fadeOut' : 'fadeIn']();
	    $('li.next')[index == opts.slideCount - 1 ? 'fadeOut' : 'fadeIn']();
	    $('ul.slideshow').animate({height: curImgHeight}, 250);   
	}

	var numThumbs = $('ul.pager li').size();
	var thumbWidth =  54;
	var thumbTotalWidth = numThumbs * thumbWidth;
	
	
	
	
	$('ul.pager').width(thumbTotalWidth);
	$('#pager_wrapper').scrollTo(0);
	
	if ( numThumbs >= 5 ) {
		$('#pager_wrapper').width(270);
		$('ul#controls').width(298);
	} else {
		$('#pager_wrapper').width(thumbTotalWidth);
		$('ul#controls').width(thumbTotalWidth + 28);
	}
	
	
	$('ul#controls li.next a').click(function(){
		 $("#pager_wrapper").stop().scrollTo( '+=54', 500, {axis:'x'} );
	});

	$('ul#controls li.prev a').click(function(){
	     $("#pager_wrapper").stop().scrollTo( '-=54', 500, {axis:'x'} );
	});	
	
	$("ul.slideshow li").hover(function(){
		var captionHeight = $(this).find('p.photo_details:visible').height() +10;
		$(this).children('p.photo_details').animate({ bottom: -captionHeight}, 500);
		},function(){
		$(this).children('p.photo_details').animate({ bottom: 0}, 500);
	});
	

// Classifieds - Show/hide the subcategory menu
	$('ul#categories li ul.subcategories').hide();
		
	$('ul#categories li.more').click(function(){
		$(this).attr('id','active').children('ul.subcategories').fadeIn('fast').parents('ul#categories').children('li:not(.other)#active').attr('id','inactive');
		return false;
	});

	$('ul#categories li.more').mouseleave(function(){
		$(this).children('ul.subcategories').fadeOut('fast').parent('li#active').removeAttr('id').parents().find('li#inactive').attr('id','active');	
	});
	
	
// Main Navigation - Show/hide the dropdown menu
	$('ul#main_nav li ul.dropdown').hide();
	
	$('ul#main_nav li').mouseenter(function(){
		$(this).children('ul.dropdown').fadeIn('fast');
	});
	
	$('ul#main_nav li').mouseleave(function(){
		$(this).children('ul.dropdown').fadeOut('fast');	
	});
		
	
// Swaps default input value when in focus
	$('ul#sub_nav input#search_field').each(function() {
	    var default_value = this.value;
	    $(this).focus(function() {
	        if(this.value == default_value) {this.value = '';}
	    });
	    $(this).blur(function() {
	        if(this.value == '') {this.value = default_value;}
	    });
	});	 
	
	
// Login input field configuration - default values and bg images
	$('#login input#username, #login input#password').attr('value','');
	$('#login input#password').css('background','#f2f1ec url(i/password_input_bg.gif) no-repeat 0 center');
	$('#login input#username').css('background','#f2f1ec url(i/username_input_bg.gif) no-repeat 0 center');

	$('#login input#password').focus(function(){
		$('#login input#password').css('background','#f2f1ec');
	});	
	
	$('#login input#password').blur(function(){
		if ( $('#login input#password').val() == '') {
			$('#login input#password').css('background','#f2f1ec url(i/password_input_bg.gif) no-repeat 0 center');
		} else {
			$('#login input#password').css('background','#f2f1ec');
		}
	});

	$('#login input#username').focus(function(){
		$('#login input#username').css('background','#f2f1ec');
	});	
	
	$('#login input#username').blur(function(){
		if ( $('#login input#username').val() == '') {
			$('#login input#username').css('background','#f2f1ec url(i/username_input_bg.gif) no-repeat 0 center');
		} else {
			$('#login input#username').css('background','#f2f1ec');
		}
	});


// Makes the entire block clickable
	$("#listings table tbody > tr, #related ul li, #recent_comments ul li, #secondary.whatson div.module ul li").click(function(){
		window.location=$(this).find("a").attr("href");
		return false;
	});


// Add a class for each type of input for IE 6
	$('input[type=text]').addClass('textbox');
	$('input[type=password]').addClass('password');
	$('input[type=radio]').addClass('radio');
	$('input[type=checkbox]').addClass('checkbox');
	$('input[type=submit]').addClass('submit');


// Calendar - Settings for the calendar features
	$("#calendar ul#feature_box").cycle({
        fx:			'scrollHorz',
        timeout:	0,
        speed:		450,
        prev:		'a#prev',
        next:		'a#next',
        pager:		'ul#feature_nav'
    });
    

// Calendar - Settings for the sidebar calendar
	$("#sidebar_calendar ul.calendar_list").cycle({
        fx:			'scrollHorz',
        timeout:	0,
        speed:		450,
        prev:		'a.prev',
        next:		'a.next',
		prevNextClick: function(isNext, zeroBasedSlideIndex, slideElement) {
			if (isNext) curCalendarPos++;
			else curCalendarPos--;
			updateCalendar(curCalendarPos);
			initCalendar();
			updateCalendarCategories();
		}
	});


// Homepage - Classifieds - Settings for the sliding homepage items.
	$("ul.classified_list").cycle({
        fx:			'scrollHorz',
        timeout:	0,
        speed:		450,
        prev:		'#classified a.prev_button',
        next:		'#classified a.next_button',
		prevNextClick: function(isNext, zeroBasedSlideIndex, slideElement) {
			//alert(isNext?"Next":"Prev");
			if (isNext) curClassifiedPos++;
			else curClassifiedPos--;
			if (curClassifiedPos<0) curClassifiedPos=5;
			else if (curClassifiedPos>5) curClassifiedPos=0;
			updateClassifieds(curClassifiedMode,curClassifiedPos,0);
			//initCalendar();
			//updateCalendarCategories();
		}
	});


// Settings for the feature slideshow on homepage
	$("#feature ul#feature_list").cycle({
        fx:			'fade',
        timeout:	6000,
        speed:		850,
        pause:		1,
        pager:		'ul#feature_nav',
        pagerAnchorBuilder: function(idx, slide) { 
			return '<a class="pager" href="#">'+ idx +'</a>';
		}
    });
    
    $('ul#feature_nav li:eq(0)').addClass('activeSlide');
    
    $('ul#feature_nav li:eq(0)').click(function() { 
    	$('#feature ul#feature_list').cycle(0); 
    	return false; 
	});
	
	$('ul#feature_nav li:eq(1)').click(function() { 
    	$('#feature ul#feature_list').cycle(1); 
    	return false; 
	});
	
	$('ul#feature_nav li:eq(2)').click(function() { 
    	$('#feature ul#feature_list').cycle(2); 
    	return false; 
	});
	
	$('ul#feature_nav li:eq(3)').click(function() { 
    	$('#feature ul#feature_list').cycle(3); 
    	return false; 
	});
	
	$.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex) { 
	    $(pager).find('li').removeClass('activeSlide') 
	        .filter('li:eq('+currSlideIndex+')').addClass('activeSlide'); 
	};


// Settings for the Classifieds module on homepage

	$("#home ul#categories li.ajax").click(function(){
		curClassifiedMode = $(this).attr('class');
		curClassifiedPos=0;
		var catLinkHref = $(this).children('a').attr('href');
		var CatListingText=updateClassifieds(curClassifiedMode,curClassifiedPos,1);

		switch (curClassifiedMode) {
			case "housing ajax": caption="View All Housing"; break;
			case "textbooks ajax": caption="View All Textbooks"; break;
			default: caption="View All Classifieds"; break;
		}

		$('#classified a.view_all"').attr('href', catLinkHref).text(caption);

		var activeCatListing = $('ul.classified_list');
		activeCatListing.fadeOut(350, function() {
			
			 o=document.getElementById("classified_"+curClassified);
			 if (o) o.innerHTML=CatListingText;
		
			 if ( $('ul#categories li.'+curClassifiedMode+'') ) {
				$('ul#categories li.'+curClassifiedMode+'').attr('id','active');				// Assign an 'active' id to category when clicked
				$('ul#categories li:not(.'+curClassifiedMode+')').attr('id','');
			 }
		
			 activeCatListing.fadeIn(350);														// Show/hide the appropriate classified category
		});

		return false;
	});

	$("#home ul#categories li.more ul li, ul#categories li.more ul li").click(function(){
		window.location=$(this).find("a").attr("href");
		return false;
	});

	var galleryChildren = $('ul.classified_list#everything').children().size();
	var itemWidth =  180;
	var galleryWidth = galleryChildren * itemWidth;
	var galleryPadding = galleryChildren * 1 +1;
	$('ul.classified_list#everything').css('width', galleryWidth + galleryPadding);


// Ride Share - Show/hide the ride details (may not use this on final site)
	$('ul#ride_list div.desc, ul#ride_list div.map').hide();

	$('ul#ride_list li.ride').click(function(){
		$(this).toggleClass('active');
		
		if( $(this).hasClass('.active') ) {

			var ride_id=$(this).attr("ride_id");
			$(this).children('div.desc').html(updateRideList(ride_id));
			$(this).children('div.desc').show();
		} else {
			$(this).children('div.desc').hide();
		}
	});
	
	
// Classifieds - Tooltip pops up when each <tr> is hovered
	
	//Select all anchor tag with rel set to tooltip  
	$('#listings table tbody > tr').mouseover(function(e){  
	      
	    var itemSummary = $(this).find('div.summary').text();
	    	    	    
	    $(this).parents('#listings').append('<div class="tooltip"><p class="summary">' + itemSummary + '</p></div>');       
	      
	    //Set the X and Y axis of the tooltip  
	    $('.tooltip').css('top', e.pageY );  
	    $('.tooltip').css('left', e.pageX );  
	    $('.tooltip').fadeIn('500');  
	    $('.tooltip').fadeTo('10',0.95);  
	      
	}).mousemove(function(e){  
	      
	    $('.tooltip').css('top', e.pageY - $('#listings').offset().top - 10 );  
	    $('.tooltip').css('left', e.pageX - $('#listings').offset().left + 45 );  
	    	      
	}).mouseout(function(){  
	    $(this).parents('#listings').children('div.tooltip').remove();  
	});
	
	
	
function initCalendar() {

// Calendar - Filter the different event types on calendar	
	$('#calendar ul#legend li').click(function(){
		var eventType = $(this).attr('class');	
		var eventNotActive = $('table.calendar td.day ul li.'+eventType+':not(.active)');
		var eventActive = $('table.calendar td.day ul li.'+eventType+'.active');
		
																									
		$(this).parent().find('span.on').removeClass('on');																						// Remove the 'on' class for events
		$(this).children('span').toggleClass('active');																							// Add 'active' class to clicked event (legend)
																									
		$('table.calendar td.day span.'+eventType+'').toggleClass('active');																	// Add 'active' class to clicked event (calendar)
		$('table.calendar td.day span.active').fadeIn();																						// Show the 'active' event indicators (calendar)
		$('table.calendar td.day span:not(.active,.number)').fadeOut();																			// Hide other events but show date box
				
		eventNotActive.attr('class',''+eventType+' active').show().parent().children('li:not(.active,.large_num)').removeClass('on').hide();	// Add 'active' class to clicked event (events)
		eventActive.attr('class',''+eventType+'').hide();																						// Remove 'active' class and restore original class (events)
	});																								
																									
	$('#calendar ul#legend li.all').click(function(){												
		$('table.calendar td.day span').fadeIn();																								// Show all events in calendar
		$(this).parent().find('li:not(.all) span.active').removeClass('active');																// Remove 'active' class from anything but 'all'
		$(this).children('span').addClass('active');																							// Add 'active' class when 'all' type is clicked
		$('table.calendar td.day span').removeClass('active');																					// Remove 'active' class from all events (calendar)
		$('table.calendar td.day ul li:not(.large_num,.more_events)').removeClass('active').addClass('on').show();								// Remove 'active' class and add 'on' class
	});																								
																									
	$('#calendar ul#legend li:not(.all)').click(function(){											
		$(this).parent().find('li.all span.active').removeClass('active');																		// Remove 'active' class from 'all' if other types are clicked
	});																								
																									
				
																									
// Calendar - Pop-up list of all events when day is hovered												
	$('table.calendar td.day ul li:not(.large_num,.more_events)').toggleClass('on').show();														// Show all events by assigning 'on' class
	$('#sidebar_calendar table.calendar td.day li.more_events').remove();																		// Remove 'more_events' link if on 'Whats On' page

		
		// mousenter => click
	// Homepage																							
	$("#calendar table.calendar td.day").click(function(){												
		if( $(this).find('span:visible').size() > 1 ) {																							// If events are visible then show pop-up list of events
			$(this).find('ul').fadeIn('fast');
																	
			$(this).children('ul li:not(.active,.on,.large_num)').hide();																		// Hide events that don't have an 'active' class
			$(this).find('ul').children('li:not(.large_num,.more_events,.active):gt(5)').hide();												// If more than 6 events listed, hide the rest
											
			if( $(this).find('ul').children('li:not(.large_num,.more_events):visible').size() > 5 ) { $('li.more_events').show(); }				// If 6 or more events are visible, show more link
			else { $('li.more_events').hide(); }
			
		} else {
			$(this).find('ul').fadeOut('fast');
		}
	});
	
	
	// Sidebar
	$("#sidebar_calendar table.calendar td.day").click(function(){			
		//var selectOption = $(this).find('span:visible:not(.number)').attr('class');
				
		$('#event_list').empty();																												// Empty the div on each click, before adding events
		$(this).find('ul').clone().appendTo('#event_list').slideDown();
		//$(this).find('ul').children('li.'+selectOption+',.large_num)').hide();
		
		updateCalendarCategories();
	});
	
	$("table.calendar td.day").mouseleave(function(){
		$(this).find('ul').fadeOut('fast');
	});

}
initCalendar();

	$('#sidebar_calendar a.prev, #sidebar_calendar a.next').click(function() {																	// Empty the #event_list div when month changes
		$('#event_list').empty();
		$('select:eq(0)').attr('selected', 'selected');
	});

	function updateCalendarCategories() {
		var selectedOption = $('select :selected').attr('label');

		if ( $('select :selected:eq(0)') ) {																								// If the first option is selected, show all events
				$('table.calendar td.day span, table.calendar ul li, #event_list ul li').fadeIn().show();
		}
		
		if (selectedOption.match(/\w/)) {
			$('table.calendar td.day span.'+selectedOption+'').fadeIn().show();	
			$('table.calendar td.day span:not(.'+selectedOption+',.number)').fadeOut().hide();		
			$('table.calendar ul, #event_list ul').children('li:has(.'+selectedOption+',.large_num,.active)').fadeIn().show();
			$('table.calendar ul, #event_list ul').children('li:not(.'+selectedOption+',.large_num,.active)').fadeOut().hide();
		}
		else {
			$('table.calendar td.day span').fadeIn().show();	
		}
	}

	$('#sidebar_calendar select').change(function() {
		$('#event_list').empty();
		updateCalendarCategories();
	});	
});