/* EV FAN CLUB */
/* JAVASCRIPT
----------------------------------------------------------------------------- */
	var $ = jQuery.noConflict();

	try{Typekit.load();}catch(e){}

	   $(document).ready(function() {
		$.expr[':'].external = function(obj){
		    return !obj.href.match(/^mailto\:/)
		            && (obj.hostname != location.hostname);
		};
		$(".events li.public.available a, a.action.tickets").attr('target','_blank');

		var lightbox = {
			viewportwidth: null,
			viewportheight: null,
			screen: $('#screen'),
			lightboxes: $('.lightbox'),
			getViewport: function() {
			    if (typeof window.innerWidth != 'undefined'){
				   this.viewportwidth = window.innerWidth,
				   this.viewportheight = window.innerHeight
			    }
			    else if (typeof document.documentElement != 'undefined'
				  && typeof document.documentElement.clientWidth !=
				  'undefined' && document.documentElement.clientWidth != 0){
				    this.viewportwidth = document.documentElement.clientWidth,
				    this.viewportheight = document.documentElement.clientHeight
			    }else{
				    this.viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
				    this.viewportheight = document.getElementsByTagName('body')[0].clientHeight
			    }
			},
			alignElements: function(eles) {
			   $(eles).each(function(){
				target = $(this);
				target.css({
				   top: (lightbox.viewportheight/2) - 150 - target.height()/2 + 'px',
				   left: lightbox.viewportwidth/2 - 30 - target.width()/2 + 'px'
				});
			   });
			},
			toggleLightboxes: function(ele){
			   if( this.lightboxes.is(':visible') ){
				this.lightboxes.fadeOut('fast');
				this.screen.fadeOut('fast');
			   } else {
				this.screen.fadeIn(function(){
				   ele.fadeIn('fast');
				});
			   }
			}
		}
		lightbox.getViewport();
		login_modal = $('div.login-modal.lightbox');

		if( login_modal.length ){
			lightbox.alignElements(login_modal);
			$(window).resize(function(){lightbox.getViewport(); lightbox.alignElements(login_modal)});
			$('#screen').click(function(){lightbox.toggleLightboxes(login_modal)});
			$('.login a').click(function(e){
				e.preventDefault();
				lightbox.alignElements(login_modal);
				lightbox.toggleLightboxes(login_modal);
			});
		}

		// Automatically select RSVP on events detail page
		var ev_rsvp = $('form[name=eventModifyForm]');
		if( ev_rsvp.length ){
		   ev_rsvp.find('label').click(function(){
		   $(this).find('input').trigger('click');
			ev_rsvp.find('input.save').trigger('click');
		   	$('#rsvp .toggle li').animate({opacity:0.5},300);
		   });
		}

		var ev_member_card = $('.widget-member.browse li.member');
		if( ev_member_card.length ){
		   ev_member_card.hover(function(){
			ev_member_card.not($(this)).stop().animate({opacity:0.5}, 200);
		   }, function(){
			ev_member_card.stop().animate({opacity:1}, 200);
		   })
		}

		var ev_addcomment = $('#addcomment');
		if( ev_addcomment.length ){
		   $('textarea', ev_addcomment).focus(function(){
			$(this).addClass('focused');
		   });
		   $('textarea', ev_addcomment).blur(function(){
			$(this).removeClass('focused');
		   })
		}

		   var tweet_container = $('div#tweet');
		   var widget_event = $('div.widget-event');

		   function rotate(ele, container){
			  active = $( ele + '.active', container);
			  next = active.next().length ? $(active.next()) : $(ele, tweet_container).eq(0);
			  next.css({top:(80 - next.height()/2) + 'px'});
			  active.fadeOut(function(){
			     next.fadeIn().addClass('active');
			     setTimeout(function(){rotate(ele, container);}, 5000);
			  }).removeClass('active');
		     }

		     if( tweet_container.length ){
			  tweet_container.tweet({
				username: ["AmyLeeEV", "Evanescence"],
				join_text: "auto",
				count: 10,
				auto_join_text_default: "Amy Lee says",
				auto_join_text_ed: "Amy Lee",
				auto_join_text_ing: "Amy Lee was",
				auto_join_text_reply: "Amy Lee replied to",
				auto_join_text_url: "Amy Lee says",
				loading_text: "loading tweets...",
				template:"<p class=\"tweet_join\">{screen_name} Says</p><p class=\"tweet-body\">{text}</p><p>{time}</p><a href=\"{user_url}\" target=\"blank\">Follow {screen_name} on Twitter</a>"
			  }).bind("loaded", function(){
			     first_tweet = $('ul li', tweet_container).eq(0);
			     first_tweet.css({top:(80 - first_tweet.height()/2) + 'px'}).addClass('active');
			     setTimeout(function(){rotate('li', tweet_container);}, 5000);
			  }).bind("loaded",
				function(){
					$('p.tweet_join').each(function(){
						$(this).html($(this).text().replace("AmyLeeEV", "Amy Lee"));
					})
				});
		     }



		     if( widget_event.length ){
			  rsvp_toggle = $('.toggle label', widget_event);

			  if( rsvp_toggle.length ){
			     //we have rsvp
			     rsvp_toggle.click(function(e){
				clicked_li = $(this).parents('li.toggle');
				$('.active', clicked_li.parents('ul.toggle')).removeClass('active');
				clicked_li.addClass('active');
			     });
			     rsvp_toggle.each(function(){
				  cur_input =  $('input', $(this));
				  if( cur_input.is(':checked') ){
				     cur_input.parents('li.toggle').addClass('active');
				  }
			     });
			  }
		     }

		     // lightbox
		     var screen = $('#screen');

		     if( screen.length ){

		     }
	});


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


	$(window).load(function() {


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

		// 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 CALLS
----------------------------------------------------------------------------- */

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

		readmore();

	}

	killTabs();
	tabs();
	paging();
	truncate();
	comments();


/* 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 > 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');
						if( $('#video div.flash div').length ){
							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: 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) {

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

		}

	}



