/* DARIUS RUCKER */
/* Javascript Functions
----------------------------------------------------------------------------- */


	$(document).ready(function() {
	
		//MEET GREET LINK ADDITION TO PHOTOS TAG LIST
		$('<li class="meetgreet"><a href="http://www.flickr.com/photos/dariusrucker" target="_blank">Meet and Greet Photos (JAN 2011)</a></li><li class="meetgreet"><a href="https://www.richardsandsouthern.com/store/meet-greet-pictures-c-607.html" target="_blank">Meet and Greet Photos</a></li>').insertAfter('#page-photos #scope div.widget-tag.list ul li:last-child');
	
	});


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


	$(window).load(function() {


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


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

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

	}, function() {

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

	});



/* HIDING MEMBER TALLY ON HOME PAGE
--------------------------------------------------------------------------- */


	$('body#page-index #content div.widget-member.browse h3 em').remove();


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


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


/* FUNCTION CALLS
----------------------------------------------------------------------------- */


	if (page == "index") {
	
		var link = $('div.widget-event.program a.browse').attr('href').replace(/featured/, '');
		$('div.widget-event.program a.browse').attr('href', link);
	
	}

	if (page == "index" || page == "news" || section == "client") {

		readmore();

	}
	
	if (section != "webslice" ) {
	
		killTabs();
		tabs();
		paging();
		truncate();
		comments();
		wording();
		twitter();
		banners();
		//overlay();
		
		if (page == "photos") mobileRoadie();

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

		// PLUGIN: CLIQUE FORMS (Adds text into input forms)
		$('#mailing_list_entry_email').cliqueForms({ placeholder: "Enter Your Email Address" });

	}
		
	if (page == "discography") {
	
		playPreviewTracks();
	
	}
	
	if (page == "webslice-photos") {
	
		photoSlideshow();
		
	}
	
	cufon();
	webslice();
	

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


	}


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


	});


/* FUNCTION: WORDING CHANGES
----------------------------------------------------------------------------- */


	function wording() {

		$('widget-account modify a.submit').text('Save Changes');
		$('widget-account modify a.submit').text('Save Changes');
		$('#page-vip .widget-item div.purchase h4').text('VIP Packages on sale now');
		$('#page-vip .widget-item div.purchase.limited h4').text('VIP Packages are currently unavailable');
		Cufon.refresh();
		
	}



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


	function cufon() {

		var tradegothic_bold = [ 
		
			'h1', 
			'h2', 
			'h3', 
			'h4', 
			'h4 a',
			'#navigation li a',
			'ul.tabs li a',
			'ul.tabs li strong',
			'a.follow',
			'a.action:not(#signin a.action)',
			'ul.pages a',
			'.widget-event td.date',
			'.widget-event td.details',
			'.widget-event td.location',
			'#page-index div.widget-event a.date',
			'#page-index div.widget-event ul.tickets a',
			'.widget-editorial ul.headlines li a:not(a.comments)',
			'#upsell ul'
			
		];

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

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


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

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


/* 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') {

			$('div#features > ul.tabs').tabs({

				fxSlide: false,

				show: function() {
				
					Cufon.refresh();

					// 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');
						eval(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)();

					}

					else {

					}

				}

			});

		}

		// JAVASCRIPT TABS
		$('div.tabbed').not("#features, .ajax, .static").children('ul.tabs').tabs({

			fxSlide: false,

			click: function() {
			
				Cufon.refresh();

			}, load: function() {

				truncate();
				Cufon.refresh();

			}, show: function() {

				truncate();
				Cufon.refresh();

			}

		});

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

			fxSlide: false,

			click: function() {

				loading('start');
				Cufon.refresh();

			}, load: function() {

				truncate();
				loading('stop');
				Cufon.refresh();

			}, show: function() {

				truncate();
				loading('stop');
				Cufon.refresh();

			}

		});

	}
	

/* 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) {

				sifr();
				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>');

			}

		});

		// EVENT LISTING ON INDEX PAGE: SHORTEN LOCATION
		$('#page-index div.widget-event td.details').each(function() {

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

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

				$(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: TRACK LISTING PREVIEW PLAYER
----------------------------------------------------------------------------- */


	function playPreviewTracks() {

		var player = $('div.widget-media').children('div.flash').children('div').attr('class');

		$('div.widget-release.tracks table td.listen a').addClass('stopped');

		$('div.widget-release.tracks table td.listen a.stopped').live('click', function() {
		
			var track = $(this).attr('href');

			$('div.widget-release.tracks table td.listen a').addClass('stopped').text('Listen');
			Cufon.refresh();
			$(this).removeClass('stopped').addClass('playing').text('Stop');

			document[player].pauseClip();
			document[player].playClip(track);
			
			return false;

		});
		
		$('div.widget-release.tracks table td.listen a.playing').live('click', function() {

			$(this).removeClass('playing').addClass('stopped').text('Listen');
			Cufon.refresh();
			document[player].pauseClip();
			return false;
		
		});

	}
	

/* FUNCTION: WEBSLICE
----------------------------------------------------------------------------- */

	
	function webslice() {
	
		$('#webslice a:not(#navigation a)').attr('target', '_blank');
	
	}
	
	
/* FUNCTION: OVERLAY
----------------------------------------------------------------------------- */


	function overlay() {
	
		$('div#overlay').click(function() {
		
			$(this).fadeOut();
			
		});
		
	}
	
	
/* FUNCTION: MOBILE ROADIE
--------------------------------------------------------------------------- */

	function mobileRoadie() {
		
		$('div.widget-tag.browse > ul').append('<li class="fan"><a href="?photos=fan">Fan Photos</a></li>');

		var showphotosLoaded = false;
		var fanwallLoaded = false;
		var $fanphotos = $('#fan-photos ul.photos');

		// Temporarily hide photo listing until all feeds are loaded		
		$fanphotos.css('visibility', 'hidden');

		// Set up showphotos feed
		$.get(site_host + '/feeds/external/showphotos', function(photos) {

			var photosLoaded = 0;
			var numberOfPhotos = $(photos).find('item').length;

		    $(photos).find('item').each(function() {

				var image = $(this).children('imgpath').text();
				var caption = $(this).children('caption').text();
				var timestamp = $(this).children('created').text();

				if (image == '') {
				
					numberOfPhotos--;
				
				} else {

					$('<li><img src="' + image + '" alt="' + caption + '" title="' + caption + '" /><strong> ' + timestamp + '</strong></li>').appendTo($fanphotos);
					photosLoaded++;
					
				}
				
				if (photosLoaded >= numberOfPhotos) showphotosLoaded = true;

		    });

		});

		// Set up fanwall feed
		$.get(site_host + '/feeds/external/fanwall', function(photos) {
			
			var photosLoaded = 0;
			var numberOfPhotos = $(photos).find('item').length;
			
		    $(photos).find('item').each(function() {

				var image = $(this).children('imgpath').text();
				var caption = $(this).children('caption').text();
				var timestamp = $(this).children('created').text();

				if (image == '') {
				
					numberOfPhotos--;
				
				} else {

					$('<li><img src="' + image + '" alt="' + caption + '" title="' + caption + '" /><strong> ' + timestamp + '</strong></li>').appendTo($fanphotos);

					photosLoaded++;
					
				}
				
				if (photosLoaded >= numberOfPhotos) fanwallLoaded = true;

		    });

		});

		var interval = setInterval(function() {

			// Sort photos by timestamp once feeds are loaded
			if (showphotosLoaded && fanwallLoaded) {
		
				var mylist = $('ul.photos');
				var listitems = mylist.children('li').get();
				listitems.sort(function(a, b) {
				   var compA = $(a).children('p').text().replace(/-/g, '').replace(/ /g, '').replace(/:/g, '');
				   var compB = $(b).children('p').text().replace(/-/g, '').replace(/ /g, '').replace(/:/g, '');
				   return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
				})
				$.each(listitems, function(idx, itm) { mylist.append(itm); });
			
				clearInterval(interval);
				
				// Unhide photo listing
				$fanphotos.css('visibility', 'visible');

			}
		
		}, 33);

	}
