var playerToggle, scrollbarInit, cycleInit, flashInit, lightboxInit, showNewsletterInit,
	cornerInit, listenInit, ieHoverInit, flashVideoPlayerInit, dynamicSearchInit,
	checkboxInit, labelifyInit, formInit, contentAllInit, launchBarCall, artworkInfoRolloverInit,
	showNewsletter, protocol, addthis_config, selectAlbum,
	bodyClassLookup, bodyClassLookupHash, bodyClassLookupTable,
	flashUrl, playerOptions, RecaptchaOptions,
	historyLoad, historyLookup, historyJS = true;

if(window.location.href.indexOf('https') < 0) {
	protocol = 'http://';
}
else {
	protocol = 'https://';
}
addthis_config = {
	data_use_flash:	false
};

siteUrl =  'http://www.elviscostello.com/';
flashUrl = 'http://www.elviscostello.com/flash/';

// For the AS3 part of the player to communicate with the AS2 part of the player :S
function getPlayer(){
	return document.getElementById('media-player-flash');
}

function selectAlbum(id){
	var swf = getPlayer();
	if(!swf) {
		return;
	}
	swf.selectAlbum(id);
};



function selectCoverflowAlbum(id){
	var swf = getPlayer();
	if(!swf) {
		return;
	}
	
	swf.selectCoverflowAlbum(id);
}

function addTrackQS(qs){
	var swf = getPlayer();
	if(!swf) {
		return;
	}
	
	swf.addTrackQS(qs);
}

function playerLoaded(){
	if(typeof autoPlayAudio !== 'undefined' && autoPlayAudio === true) {
		listenInit();
	}
}

playerOptions = {
	duration:	500,
	active:		'player-open',
	easing:		'swing',
	closed:		36,
	open:		558
};
bodyClassLookupTable = {
	'homepage':					'homepage',
	'footer-legal':				'legal static',
	'footer-sitemap':			'static sitemap',
	
	'nav-biography':			'static biography',
	'nav-lyrics':				'lyrics',
	'nav-the-wheel':			'the-wheel',
	'nav-guitar-room':			'guitar-room',
	'nav-tools':				'tools',
	'nav-news':					'news',
	'nav-shop':					'shop',
	'nav-tours-events':			'tours-events',

	'nav-lies-inventions':		'static biography lies-inventions',
	'nav-words':				'lyrics words',
	'nav-frets-struts':			'tools frets-struts',
	'nav-yellow-press':			'news yellow-press',
	'nav-press':				'news yellow-press',
	'nav-purchase':				'shop purchase',
	'nav-dance-card':			'tours-events dance-card',

	'nav-discography':			'discography discography-albums',
	'discography-albums':		'discography discography-albums',
	'discography-singles':		'discography discography-singles',
	'discography-dvd':			'discography discography-dvd',

	'nav-records':				'discography records discography-albums',
	'records-albums':			'discography records discography-albums',
	'records-singles':			'discography records discography-singles',
	'records-dvd':				'discography records discography-dvd',

	'nav-media':				'media media-audio',
	'media-audio':				'media media-audio',
	'media-video':				'media media-video',
	'media-gallery':			'media media-gallery',
	
	'nav-look-listen':			'media look-listen media-audio media-recordings',
	'media-recordings':			'media look-listen media-audio media-recordings',
	'media-flicks':				'media look-listen media-video media-flicks',
	'media-smudges':			'media look-listen media-gallery media-smudges',

	'media-account':			'media media-account',
	'media-details':			'media media-account',
	'media-details-thank-you':	'media media-account'
};
historyLookup = function(hash) {
	var bodyClass = '',
		active = null,
		playerOpen = false,
		title = 'Elvis Costello',
		options = {};

	if(hash && typeof bodyClassLookupTable !== 'undefined') {
		if(hash === '/' || hash === 'index.html') {
			bodyClass	= bodyClassLookupTable.homepage;
		}
		else if(hash.match(/^\/?(biography|lies-inventions)/)) {
			bodyClass	= bodyClassLookupTable['nav-lies-inventions'];
			active		= '#nav-lies-inventions';
			title		= 'Lies & Inventions § ' + title;
		}
		else if(hash.match(/^\/?(lyrics|words)/)) {
			bodyClass	= bodyClassLookupTable['nav-words'];
			active		= '#nav-words';
			title		= 'Words § ' + title;
		}
		else if(hash.match(/^\/?(discography-dvd|records-dvd)/)) {
			bodyClass	= bodyClassLookupTable['records-dvd'];
			active		= '#nav-records';
			title		= 'DVDs § Records § ' + title;
		}
		else if(hash.match(/^\/?(discography-singles|records-singles)/)) {
			bodyClass	= bodyClassLookupTable['records-singles'];
			active		= '#nav-records';
			title		= 'Singles § Records § ' + title;
		}
		else if(hash.match(/^\/?(discography|records)/)) {
			bodyClass	= bodyClassLookupTable['nav-records'];
			active		= '#nav-records';
			title		= 'Albums § Records § ' + title;
		}
		else if(hash.match(/^\/?the-wheel/)) {
			bodyClass	= bodyClassLookupTable['nav-the-wheel'];
			active		= '#nav-the-wheel';
			title		= 'The Wheel § ' + title;
		}
		else if(hash.match(/^\/?(tools|frets-struts)/)) {
			bodyClass	= bodyClassLookupTable['nav-frets-struts'];
			active		= '#nav-frets-struts';
			title		= 'Frets & Struts § ' + title;
		}
		else if(hash.match(/^\/?(news|yellow-press)/)) {
			bodyClass	= bodyClassLookupTable['nav-yellow-press'];
			active		= '#nav-yellow-press';
			title		= 'Yellow Press § ' + title;
		}
		else if(hash.match(/^\/?(media-gallery|look-listen-smudges|smudges)/)) {
			bodyClass	= bodyClassLookupTable['media-smudges'];
			active		= '#nav-look-listen';
			title		= 'Smudges § ' + title;
		}
		else if(hash.match(/^\/?product-detail/)) {
			bodyClass	= bodyClassLookupTable['nav-look-listen'];
			active		= '#nav-look-listen';
			title		= 'Product Detail § ' + title;
		}
		else if(hash.match(/^\/?(media-video|video-detail|look-listen-flicks|flicks)/)) {
			bodyClass	= bodyClassLookupTable['media-flicks'];
			active		= '#nav-look-listen';
			title		= 'Flicks § ' + title;
		}
		else if(hash.match(/^\/?(media-account|register|cart|forgot)/)) {
			bodyClass	= bodyClassLookupTable['media-account'];
			active		= '#nav-look-listen';
			title		= 'My Account § ' + title;
		}
		else if(hash.match(/^\/?(media|look-listen|look-listen-recordings|recordings)/)) {
			bodyClass	= bodyClassLookupTable['nav-look-listen'];
			active		= '#nav-look-listen';
			title		= 'Recordings § ' + title;
		}
		else if(hash.match(/^\/?(tours|events|dance-card)/)) {
			bodyClass	= bodyClassLookupTable['nav-dance-card'];
			active		= '#nav-dance-card';
			title		= 'Dance Card § ' + title;
		}
		else if(hash.match(/^\/?legal/)) {
			bodyClass	= bodyClassLookupTable['footer-legal'];
			title		= 'Legal § ' + title;
		}
		else if(hash.match(/^\/?site(-)?map/)) {
			bodyClass	= bodyClassLookupTable['footer-sitemap'];
			title		= 'Sitemap § ' + title;
		}
	}
if(jQuery('body').is('.' + playerOptions.active)) {
		bodyClass += ' ' + playerOptions.active;
		playerOpen = true;
	}

	options = {
		'bodyClass':	bodyClass,
		'active':		active,
		'playerOpen':	playerOpen,
		'title':		title
	};

	return options;
};
bodyClassLookup = function($$, $container){
	var bodyClass = '', containerId;

	containerId = $container.attr('id');

	if(typeof bodyClassLookupTable !== 'undefined') {
		if(typeof containerId !== 'undefined' && typeof bodyClassLookupTable[containerId] === 'string') {
			bodyClass = bodyClassLookupTable[containerId];
		}
		else if($container.is('h1.fn.org')) {
			bodyClass = bodyClassLookupTable.homepage;
		}
		else if($$.closest('#news-tabs').length === 1) {
			// from the homepage, check which is active, news or events
			if($$.closest('#news-tabs').find('ul li.active').attr('id').toLowerCase() === 'tab-list-tours-events') {
				bodyClass = bodyClassLookupTable['nav-dance-card'];
			}
			else {
				bodyClass = bodyClassLookupTable['nav-yellow-press'];
			}
		}
		else if($$.closest('#news-tabs-news, li.hentry, ul.tags, h2.entry-title').length === 1) {
			bodyClass = bodyClassLookupTable['nav-yellow-press'];
		}
	}
	if(jQuery('body').is('.' + playerOptions.active)) {
		bodyClass += ' ' + playerOptions.active;
	}

	return bodyClass;
};
contentAllInit = function() {
	if(jQuery.fn.addSlider) {
		jQuery('#content-slider').addSlider();
	}
	if(typeof dynamicSearchInit === 'function') {
		dynamicSearchInit();
	}
	if(typeof formInit === 'function') {
		formInit();
	}
	if(typeof checkboxInit === 'function') {
		checkboxInit();
	}
	if(typeof labelifyInit === 'function') {
		labelifyInit();
	}
	if(typeof scrollbarInit === 'function') {
		scrollbarInit();
	}
	if(typeof cycleInit === 'function') {
		cycleInit();
	}
	if(typeof lightboxInit === 'function') {
		lightboxInit();
	}
	if(typeof flashInit === 'function') {
		flashInit();
	}
	if(typeof ieHoverInit === 'function') {
		ieHoverInit();
	}
	if(typeof listenInit === 'function') {
		listenInit();
	}
	if(typeof showNewsletterInit === 'function') {
		showNewsletterInit();
	}
	if(typeof artworkInfoRolloverInit === 'function') {
		artworkInfoRolloverInit();
	}

	if(jQuery('body').is('.biography')) {
		jQuery('html, body').scrollTop(0);
	}

	if(jQuery('head script[src*="addthis.com"]').length === 1
	&& typeof addthis_open === 'function'
	&& typeof addthis_close === 'function') {
		jQuery('a.addthis_button').bind('mouseover mouseout', function(event){
			switch(event.type) {
				case 'mouseover':
					return addthis_open(this, '', '[URL]', '[TITLE]');
					break;

				case 'mouseover':
					return addthis_close();
					break;
			}
		});
	}
};

historyLoad = function(getUrl, follow) {
	var $$, $li, offset,
	history = historyLookup(getUrl);

	if(getUrl) {
		if(typeof follow !== 'undefined' && follow === false) {
			if(getUrl.match(/(records|discography|lyric|words|product|dvd|video)-(more|detail)/)) {
				// find the element the URL relates to
				$$	= jQuery('a.product[href*="' + getUrl + '"]');
				$li	= $$.closest('li');

				if($li.is(':not(.shown)')) {
					$$.trigger('click');
				}
			}

			return true;
		}

		jQuery.post(getUrl, {page: true}, function(html){
			//jQuery('#content').replaceWith(jQuery(html).find('#content'));
			jQuery('#content').replaceWith(html); // @paul
			document.title = history.title;

			jQuery('body').attr('class', history.bodyClass);
			if(typeof history.active === 'string') {
				jQuery(history.active).addClass('active').siblings().removeClass('active');
			}
			else {
				jQuery('#nav li').removeClass('active');
			}

			contentAllInit();

			if(history.playerOpen === true) {
				/*
				// player is open, scroll down to the content?
				offset = jQuery('#content').offset();
				if(jQuery(window).scrollTop() < 50) {
					jQuery('html, body').animate({
						scrollTop: offset.top
					}, 'slow');
				}
				*/
			
				// player is open, close it so content is shown
				jQuery('#media-player-flash').callFlash('player', null, 'close');
			}
		});
	}
};

if(typeof jQuery !== 'undefined') {
	jQuery.noConflict();
	jQuery(document).ready(function($) {
		var $body, $nav, $content, $launchBar, $overlay, $comingSoon, $contentSlider,
			$mediaPlayer,
			$galleryLightbox,
			customScrollbar = true,
			cycleOptions,
			$configSearch, $filterable,
			$listen, listenLength,
			wto, $area, search,
			addPlayer = true;

		/**
		* General
		*/
		$body			= $('body').addClass('jquery');
		$nav			= $('#nav');
		$content		= $('#content');
		$mediaPlayer	= $('#media-player');
		$overlay		= $('#overlay');
		$comingSoon		= $('#coming-soon');
		$contentSlider	= $('#content-slider');


		/**
		* @section Hover state for IE6
		* --------------------------------------------------------------------------------------------------
		*/
		ieHoverInit = function() {
			if($.browser.msie === true && $.browser.version < 7) {
				$('#content-config-view, #content-config-type, div.buy, ul.options li.listen, div.option').hover(
					function(event){
						$(this).addClass('hover');
					},
					function(event){
						$(this).removeClass('hover');
					}
				);
			}
		};
		ieHoverInit();


		/**
		* @section History
		* @see http://www.mikage.to/jquery/jquery_history.html
		* --------------------------------------------------------------------------------------------------
		*/
		if(historyJS === true && typeof $.historyInit !== 'undefined' && typeof $.historyLoad !== 'undefined') {
			// initialise the history plugin
			$.historyInit(historyLoad, 'jquery_history.html');

			// hijax these links
			navJsSelector  = '#nav a, #header h1 a, #footer-legal a, #footer-sitemap a, #content-config-nav a';
			navJsSelector += ', #news-tabs div.content a, h2.entry-title a, li.hentry a, ul.tags a';
			navJsSelector += ', li.lyrics a, p.view-album a, p.create-account a, #loginform a';
			navJsSelector += ', #content-config-mini-cart li a, #content-config-user a, #account-options a, #buy-options a, #cart-links a';
			navJsSelector += ', body.biography #content-primary div.pagination a';
			navJsSelector += ', body.sitemap #content-primary li a';
			
			$navJs = $(navJsSelector);
			$navJs.live('click.navigation', function(event){
				var $$, $container, getUrl;

				$$			= $(event.target).closest('a');
				$container	= $$.closest('li, p, h1');

				// do not want to auto-load these links
				if($container.is('#nav-shop, #nav-purchase, #footer-social-rss, #footer-social-facebook, #footer-social-myspace')) {
					return true;

				}
				if($$.length === 1) {
					$$.blur();
					event.preventDefault();

					getUrl	= $$.attr('href');
					getUrl	= getUrl.replace(document.location.hostname, '');
					getUrl	= getUrl.replace(document.location.protocol, '');
					getUrl	= getUrl.replace(/^.*#/, '');
					getUrl	= getUrl.replace(/\/{1,}/, '/');
					
					$.historyLoad(getUrl);
				}
			});
		}
		launchBarCall = function(id){
			var $$ = $('#' + id);
			if($$.length === 1) {
				$$.children('a').trigger('click');
			}
		};


		/**
		* @section AJAX forms
		* --------------------------------------------------------------------------------------------------
		*/
		formInit = function() {
			$('#content form').not('#search, #coming-soon form, #newsletter-signup').bind('submit.ajax', function(event){
				var $$ = $(this);

				$.ajax({
					url:		$$.attr('action'),
					type:		$$.attr('method'),
					data:		$$.serialize(),
					cache:		false,
					dataType:	'html',
					success:	function(data){
						//jQuery('#content').replaceWith(jQuery(data).find('#content'));
						jQuery('#content').replaceWith(data); // @Paul
						contentAllInit();
					},
					beforeSend:	function(XMLHttpRequest){},
					error:		function (XMLHttpRequest, textStatus, errorThrown) {},
					complete:	function(event, XMLHttpRequest, ajaxOptions) {}
				});

				event.preventDefault();
			});
		};
		formInit();


		/**
		* @section Social Links - new window
		* --------------------------------------------------------------------------------------------------
		*/
		$('#footer-social a, #nav-shop a, a[rel~=external], a.addthis_button').live('click', function(event){
			var $$, href;

			$$		= $(event.target).closest('a');
			href	= $$.attr('href');

			if($$.length === 1) {
				window.open(href);
				$$.blur();
				event.preventDefault();
			}
		});


		/**
		* @section Config
		* --------------------------------------------------------------------------------------------------
		*/
		$configSearch = $('#search');

		$('#content-config-view a, #content-config-type a').live('click.configField', function(event){
			var $$, $li, $ul, $div, href, field, type;

			$$			= $(event.target).closest('a');
			$li			= $$.closest('li');
			$ul			= $$.closest('ul');
			$div		= $$.closest('div[id]');
			href		= $$.attr('href');
			field		= href.replace(/.*\?id=([a-z0-9]*)/i, '$1');
			type		= href.replace(/.*\?type=([a-z0-9]*)/i, '$1');
			event.data	= {
				active: 'active'
			};

			if($li.is(':not(.' + event.data.active + ')')) {
				$li.siblings().filter('.selected').remove();
				$li.siblings().removeClass('hide').removeClass(event.data.active).children().removeClass(event.data.active);
				$li.clone().addClass('selected').prependTo($ul).addClass(event.data.active).children().addClass(event.data.active);
				$li.addClass('hide');
				
				if($div.is('#content-config-view') && typeof field !== 'undefined') {
					$.set_search_item('field', field); // @paul
				}
				if($div.is('#content-config-type') && typeof type !== 'undefined') {
					$.set_search_item('type', type); // @paul
				}

				// close the listing
				$ul.addClass('close');
				window.setTimeout(function(){
					$ul.removeClass('close');
				}, 75);
				
			}
			event.preventDefault();
			$$.blur();
		});

		/*
		$('#discography-albums a, #discography-singles a, #discography-dvd a').live('click.configSection', function(event){
			var $$, $li, $ul, section;

			$$			= $(event.target).closest('a');
			$li			= $$.closest('li');
			$ul			= $$.closest('ul');
			section		= $li.attr('id').toLowerCase().replace('discography-','');
			event.data	= {
				active: 'active'
			};

			if($$.is(':not(.' + event.data.active + ')')) {
				$li.addClass(event.data.active).children().addClass(event.data.active);
				$li.siblings().removeClass(event.data.active).children().removeClass(event.data.active);

				debug.log(section);
				//$.set_search_item('section', section); // @paul

				// close the listing
				$ul.addClass('close');
				window.setTimeout(function(){
					$ul.removeClass('close');
				}, 75);
			}
			
			event.preventDefault();
			$$.blur();
		});
		*/

		$('#content-config-filter a').live('click.configFilter', function(event){
			var $$, $li, $ul, href, filter;

			$$			= $(event.target).closest('a');
			$li			= $$.closest('li');
			$ul			= $$.closest('ul');
			href		= $$.attr('href');
			filter		= href.replace(/.*\?filter=([a-z0-9]*)/i, '$1');
			event.data	= {
				active: 'active'
			};

			if($li.is(':not(.' + event.data.active + ')')) {
				$li.addClass(event.data.active).children().addClass(event.data.active);
				$li.siblings().removeClass(event.data.active).children().removeClass(event.data.active);

				$.set_search_item('alphanum', filter); // @paul

				// close the listing
				$ul.addClass('close');
				window.setTimeout(function(){
					$ul.removeClass('close');
				}, 75);
			}
			event.preventDefault();
			$$.blur();
		});

		$('#search').filter('.auto-search').bind('submit.configSearch', function(event){
			var $$, $submit;

			$$		= $(event.target);
			$submit	= $('input:submit', $$);

			if($.fn.addSlider) {
				/*
				$contentSlider.addSlider({
					url: 'ajax/discography-list.html'
				});
				*/

				event.preventDefault();
				$submit.blur();
			}
		});


		/**
		* @section Slider setup
		* --------------------------------------------------------------------------------------------------
		*/
		if($.fn.addSlider) {
			$contentSlider.addSlider();
		}


		/**
		* @section Tabs
		* --------------------------------------------------------------------------------------------------
		*/
		$('.tabs ul.tab-list > li > a').live('click.tabs', function(event){
			var $$, $li, $ul, $parent,
				href, rel, hrefSplit, id, $div,
				active;

			$$		= $(event.target).closest('a');
			$li		= $$.closest('li');
			$ul		= $$.closest('ul');
			$parent	= $$.closest('div[id]');
			active	= 'active';

			if($$.is('a') && $ul.is('.tab-list')) {
				rel		= $$.attr('rel');
				href	= $$.attr('href');

				if(rel === '') {
					hrefSplit	= href.split('/');
					id			= 'tab-' + hrefSplit[hrefSplit.length - 1].replace('.html', '').replace(/^\/?/, '');
				}
				else {
					id = rel;
				}

				$div = $('#' + id);

				if($div.length === 1 && $div.is(':not(.' + active + ')')) {
					$div.add($li).addClass(active)
						.siblings().removeClass(active);

					$$.addClass(active);
					$li.siblings().children('a').removeClass(active);

					scrollbarInit('#' + id);
				}
				
				$$.blur();
				event.preventDefault();
			}
		});

		
		/**
		* @section Player
		* --------------------------------------------------------------------------------------------------
		*/
		playerToggle = function(shown){
			if($body.is('.' + playerOptions.active)) {
				$mediaPlayer.animate({height: playerOptions.closed}, {
					duration:	playerOptions.duration,
					easing:		playerOptions.easing,
					complete:	function(){
						$body.removeClass(playerOptions.active);
					}
				});
			}
			else {
				$mediaPlayer.animate({height: playerOptions.open}, {
					duration:	playerOptions.duration,
					easing:		playerOptions.easing,
					complete:	function(){
						$body.addClass(playerOptions.active);
					}
				});
			}
		};
		if($body.is('.' + playerOptions.active)) {
			// tell player to open...
			$mediaPlayer.callFlash('toggle', true, 'player');
		}


		/**
		* @section Listen buttons
		* --------------------------------------------------------------------------------------------------
		*/
		listenInit = function() {
			// fix the z-index issue
			$listen			= $('li.listen, a.listen, li.listen-play, li.listen-queue');
			listenLength	= parseInt($listen.length, 10) + 52000;
			$.each($listen, function(i){
				$(this).closest('ul.options, div.option').css('z-index', listenLength--);
			});
			
			// external interface for player
			$listen.bind('click.listen', function(event){
				var $$, $li, $listen, $ul, play_id, play, playCall;

				$$			= $(event.target).closest('a');
				$li			= $$.closest('li');
				$listen		= $$.closest('li.listen, div.option');
				$ul			= $$.closest('ul');
				play		= false;
				play_id		= (typeof $$.attr('rel') !== 'undefined' && $$.attr('rel') !== '') ? $$.attr('rel').replace('id-', '') : $listen.find('a[rel]:first').attr('rel').replace('id-', '');

				if($li.is('.listen-play, .listen') || ($$.is('.listen') && $listen.is('.option')) || $$.is('.listen.now')) {
					play		= true;
					playCall	= 'playTrack';
				}
				else if($li.is('.listen-queue')) {
					play		= true;
					playCall	= 'addTrackToPlaylist';
				}

				if(play === true) {
					$('#media-player-flash').callFlash(playCall, play_id, null);
				}

				// close the listing
				$ul.addClass('close');
				window.setTimeout(function(){
					$ul.removeClass('close');
				}, 75);

				event.preventDefault();
				$$.blur();
			});

			
			if(typeof autoPlayAudio !== 'undefined' && autoPlayAudio === true) {
				// timeout to make sure flash player has completed loaded
				//setTimeout(function(){
					$listen.filter('a.listen').trigger('click.listen');
				//}, 1000);
			}
		};
		listenInit();



		/**
		* @section Close buttons
		* --------------------------------------------------------------------------------------------------
		*/
		$('a.close, a.cancel').live('click.close', function(event){
			var $$, $hproduct, $popup, href, $div;

			$$			= $(event.target).closest('a');
			$hproduct	= $$.closest('div.hproduct');
			$popup		= $$.closest('#popup');
		
			if($$.is('a')) {
				if($hproduct.length === 1) {
					$hproduct.closest('li').children('a').eq(0).trigger('click.sliderContent', {force: 'close'});
				}
				else if($popup.length === 1) {
					$popup.children('form').fadeOut(1000, function(){
						$popup.closest('#overlay').fadeOut(500);
					});
				}
				else {
					href	= $$.attr('href');
					$div	= $(href);
					$div.hide();	
				}
				
				$$.blur();
				event.preventDefault();
			}
		});


		/**
		* @section Input default text
		* @desc Moves the associated label text inside the input as "default" value
		* @uses labelify()
		* @see http://www.kryogenix.org/code/browser/labelify/
		* --------------------------------------------------------------------------------------------------
		*/
		labelifyInit = function() {
			if($.fn.labelify) {
				$('#search').find('input:not(:submit):not([disabled])').labelify({
					text: 'label'
				});
				$('#coming-soon input:text').labelify();
			}
		};
		labelifyInit();


		/**
		* Checkbox / Radio Button styling
		* @uses checkbox()
		* @see http://widowmaker.kiev.ua/checkbox/
		* --------------------------------------------------------------------------------------------------
		*/
		checkboxInit = function() {
			if($.fn.checkbox) {
				$('input:checkbox').checkbox({
					cls:	'custom-checkbox',
					empty:	'http://img.elviscostello.com/css/misc/empty.png'
				}).closest('div.checkbox').addClass('custom-checkbox');
				/*
				$('input:radio').checkbox({
					cls:	'custom-radio',
					empty:  'http://img.elviscostello.com/css/css/misc/empty.png'
				}).closest('div.radio').addClass('custom-radio');
				*/
			}
		};
		checkboxInit();


		/**
		* @section Scrollbar
		* @uses jScrollPane()
		* @uses mousewheel() (optional)
		* @see http://www.kelvinluck.com/assets/jquery/jScrollPane/jScrollPane.html
		* --------------------------------------------------------------------------------------------------
		*/
		scrollbarInit = function(context){
			if(typeof customScrollbar !== 'undefined' && customScrollbar === true && $.fn.jScrollPane) {
				$(context).find('div.content:not(.video):visible').jScrollPane({
					scrollbarWidth:		16,
					scrollbarMargin:	0,
					showArrows:			false
				});
			}
		};
		scrollbarInit($content);


		/**
		* @section Rotating Quotes
		* @uses cycle()
		* @see http://www.malsup.com/jquery/cycle/
		* --------------------------------------------------------------------------------------------------
		*/
		cycleInit = function() {
			if($.fn.cycle) {
				cycleOptions = {
					fx:				'fade',			// name of transition effect
					timeout:		4000,			// milliseconds between slide transitions (0 to disable auto advance)
					speed:			500,			// speed of the transition
					startingSlide:	0,				// zero-based index of the first slide to be displayed,
					next:			null,			// selector for element to use as click trigger for next slide
					prev:			null,			// selector for element to use as click trigger for previous slide
					pager:			null,			// selector for element to use as pager container
					prevNextClick:	function(isNext, zeroBasedSlideIndex, slideElement) {
						$(this.next).add(this.prev).children('a').blur();
					},
					pagerClick:		function(zeroBasedSlideIndex, slideElement){
						$(this.pager).children('a').blur();
					}
				};
				
				$('div.artwork ul, img.gallery').each(function(i){
					var $$, $li, $div,
						$pager, $arrows,
						selector,
						options,
						$images,
						iiImages = 0, partsImages = [],
						partsArrow = [], iiArrow = 0,
						arrows = ['Previous', 'Next'];

					$$			= $(this);
					$div		= $$.parent();
					
					// build the gallery
					if($$.is('img')) {
						if(typeof galleryImages !== 'object') {
							return false;
						}

						$div		= $('<div />').addClass('gallery');
						$images		= $('<ul />').addClass($$.attr('class'));
						galleryImages.unshift($$.attr('src'));
						$.each(galleryImages, function(i, src){
							partsImages[iiImages++] = '<li>';
							partsImages[iiImages++] = '<img src="' + src + '" alt="" title="" />';
							partsImages[iiImages++] = '</li>';
						});
						$images.append(partsImages.join('')).appendTo($div);
						$$.replaceWith($div);
						$$ = $images;
					}

					$li			= $('li', $$);
					selector	= $div.get(0).tagName.toLowerCase() + '.' + $div.get(0).className + ' ';
					options		= {
						startingSlide:	$li.filter('.active').prevAll().length
					};

					// different effect
					if($$.is('.fade')) {
						options.fx = 'fade';
					}

					// create the arrows
					if($$.is('.prevNext') && $li.length > 1) {
						options.next = selector + '.next';
						options.prev = selector + '.previous';

						$arrows = $('<ul />').addClass('cycle-arrows');

						$.each(arrows, function(i, val){
							partsArrow[iiArrow++] = '<li class="' + val.toLowerCase() + '">';
							partsArrow[iiArrow++] = '<a href="#' + val.toLowerCase() + '">';
							partsArrow[iiArrow++] = val;
							partsArrow[iiArrow++] = '</a>';
							partsArrow[iiArrow++] = '</li>';
						});

						$arrows.append(partsArrow.join('')).appendTo($div);
						$div.addClass('has-cycle-arrows');
					}

					// create the clickable pager
					if(($$.is('.pager') || $div.is('.artwork')) && $li.length > 1) {
						options.pager = selector + '.cycle-pager';

						$pager = $('<ul />').addClass('cycle-pager');
						if($div.is('.artwork')) {
							$pager.css('width', (11 * $li.length) + 'px');
						}
						$pager.appendTo($div);
						$div.addClass('has-cycle-pager');
					}

					$$.cycle($.extend({}, cycleOptions, options));
				});
			}
		};
		cycleInit();


		/**
		* @section Lightbox image popup
		* @uses lightBox()
		* @see http://leandrovieira.com/projects/jquery/lightbox/
		* --------------------------------------------------------------------------------------------------
		*/
		lightboxInit = function() {
			if($.fn.lightBox) {
				$galleryLightbox = $('ul.gallery > li > a');
				$galleryLightbox.lightBox({
					overlayBgColor:			'#000',
					overlayOpacity:			0.85,
					containerBorderSize:	5,
					imageBlank:				'http://img.elviscostello.com/css/misc/lightbox/blank.gif',
					imageLoading:			'http://img.elviscostello.com/css/misc/lightbox/loading.gif',
					imageBtnClose:			'http://img.elviscostello.com/css/misc/lightbox/close.gif',
					imageBtnPrev:			'http://img.elviscostello.com/css/misc/lightbox/prev.gif',
					imageBtnNext:			'http://img.elviscostello.com/css/misc/lightbox/next.gif'
				});
			}
		};
		lightboxInit();


		/**
		* @section Coming Soon
		* --------------------------------------------------------------------------------------------------
		*/
	   showNewsletter = function(){
			$overlay = $('#overlay');
			$overlay.fadeIn(500, function(){
				// include the captcha
				$.getScript('http://api.recaptcha.net/js/recaptcha_ajax.js', function(){
					// create the captcha
					Recaptcha.create('6Lf-6wkAAAAAAGpLj6ih2nAlK4Wyuu4eAbeENXmr', 'recaptcha', {
					   theme:		'blackglass',
					   tabindex:	3
					});

					// fade us in
					$overlay.find('form').fadeIn(1000);
					$overlay.find('input:first').focus();
				});

			}).find('form').hide();
		};
		showNewsletterInit = function() {
			$('#coming-soon form').bind('submit.comingSoon', function(event){
				var $$, val;

				$$	= $(event.target).closest('form');
				val	= $('input.email', $$).val();

				$('input.email', $overlay).val(val);

				showNewsletter();
				event.preventDefault();
			});
		};
		showNewsletterInit();


		/**
		* @section IE
		* @subsection Fix Basic Rounded Corners
		* --------------------------------------------------------------------------------------------------
		*/
		cornerInit = function(){
			if($.fn.corner) {
				$('div.buy ul').corner('round 10px cc:#aeaeae');
			}
		};
		cornerInit();


		/**
		* @section Flash Embed
		* @uses swfobject
		* @see http://code.google.com/p/swfobject/
		* --------------------------------------------------------------------------------------------------
		*/
		flashInit = function() {
			if(typeof swfobject !== 'undefined' && swfobject.getFlashPlayerVersion().major > 8) {
				var flashParams					= {},
					flashFlashvars				= {},
					mediaPlayerflashParams		= {},
					mediaPlayerflashFlashvars	= {},
					theWheelflashParams			= {},
					theWheelflashFlashvars		= {},
					videoPlayerflashParams		= {},
					videoPlayerflashFlashvars	= {},
					launchBarParams				= {},
					launchBarFlashvars			= {},
					$mediaPlayerFlash,
					$theWheel,
					$theWheelFlash,
					$videoPlayer,
					$videoPlayerFlash;

				flashParams = {
					loop:				'false',
					menu:				'false',
					play:				'true',
					//salign:			'lt',
					scale:				'noscale',
					quality:			'high',
					allowScriptAccess:	'true',
					allowFullScreen:	'true'
				};
				
				// media player
				$mediaPlayer = $('#media-player');
				if($mediaPlayer.length === 1 && addPlayer === true) {
					addPlayer					= false;
					$mediaPlayerFlash			= $('<div />').attr('id', $mediaPlayer.attr('id') + '-flash');
					mediaPlayerflashParams		= $.extend({}, flashParams, {wmode: 'transparent'});
					mediaPlayerflashFlashvars	= $.extend({}, flashFlashvars, {});
					$mediaPlayer.append($mediaPlayerFlash);
					swfobject.embedSWF(flashUrl + 'player/player.swf', $mediaPlayerFlash.attr('id'), '100%', playerOptions.open, '8.0.0', null, mediaPlayerflashFlashvars, mediaPlayerflashParams, {}, function(){
						$body.addClass('hasMediaFlash');
					});
				}

				// the wheel
				$theWheel = $('#the-wheel');
				if($theWheel.length === 1) {
					$theWheelFlash			= $('<div />').attr('id', $theWheel.attr('id') + '-flash');
					theWheelflashParams		= $.extend({}, flashParams, {wmode: 'transparent'});
					theWheelflashFlashvars	= $.extend({}, flashFlashvars, {
						sXMLFile: siteUrl + 'wheelxml'
					});
					$theWheel.append($theWheelFlash);
					swfobject.embedSWF(flashUrl + 'wheel/wheel.swf', $theWheelFlash.attr('id'), 988, 514, '8.0.0', null, theWheelflashFlashvars, theWheelflashParams, {}, function(){
						$body.addClass('hasTheWheel');
						$theWheel.children('form').remove();
					});
				}

				// video
				flashVideoPlayerInit = function(id, cp, selector, context){
					$videoPlayer = $(selector, context);
					if($videoPlayer.length === 1) {
						$videoPlayerFlash			= $('<div />').attr('id', 'flash-' + id);
						videoPlayerflashParams		= $.extend({}, flashParams, {});
						videoPlayerflashFlashvars	= $.extend({}, flashFlashvars, {
							//cP: 'file:///Z:/Elvis Costello/Site - Main/' + cp
							videoPath: cp, // @paul;
							autoPlay: 1
							
						});
						$videoPlayer.replaceWith($videoPlayerFlash);
						swfobject.embedSWF(flashUrl + 'video/full_player.swf', $videoPlayerFlash.attr('id'), 350, 300, '9.0.0', null, videoPlayerflashFlashvars, videoPlayerflashParams, {}, function(){
							$body.addClass('hasVideoFlash');
							$('#' + $videoPlayerFlash.attr('id')).addClass('video-player');
						});
					}
				};
				if(typeof videoPlayer === 'object' && typeof videoPlayer.id !== 'undefined' && typeof videoPlayer.videoPath !== 'undefined') {
					flashVideoPlayerInit(videoPlayer.id, videoPlayer.videoPath, 'img.video-player', document);
				}
				
				// launch bar
				$launchBar = $('#launch-bar');
				if($launchBar.length === 1) {
					$launchBarFlash			= $('<div />').attr('id', $launchBar.attr('id') + '-flash');
					launchBarParams			= $.extend({}, flashParams, {wmode: 'transparent'});
					launchBarFlashvars		= $.extend({}, flashFlashvars, {
						sXMLFile: flashUrl + 'home/web.xml'
					});
					$launchBar.append($launchBarFlash);
					swfobject.embedSWF(flashUrl + 'home/ads.swf', $launchBarFlash.attr('id'), '100%', 113, '8.0.0', null, launchBarFlashvars, launchBarParams, {}, function(){
						$body.addClass('hasLaunchBarFlash');
					});
				}
			}
		};
		flashInit();



		/**
		* @section Search
		* --------------------------------------------------------------------------------------------------
		*/
		$area	= $('#area');
		search	= {
			section:	'',
			area:		$area.val(),
			alphanum:	'',
			field:		'',
			type:		'',
			term:		''
		};

		// search term switch and setup the slider
		$.set_search_item = function(field, value) {
			switch(field) {
				case 'alphanum':
					search.alphanum = value;
					search.term = '';
					break;

				case 'section':
					search.section = value;
					break;

				case 'term':
					search.term = value;
					search.alphanum = '';
					break;

				case 'field':
					search.field = value;
					break;

				case 'type':
					search.type = value;
					break;
			}
			search.area = $('#area').val();

			if($.fn.addSlider) {
				$contentSlider = $('#content-slider');
				if($contentSlider.length !== 0) {
					$contentSlider.addSlider({
						//url:		'ajax/discography.small.html'
						url:		'/include/ajaxFunctions.php', // @PAUL
						options:	search
					});
				}
			}
		};
		
		// dynamic search
		dynamicSearchInit = function() {
			$filterable		= $('.filterable');
			$configSearch	= $('#search');
			$area			= $('#area');
			if($filterable.length > 0) {
				$('input:text', $configSearch).bind('keyup.searchTerm', function(event){
					var $$, filter;

					$$		= $(this);
					filter	= $$.val();

					if($.trim(filter) === '') {
						$filterable.add($filterable.closest('tr')).removeClass('hidden').removeClass('filtered');
					}
					else {
						$filterable.each(function(){
							var $filter, $hide, headers;
							$filter = $(this);
							$hide	= $filter;

							if($filter.is('td') || $filter.is('th')) {
								$hide = $filter.closest('tr');
							}

							if($filter.text().search(new RegExp(filter, 'i')) < 0) {
								$hide.addClass('hidden').removeClass('filtered');
							}
							else {
								$hide.removeClass('hidden').removeClass('filtered');
								if($filter.is('td.in-album')) {
									headers = $filter.attr('headers').replace('cart-item ', '');
									$('#' + headers).closest('tr').removeClass('hidden').addClass('filtered');
								}
							}
						});
					}
				}).attr('autocomplete', 'off');
			}
			else {
				// ajax lookup search
				$configSearch.filter('.auto-search').find('input:text').bind('keyup.searchTerm', function(event){
					if(typeof wto !== 'undefined'){
						window.clearTimeout(wto);
					}
					wto = window.setTimeout(function() {
						$.set_search_item('term', $configSearch.find('input:text').val());
					}, 200);
				}).attr('autocomplete', 'off');

				if($area.length > 0) {
					//$.set_search_item('term', '');
				}
			}
			$configSearch.bind('submit.search', function(event){
				event.preventDefault();
			});
		};
		dynamicSearchInit();


		// rollover artwork shows information
		artworkInfoRolloverInit = function(){
			if($('body').is('.media')) {
				$('#content-slider li a.product').each(function(i, el){
					var $$, $span,
						title;

					$$		= $(el);
					title	= $$.attr('title');
					$span	= $('<span />').addClass('info').text(title);

					$$.append($span);
				});
			}
		}
		artworkInfoRolloverInit();

	});

	// everything for this page is downloaded
	// fetch other common used graphics
	jQuery(window).load(function() {
		jQuery.preloadImages(
			'http://img.elviscostello.com/css/overlay/blackout.gif',
			'http://img.elviscostello.com/css/overlay/mask.png',
			'http://img.elviscostello.com/css/overlay/popup.png',
			'http://img.elviscostello.com/css/sprites/buttons-action.jpg',
			'http://img.elviscostello.com/css/sprites/buttons.png',
			'http://img.elviscostello.com/css/sprites/buttons-small.gif',
			'http://img.elviscostello.com/css/sprites/tabs.jpg',
			'http://img.elviscostello.com/css/misc/hproduct/container.jpg',
			'http://img.elviscostello.com/css/misc/hproduct/container-lyrics.jpg',
			'http://img.elviscostello.com/css/misc/wheel/bg.jpg',
			'http://img.elviscostello.com/css/misc/wheel/push.gif',
			'http://img.elviscostello.com/css/misc/news/tab-container.jpg',
			'http://img.elviscostello.com/css/misc/news/container-repeat.jpg',
			'http://img.elviscostello.com/css/misc/guitar-room/bg.jpg',
			'http://img.elviscostello.com/css/misc/events/bg.jpg',
			'http://img.elviscostello.com/css/misc/events/table-body.jpg',
			'http://img.elviscostello.com/css/misc/cart/container.jpg',
			'http://img.elviscostello.com/css/misc/bio/container-repeat.jpg'
		);

		if(jQuery.fn.cycle) {
			jQuery('#elvis ul').cycle({
				fx:			'fade',
				timeout:	5000,
				speed:		500
			}).find('img').show();
		}
	});


	// Flash External Interface
	jQuery.fn.callFlash = function(callback, variables, prefix) {
		var $$, dom, data = 0;
		$$ = jQuery(this);
		if($$.length === 1) {
			dom = $$.get(0);
			if(typeof prefix === 'string') {
				callback = prefix + callback.charAt(0).toUpperCase() + callback.substr(1);
			}
			if(typeof dom !== 'undefined' && typeof dom[callback] === 'function') {
				data = dom[callback](variables);
			}
		}
		return data;
	};


	// Preload images
	jQuery.preloadImages = function() {
		jQuery.each(arguments, function(i){
			jQuery('<img />').bind('load', function(){}).attr('src', this);
		});
	};
}

/**
* @section Look & Listen - purchasing functions
* --------------------------------------------------------------------------------------------------
*/

// JavaScript Document
function fnAddItem(url){
	var qty = document.getElementById('qty').value
	window.location= url + '///' + qty;	
	return false;
}


function fnAddTracksMultiple(url){
	window.location= url;
	return false;
}

//=============================== Paypal ================================================//
//paypal callback functions
function processPPTx(data) {
	var url = "/scripts/php/paypal-response.php";
	jQuery.post(url, data, processPPTx_result);

}

function processPPTx_result(http) {
	var $pp,$pm, response, data, processTimeout;
	$pp = jQuery('#paypaloutput');
	$pm = jQuery('#paypalMsg');
	response = http.split("|-|");
	if (response[0] == '0' || response[0] == 0) {
		$pp.html('<div><p>Verifying.  Please wait.</p></div>');
		data = {
			'cm': response[1],
			'tx': response[2],
			'amt': response[3],
			'st': response[4]
		};
		processTimeout = setTimeout(processPPTx, 2000, data);
	} else {
		$pp.html(response[1]);
		$pm.html(response[2]);
		clearTimeout(processTimeout);
	}
}

if(typeof processPPTx === 'function' && typeof processPPTxData === 'object') {
	processPPTx(processPPTxData);
}


function chkResetPassword(){
	if(jQuery('#email').val() == ""){
		jQuery('#val_email').html("must be completed");
		return false;
	}else{
		jQuery('#val_email').html("");
	}
	if(jQuery('#request_code').val()  == ""){
		jQuery('#val_request_code').html(" must be completed");
		return false;
	}else{
		jQuery('#val_request_code').html("");
	}
	if(jQuery('#new_password').val()  == ""){
		jQuery('#val_new_password').html("must be completed");
		return false;
	}else{
		jQuery('#val_new_password').html("");
	}
	if(jQuery('#verify_password').val()  == ""){
		jQuery('#val_verify_password').html(" must be completed");
		return false;
	}else{
		jQuery('#val_verify_password').html("");
	}
	if(jQuery('#new_password').val()  != jQuery('#verify_password').val()){
		jQuery('#val_verify_password').html(" does not match");
		return false;
	}else{
		jQuery('#val_verify_password').html("");
	}
	return true;
}

function chkChangeDetails(){
	if(jQuery('#password').value == ""){
		jQuery('#val_password').html("You need to enter you password for the changes to take affect");
		return false;
	}else{
		jQuery('#val_password').html("");
	}
	if(jQuery('#customer_firstname').value == ""){
		jQuery('#val_firstname').html("Please enter your first name");
		return false;
	}else{
		jQuery('#val_firstname').html("");
	}
	if(jQuery('#customer_surname').value  == ""){
		jQuery('#val_surname').html("Please enter your surname");
		return false;
	}else{
		jQuery('#val_surname').html("");
	}
	if(jQuery('#new_password').value  != "" && jQuery('#new_password').value != jQuery('#verify_password').value){
		jQuery('#val_verify_password').html("The two new password don't match!");
		return false;
	}
	return true;
}