/* STANDARD TEMPLATE */
/* Javascript Functions
----------------------------------------------------------------------------- */


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


	$(window).load(function() {


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

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


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

	
			navigation();
		//	login();
			if (page == "index" || page == "news" || section == "client") readmore();
			killTabs();
			tabs();
			truncate();
			comments();
			paging();
			highlightSections();
		//	Cufon.replace('#page-lineup div.story a', { hover: true });

		
/* PLUGINS CALLS
----------------------------------------------------------------------------- */


/* Plugin: Clique Actions */
		
		
			try {
			$('input[type=submit]').cliqueActions();
			} catch (error) { alert('Error: Missing Clique Actions Plugin'); }
			
			if (section == "store") {

				
/* Plugin: Clique Forms */
			
		
				try {
				$('.widget-item.search input#query').cliqueForms({ placeholder: "Search Store" });
				$('.widget-item.search input#query').css('visibility', 'visible'); // Experiencing problems chaining this 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: true,
					pressure: true,

					browser: 'thumbnails', // thumbnails / slideshow
					removeEmptyThumbs: true, // Removes rows of empty thumbnails
					viewer: true, 
					preview: true, // Shows enlarged images when hovering thumbnails
					zoom: 'loupe' // loupe / 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: NAVIGATION DROP DOWN
----------------------------------------------------------------------------- */


	function navigation() {
	
		$('ul#navigation li:has(ul)').hover(function() {
	
			$(this).addClass('hover');
	
		}, function() {
	
			$(this).removeClass('hover');
	
		});
	
	}
	
	
/* FUNCTION: LOGIN
----------------------------------------------------------------------------- */


	function login() {
	
		if (page == "login") {
	
			// 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: 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 ( $(this).find('div.story > *:last-child').hasClass('lead') ) {

				// IF LAST CHILD IS P.LEAD
				$(this).find('div.story p.lead:last').append('&nbsp;&nbsp;<a class="readmore" href="' + link + '">Read More...</a>');

			} else {

				// IF LAST CHILD IS ANOTHER ELEMENT
				$(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') {

			// Photos is the first tab, begin cycling
			if ( $('#photos').length > 0 && $('ul.tabs li:first-child a.tab').attr('href') == "#photos" ) {
				
				$('div#features > ul.tabs').tabs();
								
				$('#photos').addClass('pane').show();
			
				photoSlideshow();
				
				if ($('div#features div.widget-photo.slideshow ul.photos > li[class!=last]').length > 0) {

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

						}

					)
				
				}
			
			}
			
			else {

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

			}, load: function() {

				truncate();

			}, show: function() {

				truncate();

			}

		});

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

			fxSlide: false,

			click: function() {

				loading('start');

			}, load: function() {

				truncate();
				loading('stop');

			}, show: function() {

				truncate();
				loading('stop');

			}

		});

	}


/* FUNCTION: AJAX LOADING INDICATOR
----------------------------------------------------------------------------- */


	function loading(event) {

		if (event == "start") {

			// SHOW LOADING INDICATOR

		} else {

			// HIDE LOADING INDICATOR
		
		}

	}


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

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

			}

		});

	}


/* FUNCTION: COMMENTS
--------------------------------------------------------------------------- */


	function comments() {

		// ADDS CANCEL BUTTON FUNCTIONALITY
		if ($('#comment').parent().siblings('ul.tabs').children().length > 1) {

			$('#comment a.cancel').click(function() {

				$('#comment textarea').val('');
				$('#comment').parent().siblings("ul.tabs").tabsClick(1);
				return false;

			});

		}

		// REMOVES CANCEL BUTTON IF NO COMMENTS EXIST
		else {

			$('#comment a.cancel').remove();

		}

	}


/* FUNCTION: HIGHLIGHT CONTENT SECTIONS
--------------------------------------------------------------------------- */


	function highlightSections() {

		$('div.widget-editorial.sections ul.headlines li').each(function() {
		
			$(this).find('strong, em, span, a.comments').remove();
		
			if ($('div.widget-editorial h4').text() == $(this).children('a').text()) {

				$(this).children('a').wrap('<strong></strong>');
			
			}
		
		});
		
	}