/* JANET JACKSON */
/* Javascript Triggers and Custom Functions
----------------------------------------------------------------------------- */


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


	$(document).ready(function() {
		
		// Prep elements for animation
		$('#ticker').css('width', 0);

		if (ie6 == false) {
		
			$('#bar').css('opacity', '0');
			
		}

		// Normal				
		$('#network').find('a').each(socialSitesContract);
		$('#network').find('a').addClass('hover');

		if (page == "photos") {
		
			if ($('#page_number').text() == "1") {

				// First Page
				$('ul.photos > li:nth-child(1)').addClass('current');
				$('ul.photos > li:nth-child(2)').addClass('next');
				$('ul.photos > li:nth-child(3)').hide();
	
			} else {
			
				// Other Pages
				$('ul.photos > li:nth-child(1)').addClass('prev');
				$('ul.photos > li:nth-child(2)').addClass('current');
				$('ul.photos > li:nth-child(3)').addClass('next');
			
			}
		
		}
		

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

		
	});
	
	
/* BEGIN LOAD FUNCTION
----------------------------------------------------------------------------- */	
	
	
	$(window).load(function() {

		if (section != "messageboard" && section != "checkout" ) {
		
			tabs('removeEmptyTabs');
			twitter();
			socialSites();
			cufon();
			bar();
			
			if (page == "photos") photoBrowser();
			
			// PLUGIN: CLIQUE ACTIONS (Replaces submit buttons with text links)
			try { $('input[type=submit]').cliqueActions(); } catch (error) { alert('Error: Missing Clique Actions Plugin'); }

		}
	
	
/* ALBUM LIST
----------------------------------------------------------------------------- */


		if (page == "videos") {
			
			// Cloning Album Tags - adding to Tag Listing
			$('#tags-albums > ul').clone().appendTo('#tags li:contains(Music Videos)');
			$('#tags li > ul').addClass('albums');
			$('#tags-albums').hide();
			
			// Hiding Sub-Categories
			$('#tags ul.albums li a:contains(Everything)').parent('li').hide();
			$('#tags ul.albums li a:contains(Janet Jackson)').parent('li').hide();
			$('#tags ul.albums li a:contains(Dream Street)').parent('li').hide();
			
		}


/* MAKE ME COOKIE
----------------------------------------------------------------------------- */


		if (page == "gift") {
		
			if ($('#gift').length > 0) {
				$.cookie('mailinglist','subscribed', { expires: 30 });
			}
			
			if ($.cookie('mailinglist') == 'subscribed' && $('#gift').length == 0) {
				window.location = '/gift?give=gift';
			}
		
		}


/* AUTO TEXT AREA HIGHLIGHT
----------------------------------------------------------------------------- */


		if (page == "webkit") {
		
			$('#webkit textarea').click(function() {
			
				this.select();
			
			});
		
		}


/* POLLS
----------------------------------------------------------------------------- */


		$('div.widget-poll td strong:contains("CORRECT")').parents('tr').addClass('correct');

		if (page == "index") {
		
			// Remove empty open poll container
			if ($('#featured #open').find('form').length == 0) {
			
				$('#featured #open').remove();
				
			}

			$('#featured div.widget-poll h3').wrapInner('<a href="#"></a>');

			// Add hover to heading
			$('#featured div.widget-poll h3 a').hover(function() {
			
				$(this).parent().parent().addClass('hover');
			
			}, function() {
			
				$(this).parent().parent().removeClass('hover');
			
			});
			
			$('#featured div div.widget-poll:first-child').addClass('selected');
			
			// Toggle
			$('#featured div.widget-poll h3 a').click(function() {
			
				$('#featured div.widget-poll').removeClass('selected');
				$(this).parent().parent().addClass('selected');
				Cufon.refresh();
				
				
				return false;
			
			});
			
			// Add placeholder if empty
			if ($('#featured #polls').children().length == 0) {
			
				$('#featured #polls').addClass('placeholder');
				$('#featured #polls').append('<h3>Thank you for your vote.</h3><p>Results will be announced later, please check back regularly.</p>');
				
			}

		}
		
		if (page == "question") {
		
			// Work around for Browse Link bug
			$('div.widget-poll.browse a.browse').attr('href', '/trivia');
		
		}
		
		
/* END LOAD FUNCTION
----------------------------------------------------------------------------- */	


	});


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


	function tabs() {

		// JAVASCRIPT TABS
		$('div.tabbed').tabs({

			fxSlide: false
			
		});

	}


/* FUNCTION: TWITTER FEED
----------------------------------------------------------------------------- */


	function twitter() {
	
		$('div.tweets').tweet({
		
        	username: "janetjackson",
          	count: 5,
          	loading_text: "Loading tweets..."

     	 });
     	 
     	 // Using setTimeout since tweet plugin doesn't have callback
     	 setTimeout(cufon, 1000);
     	 setTimeout(function() {
     	 
			$('#ticker ul.tweet_list').cycle({
	
				speed: 300,
				timeout: 7000
	
			});
     	 
     	 }, 1000);
   	 
	}


/* FUNCTIONS: SOCIAL SITES LINKS
----------------------------------------------------------------------------- */


	function socialSites() {
	
		// Hover
		$('#network').find('a').hover(socialSitesExpand,socialSitesContract);
		
	}
			
	function socialSitesExpand() {
		
		// Stop current animation
		$(this).stop();
		
		// Get enlarged logo size
		$(this).addClass('hover');
		var largeWidth = $(this).width();
		$(this).removeClass('hover');
	
		// Animate			
		$(this).animate({
		
			width: largeWidth,
			backgroundPosition: '-42px 0px'
		
		});
		
	}
	
	function socialSitesContract() {
		
		// Stop current animation
		$(this).stop();

		// Animate
		$(this).animate({
		
			width: '42px',
			backgroundPosition: '0px 0px'
		
		}, 400, function() {
		
			// Reset Styles
			$(this).removeAttr('style');
			$(this).removeClass('hover');
		
		});	
	
	}


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


	function cufon() {

		var futura_book = [
		
			'h2', 
			'h4',
			'ul.leads strong',
			'div.widget-editorial.display strong',
			'#news ul.headlines strong',
			'#news ul.headlines li a',
			'#twitter ul.headlines strong',
			'#twitter ul.headlines li a',
			'#page-story ul.headlines strong',
			'#page-story ul.headlines li a',
			'#twitter #info a.follow',
			'ul.tweet_list > li',
			'#page-photos ul.photos strong',
			'#page-janet #highlights p',
			'#player div.widget-media div.details a',
			'#page-causes h3',
			'#page-causes ul.profile li strong',
			'#page-janet a.readmore',
			'#page-janet a.learnmore',
			'#page-janet #causes h3',
			'#page-janet #causes strong',
			'#page-news h3',
			'#page-trivia h3', 
			'#page-question h3', 
			'#page-biography div.story h3',
			'#legals h3',
			'div.widget-alert h3',
			'div.widget-alert p',
			'#page-gift h3.gift',
			'#page-gift p.preface', 
			'#page-webkit #webkit ul.tabs a',
			'#page-webkit #webkit dt'
			
			
		];
		
		var bin_regular = [
		
			'ul.filters li a', 
			'#trivia h3'
			
		];
		
		Cufon.replace(futura_book, { fontFamily: 'Futura Book', hover: true });
		$(futura_book.toString()).css('visibility', 'visible');

		Cufon.replace(bin_regular, { fontFamily: 'BIN Regular', hover: true });
		$(bin_regular.toString()).css('visibility', 'visible');
				
	}

/* FUNCTION: BAR 
----------------------------------------------------------------------------- */


	function bar() {

		setTimeout(function() {

			$('#bar').css('visibility', 'visible');
	
			// Twitter Ticker
			$('#ticker').animate({ width: '100%' }, 300);
			
			if (ie6 == false) {
			
				$('#bar').animate({ opacity: '1' }, 300);
				
			}
			
		}, 300);
	
	}


/* FUNCTION: PHOTO BROWSER
----------------------------------------------------------------------------- */

	
	function photoBrowser() {

		
		// Enlarged Image
		$('ul.photos li.current a').attr('href', '#').click(function() { return false; })
		var image = $('ul.photos li.current img').attr('src').replace('thumb.', '');
		$('ul.photos li.current img').attr('src', image).load(function() { $(this).fadeIn(); });
		

		// Navigation
		var current = $('#page_number').text() * 1;
		var next = "?page_number=" + (current + 1);
		var prev = "?page_number=" + (current - 1);

		if ($('ul.photos li.next a.comments').length > 0) $('ul.photos li.next a.comments').attr('href', next).attr('title', 'Next Photo');
		if ($('ul.photos li.prev a.comments').length > 0) $('ul.photos li.prev a.comments').attr('href', prev).attr('title', 'Previous Photo');
		
		// Hover Effects
		$('ul.photos > li.prev, ul.photos > li.next').hover(function() {
		
			$(this).addClass('hover');
		
		}, function() {
		
			$(this).removeClass('hover');
		
		});
		
	}