if(typeof jQuery != 'undefined') {
	jQuery.noConflict();
	jQuery(document).ready(function($) {
		var $body, $player, $tabs, $lists,
			$imageDialog,
			$flash, flashDefaults,
			$audioPlayer, audioPlayerId,
			customScrollbar = true;
		
		$body	= $('body');
		$player = $('#media-player');
		$tabs	= $('div.tab', $player);
		$lists	= $('div.list', $tabs);
	
	// 11/08/2009 14:42 SMK: Added innerfade
	$('ul#our_sponsors').innerfade({
		speed: 'slow',
		timeout: 4000,
		type: 'sequence',
		containerheight: '45px'
	});

	// 25/08/2009 14:42 SMK: Added innerfade
	$('ul#header_gallery').innerfade({
		speed: 'slow',
		timeout: 4000,
		type: 'sequence',
		containerheight: '198px'
	});

		
		// cross-browser stripping...
		$('li', $tabs).filter(':odd').addClass('odd');
		
		
		// setup the dialog
		$imageDialog = $('<div />').attr('id', 'imageDialog').dialog({
			autoOpen:		false,
			resizable:		false,
			draggable:		false,
			closeOnEscape:	true,
			stack: 			false,
			position: 		['center', 50],
			width: 			500,
			height:			413,
			modal: 			true,
			show: 			'drop'
		});
		
		
		// setup the video player
		flashDefaults = {
			height:				180,
			width:				240,
			allowFullScreen:	true,
			allowScriptAccess:	'always'
		};
		
		// initialise the first video		
		$flash = $('#video-player');
		$flash.flash($.extend(flashDefaults, {
			swf: $flash.closest('div[id].tab').find('div.list li.active a').attr('href')
		}));
		
		
		// setup the audio player
		AudioPlayer.setup('/flash/audio/player.swf', {
			initialvolume:		'60',
			transparentpagebg:	"yes",
			width:				'100%',
			autostart: 			'no',
			loop:				'no',
			animation:			'no',
			bg: 				'393C3E',
			leftbg: 			'4C4F52',
			rightbg: 			'4C4F52',
			lefticon:			'C8D9E4',
			righticon:			'C8D9E4',
			rightbghover:		'4C4F52',
			voltrack:			'393C3E',
			volslider:			'C8D9E4',
			tracker:			'4C4F52',
			track: 				'393C3E',
			border: 			'393C3E',
			text: 				'FFFFFF',
			loader:				'C8D9E4'
		});
		
		// initialise the first audio entry
		audioPlayerId 	= $.audioPlayerId();
		$audioContent	= $tabs.filter('#media-player-audio').find('div.content');
		$audioPlayer	= $audioContent.children('a.audio').attr('id', audioPlayerId);
		
		AudioPlayer.embed(audioPlayerId, {  
			soundFile:	$audioPlayer.attr('href'),
			titles:		$audioContent.children('div.info').children('h4').text(),
			artists:	$audioContent.children('div.info').children('p').text()
		});
		
		
		// Our custom replace content section
		$lists.bind('click.loadContent', {active: 'active', duration: 750}, function(event){
			var $$, $li, $div, $content, $list,
				type, updateContent,
				contentPath, contentTitle, contentDesc,
				$info, $title, $description,
				$media, $image, $param, $embed,
				audioPlayerId;
			
			$$ 	= $(event.target).closest('a');
			if($$.is('a') && !$$.parent().is('.active')) {
				$li				= $$.closest('li');
				$div 			= $li.closest('div[id].tab');
				$content		= $div.children('div.content');
				$list			= $div.children('div.list');
				type			= $div.attr('id').toLowerCase().replace('media-player-', '');
				updateContent 	= false;
				
				contentPath		= $$.attr('href');
				contentTitle	= $$.children('span.title, span.artist').text();
				contentDesc		= $$.children('span.description').text();
				
				$info 			= $('<div />').addClass('info');
				$title			= $('<h4 />').text(contentTitle);
				$description	= $('<p />').text(contentDesc);
				
				// find out which type of content we are creating
				switch(type) {
					case 'images':
						updateContent = true;
						
						$media	= $('<a />').attr('href', contentPath.replace('_m.jpg', '.jpg'))
											.addClass('dialog')
											;
						
						$image	= $('<img />').attr('src', contentPath)
											  .appendTo($media)
											  ;
						break;
						
					case 'videos':
						updateContent = true;
						
						$media 	= $('<div />').attr('id', 'video-player')
											  .flash($.extend(flashDefaults, {
											  		swf: contentPath
												}))
											  ;
						
						break;
						
					case 'audio':
						updateContent = true;
						audioPlayerId = $.audioPlayerId();
						
						$media 	= $('<a />').addClass('class')
											.attr('src', contentPath)
											.attr('id', audioPlayerId)
											;
						break;
				}
				
				// if we are still updating, hide the content, empty, replace and fade in
				if(contentPath !== '' && updateContent === true) {
					$info.append($title).append($description);
					$content.wrapInner('<div />').children().fadeOut(event.data.duration, function(){
						$content.empty().append('<div />').children().hide().append($media).append($info);
						
						// initialise audio after it is added to the DOM
						if(type === 'audio' && typeof audioPlayerId !== 'undefined') {
							AudioPlayer.embed(audioPlayerId, {  
								soundFile:	contentPath,
								titles:		contentTitle,
								artists:	contentDesc
							});
						}
						
						$content.children().fadeIn(event.data.duration);
					}).find('#video-player').html('');
					
					$div.find('li').removeClass(event.data.active);
					$li.addClass(event.data.active);
				}
			}
				
			$$.blur();
			event.preventDefault();
		});
		
		
		// Tabs
		if($.ui && $.ui.tabs) {			
			$tabs.buildTabs().parent().tabs({
				fx:		{
					opacity:	'toggle',
					duration:	500
				},
				cache:	true,
				select:	function(event, ui) {},
				show:	function(event, ui) {
					//$.scrollBar($(ui.panel).find('div.list'), customScrollbar);
				}
			}).bind('tabsselect', function(event, ui) {
				$(ui.tab).blur();
			});
		}
		
		
		// Accordion
		if($.ui && $.ui.accordion) {
			$tabs.children('div.accordion').accordion({
				header: 		'h3.accordion-title',
				active:			'li.default h3',
				animated:		'slide',
				autoHeight:		false,
				collapsible:	true,
				change:			function(event, ui) {
					$.scrollBar($lists, customScrollbar);
				},
				changestart:	function(event, ui) {
				}
			}).find('li:has(ul)').addClass('accordion-heading');
		}
		
		
		// Dialog
		if($.ui && $.ui.dialog) {			
			$('a.dialog').live('click.dialogOpen', function(event){
				var $$, $div, title,
					$img;
				
				$$ 		= $(event.target).closest('a');
				$div	= $$.closest('div');
				title	= $div.find('h4').text();
				$img	= $('<img />').attr('src', $$.attr('href'));
				
				$imageDialog.html($img)
							.dialog('option', 'title', 	title)
							.dialog('open');
							
				$$.blur();
				event.preventDefault();
			});
			
			$body.bind('click.closeDialog', function(event){
				if($(event.target).closest('.ui-dialog').length !== 1) {
					$imageDialog.dialog('close');
				}
			});
		}
		
		
		// custom scrollbars
		$.scrollBar($lists, customScrollbar); 
	});
	
	
	(function($){
		jQuery.fn.buildTabs = function(){
			var $ul = $('<ul />').addClass('ui-tabs-nav');
			$(this).each(function(){
				var $$, $title,
					id, text,
					$li, $a;
				
				$$ 		= $(this);
				$title 	= $('h2.tab-title', $$);
				
				id		= $$.attr('id');
				text	= $title.text();
				
				$li		= $('<li />');
				$a		= $('<a />').text(text).attr('href', '#' + id);
				
				if($$.is('.default')) {
					$li.addClass('ui-tabs-selected');
				}
				
				$ul.append($li.append($a));
			}).addClass('ui-tabs-panel').parent().prepend($ul);
			
			return this;
		};
		
		$.audioPlayerId = function() {
			return 't' + new Date().getTime();
		};
		
		$.scrollBar = function($element, enabled) {
			if(typeof enabled !== 'undefined' && enabled === true && $.fn.jScrollPane) {
				$element.jScrollPane();
			}
		};
		
	})(jQuery);
}

// private debug function
var debug = function($obj) {
	if(window.console && window.console.log) {
		window.console.log($obj);
	}
};