/* WILLIE NELSON */
/* Javascript Triggers and Custom Functions
----------------------------------------------------------------------------- */

	$(document).ready(function() {

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

	});


	$(window).load(function() {

		if (section != "messageboard" && section != "checkout" ) {
		
			addThisLeads();
			navigation();
			//login();
			if (page == "index" || page == "news" || section == "client") readmore();
			if (page == "index" || page == "home") twitter();
			//if (section == "store") store();
			tabs('removeEmptyTabs');
			truncate();
			comments();
			paging();
			captionWidth();
			markup();
			
		}
		
		loginRedirect();
		cufon();
		externalLinks();
		
		addthis.button('.addthis_button-leads');

	});

/* FUNCTION: LOGIN PAGE REDIRECT
----------------------------------------------------------------------------- */

	function loginRedirect() {
				
		$('#signin input[name="redirect_to"]').attr('value', '/redirect');
		
	}

	
/* FUNCTION: LOGIN PAGE PROMO
----------------------------------------------------------------------------- */

	function login() {
	
		if (page == "login") {
	
			$('#signin input[name="redirect_to"]').attr('value', '/redirect');
	
			// APPEND NOTE TO SIGN IN BUTTON
			$('div.widget-account.signin em.email').append(' *');
			$('div.widget-account.signin ul.actions').after('<small class="note">* You must now use your email address to sign in. Contact <a href="mailto:support@allaccesstoday.com" target="_blank">customer support</a> if you do not remember the email address on your account.</small>');
	
			// START PACKAGE TIER SLIDESHOW
			$('ul.tiers').before('<div id="paging">').cycle({
	
				fade: true,
				speed: 400,
				timeout: 9000,
				pager: '#paging',
				before: function() { if (window.console) console.log(this.src); }
	
			});
	
			// START UPSELL BENEFITS SLIDESHOW
			$('div#benefits ul').cycle({
	
				fade: true,
				speed: 400,
				timeout: 6000
	
			});
	
			// CUSTOM PAGING LINKS
			$('#paging a:nth-child(1)').livequery(function() { $(this).addClass('basic'); });
			$('#paging a:nth-child(1)').livequery(function() { $(this).text('Basic'); });
			$('#paging a:nth-child(2)').livequery(function() { $(this).addClass('deluxe'); });
			$('#paging a:nth-child(2)').livequery(function() { $(this).text('Deluxe'); });
			$('#paging a:nth-child(3)').livequery(function() { $(this).addClass('premier'); });
			$('#paging a:nth-child(3)').livequery(function() { $(this).text('Premier'); });
			
		}
		
	}
	

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

	function hideCufon() {

		fairplex = [
		'h3:not(#upsell h3, #signin h3)',
		'div.display h4'
		];
		
		fairplexInset = [
		'#navigation a',
		'#title h2',
		'#title #navigation-messageboard a span'
		];
		
		fairplexShadow = [
		'#upsell li h4',
		'#upsell h3'
		]
		
		$(fairplex.toString()).css('visibility', 'hidden');
		$(fairplexInset.toString()).css('visibility', 'hidden');
		$(fairplexShadow.toString()).css('visibility', 'hidden');

	}
	
	function cufon() {
		
		Cufon.replace(fairplexShadow, { textShadow: '1px 1px #000', fontFamily: 'fairplex', hover: true })
		$(fairplexShadow.toString()).css('visibility', 'visible');
			
		Cufon.replace(fairplex, { fontFamily: 'fairplex', hover: true });
		$(fairplex.toString()).css('visibility', 'visible');
		
		Cufon.replace(fairplexInset, { textShadow: '-1px -1px #000', fontFamily: 'fairplex', hover: true })
		$(fairplexInset.toString()).css('visibility', 'visible');
				
	}


/* FUNCTION: STORE
--------------------------------------------------------------------------- */

	function 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 {
		// FIX POTENTIAL "PHOTOS" CLASS CONFLICT
		$('div.widget-item.display.photos').each(function() { if ($(this).find('div.details').length > 0) $(this).removeClass('photos'); });
		// FIRE PLUGIN
		$('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'); } }
		
	}


/* FUNCTION: EXTERNAL LINKS
----------------------------------------------------------------------------- */

	function externalLinks() {		

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

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

			return false;

		});
		
	}
	
	
/* FUNCTION: Twitter
----------------------------------------------------------------------------- */

	function twitter() {

		$('#twitter div.tweets').tweet({

			username: "willienelson",
          	count: 5,
          	loading_text: "Loading tweets..."

		});

	}


/* FUNCTION: AddThis - Leads
----------------------------------------------------------------------------- */

	function addThisLeads() {
						
		var addthis_config = {
			
			ui_header_color: '#000000',
		    data_track_clickback: true

		}

		$('ul.leads > li').each(function() {

				var editorialURL = $(this).children('h4').children('a').attr('href');
				var editorialTitle = $(this).children('h4').text();

				$(this).append('<div class="share"><a class="addthis_button-leads" addthis:url="' + site_host + editorialURL + '" addthis:title="' + editorialTitle + ' &raquo; Willie Nelson">Share This</a></div>');
				
		});
		
	}
	
	
/* FUNCTION: Caption Width
----------------------------------------------------------------------------- */

	function captionWidth() {
		
		$('div.widget-editorial div.story p img + small').each(function() {
		
			var imageWidth = parseInt($(this).siblings('img').width());
			var captionPaddingLeft = parseInt($(this).css('padding-left'));
			var captionPaddingRight = parseInt($(this).css('padding-right'));
			var captionPadding = captionPaddingLeft + captionPaddingRight;
			var captionWidth = imageWidth - captionPadding; 
			
			$(this).css('width', captionWidth);
			
			
		});
		
		$('div.widget-editorial div.story p a.photo + small').each(function() {
		
			var imageWidth = parseInt($(this).siblings('a.photo').width());
			var captionPaddingLeft = parseInt($(this).css('padding-left'));
			var captionPaddingRight = parseInt($(this).css('padding-right'));
			var captionPadding = captionPaddingLeft + captionPaddingRight;
			var captionWidth = imageWidth - captionPadding; 
			
			$(this).css('width', captionWidth);
			
			
		});
		
	}


/* FUNCTION: Markup
----------------------------------------------------------------------------- */	
	
	function markup() {
	
		// Change copy of join alert
		$('div.widget-alert p:contains("or join today to continue, details may be found below")').each(function() {

			$(this).html('<p>Please <a href="/login">Sign In</a> or join the fan club today to continue, details may be found at <a href="http://clubluck.willienelson.com">http://clubluck.willienelson.com</a>.</p>');
			
		});

		// Add Pre-sale TBA to tagged events on events page
		$('<p><strong>Pre-sale TBA</strong></p>').prependTo('#page-upcoming table.events tr.presale_tba td.tickets');

	}
	
