/* JESSE McCARTNEY */
/* Javascript Functions
----------------------------------------------------------------------------- */


	$(document).ready(function() {
	
		markup();
	
	});


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


	$(window).load(function() {


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


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


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


	if (page == 'login') {

		// START UPSELL BENEFITS SLIDESHOW
		$('ul#benefits').cycle({

			fade: true,
			speed: 400,
			timeout: 6000

		});

	}


/* MAILING LIST
----------------------------------------------------------------------------- */


	$("#mailinglist fieldset label:first").hide();
	
	if ($("#mailinglist #mailing_list_entry_email").val() == "") {

		$("#mailinglist #mailing_list_entry_email").val("Enter your e-mail address").attr("title", "Enter your e-mail address");
	
	}
	
	$("#mailinglist label.error").each(function() {

		var message = $(this).children('strong').text();
		$(this).children('input[type=text]').val(message).attr("title", message);
	
	});			

	$("#mailinglist input[type=text]").click(function() {
	
		var label = $(this).attr("title");
		if (label == $(this).val()) {
		
			$(this).val("");
		
		}			
	
	});
	
	$("#mailinglist input[type=text]").blur(function() {
	
		var label = $(this).attr("title");
		if ($(this).val() == "") {
		
			$(this).val(label);
		
		}
		
	});


/* PLAY EXCLUSIVE FAN CLUB MESSAGE
----------------------------------------------------------------------------- */


	if (page == 'index' || page == 'home') {

		$('div.prompt a.listenmessage').click(function() {

			$('#features ul.tabs').tabsClick(4);
			return false;

		});

	}


/* CHANGE TEXT OF RSVP LINKS
----------------------------------------------------------------------------- */


	$('div.widget-event.program.callout ul.events li').each(function() {

		$(this).children('a.rsvp').text("See Who's Going");
		$(this).children('a.rsvp').addClass('action').wrap('<li></li>');
		$(this).children('li').prependTo($(this).find('ul.actions'));
		
		$('a.rsvp').show();
	
	});
	
	
/* STORE TRIGGERS
----------------------------------------------------------------------------- */	


		if (section == "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 {
			$('div.widget-item.display:not(.photos, .gift)').cliqueItemWidget({
			
				checkStock: false,
				pressure: false,
				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: 'none' // 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 CALLS
----------------------------------------------------------------------------- */


	killTabs();
	tabs();
	paging();
	truncate();
	comments();
	sifr();
	twitter();
	store();


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


	}


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


	});


/* TWITTER FEED
----------------------------------------------------------------------------- */


	function twitter() {

		$("div#twitter-store div.tweets").tweet({

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

     	 });


	}
	

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

			$('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();
						$('#messages 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)();

					}

					// MESSAGES TAB
					else if ($('#messages:visible').length > 0) {

						$('#video div.flash div').empty();

						photoSlideshow();

						var messages_player = $('#messages div.flash div').attr('id');
						eval(messages_player)();

					}

					// VIDEO TAB
					else if ($('#video:visible').length > 0) {

						$('#radio div.flash div').empty();
						$('#messages 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() {

			}, load: function() {

				truncate();
				sifr();

			}, show: function() {

				truncate();
				sifr();

			}

		});

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

			fxSlide: false,

			click: function() {

				loading('start');

			}, load: function() {

				truncate();
				sifr();
				loading('stop');

			}, show: function() {

				truncate();
				sifr();
				loading('stop');

			}

		});

	}


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

				truncate();
				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 .member dd.status').each(function() {

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

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

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

			}

		});

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

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

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

				$(this).text(text);
				$(this).append('<var>&nbsp;&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: STORE
--------------------------------------------------------------------------- */

	
	function store() {

		$('div.widget-item.browse ul.items li').each(function() {
			
			if ($(this).hasClass("new_fit"))
			$(this).append('<em>New Fit</em>');
				
		});
			
	}
	
	
/* FUNCTION: MARKUP CHANGES
----------------------------------------------------------------------------- */


	function markup() {
	
		$('#cart a.change').text('View Cart');
		
	}