/* STANDARD TEMPLATE */
/* Javascript Functions
----------------------------------------------------------------------------- */


	$(document).ready(function(){
	
		$('#zodiac_sign').parent().hide();
		$('dt:contains(Zodiac)').hide().next().hide();
	
	});


/* POST-LOAD FUNCTION
----------------------------------------------------------------------------- */


	$(window).load(function() {


		chat();


/* ENDORSEMENTS SECTION
----------------------------------------------------------------------------- */

		if (section == 'endorsements') {
			
			// Photos
			$('div.widget-photo ul.photos li a img').each(function(){
				
				var link = $(this).attr('src').replace('thumb.', '');
				
				$(this).parent().attr('href', link);
				$(this).parent().parent().append('<a href="' + link + '" title="Download Hi-Res Photo" class="download">Download Hi-Res Photo</a>')
								
			});
			
			
		}

/* UPCOMING PERFORMANCES TABLE
----------------------------------------------------------------------------- */
	
		if (section == 'tour') {
			
			$('#appearances table.events th.location').text('Network');
			$('#appearances table.events th.details').text('Show');
			$('#appearances table.events th.rsvp').text('People Watching');
			
		}
	

/* MOBILE LIST - US ONLY
----------------------------------------------------------------------------- */

	
		if (page == 'index') {
			
			$('div#text label em').text('Mobile Number (US Only)');
			
		}
			
		
/* MEMBER NAVIGATION DROPDOWN
----------------------------------------------------------------------------- */
	
	
		$('ul#navigation li:has(ul)').hover(function() {
	
			$(this).addClass('hover');
	
		}, function() {
	
			$(this).removeClass('hover');
	
		});
		
	
/* EXTERNAL LINKS
----------------------------------------------------------------------------- */


		$('a[rel=external]').click(function() {

			var link = $(this).attr('href');
			window.open(link);

			return false;

		});
	

/* OPEN MULTIMEDIA WINDOWS
----------------------------------------------------------------------------- */


		$('#multimedia table tr td a').click(function() {
			
			var location = $(this).attr('href');

			window.open(location, 'multimedia', 'width=550, height=400, scrollbars=0, status=0, toolbar=0, location=0, resizable=0, menubar=0');
			return false;

		});
		
		
/* MY BELIEFS CHANGES
----------------------------------------------------------------------------- */
	
	
		if (section == "mybeliefs") {
					
			// Hiding 'Anonymous Member' byline + photo on Conversion Stories
			$('#stories ul.headlines li').each(function() {
		
				var name = $(this).children('em').children('a').text();
	
				if (name == "Anonymous_Member") {
				
					$(this).children('em').remove();
					$(this).children('a.picture').remove();
				
				}
			
			});
			
			// Related Questions
			if (page == "question") {
				
				$('#question div.tags strong').text('Related Questions About:');
				
			}
		
			// Hide "Everything" Tag
			$('div.widget-tag li a:contains("Everything")').parent('li').remove();
				
					
			// Modifying the "Submit a Story" & "Submit a Question" form
			if (page == 'submit_question') {
		
				$('#submit label em:contains("Headline")').text('Question Summary');
				$('#submit label.post em').text('Your Question');
				$('#submit label em:contains("Share with")').parent('label').attr('class','share');
				$('#submit fieldset').hide();
		
		
			} else if (page == 'submit_conversion') {
	
				$('#submit label em:contains("Headline")').text('Title');
				$('#submit label.post em').text('Story');
				$('#submit fieldset label em').text("Don't allow others to comment on my conversion story");
				$('#submit label em:contains("Share with")').parent('label').attr('class','share');
	
			}
						
			// Hiding actions on Question post for authors
			$('#page-question #story ul.actions.author').remove();
			
			// Hiding actions on Conversion Story post for authors
			$('#page-conversion_story #story ul.actions.author').remove();
				
			
			// Modifying Alert Widget on Thank You page
			if (page == 'thank_you' && tag.match('Questions_and_Answers')) {
	
				$('#page-thank_you div.widget-alert.success h3:contains("Your blog has been successfully updated.")').text('Your question has been successfully submitted.');
				
			} else if (page == 'thank_you') {
				
				$('#page-thank_you div.widget-alert.success h3:contains("Your blog has been successfully updated.")').text('Your conversion story has been successfully submitted.');
				
			}
			
			// Adding Questions Placeholder
			if (page == 'questions' && $('#questions').length == 0) {
							
				$('#content ul.actions:first').after('<div class="pane placeholder"><h3>There are no questions for this topic</h3></div>')
				
			}
		
		}
			

/* SITE-ONLY
----------------------------------------------------------------------------- */

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


/* CLIQUE SIGN IN
----------------------------------------------------------------------------- */

		
			if (page == 'login') {
		
				// START PACKAGE TIER SLIDESHOW
				$('ul.tiers').before('<div id="paging">').cycle({
		
					fade: true,
					speed: 400,
					timeout: 10000,
					pager: '#paging',
					before: function() { if (window.console) console.log(this.src); }
		
				});
		
				// CUSTOM PAGING LINKS
				$('#paging a:nth-child(1)').livequery(function() { $(this).addClass('gold'); });
				$('#paging a:nth-child(1)').livequery(function() { $(this).text('Gold'); });
				
				$('#paging a:nth-child(2)').livequery(function() { $(this).addClass('silver'); });
				$('#paging a:nth-child(2)').livequery(function() { $(this).text('Silver'); });
				
				$('#paging a:nth-child(3)').livequery(function() { $(this).addClass('bronze'); });
				$('#paging a:nth-child(3)').livequery(function() { $(this).text('Bronze'); });
				
				$('#paging a:nth-child(4)').livequery(function() { $(this).addClass('free'); });
				$('#paging a:nth-child(4)').livequery(function() { $(this).text('Free'); });
		
			}
		

/* FUNCTION CALLS
----------------------------------------------------------------------------- */
			
		
			if (page == "index" || page == "news" || section == "client" || section == "thennow") {
		
				readmore();
		
			}
		
			$('input[type=submit]').cliqueActions();
			killTabs();
			tabs();
			paging();
			truncate();
			comments();
			audioPlayer();
			twitter();
			
	
/* CAROUSEL
----------------------------------------------------------------------------- */	
			
			
			if (page == 'release') {
		
				$('#discography').show();
				$('#discography ul.releases').jcarousel({vertical:true,scroll:4});
				
			}
		

/* STORE
----------------------------------------------------------------------------- */	
		
					
			if (section == "store") {
			
				// SUB-DEPARTMENTS
				$('#subcategories').each(function() {
				
					var category = $('#subcategories h3').text();
					$('#subcategories ul').find('a:contains("Everything")').parent().remove();
					$('#subcategories ul').appendTo('#departments li:has(a:contains("' + category + '"))');
					$('#subcategories').remove();
				
				});
			
				// 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: false
				
				});
				$('.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'); } }
			
			}
			

/* END SITE-ONLY
----------------------------------------------------------------------------- */


		}


/* END POST-LOAD FUNCTIONS
----------------------------------------------------------------------------- */


	});
	

/* FUNCTION: Twitter
----------------------------------------------------------------------------- */


	function twitter() {
		
		if (page == "dancing" || page == "index") {
			
			$('#twitter div.tweets').tweet({

				username: "donnyosmond",
	          	count: 1,
	          	loading_text: "Loading tweets..."

			});
			
		}
		
	}


/* FUNCTION: In Page Audio Clip Player
----------------------------------------------------------------------------- */


	function audioPlayer() {
	if ($('#audio').length > 0) {
		
		var player = $('#audio').children('div.flash').children('div').attr('class').replace('flash ','');
		var prompt = player.replace('instance_', 'player_');
			
		// Add the class "Listen" to all actions that contain the word "Listen"
		// Case-sensitive, so it's just looking for "isten"
		$('a:contains("isten")').each(function(){
			
			$(this).addClass('listen');
			
		});
		
		// Toggle action between "Stop" and "Listen"
		$('a.listen').toggle(
			
			function() {
				
				var clip = $(this).attr('href');
										
				$('a.stop').removeClass('stop').addClass('listen').text('Listen');
				$(this).removeClass('listen').addClass('stop').text('Stop');			
				
				// First Play Prompt
				if ($('#audio a.prompt').length > 0) {
					
					$('#audio a.prompt').click();
					
					setTimeout(function(){

						document[player].playClip(clip);

					}, 2000);
					
				} else {
					
					document[player].playClip(clip);
					
				}
				
				return false;

			},function(){
				
				document[player].pauseClip();
				$(this).removeClass('stop').addClass('listen').text('Listen');	
				
				return false;
			
			}
			
		);
	
	}			
	}
	
	
/* FUNCTION: MOVE "READ MORE" LINK
----------------------------------------------------------------------------- */ 


	function readmore() {
		
		$('div.widget-editorial.browse ul.leads > li').each(function() {
						
			var link = $(this).find('a.display').attr('href');
			$(this).find('ul.actions').remove();
			
			// IF LAST CHILD IS P.LEAD
			if ( $(this).find('div.story > *:last-child').hasClass('lead') ) {

				$(this).find('div.story p.lead:last').append('&nbsp;&nbsp;<a class="readmore" href="' + link + '">Read More...</a>');

			}
									
			// IF LAST CHILD IS ANOTHER ELEMENT
			else {

				$(this).find('div.story > *:last-child').after('<br /><a class="readmore" href="' + link + '">Read More...</a>');

			}			

		});

	}


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


	function killTabs() {

		// REMOVE EMPTY JAVASCRIPT TAB AND PANE
		$('div.tabbed').not(".static, .ajax").children('ul.tabs').children('li').each(function() {

			var pane = $(this).children('a').attr('href');

			if ( $(this).parent().parent().find(pane).children('*').length == 0 ) {

				$(this).remove();
				$(this).parent().parent().find(pane).remove();

			}

		});

		// REMOVE TABBED PANEL
		$('div.tabbed').not(".static, .ajax").children('ul.tabs').each(function() {

			if ($(this).children('li').length == 0) {

				$(this).parent().remove();

			}

		});

	}


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


	function tabs() {

		// FEATURES BOX TABS
		if (page == 'index' || page == 'dancing') {

			// Photos is the first tab, begin cycling
			if ( $('#photos').length > 0 && $('ul.tabs li:first-child a.tab').attr('href') == "#photos" ) {
				
				$('div#features > ul.tabs').tabs();
								
				$('#photos').addClass('pane').show();
			
				photoSlideshow();
				
				if ($('div#features div.widget-photo.slideshow ul.photos > li[class!=last]').length > 0) {

					$('div#features div.widget-photo.slideshow div a, a#photos_back, a#photos_next').hover(

						function() {

							$('a#photos_back').show();
							$('a#photos_next').show();

						}, function() {

							$('a#photos_back').hide();
							$('a#photos_next').hide();

						}

					)
				
				}
			
			}

			else {

				$('div#features > ul.tabs').tabs({
	
					fxSlide: false,
	
					show: function() {
	
						// PHOTOS TAB
						if ($('#photos:visible').length > 0) {
	
							$('#video div.flash div').empty();
							$('#radio div.flash div').empty();
	
							photoSlideshow();
	
							$('div#features div.widget-photo.slideshow div a, a#photos_back, a#photos_next').hover(
			
								function() {
	
									$('a#photos_back').show();
									$('a#photos_next').show();
	
								},
								function() {
	
									$('a#photos_back').hide();
									$('a#photos_next').hide();
	
								}
	
							)
	
						} 
	
						// RADIO TAB
						else if ($('#radio:visible').length > 0) {
	
							$('#video div.flash div').empty();
	
							photoSlideshow();
	
							var radio_player = $('#radio div.flash div').attr('id');
							window[radio_player]();
	
						}
	
						// VIDEO TAB
						else if ($('#video:visible').length > 0) {
	
							$('#radio div.flash div').empty();
	
							photoSlideshow();
	
							var video_player = $('#video div.flash div').attr('id');
							eval(video_player)();
	
						}
						
						// OTHER VIDEO TAB
						else if ($('#other-video:visible').length > 0) {
	
							$('#radio div.flash div').empty();
	
							photoSlideshow();
	
							var video_player = $('#other-video div.flash div').attr('id');
							eval(video_player)();
	
						}
						
						else {
							
							
						}
	
					}
					
				});

			}

		}


		// LIVE BROADCAST TOGGLE
		var local_date = new Date();
		var utc = local_date.getTime() + local_date.getTimezoneOffset() * 60000;
		var pst_date = new Date(utc + 3600000 * -8);
		var day = pst_date.getDay();
		var hour = pst_date.getHours();
		
		if (page == "index" && day == 4 && hour >= 16 && hour < 19) {

			$('#features a.tab.live').show().click();
			
		}
		
		
		// THEN & NOW TABS
		if (page == 'then_and_now') {
			
			// Exclusive Photos is the first tab, begin cycling
			if ( $('#exclusive-photos div.widget-photo').length > 0 && $('ul.tabs li:first-child a.tab').attr('href') == "#exclusive-photos" ) {

				$('div#media > ul.tabs').tabs();
						
				$('#exclusive-photos').addClass('pane').show();
			
				photoSlideshow();
				
				if ($('div#media div.widget-photo.slideshow ul.photos > li[class!=last]').length > 0) {
					
					$('div#media div.widget-photo.slideshow div a, a#exclusive-photos_back, a#exclusive-photos_next').hover(
							
						function() {
						
							$('a#exclusive-photos_back').show();
							$('a#exclusive-photos_next').show();

						}, function() {
						
							$('a#exclusive-photos_back').hide();
							$('a#exclusive-photos_next').hide();

						}

					)

				}
			
			}

			// Photos is the first tab, begin cycling
			else if ( $('#photos').length > 0 && $('ul.tabs li:first-child a.tab').attr('href') == "#photos" ) {
				
				$('div#media > ul.tabs').tabs();
								
				$('#photos').addClass('pane').show();
			
				photoSlideshow();
				
				if ($('div#media div.widget-photo.slideshow ul.photos > li[class!=last]').length > 0) {

					$('div#media div.widget-photo.slideshow div a, a#photos_back, a#photos_next').hover(

						function() {

							$('a#photos_back').show();
							$('a#photos_next').show();

						}, function() {

							$('a#photos_back').hide();
							$('a#photos_next').hide();

						}

					)
				
				}
			
			}

			else {

				$('div#media > ul.tabs').tabs({
					
					fxSlide: false,
	
					show: function() {
	
						// EXCLUSIVE PHOTOS TAB
						if ($('#exclusive-photos:visible').length > 0) {
						
							$('#exclusive-video div.flash div').empty();
							$('#video div.flash div').empty();
							
							photoSlideshow();
							
							if ($('div#media div.widget-photo.slideshow ul.photos > li[class!=last]').length > 0) {
																													
								$('div#media div.widget-photo.slideshow div a, a#exclusive-photos_back, a#exclusive-photos_next').hover(
										
									function() {
									
										$('a#exclusive-photos_back').show();
										$('a#exclusive-photos_next').show();
	
									},
									function() {
									
										$('a#exclusive-photos_back').hide();
										$('a#exclusive-photos_next').hide();
	
									}
	
								)
								
							}
	
						}
						
						// EXCLUSIVE VIDEOS TAB
						else if ($('#exclusive-videos:visible').length > 0) {
	
							$('#videos div.flash div').empty();
	
							photoSlideshow();
	
			
							if ($('#exclusive-videos div.widget-media').length > 0) {
																	
								var exclusive_video_player = $('#exclusive-videos div.flash div').attr('id');
								eval(exclusive_video_player)();
															
							}
	
						}
	
						// PHOTOS TAB
						else if ($('#photos:visible').length > 0) {
							
							$('#exclusive-video div.flash div').empty();
							$('#video div.flash div').empty();
							
							photoSlideshow();
							
							if ($('div#media div.widget-photo.slideshow ul.photos > li[class!=last]').length > 0) {
	
								$('div#media div.widget-photo.slideshow div a, a#photos_back, a#photos_next').hover(
			
									function() {
	
										$('a#photos_back').show();
										$('a#photos_next').show();
	
									},
									function() {
	
										$('a#photos_back').hide();
										$('a#photos_next').hide();
	
									}
	
								)
								
							}
	
						}
	
						// VIDEOS TAB
						else if ($('#videos:visible').length > 0) {
	
							$('#exclusive-videos div.flash div').empty();
	
							photoSlideshow();
							
							if ($('#videos div.widget-media').length > 0) {
	
								var video_player = $('#videos div.flash div').attr('id');
								eval(video_player)();
								
							}
							
						}
						
						else {
							
							
						}
	
					}
					
				});
			
			}

		}
		
		// ENDORSEMENT TABS
		if (page == 'endorsements') {
			
				$('div#features > ul.tabs').tabs({
					
					fxSlide: false,
	
					show: function() {
	
						// COMMERCIALS TAB
						if ($('#commercials:visible').length > 0) {
							
							$(this).addClass('pane').show();
	
							$('#videos div.flash div').empty();	
			
							if ($('#commercials div.widget-media').length > 0) {
																	
								var exclusive_video_player = $('#commercials div.flash div').attr('id');
								eval(exclusive_video_player)();
															
							}
	
						}
	
						// VIDEOS TAB
						else if ($('#other-videos:visible').length > 0) {
							
							$(this).addClass('pane').show();
	
							$('#commercials div.flash div').empty();
							
							if ($('#other-videos div.widget-media').length > 0) {
	
								var video_player = $('#other-videos div.flash div').attr('id');
								eval(video_player)();
								
							}
							
						}
						
						else {
							
							
						}
	
					}
					
				});
			
		}
		
		// JAVASCRIPT TABS
		$('div.tabbed').not("#features, .ajax, .static, #media").children('ul.tabs').tabs({

			fxSlide: false,

			click: function() {

			}, load: function() {

				truncate();

			}, show: function() {

				truncate();

			}

		});

		// AJAX TABS
		$('div.tabbed.ajax > ul.tabs').tabs({

			fxSlide: false,

			click: function() {

				loading('start');

			}, load: function() {

				truncate();
				loading('stop');
				chat();

			}, show: function() {

				truncate();
				loading('stop');
				chat();

			}

		});

	}


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


	function loading(event) {

		/*if (event == "start") {

			$('#loading').animate({
			left: '0px'
			}, 300);

		} else {

			$('#loading').animate({
			left: '-54px'
			}, 300);

		}*/

	}


/* FUNCTION: PAGING FOR TABS
----------------------------------------------------------------------------- */ 


	function paging() {

		// AJAX TABS
		$('div.tabbed.ajax ul.paging a').livequery('click', function(event) {

			loading('start');

			$(this).parents('div.pane').load($(this).attr('href'), function(data) {

				loading('stop');

			});

			return false;

		});

		// JAVASCRIPT TABS
		$('div.tabbed:not(.ajax):not(.static) ul.paging a').each(function() {

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

		});

	}


/* FUNCTION: TRUNCATE TEXT
----------------------------------------------------------------------------- */


	function truncate() {

		// DESCRIPTIONS OF TITLED BROWSE WIDGETS
		$('div#content div.titled strong').each(function() {

			if ($(this).text().length >= 55) {

				var text = $(this).text().slice(0,55);

				$(this).text(text);
				$(this).append('<var>&nbsp;&hellip;</var>');

			}

		});

		// FEATURES BOX: PHOTO TITLE
		$('div#features #photos h4').each(function() {

			if ($(this).text().length >= 45) {

				var text = $(this).text().slice(0,45);

				$(this).text(text);
				$(this).append('<var>&nbsp;&hellip;</var>');

			}

		});

		// MEMBER STATUS: SPLIT GRID
		$('div.grid.split div#content .member dd.status').each(function() {

			if ($(this).text().length >= 50) {

				var text = $(this).text().slice(0,50);

				$(this).text(text);
				$(this).append('<var>&nbsp;&hellip;</var>');

			}

		});

		// MEMBER STATUS: TWO THIRDS GRID
		$('div.grid.two_thirds div#content .member dd.status').each(function() {

			if ($(this).text().length >= 80) {

				var text = $(this).text().slice(0,80);
		
				$(this).text(text);
				$(this).append('<var>&nbsp;&hellip;</var>');
				
			}
		
		});

		// MEMBER ALIAS: SPLIT GRID
		$('div.grid.split div#content .member a.alias strong').each(function() {

			if ($(this).text().length >= 15) {

				var text = $(this).text().slice(0,15);

				$(this).text(text);
				$(this).append('<var>&nbsp;&hellip;</var>');

			}

		});

		// MEMBER ALIAS: TWO THIRDS GRID
		$('div.grid.two_thirds div#content .member a.alias strong').each(function() {

			if ($(this).text().length >= 30) {

				var text = $(this).text().slice(0,30);

				$(this).text(text);
				$(this).append('<var>&nbsp;&hellip;</var>');

			}

		});
		
		// STORE ITEM LISTINGS
		$('div.grid.three_fourths.store #content ul.items dt a').each(function() {

			if ($(this).text().length > 35) {

				var text = $(this).text().slice(0,35);

				$(this).text(text);
				$(this).append('<var>&hellip;</var>');

			}
		
		});
		

	}


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


	function comments() {

		// ADDS CANCEL BUTTON FUNCTIONALITY
		if ($('#comment').siblings('ul.tabs').children().length > 1) {

			$('#comment a.cancel').click(function() {

				$('#comment textarea').val('');
				$('#comment').siblings("ul.tabs").tabsClick(1);
				return false;

			});

		}

		// REMOVES CANCEL BUTTON IF NO COMMENTS EXIST
		else {

			$('#comment a.cancel').remove();

		}

	}
	
	
/* FUNCTION: CHAT PROMPT
--------------------------------------------------------------------------- */


	function chat() {
	
		$('#dim').click(function() {
		
			$(this).siblings('.upsell').show();
			$(this).addClass('dim');

		});

	}