/* CARRIE UNDERWOOD */
/* Javascript
-------------------------------------------------------------------------------- */

/* READY FUNCTION
----------------------------------------------------------------------------- */


	$(document).ready(function() {

		
		// MAKE MARKUP CHANGES
		markup();
				
		// HOME PAGE: FANS TAB
		$('#page-index #updated h3 em').remove();
		
		// HOME PAGE: LISTENING PARTY
		if (page == "index") $('#listen .details a').click(function() { return false; });
		
		
/* HIDE UNTIL LOADED
----------------------------------------------------------------------------- */


		$('.program strong').hide();
		$('.tabbed:not(.static) > div').hide();
		
		
/* COMMENTS
--------------------------------------------------------------------------- */


		// Get Comment Tally
		var commentTally = $('#comments h3').text();

		// Inject Comment Tally into New Heading
		$('#browse-comments div.tally h3').text(commentTally);
		
		// Post Comment Sliding
		$('#comment').hide();

		$('a.postcomment').click(function() {

			$('#comment').slideToggle('fast');
			// replace();
			return false;

		});

		$('a.firstcomment').click(function() {

			$('#comment').slideToggle('fast');
			// replace();
			return false;

		});
		
		$('body:not(#page-comment, #page-ask) #comment a.cancel').click(function() {
		
			$('#comment').slideToggle('fast');
			
			return false;
			
		});		
		
		// Add Comment Point to Each Post
		$('#comments ul.comments li').each(function() {
		
			$(this).append('<div class="commentpoint"></div>');
			
		});
		
		

/* END READY FUNCTION
----------------------------------------------------------------------------- */


	});


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


	$(window).load(function() {
	
	

		// Load Featured Awards
		if (page == 'index') {
	
			// Show Featured Box
			$('#featured').show();
			
			// Rotate Featured Awards
			rotate("Featured Awards");

			// Rotate Featured Events
			rotate("Featured Events");

			// Embed MyPlay Flash
			$('#featured li.music a').click(function() {
			
				$('#featured #music').append('<iframe src="http://myplay.com/audio_player/carriefanclub/83/490019/490020?allowBrowsing=0" width="859px" height="440px"></iframe>');
			
			});
			
			//Cycle Photos Tab Images
			photosTab();
			
		}
		
		killTabs();
		if (page != "top") tabs();
		tooltips();
		if ($('#announcements').length > 0) $('#announcements').cliqueAnnouncements();
		cufon();
		slideshow();


/* OPEN CHAT WINDOW
----------------------------------------------------------------------------- */


		$('a[href=#chat]').click(function() {
		
			window.open('/chat', 'chat', 'width=750, height=550, scrollbars=0, status=1, toolbar=0, location=0, resizable=1, menubar=0');
			return false;

		});


/* ADD 'LAST' CLASS ON LAST TAB
----------------------------------------------------------------------------- */


	$('ul.tabs li:last-child').addClass('last');



/* MEMBER NAVIGATION DROPDOWN
----------------------------------------------------------------------------- */


	$('ul#navigation li:has(ul)').hover(function() {

		$(this).addClass('hover');

	}, function() {

		$(this).removeClass('hover');

	});


/* EXTERNAL LINKS
----------------------------------------------------------------------------- */


		$('a[rel=external]').click(function() {
		
			link = $(this).attr('href');
			window.open(link);
			
			return false;
	
		});


/* ADD LINK TO PLAY ON PLAYER
----------------------------------------------------------------------------- */


		// $('#playon #audio').append('<a class="preorder" href="http://www.amazon.com/gp/product/B002DYJAIY?ie=UTF8&tag=sparkart-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=B002DYJAIY" target="_blank">Want to hear more? Click here to pre-order a copy of Play On</a>');
		

/* CHANGE WORDING ON ELEMENTS
----------------------------------------------------------------------------- */


		$('#browsenetwork a.browse').text('fan club');
		$('#browsenetwork a.join').text('join now');
		$('th.rsvp').text('attending');
		$('#video a.browse').text('Browse All Videos');
		$('#tags dt.updated').text('New Photos Added');
		$('#page-payandship-code #group-redeemcode p').text('Please enter in your code (including Ticketmaster redemption codes).');
		$('#upload_my_moment .upload h4').text('Upload your photo to the "My Moment With Carrie" gallery!');
		$('#upload_my_moment .upload p').text('Got a cool picture of yourself with Carrie Underwood? Share it with us! Please remember that images must be in the JPEG format and will be automatically cropped and re-sized as needed.');
		$('body.fanparty #confirmation .toggle.going a.going').text("Enter me in the RSVP Lottery");
		$('body.fanparty #confirmation .toggle.going strong.going').text("Thank You For Entering");
		$('body.fanparty #event h4.date').text('The Official Carrie Underwood Fan Club Party 2010 - June 8th, 2010');

	
	
/* BACK LINKS
----------------------------------------------------------------------------- */


		$('a[href=#back]').click(function() {
		
			history.go(-1);
			return false;
		
		});
		
		
/* EDIT STATUS MESSAGE
----------------------------------------------------------------------------- */


		$('#member a.change').click(function() {
		
			$(this).parent().find('.widget-profile.modify.status').show();
			$(this).parent().find('dd.status').hide();
			$(this).hide();

			return false;
	
		});
		
		
/* EDIT STATUS MESSAGE (Carrie Page)
----------------------------------------------------------------------------- */
		


/* EDIT SITE HEADER / SITE ADDRESS
----------------------------------------------------------------------------- */


		$('a.edit').click(function() {
		
			$('#site_modify').fadeIn('200');
			$(this).hide();
			return false;
			
		});
		
		$('#address a.create').click(function() {
		
			$('#site_address').fadeIn('200');
			return false;
			
		});
		
		if ($('#site_address .error').length > 0) {
		
			$('#site_address').show();
		
		}
	
	
/* LOOK FOR SITE ADDRESS
----------------------------------------------------------------------------- */

		
		if ($('.widget-network.display > em').length < 1) $('#address').show();
		
		
/* UPSELL PAGE
----------------------------------------------------------------------------- */


		if ($('#upsell').length > 0) {
			
			$('#benefits').each(function() {
			
				// START SLIDESHOW
				
				$(this).before('<div id="paging">').cycle({
				fx: 'fade',
				speed: 500,
				timeout: 6500,
				pager: '#paging',
				before: function() { if (window.console) console.log(this.src); }

				});
				
				// CUSTOMIZE PAGING LINKS
				
				$('#paging a:nth-child(1)').livequery(function() { $(this).addClass('content'); });
				$('#paging a:nth-child(1)').livequery(function() { $(this).text('Content'); });
				
				$('#paging a:nth-child(2)').livequery(function() { $(this).addClass('site'); });
				$('#paging a:nth-child(2)').livequery(function() { $(this).text('Site'); });
				
				$('#paging a:nth-child(3)').livequery(function() { $(this).addClass('comments'); });
				$('#paging a:nth-child(3)').livequery(function() { $(this).text('Comments'); });
				
				$('#paging a:nth-child(4)').livequery(function() { $(this).addClass('presales'); });
				$('#paging a:nth-child(4)').livequery(function() { $(this).text('Pre-Sales'); });
				
				$('#paging a:nth-child(5)').livequery(function() { $(this).addClass('greets'); });
				$('#paging a:nth-child(5)').livequery(function() { $(this).text('Meet &amp; Greets'); });
				
				// $('#paging a:nth-child(6)').livequery(function() { $(this).addClass('party'); });
				// $('#paging a:nth-child(6)').livequery(function() { $(this).text('Fan Club Party'); });
				
				$('#paging a:nth-child(6)').livequery(function() { $(this).addClass('merch'); });
				$('#paging a:nth-child(6)').livequery(function() { $(this).text('Merch'); });
				
				// UNHIDE SLIDESHOW
				
				$(this).parent().css('visibility','visible');
			
			});
		
		}


/* MODIFY REFERRAL LEADERBOARD
----------------------------------------------------------------------------- */


	if ($('#referrals').length > 0) {
	
		$('td.tally em').each(function() {
		
			var tally = $(this).text().split(',');
			var tally = tally[0].replace('Paid ','');

			$(this).text(tally);
	
		});	
	
	}
	
	
/* ADD NEW LIST ITEM INTO GALLERY LISTING
----------------------------------------------------------------------------- */


	$('#page-gallery #tags ul.tags').prepend('<li class="disneys_american_idol_experience unique"><a href="/photo/all/disneys_american_idol_experience"><img src="http://redirect-staging.media.sparkart.net/carrieunderwood/content/tags/thumb.tag_2692.jpg" alt="Disney American Idol Experience"/></a><h4><a href="/photo/all/disneys_american_idol_experience">Disney American Idol Experience</a></h4><dl><dt class="photos">Photos</dt><dd class="photos">Interactive</dd></dl></li>');


/* CHANGING RSVP LANGUAGE
----------------------------------------------------------------------------- */

	if (page == "event" && topic == "june__7_2011") {

		$('#confirmation a.going').text('Enter Me in the RSVP Lottery');
		
	}

	
/* END LOAD FUNCTION
----------------------------------------------------------------------------- */


	}); 
	

/* FUNCTION: LOAD FEATURED AD ON CONTESTS TAB
--------------------------------------------------------------------------- */


	function loadAd() {
	
		var so = new SWFObject("/flash/banner.swf?unit=ad-featured&xml=http://www.carrieunderwood.fm/feeds/banners", "advertising-ad-featured", "120", "240", "8", "#ffffff");
		so.addParam("wmode", "transparent");
		so.write("ad-featured");
		
	}
	
	
/* FUNCTION: TABS
----------------------------------------------------------------------------- */


	function tabs() {
		
		$('.tabbed:not(.static) > ul.tabs').tabs({
					
			cache: true,		
			spinner: '',		
			click: function(event, ui) {			
	
				$('div.loading').fadeIn();
				Cufon.refresh();
				
			}, load: function(event, ui) {
						
				$('div.loading').fadeOut();

				// markup();
				// photoTabs();
				ajaxTabMarkup();
				galleryTabs();
				killTabs();
				tooltips();
				Cufon.refresh();
				fixPaging();
			
			}, show: function() {

				$('div.loading').fadeOut();

				// markup();
				// photoTabs();
				galleryTabs();
				killTabs();
				tooltips();
			//	Cufon.refresh();
				
				// FIX AUTOPLAY FOR IE
				
				if (page == "index" && $('#videos.pane:visible').length > 0) {

					window[$('#video').children('.flash').children('div').attr('id')];
				
				} else if (page == "index" && $('#audio.pane:visible').length > 0) {

					window[$('#audio').children('.flash').children('div').attr('id')];
				
				} else {

					$('#video, #audio').children('.flash').children('div').empty();
				
				}
				
			}
	
		});

	
		/* Center Tabs */
			
			
		$('.tabbed:not(#featured.tabbed, #featured.tabbed .tabbed, .pane .tabbed) > ul.tabs').each(function() {
		
			var containerWidth = $(this).width();
			var tabsWidth = 0;
		
			$(this).children().each(function() {
			
				tabsWidth += $(this).width();
			
			});
			
			var paddingLeft = (containerWidth - tabsWidth) / 2;
			
			$(this).css('padding-left', paddingLeft + 'px');
			$(this).css('width', containerWidth - paddingLeft + 'px');
		
		});
		
	}
		

/* FUNCTION: PHOTO BROWSING TABS
--------------------------------------------------------------------------- */


	function photoTabs() {
	
		$('#tab-1-2 > ul.tabs').tabs({
					
			click: function() {			
	
				$('div.loading').fadeIn();
				Cufon.refresh();
				
			}, load: function() {
						
				$('div.loading').fadeOut();
				slideshow();
				Cufon.refresh();

			}, show: function() {

				$('div.loading').fadeOut();
				slideshow();
				Cufon.refresh();

			}
	
		});

	}


/* FUNCTION: PHOTO GALLERY TABS
--------------------------------------------------------------------------- */


	function galleryTabs() {
	
		$('ul.tabs.browser').tabs({
					
			click: function() {			
	
				$('div.loading').fadeIn();
				slideshow();
				
			}, load: function() {
						
				$('div.loading').fadeOut();
				slideshow();
			
			}, show: function() {

				$('div.loading').fadeOut();
				slideshow();
				
			}
	
		});

	}
	

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


	function killTabs() {
	
	
		// SITE
		if (page == "site") {
		
			if ($('.tabbed #blog').length < 1) $('.tabbed .blog').remove();
			if ($('.tabbed #favorites div').length < 1) $('.tabbed .favorites, .tabbed #favorites').remove();
			if ($('.tabbed #recent div').length < 1) $('.tabbed .recent, .tabbed #recent').remove();
			if ($('.tabbed #photos div div').length < 1) $('.tabbed .photos, .tabbed #photos').remove();
			if ($('.tabbed #friends div').length < 1) $('.tabbed .friends, .tabbed #friends').remove();
			if ($('.tabbed > div').length < 1) $('.tabbed').remove();
			
		}
		
		// TOUR
		if (page == "tour") {
		
			if ($('.tabbed #presales').length < 1) $('ul.tabs .presales').remove();
			if ($('.tabbed #appearances').length < 1) $('ul.tabs .appearances').remove();
			if ($('#upcoming > div').length < 1) $('#upcoming').remove();

		}
		
		// ACE
		if (page == "ace") {
		
			if ($('.tabbed #friends').length < 1) $('ul.tabs .friends').remove();
			
		}
	
	}
	
	
/* FUNCTION: CUFON REPLACEMENTS
----------------------------------------------------------------------------- */


	function cufon() {

		var eurofurence_light = [
		
			
		];
		
		var advantgarde_regular = [
		
		'.upsell.referral-prompt h3',
		'#entry h3',
		'#site h3',
		'#my_moment h3',
		'h3.heading',
		'.upsell.referral-prompt p',
		'#member dt.alias',
		'#navigation a:not(#navigation li li a)',
		'#navigation li li.fans',
		'#currently dd.status',
		'#page-fans #referral p.description',
		'.placeholder p',
		'.prompt.site h3',
		'.prompt.firstcomment h3',
		'#browse-comments .tally h3',
		'#announcements h4 a',
		'#announcements .confirmation h3',
		'#ticketmaster h3 strong'
		
		];
		
		var advantgarde_medium = [
		
		'#link h2',
		'body.section-legal h2',
		'#main #latest-headlines h3',
		'#community h3',
		'#sidebar div:not(.widget-poll, #my_moment) h3',
		'#related-photos h3',
		'#currently h3',
		'#tag h3',
		'#album #details h3',
		'#page-fans #photos h3',
		'#page-fans #entries h3',
		'#page-fans #referrals h3',
		'#welcome h3:not(div.markup h3)',
		'#page-fans #referrals em',
		'#photo h4',
		'#member dd.status',
		'ul.tabs a',
		'ul.tabs strong',
		'#featured div.details a:not(#featured a.comments, #listen .details a)',
		'#links li a',
		'#page-video #videos h3',
		'#event-presales h3',
		'#tips h3'
		
		];

		Cufon.replace(advantgarde_regular, { fontFamily: 'Avant Garde Regular', hover: true });
		Cufon.replace(advantgarde_medium, { fontFamily: 'Avant Garde Medium', hover: true });

		$(advantgarde_regular.toString()).css('visibility', 'visible');
		$(advantgarde_medium.toString()).css('visibility', 'visible');
				
	}
	
	
/* CREATE GALLERY SLIDESHOW
----------------------------------------------------------------------------- */

	
	function slideshow() {
		
		$('.slideshow').each(function() {

			if ($(this).children('ul.photos').children('li').length > 1) {
			
				$(this).append('<a href="#prev" id="prev" class="paging">Previous</a><a href="#next" id="next" class="paging">Next</a>');
				$(this).children('ul.photos').cycle({
			
					fade: true,
					speed: 400,
					timeout: 8000000,
					next: '#next',
					prev: '#prev' 
				
				});
			
			}
		
		});

		$('#my_moment').each(function() {
		
			$(this).children('ul.photos').cycle({
			
				fx: 'scrollLeft',
				speed: 400,
				timeout: 12000
				
			});
		
		});
		
	}


/* ROTATE FEATURED AWARDS
----------------------------------------------------------------------------- */

	
	function rotate(object) {

		switch(object) {

			// Rotate Featured Banners
			case "Featured Awards":
	
				$.get('/feeds/banners', function(data) {
	
					// Load banners
									
					$(data).find('unit').each(function() {
	
						if ($(this).find('name').text() == "featured_awards") {
	
							var slideshow = '<div id="slideshow">';
							var tabs = '<ul class="tabs">';
							var bannerNumber = 1;
							var unitName = $(this).find('name').text();
	
							$(this).find('banner').each(function() {
	
								var url = $(this).children('url').text();
								var image = $(this).children('image').text();
								var caption = $(this).children('description').text();
	
								tabs += '<li><a href="#banner-' + unitName + '-' + bannerNumber + '">' + caption + '</a></li>';
	
								// Add corners mask
								slideshow += '<a id="banner-' + unitName + '-' + bannerNumber + '" href="' + url + '"><img src="' + image + '" alt="' + caption + '" /></a>';
	
								bannerNumber++;
	
							});
	
							slideshow += '</div>';
							tabs += '</ul>';
	
							$(slideshow).appendTo('#awards');
							$(tabs).appendTo('#awards');
	
						}
	
					});
					
					$('#awards ul.tabs > li:last-child').addClass('last');
					$('#awards ul.tabs').tabs({ fx: { opacity: 'toggle', duration: 'slow' } });
					$('#awards ul.tabs').tabs('rotate', 5000);
					Cufon.refresh();
	
				});
				
				break;

			// Rotate Featured Events Banner
			case "Featured Events":
	
				$.get('/feeds/banners', function(data) {
	
					// Load banners
									
					$(data).find('unit').each(function() {
	
						if ($(this).find('name').text() == "featured_events") {
	
							var slideshow = '<div id="slideshow">';
							var tabs = '<ul class="tabs">';
							var bannerNumber = 1;
							var unitName = $(this).find('name').text();
	
							$(this).find('banner').each(function() {
	
								var url = $(this).children('url').text();
								var image = $(this).children('image').text();
								var caption = $(this).children('description').text();
	
								tabs += '<li><a href="#banner-' + unitName + '-' + bannerNumber + '">' + caption + '</a></li>';
	
								// Add corners mask
								slideshow += '<a id="banner-' + unitName + '-' + bannerNumber + '" href="' + url + '"><img src="' + image + '" alt="' + caption + '" /></a>';
	
								bannerNumber++;
	
							});
	
							slideshow += '</div>';
							tabs += '</ul>';
	
							$(slideshow).appendTo('#tour');
							$(tabs).appendTo('#tour');
	
						}
	
					});
					
					$('#tour #banners ul.tabs > li:last-child').addClass('last');
					$('#tour #banners ul.tabs').tabs({ fx: { opacity: 'toggle', duration: 'slow' } });
					$('#tour #banners ul.tabs').tabs('rotate', 5000);
					Cufon.refresh();
	
				});
				
				break;

			default:
				
		}
		
	}


/* FUNCTION: CYCLE PHOTOS TAB IMAGES
----------------------------------------------------------------------------- */


	function photosTab() {

		// SHOW TITLES ON #HEADER HOVER
		$('#featured li.photos').hover(function() {

			$(this).addClass('hover');

		}, function() {

			$(this).removeClass('hover');

		});
		
		// CYCLE VIDEO THUMBNAILS
		$('#featured li.photos ul').cycle({

			fx: 'scrollLeft',
			speed: 400,
			timeout: 6000

		});

	}
	
	
/* FUNCTION: FIX AJAX TAB PAGING
----------------------------------------------------------------------------- */


	function fixPaging() {
	
		$('#page-media #videos ul.paging a').click(function() {

			$('div.loading').fadeIn();
				
			$(this).parents('div.pane').load($(this).attr('href'), function(data) { 
			
				$('div.loading').fadeOut();			
				fixPaging();
			
			});
			
			return false;

		});	
	
	}
