/* BON JOVI */
/* Javascript Triggers and Custom Functions
----------------------------------------------------------------------------- */


	$(window).load(function() {

		if (section != "messageboard" && section != "checkout") {

			if (page == "index") {

				highlights();
				twitter();

			}

			if (page == "backstagejbj") {
				
				highlights();
				featuredFan();
				
			}
			
			if (page == "live" || page == "test-lastnight") {
			
				lastNight();
				
			}
					
			if (page == "contest" || page == "mission") contestLegals();
			if (page == "release") swapAlbumCovers();

			// CLIQUE UPSELL PANEL
			if (page == 'login') {

				// START CYCLE
				$('ul.benefits').before('<div class="pager">').cycle({

					fade: true,
					speed: 400,
					timeout: 9000,
					pager: 'div.pager',
					before: function() { if (window.console) console.log(this.src); }

				});

				// CUSTOM PAGER TITLES
				$('div.pager a:nth-child(1)').livequery(function() { $(this).addClass('presales'); });
				$('div.pager a:nth-child(1)').livequery(function() { $(this).text('Presales'); });
				$('div.pager a:nth-child(2)').livequery(function() { $(this).addClass('messages'); });
				$('div.pager a:nth-child(2)').livequery(function() { $(this).text('Exclusive Videos'); });
				$('div.pager a:nth-child(3)').livequery(function() { $(this).addClass('community'); });
				$('div.pager a:nth-child(3)').livequery(function() { $(this).text('Community'); });
				$('div.pager a:nth-child(4)').livequery(function() { $(this).addClass('packages'); });
				$('div.pager a:nth-child(4)').livequery(function() { $(this).text('VIP Access'); });
				$('div.pager a:nth-child(5)').livequery(function() { $(this).addClass('meet'); });
				$('div.pager a:nth-child(5)').livequery(function() { $(this).text('Meet & Greets'); });
				$('div.pager a:nth-child(6)').livequery(function() { $(this).addClass('archives'); });
				$('div.pager a:nth-child(6)').livequery(function() { $(this).text('Archives'); });

			}

			// PLUGIN: CLIQUE ACTIONS (Replaces submit buttons with text links)
			try { $('input[type=submit]').cliqueActions(); } catch (error) { alert('Error: Missing Clique Actions Plugin'); }

			// STORE-ONLY
			if (section == "store") {

				// PLUGIN: CLIQUE FORMS
				try {
				$('.widget-item.search input#query').cliqueForms({ placeholder: "Search Store" });
				$('.widget-item.search input#query').css('visibility', 'visible'); // Will not chain to previous function?
				$('#mailing_list_entry_email').cliqueForms({ placeholder: "Enter Your Email Address" });
				$('#mailing_list_entry_mobile').cliqueForms({ placeholder: "Enter Your Mobile Number" });
				} catch (error) { alert('Error: Missing Clique Forms Plugin'); }

				// PLUGIN: CLIQUE ITEM WIDGET
				try {
				$('div.widget-item.display:not(.photos, .gift)').cliqueItemWidget({

					checkStock: true,
					pressure: true,
					browser: 'thumbnails', // thumbnails / slideshow
					removeEmptyThumbs: true, // Removes rows of empty thumbnails
					viewer: true, // Shows enlarged images
					preview: false, // Preview enlarged images when hovering thumbnails
					zoom: 'drag' // loupe / drag / none

				});
				$('.widget-item.browse.tooltip').cliqueItemWidget();
				$('.widget-item.filter').cliqueItemWidget();
				} catch (error) { alert('Error: Missing Clique Item Plugin'); }

				// PLUGIN: CLIQUE ALERT WIDGET
				try { $('div.widget-alert.added').cliqueAlertWidget(); } catch (error) { alert('Error: Missing Clique Alert Plugin'); }

				// PLUGIN: CLIQUE UPLOADER
				if (page == "item" && $('#review').length > 0) { try { $('#review').cliquePhotoUploader(); } catch (error) { alert('Error: Missing Clique Uploader Plugin'); } }

			}

			readMore();
			markup();
			tabs();
			centerActions();
			memberPlaceholders();
			comments();
			chat();
			videoUploader();
			banners();
			editorialActions();
		
			if (page == "index") countdown();
			if (page != "login" && section !== "checkout" && page !== "referral") cufon();
		
		}

		$('#hide_page').show();
		downloadBlocker();
		$('#title').show();

	});
	

/* FUNCTION: Automatic Actions in Editorial Content
----------------------------------------------------------------------------- */


	function editorialActions() {

		if (section == "contests") {
						
			$('div.story > ul > li > a').each(function() {
			
				$(this).addClass('action');
				$(this).parent('li').parent('ul').addClass('actions');
			
			});
		
		}

		// Convert Code Blocks into Selectable Inputs

		$('code').click(function() {

			if ($(this).next().hasClass('code')) {
				$(this).hide();
				$(this).next().show().focus().select();
			} else {
				$("<input class='code' type='text' value='" + $(this).text() + "' />").insertAfter($(this));
				$(this).hide();
				$(this).next().focus().select();
			}
		
			$(this).next().blur(function() {
				$(this).prev().show();
				$(this).hide();
			});
	
		});

	}
	
	

/* FUNCTION: Featured Member
----------------------------------------------------------------------------- */


	function featuredFan() {
		
		var profileLink = $('#featured-fan div.widget-member a.alias').attr('href');
		
		$('#featured-fan ul.actions.profile li a.profile').attr('href', profileLink);
		$('#featured-fan div.widget-member dl dt.comments').text('Comments');
		$('#featured-fan div.slideshow').each(function() {

			photoSlideshow();
			
		});
		
	}


/* FUNCTION: LAST NIGHT
----------------------------------------------------------------------------- */


	function lastNight() {

		if ($('#last-night').length > 0) {

			var link = $('#last-night div.widget-event a.details').attr('href');

			// APPEND LOCATION TO HEADING
			var location = $('#last-night div.widget-event h4 address').text();
			$('#last-night div.widget-event h3').text("Last Night In " + location);
			$('#last-night div.widget-event h3').wrapInner('<a href="' + link + '"></a>');

			// APPEND ACTION LINKS
			var actions = '';
			if (signed_in) actions += '<li><a class="action upload" href="/upload' + link.replace('show/', '') + '">Upload A Photo or Video</a></li>';
			actions += '<li><a class="action setlist" href="' + link + '">See The Set List</a></li>';
			actions += '<li><a class="action went" href="' + link.replace('show/', 'members/') + '">See Who Went</a></li>';
			actions += '<li><a class="action discuss" href="' + link + '">Discuss The Show</a></li>';
			$('#last-night div.widget-event > ul.actions').prepend(actions);

			// SHOW CONTAINER
			$('#last-night').show();

		}

	}
	

/* FUNCTION: HIGHLIGHT NEWS
----------------------------------------------------------------------------- */


	function highlights() {

		$('div.widget-editorial.highlights').each(function() {
		
			$featured = $(this);
			
			var numberOfLeads = $featured.children('ul.leads').children().length;

			switch(numberOfLeads) {

				case 4:
					$featured.addClass('four');
					break;
				case 3:
					$featured.addClass('three');
					break;
				case 2:
					$featured.addClass('two');
					break;
				case 1:
					$featured.addClass('one');
					break;

			}

			// SELECT FIRST PANEL
			$featured.children('ul.leads > li:first').addClass('first selected');

			// MAKE ENTIRE PANEL CLICKABLE
			$featured.children('ul.leads > li').css('cursor', 'hover').click(function() {
			
				window.location = $(this).find('a.display').attr('href');

			});
					
			// TRUNCATIONS
			$featured.find('div.story .lead').hide();

			if ($featured.hasClass('four')) {

			//	$featured.find('h4 a').truncate({ length: 45 });
				$featured.find('div.story .lead:first-child').show().truncate({ length: 45 });
			
			} else if ($featured.hasClass('three')) {

				$featured.find('h4 a').truncate({ length: 50 });
				$featured.find('div.story .lead:first-child').show().truncate({ length: 100 });
			
			} else if ($featured.hasClass('two')) {

				$featured.find('h4 a').truncate({ length: 65 });
				$featured.find('div.story .lead:first-child').show().truncate({ length: 200 });

			} else {

				$featured.find('h4 a').truncate({ length: 80 });
				$featured.find('div.story .lead:first-child').show().truncate({ length: 400 });
			
			}
			
			if (numberOfLeads > 1) {
			
				// START TIMER
				var timer = setInterval( "carousel($featured)", 10000 ); // Custom carousel timer
	
				// PAUSE CAROUSEL ON HOVER + RESTART CAROUSEL ON HOVER OFF
				$featured.find('ul.leads > li').hover(function() {
	
					$featured.find('ul.leads > li').removeClass('selected');
					$(this).addClass('selected');
					Cufon.refresh('div.widget-editorial.highlights h4');
					clearInterval(timer); // Pause carousel
		
				}, function() {
		
					timer = setInterval( "carousel($featured)", 10000 ); // Unpause carousel
		
				});
			
			}
		
			$featured.show();
		
		});
	
	}
	
	function carousel($list) {

		var $selected = $list.find('.selected');

		if ($selected.hasClass('last')) {

			var $next = $selected.parent().children().eq(0); // Select first item in list

		} else {

			var $next = $selected.next();

		}

		$next.addClass('selected');
		$selected.removeClass('selected');
		Cufon.refresh('div.widget-editorial.highlights h4');

	}


/* FUNCTION: CONTEST LEGALS
----------------------------------------------------------------------------- */


	function contestLegals() {

		$('div.widget-poll input[type=submit]').val('Enter the Contest');

		// Create Legals Box & Expand/Contract Legals Button
		$('div.story').find('ins').parent('p').wrap('<ul class="actions"><li><a href="#" class="action legal expand"></a></li></ul>');
		$('div.story').find('a.legal').text('Expand Terms and Conditions');
		$('div.story').find('ul.actions').nextAll().wrapAll('<div class="legal"><div class="markup"><div></div></div></div>');
		$('div.story').find('a.legal').click(function() {
	
			if ($(this).hasClass('expand')) {
			
				$(this).text('Collapse Terms and Conditions');
				$(this).removeClass('expand').addClass('collapse');
				$(this).parent('li').parent('ul.actions').siblings('div.legal').addClass('expand');
			
			} else {

				$(this).text('Expand Terms and Conditions');			
				$(this).removeClass('collapse').addClass('expand');
				$(this).parent('li').parent('ul.actions').siblings('div.legal').removeClass('expand');

			}

			if (typeof(window['Cufon']) != "undefined") Cufon.refresh();
			return false;

		});
		
		if (page == "contest") {
			
			$('div.story div.legal p:first').prepend('<h3>Contest Terms and Conditions</h3>').show();
			
		} else if (page == "mission") {
			
			$('div.story div.legal p:first').prepend('<h3>Mission Terms and Conditions</h3>').show();
			
		}
		
		
		// Additional Functionality for Legal Agreement
		if ($('div.story div.legal').length > 0 ) {
			
			if (page == "contest") {
			
				$('div.modify.submit-media div.group.details fieldset').append('<label class="terms"><input id="contest_terms" type="checkbox" value="1" name="contest_terms" /><em>I agree to the Contest terms set forth above</em></label><br />');
			
			} else if (page == "mission") {
			
				$('div.modify.submit-media div.group.details fieldset').append('<label class="terms"><input id="contest_terms" type="checkbox" value="1" name="contest_terms" /><em>I agree to the Mission terms set forth above</em></label><br />');
			
			}
			
			// Toggling Submit Button
			$('div.modify.submit-media ul.actions input').attr('disabled','disabled').addClass('disabled');
			$('div.modify.submit-media').addClass('disabled');
			$('div.modify.submit-media ul.actions a.submit').hide();
			
			$('div.widget-photo.modify.submit-media ul.actions li:first').append('<em>Submit Photo</em>');
			$('div.widget-media.modify.submit-media ul.actions li:first').append('<em>Submit Video</em>');
										
			$('div.modify.submit-media div.group.details label.terms input').click(function() {
								
				if ( $(this).parent().parent().parent().siblings('ul.actions').children('li.submit').children('input').hasClass('disabled') ) {
									
					$(this).parent().parent().parent().siblings('ul.actions').children('li.submit').children('input').removeAttr('disabled');
					$(this).parent().parent().parent().siblings('ul.actions').children('li.submit').children('input').removeClass('disabled');
					$(this).parent().parent().parent().parent('div.modify.submit-media').removeClass('disabled');
					$(this).parent().parent().parent().siblings('ul.actions').children('li.submit').children('input').siblings('a.submit').show();
					$(this).parent().parent().parent().siblings('ul.actions').children('li.submit').children('input').siblings('em').hide();
				
				} else { 
										
					$(this).parent().parent().parent().siblings('ul.actions').children('li.submit').children('input').attr('disabled','disabled');
					$(this).parent().parent().parent().siblings('ul.actions').children('li.submit').children('input').addClass('disabled');
					$(this).parent().parent().parent().parent('div.modify.submit-media').addClass('disabled');
					$(this).parent().parent().parent().siblings('ul.actions').children('li.submit').children('input').siblings('a.submit').hide();
					$(this).parent().parent().parent().siblings('ul.actions').children('li.submit').children('input').siblings('em').show();
					
				}
				
			});
			
			
		}
		
	}


/* FUNCTIONS: TWITTER
----------------------------------------------------------------------------- */


	// LOAD TWITTER FEED
	function twitter() {

		$('#twitter div.tweets').tweet({
					
			username: "bonjovi",
			count: 5,
			loading_text: "Loading tweets..."
			
		});

	}

	// ROTATE TWEETS
	function rotateTweets() {

		$('#twitter ul.tweet_list').cycle({

			speed: 300,
			timeout: 7000,
			before: adjustTweetHeight

		});

		$('#twitter').show();

	}

	// ADJUST TWEET HEIGHT
	function adjustTweetHeight() {

		$('div.tweets').animate({ height: $(this).outerHeight() });

	}


/* FUNCTION: SWAP FRONT + BACK ALBUM COVERS
----------------------------------------------------------------------------- */


	function swapAlbumCovers() {

		$('#covers li.back img').hover(function() {

			var cover = $(this).attr('src');

			$(this).parent().siblings().find('img').attr('src', cover);

		}, function() {

			var cover = $(this).attr('src').replace("Back", "Front");

			$(this).parent().siblings().find('img').attr('src', cover);

		});

	}


/* FUNCTION: DOWNLOAD BLOCKER
----------------------------------------------------------------------------- */

	
	function downloadBlocker() {

		$('div.widget-photo.display img').each(function() {

			var width = $(this).width();
			var height = $(this).height();
			
			$(this).after('<img class="blocker" src="' + asset_host + '/images/content/blocker.gif" width="' + width + '" height="' + height + '" alt="" style="position: absolute; z-index: 15; display: block; top: 0; left: 0; margin: 0;" />');
		
		});

	}


/* FUNCTION: MARKUP CHANGES
----------------------------------------------------------------------------- */


	function markup() {

		// TRUNCATIONS
		$('div.widget-media.titled div a').truncate({ length: 60 }); // Compact video thumbnails
		$('div.grid.three_fourths.store #content ul.items dt a').truncate({ length: 35 }); // Store item listings

		// EVENT WIDGET
		/*$('#set a.add').text("Submit A Set List");
		$('#set.placeholder p').text("There are currently no set lists shared for this event, do you have one to submit?");
		$('#last-night a.browse').text("See Other Past Shows");
		$('#shows a.browse').text("See More Upcoming Shows");
		$('#going a.browse').text("See More Upcoming Shows");
		$('#gone a.browse').text("See Other Past Shows");*/

		// MISCELLANEOUS
		$('#uploaded a.share').text('Upload a Photo');
		$('#comments a.submit').text('Post Comment');
		$('#page-welcome div.widget-account a.submit').text('Continue');
		$('<p>Sign up for free and receive updates sent to your e-mail or phone.</p>').insertAfter('#mailinglist h3');
		$('#page-qas #qas > ul.actions').append('<li class="last"><a class="action ask" href="/ask/Questions_For_Obie/Questions_For_Obie">Ask Obie</a></li>');
		$('div.widget-media.player.video a.details').text("Details");
		// $('div.widget-media.player.video ul.actions').prepend('<li><a class="action share" href="#">Share This</a></li>');
		$('select option:contains("Paying Members Only")').text('Backstage JBJ Members Only');
		$('#search a.submit').text('Search');
		$('#update ul.actions li, #profile ul.actions.author li').addClass('emphasis');
		$('#page-questions ul.comments li ul.actions li a.change').text('Answer');

		
		$('#page-mission #submit-video ul.actions a.submit').text('Submit Video');
		$('#page-mission #submit-photo ul.actions a.submit').text('Submit Photo');
		$('#page-contest #submit-video ul.actions a.submit').text('Submit Video');
		$('#page-contest #submit-photo ul.actions a.submit').text('Submit Photo');
		
		$('#page-mission #submit-video h3').text('Submit a Video');
		$('#page-contest #submit-video h3').text('Submit a Video');
		
		if (page == "friend") {
			
			var friendAlias = $('#profile h3 strong').text();
			$('#page-friend div.widget-friend.modify div.relationship fieldset em').text(friendAlias + " is a best friend");
			
		}
		
		// Hiding jump buttons for topics with no editorial content
		if (page == "help" || page == "backstage-help") {
			
			$('div.topics').each(function() {
				
				if ($(this).children('div.widget-editorial').length == 0) {
													
					$(this).children('a.anchor.top').hide();
				
				}
			
			});
			
		}

		// MAILING LIST
		$('#mailinglist label:contains("Optional")').each(function() {
		
			$(this).children('em').append(' (Optional)');
			$(this).children('small').remove();
		
		});
		$('#mailinglist a.submit').text('Sign Up Now');
		
		// ALERTS
		$('div.widget-alert h3:contains("people would like to be your friend")').parent().append('<ul class="actions"><li><a class="action approve" href="/friends#requests">Approve Requests</a></li><li><a class="action friendsupto" href="/friends#friends">See What Your Friends Are Up To</a></li></ul>');
		$('#page-qas div.widget-alert h3:contains("Your comment has been saved")').text('Your question has been submitted');
		$('#page-qas div.widget-alert p:contains("It may take a moment for your comment to appear on the site.")').text('Obie is doing his best to answer as many questions as possible.');
		
		if ($('#page-photo #photo').hasClass('submit_photo')) {
			
			$('div.widget-alert h3:contains("Changes Saved")').text('Your Photo Has Been Submitted');
			$('div.widget-alert p:contains("It may take a few moments for your photo to appear on the site.")').text('Your photo has successfully been submitted. It may take a few moments for your photo to appear on the site.');
				
		}
		
		if ($('#page-your-videos #pending li').each().hasClass('submit_video')) {
					
			alert('Test');
			$('div.widget-alert h3:contains("Changes Saved")').text('Your Photo Has Been Submitted');
			$('div.widget-alert p:contains("It may take a few moments for your photo to appear on the site.")').text('Your photo has successfully been submitted. It may take a few moments for your photo to appear on the site.');
				
		}

		// ADD FIRST CLASS
		$('div.tabbed div.widget-media.player.video ul.controls li:first-child').addClass('first');

		// ADD BROWSE ALL BUTTON TO SIDEBAR MERCHANDISE (until bug is fixed)
		$('<ul class="actions"><li><a class="action browse" href="/store">Visit The Store</a></li></ul>').insertAfter('div#sidebar div.widget-item.pane ul.items');

		// CHANGE Q&A SUBMITTED BY TITLE
		$('li.qandas_with_obie > em, #qa > em').each(function() {

			$(this).text($(this).text().replace('By', 'Submitted by'));

		});

		// PLACEHOLDER MODIFICATIONS
		if (page == "profile" && author == true) {

			$('#favorite-videos.placeholder p').text("You haven't added any favorites yet. Browse the site and click 'Add to Favorites' whenever you see something you want to add!");
			$('#favorite-photos.placeholder p').text("You haven't added any favorites yet. Browse the site and click 'Add to Favorites' whenever you see something you want to add!");

			// Adding Update Button
			$('#profile ul.actions.author').append('<li class="emphasis"><a class="action update" href="/update">Update Your Profile</a></li>');

		} else if (page == "profile" && author == false) {

			$('#favorite-videos.placeholder p').text("Nothing has been favorited yet.");
			$('#favorite-photos.placeholder p').text("Nothing has been favorited yet.");

		}
		
		// MUSIC SECTION
		if (section == "music") {
				
			// GO TO LYRICS TAB
			$('#info a.lyrics').click(function() {
	
				var lyricsTabIndex = $('#info').tabs('length') - 1;
				var link = $(this).attr('href');
	
				$('#info').tabs('select', lyricsTabIndex); // Select the lyrics tab
				document.location.href=link;
				return false;
	
			});
	
			// ADD BACK TO TOP BUTTON
			$('#lyrics li').each(function() {
	
				$(this).append('<ul class="actions"><li><a class="action top" href="#content">Back To Top</a></li></ul>');
	
			});

			// ADD FIRST CLASS TO LATEST ALBUM
			$('ul.releases li:first-child').addClass('first');
	
			// MAKE EXTERNAL SITES NEW WINDOWS
			$('div.widget-release div.retailers dd a').click(function() {
	
				window.open($(this).attr('href'));
				return false;
	
			});

		}

		// STREET TEAM SUBMISSIONS
		if (page == "mission") {

			$('div.widget-blog h3 + label input').val('Report for ' + $('div.widget-editorial.display h4').text() );

		}

	}


/* FUNCTION: CENTER BOTTOM ACTIONS BAR
----------------------------------------------------------------------------- */


	function centerActions() {

/*		$('div.tabbed div.pane ul.actions, div.tabbed div.pane ul.paging').each(function() {

			if ($(this).attr('style') == undefined && !$(this).parent('div.pane').hasClass('.ui-tabs-hide')) {
		
				var containerWidth = $(this).width();
				var buttonsWidth = 0;
	
				$(this).children().each(function() {
				
					buttonsWidth += $(this).width();
				
				});
				
				var paddingLeft = (containerWidth - buttonsWidth) / 2;
				
				$(this).css('padding-left', paddingLeft + 'px');
				$(this).css('width', containerWidth - paddingLeft + 'px');		

			}

		});*/

	}


/* FUNCTION: MOVE "READ MORE" LINK
----------------------------------------------------------------------------- */ 


	function readMore() {

		$('div.widget-editorial.browse').not('.highlights').find('ul.leads li div.story > *:last-child').each(function() {

			var qa = $(this).parents('li').hasClass('qandas_with_obie');
			var link = $(this).parents('li').find('a.display').attr('href');
			$(this).parents('li').find('ul.actions').remove();

			// IF LAST CHILD IS A PARAGRAPH // WITHOUT A LINK AS LAST CHILD
			if ($(this).hasClass('lead')) {

				if (qa)	{

					$(this).append('&nbsp;&nbsp;<a class="readmore" href="' + link + '">Read Answer...</a>');

				} else {

					$(this).append('&nbsp;&nbsp;<a class="readmore" href="' + link + '">Read More...</a>');

				}

			}

			// IF LAST CHILD IS ANOTHER ELEMENT
			else {

				if (qa) {

					$(this).after('<p><a class="readmore" href="' + link + '">Read Answer...</a></p>');

				} else {

					$(this).after('<p><a class="readmore" href="' + link + '">Read More...</a></p>');

				}

			}

		});

	}


/* FUNCTION: LOADING INDICATOR
----------------------------------------------------------------------------- */


	function loading(event, panel) {

/*		if (event == "start") {
		
			// SHOW LOADING INDICATOR
			
			if( $(panel).hasClass('media')) {

				$(panel).find('em.spinner').width($(panel).width() - 12);
//				$(panel).find('em.spinner').height($(panel).height() - 72);
				$(panel).find('em.spinner').css('top', '32px');
				$(panel).find('em.spinner').css('left', '6px');
				$(panel).find('em.spinner').fadeIn();
			
			} else {

				$(panel).find('em.spinner').width($(panel).width() - 30);
				$(panel).find('em.spinner').height($(panel).height() - 92);
				$(panel).find('em.spinner').css('top', '42px');
				$(panel).find('em.spinner').css('left', '15px');
				$(panel).find('em.spinner').fadeIn();
				
			}

		}

		else {
		
			// HIDE LOADING INDICATOR
			$(panel).find('em.spinner').fadeOut();

		}*/

	}


/* FUNCTION: TABS
----------------------------------------------------------------------------- */


	function tabs() {

		// KILL TABS
		$('div.tabbed').each(function() {

			$panel = $(this);

			// REMOVE TABS IF CORRESPONDING PANES IS NIL
			$panel.find('ul.tabs li').each(function() {

				$tab = $(this);

				var paneID = $tab.children('a').attr('href');

				// JQUERY TAB
				if ( paneID.match('#') && $tab.parent().siblings(paneID).length == 0 ) {
	
					$tab.remove();
	
				}
				
			});
			
			$panel.append('<em class="spinner" style="display: none;"><span>Loading ...</span></em>');

		});

		// INITIALIZE TABS
		$('div.tabbed').not('.static').tabs({

			fx: false,
			spinner: '', 
			
			click: function(i) {

			}, 

			load: function() {

				$panel = $(this);

				loading('start', $panel);

			}, 

			show: function() {

				$panel = $(this);

				$($panel.children('div:visible')).each(function() {

					// MEDIA TAB (without Prompt to Play)
					if (($panel.find('div').hasClass('player.video') || $panel.find('div').hasClass('player.audio')) && !$panel.find('a.prompt')) {

						// Terminate all neighboring Flash elements
						$panel.siblings('div.ui-tabs-panel').find('div.flash div').empty();
						// document[$panel.siblings('div.ui-tabs-panel').find('div.flash div')].pauseClip();

						// Stop all neighboring slideshows
						$panel.siblings('div.ui-tabs-panel').find('div.slideshow ul.photos').cycle('stop');

						// Reload media in this tab
						$id = $panel.find('div.flash div').attr('id');
						eval($id)();

					}

					// SLIDESHOW TAB
					else if ($panel.find('div').hasClass('slideshow')) {

						// Terminate all neighboring Flash elements
						$panel.siblings('div.ui-tabs-panel').find('div.flash div').empty();
						// document[$panel.siblings('div.ui-tabs-panel').find('div.flash div')].pauseClip();

						// Stop all neighboring slideshows
						$panel.siblings('div.ui-tabs-panel').find('div.slideshow ul.photos').cycle('stop');

						// Begin + start slidesshow
						photoSlideshow();
						
						// Right-Click Blocker
						$('div.widget-photo.slideshow ul.photos > li').each(function() {
							
							$(this).children('div').children('a').prepend('<img class="blocker" src="' + asset_host + '/images/content/blocker.gif" width="718px" height="404px" alt="" style="position: absolute; z-index: 5; display: block; top: 0; left: 0; margin: 0;" />')

						});


					}

					// TEXT TAB
					else {

						// Terminate all neighboring Flash elements
						$panel.siblings('div.ui-tabs-panel').find('div.flash div').empty();

						// Pause all neighboring slideshows
						$panel.siblings('div.ui-tabs-panel').find('div.slideshow ul.photos').cycle('stop');

					}

				});
				
				// UPLOAD ACTIONS
				$('#page-fans #featured-fan-photos a.upload').attr('href', '/upload-photo');		

				// REFRESH CUFON
				if (typeof(window['Cufon']) != "undefined") Cufon.refresh();

				memberPlaceholders();
				centerActions();
				loading('stop', $panel);

			}

		});

		// PAGINATION FOR AJAX TABS
		$('div.tabbed > div.ui-tabs-panel > div.pane ul.paging a').live('click', function(event) {

			// SHOW LOADING INDICATOR
			loading('start', $(this).parents('div.tabbed'));

			var browse_params = $(this).parents('div.tabbed').find('li.ui-tabs-selected a').text().replace(/[^\w]/g, "").replace(/\s/g, "").toLowerCase();
			var link = $(this).attr('href') + "&browse=" + browse_params + "&prepend=" + prepend_links;

			$(this).parents('div.ui-tabs-panel').load(link, function(data) {

				// REFRESH CUFON
				if (typeof(window['Cufon']) != "undefined") Cufon.refresh();

				// REFRESH MARKUP


				// HIDE LOADING INDICATOR
				loading('stop', $(this).parents('div.tabbed'));

			});

			return false;

		});

		// PAGINATION FOR JAVASCRIPT TABS
		$('div.tabbed:not(.static) > div.ui-tabs-panel.pane ul.paging a').each(function() {

			var pane = $(this).parents('div.pane').attr('id'); 
			$(this).attr('href', $(this).attr('href') + '#' + pane); 

		});

	}


/* FUNCTION: COMMENTS
----------------------------------------------------------------------------- */


	function comments() {

		$('#comments a#post-comment').toggle(function() {

			$(this).addClass('expanded');
			$('#comments div.widget-comment.modify').show();
			return false;

		}, function() {

			$(this).removeClass('expanded');
			$('#comments div.widget-comment.modify').hide();
			return false;

		});

		$('#comments #reviews div.widget-blog.placeholder p').text("No one's made any reviews of this event yet. Would you like to write a review?");
		$('#comments #meetups div.widget-blog.placeholder p').text("No one's posted a meetup yet. Do you have one to post?");

	}


/* FUNCTION: CHAT POPUP
----------------------------------------------------------------------------- */


	function chat() {

		$('a.launch, li.chat a').click(function() {

			window.open( site_host + $(this).attr('href'),'Chat','width=980,height=675,resizable=0,scrollbars=0,toolbar=0,location=0,directories=0,status=0,menubar=0');
			return false;

		});

	}


/* FUNCTION: VIDEO UPLOADER
--------------------------------------------------------------------------- */


	function videoUploader() {

		if (window.location.toString().match('debug') && page == "upload-video") {

			$('div.widget-media.modify.video').cliqueUploader({ debug: true });

		} else if (page == "upload-video" || page == "mission" || page == "contest") {

			$('div.widget-media.modify.video').cliqueUploader();

		}

	}


/* FUNCTION: MEMBER PROFILE PLACEHOLDERS
--------------------------------------------------------------------------- */


	function memberPlaceholders() {

		$('div.widget-member a.picture, div.widget-friend a.picture, div.widget-comment a.picture').each(function() {

			if ($(this).children('img').attr('src').match('placeholder.gif')) {
				$(this).children('img').attr('src', $(this).children('img').attr('src').replace('placeholder.gif', 'placeholder-member' + Math.round(Math.random()*3 + 1) + '.gif'));
			}

		});

	}


/* FUNCTION: CUFON TEXT REPLACEMENTS
----------------------------------------------------------------------------- */


	function cufon() {

		var garaline = [ 

			'*:not(.story) h2', 
			'h3:not(div.widget-media.display h3, #customer h3)', 
			'div.group h4', 
			'div.display:not(.closed) h4',
			'div.widget-editorial.highlights li > h4',
			'div.tabbed:not(.compact) a.tab',
			'#title ul.categories a',
			'a.action', 
			'th:not(div.widget-poll th)', 
			'div.widget-media.player a.comments',
			'div.retailers a', 
			'a#post-comment', 
			'a#post-meetup', 
			'a#post-review',
			'#messages div a:first-child',
			'#tools ul.stories li h4',
			'#page-mission div.modify.submit-media ul.actions li em',
			'div.flag h4',
			'div.widget-tag.browse ul.tags h4 a',
			'#featured-news ul.headlines li > a:not(.comments)',
			'.section-localized #introduction h4',
			'#merchandise h3',
			'#merchandise a.visit'
			
		];

		var din_condensed = [ 

			'h2', 
			'h3:not(div.widget-media.display h3)', 
			'div.group h4', 
			'#sidebar h4', 
			'div.display:not(.closed) h4',
			'#featured-news h4',
			'#benefits li h4',
			'#packages div.online h4',
			'div.widget-editorial.highlights h4', 
			'div.tabbed:not(.compact) a.tab',
			'#title ul.categories a',
			'*:not(.bonjovi) a.action', 
			'th:not(div.widget-poll th)', 
			'div.widget-media.player a.comments',
			'a#post-comment', 
			'#messages div a:first-child',
			'#page-contest div.modify.submit-media ul.actions li em',
			'div.flag h4',
			'div.widget-tag.browse ul.tags h4 a',
			'#featured-news ul.headlines li > a:not(.comments)'
			
		];

		if (backstagejbj) {

			Cufon.replace(din_condensed, { fontFamily: 'DIN Condensed', hover: true } );
			$(din_condensed.toString()).css('visibility', 'visible');

		}

		else {

			Cufon.replace(garaline, { fontFamily: 'Garaline', hover: true } );
			$(garaline.toString()).css('visibility', 'visible');

		}

	}


/* FUNCTION: PHOTO SLIDESHOW PATTERN
----------------------------------------------------------------------------- */ 


	function photoSlideshow() {

		// $('div:visible > div.slideshow').not('.placeholder').not('.slideshow-active').each(function() {
		$('div.slideshow').not('.placeholder').not('.slideshow-active').each(function() {

			$slideshow = $(this);
			$slideshow.addClass('slideshow-active');

			if ($slideshow.attr('id') != "") {

				var slideshowID = $slideshow.attr('id');

			} else {
			
				var slideshowID = "slideshow" + Math.round(Math.random()*100 + 1); // Generate random ID if container does not have one
				$slideshow.attr('id', slideshowID);
				
			}

			// Add pager if there is more than one photo
			if ($slideshow.find('ul.photos > li').length > 1) {

				$slideshow.append('<ul class="pager"></ul>');
				$('<li><a id="' + slideshowID + '_next" class="action next" href="#">Next</a></li>').prependTo($slideshow.children('ul.pager'));
				$('<li><a id="' + slideshowID + '_back" class="action back" href="#">Back</a></li>').prependTo($slideshow.children('ul.pager'));
			
			}

			$slideshow.children('ul.photos').cycle({

				next:    '#' + slideshowID + '_next',
				prev:    '#' + slideshowID + '_back',
				pause:   1

			});

			$slideshow.hover(

				function() {

					$(this).find('a.next').show();
					$(this).find('a.back').show();

				},
				function() {

					$(this).find('a.next').hide();
					$(this).find('a.back').hide();

				}

			)

		});

	}


/* FUNCTION: FIX ANCHOR LINKS
-----------------------------------------------------------------------------*/ 


	function fixAnchorLinks() {

		/* var url = window.location.toString();

		if (url.match('#')) {
			var url = url.split('#');
			var anchor = url[1];
			var position = $('#' + anchor).offset();
			var alternative_position = $('#' + anchor).scrollTop();
		} */

		// window.scrollTo(0,position.top);
		// window.location.hash('info-membership')
			
	}
	
	
/* FUNCTIONS: THE CIRCLE COUNTDOWN
-----------------------------------------------------------------------------*/ 


	function countdown() {
	
		// Countdown
		var _so = new SWFObject(asset_host + '/flash/geoCountdown.swf', 'countdown_flash', '100%', '100%', '#000000');
		_so.addParam('allowScriptAccess', 'always');
		_so.addParam('wmode', 'transparent');
		_so.addVariable('localXML', asset_host + '/xml/nationalTime.xml');
		_so.write('timer');
		
		// Listen
		var _so = new SWFObject(asset_host + '/flash/jbj_stream.swf', 'countdown_preview', '100%', '100%', '#000000');
		_so.addParam('allowScriptAccess', 'always');
		_so.addParam('wmode', 'transparent');
		_so.write('preview');
			
	}
	
	function localizedCountdown(country) {
	
		setTimeout(function() {
	
			switch (country) {
			
				case 'CH':
				case 'DE':
				case 'AT':
				
					$('#countdown').addClass('de');
					break;
				
				case 'IE':
				case 'UK':
				case 'GB':
				
					$('#countdown').addClass('uk');
					break;
					
				case 'JP':
				
					$('#countdown').addClass('jp');
					break;
					
				case 'IT':
				case 'AN':
				
					$('#countdown').addClass('it');
					break;
					
				default:
			
					$('#countdown').addClass('us');
	
			}
			
		}, 300);

	}
	
	function countdownOver() {
	
		$('body').addClass('countdown_over');
	
	}