/* HALFWAY TO HAZARD */
/* Javascript Functions
----------------------------------------------------------------------------- */


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


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


	$(window).load(function() {


/* RANDOM BACKGROUNDS
----------------------------------------------------------------------------- */


	var random = Math.floor(Math.random()*11) + 1;
	$('body').addClass('b' + random);	

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


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

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

	}, function() {

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

	});


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


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


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


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

	}*/


/* REMOVE DOWNLOAD BUTTONS
----------------------------------------------------------------------------- */


	if (page == "song") {

		$('div.widget-media').find('a.listen').parent('li').remove();

	}


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


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

		readmore();

	}

	replaceSubmitButtons();
	killTabs();
	tabs();
	paging();
	truncate();
	comments();
	// sifr();


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


	}


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


	});


/* 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').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');
						
						if ($('#video a.prompt').length == 0) {
						
							eval(video_player)();

						}

					}

					else {

					}

				}

			});

		}

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

			fxSlide: false,

			click: function() {

			}, load: function() {

				truncate();
				sifr();

			}, show: function() {

				truncate();
				sifr();

			}

		});

		// AJAX TABS
		$('div.tabbed.ajax').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) {

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

			}

		});

	}


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

		}

	}

