/**
 *	CBC Radio Concert Calendar object
 *	Also contains the codSpawn function
 *
 *	@author		Thomas J Bradley <theman@thomasjbradley.ca>
 *	@link		http://thomasjbradley.ca
 *	@copyright	Copyright MMVIII, Thomas J Bradley
 *	@version	$Id: scripts.js 65 2008-08-31 14:37:35Z thomasjbradley $
 */

/**
 *	Radio Concert Player Scripts
 *
 *	@param	string	mediaFile
 *	@param	string	concertName
 *	@param	string	trackName
 *	@return	void
 */
function codSpawn(mediaFile, concertName, trackName){
	
	var fullURL = "http://www.cbc.ca/radio2/cod/codPlayer.html?"+mediaFile+"#"+concertName+"|"+trackName;
	window.open(fullURL, "codWindow", "location=0,menubar=0,resizable=0,width=470,height=200").focus();
	
} // END FUNCTION: codSpawn


/**
 *	Slight hack to allow paths inside javascript to use ApplicationRoot
 *	Specify paths as in css: url('/my/path') then provide a function called url that just returns the string
 *
 *	@param	string	p_path	The path to be returned
 *	@return	string
 */
function url( p_path ){
	
	return p_path;
	
} // END FUNCTION: url


/**
 *	Appends the application root to Urls
 *
 *	@param	string	p_url	The Url to get the path appended to
 *	@return	void
 */
function appRoot( p_url ){
	
	return applicationRoot + p_url;
	
} // END FUNCTION: appRoot


/**
 *	ConcertCalendar object for controlling the Ajax functionality
 */
var ConcertCalendar = {
	
	/**
	 *	Holds the current view's calendar, so the proper active states can be preserved
	 *
	 *	@access	public
	 *	@var	string
	 */
	currentCalendar : '',
	
	
	/**
	 *	Holds the last requested Url, for filtering
	 *
	 *	@access	public
	 *	@var	string
	 */
	lastUrl : '',
	
	
	/**
	 *	Initialises the toggle controls and the Ajax features
	 *
	 *	@access	public
	 *	@return	void
	 */
	initialise : function(){
		
		ConcertCalendar.initListenNow();
		ConcertCalendar.initDescriptions();
		
		$('.button').remove();
		
		$('.controls ul li a').click(
			function(){
				
				ConcertCalendar.switchView(this);
				
				return false;
				
			}
		);
		
		$('#month-title').click(
			function(){
				
				display = 'month';
				ConcertCalendar.switchView(this);
				
				return false;
				
			}
		);
		
		$('#calendar-broadcast').click(
			function(){
				ConcertCalendar.switchView(this);
			}
		);
		
		$('#calendar-concert').click(
			function(){
				ConcertCalendar.switchView(this);
			}
		);
		
		$('#programs').change(
			function(){
				ConcertCalendar.switchView(this);
			}
		);
		
		$('#locations').change(
			function(){
				ConcertCalendar.switchView(this);
			}
		);
		
		$('#genres').change(
			function(){
				ConcertCalendar.switchView(this);				
			}
		);
		
		$('#month-previous').click(
			function(){
				
				ConcertCalendar.switchCalendar('previous');
				
				return false;
				
			}
		);
		
		$('#month-next').click(
			function(){
				
				ConcertCalendar.switchCalendar('next');
				
				return false;
				
			}
		);
			
	}, // END PROPERTY: initialise
	
	
	/**
	 *	Initialises the Listen Now links
	 *
	 *	@access	public
	 *	@return	void
	 */
	initListenNow : function(){
		
		if( $('.listen-now a').length > 0 ){
			$('.listen-now a').click(
				function(){
					
					var id = $(this).attr('class').replace( /media-/, 'title-' );
					codSpawn( $(this).attr('href'), $('#'+id).html(), $(this).text() );
					
					return false;
					
				}
			);
		}
		
	}, // END PROPERTY: initListenNow
	
	
	/**
	 *	Hides all the descriptions if the display is week or month
	 *
	 *	@access	public
	 *	@return	void
	 */
	initDescriptions : function(){
		
		/**
		 *	Controls the toggling of the concert details
		 */
		$('.control').each(
			function(i){
				
				$(this).css( 'display', 'block' );
				
				$(this).click(
					function(){
						
						$('.body').eq(i).toggle();
						
						if( $(this).html() == '+' ){
							$(this).html( '&#8722;' );
						}else{
							$(this).html( '+' );
						}
						
						return false;
												
					} // END FUNCTION
				);

			} // END FUNCTION
		);
		
		if( display == 'week' || display == 'month' ){
			$('.control').click();
		}
		
	}, // END PROPERTY: initDescriptions
	
	
	/**
	 *	Sets the display to the proper view
	 *	
	 *	@access	public
	 *	@param	string	p_display	This is the display to set, if it is an invalid type, it will be ignored
	 *	@return	void
	 */
	setDisplay : function( p_display ){
		
		p_display = p_display.toLowerCase();
		
		if( p_display == 'day' || p_display == 'week' || p_display == 'month' ){
			display = p_display;
		}
		
		$('.controls .view li').removeClass('active');
		$('table.calendar tbody').removeClass('active');
		
		$('#view-'+display).addClass( 'active' );
		
		if( display == 'month' ){
			$('table.calendar tbody').addClass('active');
		}
		
	}, // END PROPERTY: setDisplay
	
	
	/**
	 *	Swtiches the current view to another view
	 *
	 *	@access	public
	 *	@param	string	p_this	The object that was clicked on
	 *	@return	void
	 */
	switchView : function( p_this ){
		
		$('.loader').remove();
		$('#cc-header ul').prepend( '<li class="loader"><span>Loading...</span></li>' );
		
		if( $(p_this).attr('href') == '' || $(p_this).attr('href') == undefined ){

			if( ConcertCalendar.lastUrl == '' ){
				var ajaxUrl = ConcertCalendar.getUrl( window.location.href );
			}else{
				var ajaxUrl = ConcertCalendar.getUrl( ConcertCalendar.lastUrl );
			}
			
			var newDisplay = display;
			
		}else{
			var ajaxUrl = ConcertCalendar.getUrl( $(p_this).attr('href') );
			ConcertCalendar.lastUrl = $(p_this).attr('href');
			var newDisplay = $(p_this).html();
		}

		ConcertCalendar.setDisplay( newDisplay );

		var xhr = $.ajax({
		    url: ajaxUrl,
		    type: 'GET',
		    dataType: 'json',
		    timeout: 45000,
		    error: function( XMLHttpRequest, textStatus, errorThrown ){
				//window.location = ajaxUrl;
		    },
		    success: function( json ){
				$('#concert-list').html( json.concertList );
				ConcertCalendar.initListenNow();
				ConcertCalendar.initDescriptions();
				$('#view-day a').attr( 'href', appRoot(json.day) );
				$('#view-week a').attr( 'href', appRoot(json.week) );
				$('#view-month a').attr( 'href', appRoot(json.month) );
				$('#view-previous a').attr( 'href', appRoot(json.previous) );
				$('#view-next a').attr( 'href', appRoot(json.next) );
				$('.controls h2').html( json.viewTitle );
				$('.loader').remove();
				
				$('table.calendar tbody').html( json.calendarBody );
				$('#month-previous').attr( 'href', appRoot(json.previousMonth) );
				$('#month-next').attr( 'href', appRoot(json.nextMonth) );
				$('#month-title').attr( 'href', appRoot(json.month) );
				$('#month-title').html( json.calendarTitle );
				
				currentCalendar = json.calendarTitle;
				ConcertCalendar.currentCalendar = json.calendarBody;
			}
		});
		
	}, // END PROPERTY: switchView
	
	
	/**
	 *	Swtiches the current calendar
	 *
	 *	@access	public
	 *	@param	string	p_this	The direct of calendar switchage
	 *	@return	void
	 */
	switchCalendar : function( p_direction ){
		
		$('.loader').remove();
		$('#cc-header ul').prepend( '<li class="loader"><span>Loading...</span></li>' );
		
		if( ConcertCalendar.currentCalendar == '' ){
			ConcertCalendar.currentCalendar = $('table.calendar tbody').html();
		}
		
		if( p_direction == 'previous' ){
			var theUrl = $('#month-previous').attr('href');
		}else{
			var theUrl = $('#month-next').attr('href');
		}
		
		var ajaxUrl = ConcertCalendar.getUrl( theUrl );
		
		var xhr = $.ajax({
		    url: ajaxUrl,
		    type: 'GET',
		    dataType: 'json',
		    timeout: 45000,
		    error: function( XMLHttpRequest, textStatus, errorThrown ){
				//window.location = ajaxUrl;
		    },
		    success: function( json ){

				$('.loader').remove();
				$('table.calendar tbody').html( json.calendarBody );
				$('#month-previous').attr( 'href', appRoot(json.previousMonth) );
				$('#month-next').attr( 'href', appRoot(json.nextMonth) );
				$('#month-title').attr( 'href', appRoot(json.month) );
				$('#month-title').html( json.calendarTitle );
	
				if( json.calendarTitle != currentCalendar ){
					$('table.calendar tbody').removeClass('active');
				}else{
					$('table.calendar tbody').html( ConcertCalendar.currentCalendar );
					if( display == 'month' ){
						$('table.calendar tbody').addClass('active');
					}
				}
				
			}
		});
		
	}, // END PROPERTY: switchCalendar
	
	
	/**
	 *	Composes the proper Ajax Url with the filters and such
	 *
	 *	@access	public
	 *	@param	string	p_url	The Url to compose
	 *	@return	string
	 */
	getUrl : function( p_url ){
		
		var baseUrl = p_url.replace( applicationRoot, applicationRoot + 'ajax/' );
		var filter = '/filter:';
		
		if( !$('#calendar-broadcast').attr('checked') ){
			filter += 'no;';
		}else{
			filter += 'yes;';
		}
		
		if( !$('#calendar-concert').attr('checked') ){
			filter += 'no;';
		}else{
			filter += 'yes;';
		}
		
		filter += $('#programs').attr('value')+';';
		filter += $('#locations').attr('value')+';';
		filter += $('#genres').attr('value');
		
		return baseUrl+filter;
	
	} // END PROPERTY: getUrl

} // END OBJECT: ConcertCalendar


/**
 *	Initialise the whole document
 */
if( document.getElementById && jQuery ){
	$(document).ready(
		function(){
			
			ConcertCalendar.initialise();
			
		} // END FUNCTION
	);
}
