
// sharing tool configuration

var addthis_config = {
     
	username: "humidbeings",
	ui_header_color: "#ffffff",
    ui_header_background: "#3366FF"
}

$(document).ready(function(){

	// handles pencil ads

		$('.pencil-ad .closed a').click(function(){
			
			$(this).parent().next().show();
			$(this).parent().hide();
			return false;
		});

		$('.pencil-ad-closer').click(function(){
			
			$(this).parent().prev().show();
			$(this).parent().hide();
			return false;
		});
	
	// handles the profile and sharing tools

		$('li.profile-tools-opener').click(function(){
			
			$(this).parent().next().toggle();
			$(this).toggleClass('open');

			$(this).parent().next().next().hide();
			$(this).next().removeClass('open');
		});

		$('li.share-tools-opener').click(function(){
			
			$(this).parent().next().hide();
			$(this).prev().removeClass('open');

			$(this).parent().next().next().toggle();
			$(this).toggleClass('open');
		});


	// handles multiple flashes

		if ($('div.success').length > 1){
			
			$('div.success:first').hide();
		}

		//if ($('div.error').length > 1){
			
			//$('div.error:first').hide();
		//}

		if ($('div.warning').length > 1){
			
			$('div.warning:first').hide();
		}


	// handles the top navigation dropdowns

		$('li.with-drop').hover(function(){
			
			$('#global-tools .tool-lists').hide();
			$('#global-tools li.opener').removeClass('open');

			var this_id = $(this).attr('id');
			$('#' + this_id + '-drop').show();
			$(this).addClass('open');
			$(this).next().addClass('no-sep');
		});

		$('div.nav-drop').hover(function(){}, function(){
			
			$(this).hide();
			$('li.with-drop').removeClass('open');
			$('#main-menu li').removeClass('no-sep');
		});

		$('#main-menu li, #header').not('.with-drop').hover(function(){
			
			$('div.nav-drop').hide();
			$('li.with-drop').removeClass('open');
			$('#main-menu li').removeClass('no-sep');
		});

		$('a.categories-opener').click(function(){
			
			$('#the-blogs-drop').show();
			$('#the-blogs').addClass('open');
			$('#the-blogs').next().addClass('no-sep');
			return false;
		});

		$('div.nav-drop p.drop-tab a').click(function(){
			
			var parent = $(this).closest('div.nav-drop');
			parent.find('table').hide();
			parent.find('p.drop-tab a').removeClass('active');
			$('#' + $(this).attr('rel')).show();
			$(this).addClass('active');
			$(this).blur();
			return false;
		});


	// switches the background image

		$('#design-switch').click(function(){
			
			$('body').css('background', '#000');
			$('#page').css('background-image', 'none');

			$.getJSON("/backgrounds/get_next/", null, function(json){
				
				bg_css = 'url(' + u + 'backgrounds/' + json.image + ') no-repeat scroll 50% 0';

				$('body').css('background', '#' + json.tile_color);
				$('#page').css('background', bg_css);
			});

			track_event('Site Background', 'Browse', null, null);
			
			$(this).blur();
			return false;
		});


	// handles the search text field behavior
		
		var main_search			= $('#i-search, #profile-search');
		main_search_default		= 'Type here to search...';

		main_search.val(main_search.val() == '' ? main_search_default : main_search.val());
		main_search.focus(function(){
			
			if ($(this).val() == main_search_default){

				$(this).val('');
			}
		});

		main_search.blur(function(){
			
			if ($(this).val() == ''){

				$(this).val(main_search_default);
			}
		});
		
		if ($('#profile-search').length > 0){

			$('#profile-search').autocomplete('/profiles/find_by_keyword', { minChars:2, onItemSelect:ac_select, cacheLength:10 });
		}

		function ac_select(o){
			
			if (o.extra) {
				
				$('#profile-search').val(o.extra[0]);
				$('#profile-search').focus();
			}
		}

	// handles tab behaviors

		$('div.tab').click(function(){
			
			ref = $(this).attr('id');

			$(this).parent().find('div.current').removeClass('current');
			$(this).addClass('current');
			
			$(this).parent().next().find('div:not(.feed-post, .pages)').hide();
			$(this).parent().next().find('div#' + ref + '-content').show();
			
			$('div.success, div.error').hide();

			track_page_view(null);

			return false;
		});

		$('a.main-tab').click(function(){
			
			ref = $(this).attr('rel');
			
			$(this).blur();
			$(this).closest('div.box').find('ul.tabs li').removeClass('active');
			$(this).parent().addClass('active');
			
			$(this).closest('div.box').find('div.cont').not('div.cont div.cont').hide();
			$('#' + ref + '-cont').show();

			track_page_view($(this).attr('href'));

			return false;
		});


	// handles pagination, soft, non-ajax

		$('.hard-scroll-list').each(function(){

			$(this).data('n_sets', Math.ceil($(this).find('.block').length / n_hard_scroller_sets));

			if ($(this).data('n_sets') > 1){
				
				$(this).data('curr_set', 0);
				$(this).find('a.hard-scroller').click(function(){
					
					var parent		= $(this).closest('.hard-scroll-list');
					var curr_set	= parseInt(parent.data('curr_set')) + 1;
					
					parent.data('curr_set', curr_set);
					parent.find('.set-' + curr_set).show();

					track_page_view(null);

					if (curr_set == parent.data('n_sets')){

						$(this).remove();
					}

				});

			} else {
				
				$(this).find('a.hard-scroller').remove();
			}
		});

		$('div.page-set').each(function(){
			
			if ($(this).attr('id') == 'activity-cont'){

				n_p_sets = 4;
			
			} else {
				
				n_p_sets = n_soft_pagination_sets;
			}

			$(this).data('n_sets', Math.ceil($(this).find('.block').length / n_p_sets));

			if ($(this).data('n_sets') > 1){

				var controls		= $(this).find('div.nav');
				var controls_list	= controls.find('ul:last');
				var controls_n		= controls_list.find('li:first');

				for (var i = 0 ; i < $(this).data('n_sets') ; i++){

					controls_n.clone().appendTo(controls_list);
				}

				controls_n.remove();
				
				controls_list.find('li a').each(function(i){
					
					$(this).attr('rel', i);
					$(this).html(i + 1);
				});

				$(this).data('curr_page', 0);
				$(this).find('a[rel=0]').addClass('active');
				$(this).find('a[rel=0]').parent().addClass('active');
				
				$(this).find('a.prev, a.next, a.number').click(function(){
					
					if (!$(this).hasClass('disabled')){
					
						var curr_page = $(this).closest('div.page-set').data('curr_page');

						if ($(this).hasClass('next')){

							$(this).closest('.nav').find('a.prev').show();
							var new_page = curr_page + 1;
						
						} else if ($(this).hasClass('prev')) {
							
							$(this).closest('.nav').find('a.next').show();
							var new_page = curr_page - 1;
						
						} else {
							
							var new_page = parseInt($(this).attr('rel'));
						}

						if (new_page == $(this).closest('div.page-set').data('n_sets') - 1){
							
							$(this).closest('.nav').find('a.next').addClass('disabled');
						
						} else {
							
							$(this).closest('.nav').find('a.next').removeClass('disabled');
						}

						if (new_page == 0){
							
							$(this).closest('.nav').find('a.prev').addClass('disabled');
						
						} else {
							
							$(this).closest('.nav').find('a.prev').removeClass('disabled');
						}
						
						$(this).closest('.nav').find('a.number').removeClass('active');
						$(this).closest('.nav').find('a.number').parent().removeClass('active');
						$(this).closest('.nav').find('a[rel=' + new_page + ']').addClass('active');
						$(this).closest('.nav').find('a[rel=' + new_page + ']').parent().addClass('active');
						$(this).closest('div.page-set').data('curr_page', new_page);
						
						$(this).closest('div.page-set').find('.block').hide();
						$(this).closest('div.page-set').find('.set-' + new_page).show();

						track_page_view(curr_uri + "/js-page-" + new_page);
					}

					$(this).blur();
					return false;
				});
			
			} else {
				
				$(this).find('div.nav').remove();
			}
		});
	
	// handles filtering

		$('div.box select').not('.attrib-control select, .jumper, .simple').change(function(){
			
			filter_places(0);
		});


	// handles photo slideshows

		$('.slideshow-preview-cont a').click(function(){
			
			$(this).closest('.slideshow-preview-cont').hide();
			$('#slideshow-in-post').show();
			$('#slideshow-in-post .image-cont:first').show();
			track_event('Slideshows', 'Open', null, null);
			return false;
		});
		
		var curr_slideshow = 0;
		var n_in_slideshow = $('#slideshow-in-post .image-cont').length;

		if ($('.slideshow-preview-cont').length > 0 && $('.blog-head p').length < 4){
			
			$('.slideshow-preview-cont a').trigger('click');
		}

		$('#slideshow-in-post a.ss-nav').click(function(){
			
			if ($(this).is('.next')){

				curr_slideshow = curr_slideshow < (n_in_slideshow - 1) ? curr_slideshow + 1 : 0;
				$(this).prev().show();
			
			} else {
				
				curr_slideshow = curr_slideshow > 0 ? curr_slideshow - 1 : (n_in_slideshow - 1);
			}

			$('#slideshow-in-post .image-cont').hide();
			$('#slideshow-in-post .image-cont').eq(curr_slideshow).show();
			$('#slideshow-in-post .nav span').html((curr_slideshow + 1) + ' of ' + n_in_slideshow);
			fit_slideshow_images();
			track_event('Slideshows', 'Browse', null, null);
			$(this).blur();
			return false;
		});
		
		$(window).load(function(){

			fit_slideshow_images();
		});


	// handles comment voting // also works for questionnaire entry voting

		init_voting();

		$('a.comment-opener, a.comment-closer').click(function(){
			
			var parent = $(this).closest('.comment-cont');
			parent.find('.comment-body, .timestamp, .threshold, .comment-closer').toggle();

			return false;
		});

	// Tags explorer

		$('#tags-cont .head a').click(function(){
			
			var parent = $(this).closest('#tags-cont');
			parent.find('.head a').removeClass('active');
			$(this).addClass('active');
			parent.find('.display').hide();
			parent.find('div.' + $(this).attr('rel')).show();
			track_event('Tags', 'Browse', null, null);
			$(this).blur();
			return false;
		});


	// Blogs / news posts
	// Grid vs. stacked view

		$('#grid-stacked div').click(function(){
			
			$(this).parent().find('div').removeClass('on');
			$(this).addClass('on');

			if ($(this).is('#grid')){

				$('ul#blogs-posts li').removeClass('stacked');

			} else { // stacked
				
				$('ul#blogs-posts li').addClass('stacked');
			}

			track_page_view(null);
		});


	// Outgoing ad tracking

		$('div.ad a').click(function(){
			
			track_event('Outbound Ad', 'Click', $(this).attr('href'), null, null);
		});


	// events calendar

		$('.event-cal:first').find('th a.left').hide();
		$('.event-cal:last').find('th a.right').hide();

		$('.event-cal th a').click(function(){
			
			var parent = $(this).closest('.event-cal');
			
			if ($(this).is('.left')){
				
				parent.prev().show();

			} else {
				
				parent.next().show();
			}

			track_event('Events', 'Browse Calendar Month', null, null);

			parent.hide();
			$(this).blur();
			return false; 
		});

		$('.event-cal td.cell').click(function(){
			
			window.location = $(this).find('a:first').attr('href');
		});

		$('.event-cal td a.current').closest('td').addClass('current');
		$('.event-cal td div.today').closest('td').addClass('today');

		if ($('.event-cal td a.current').length > 0){

			$('.event-cal').hide();
			$('.event-cal td a.current').closest('.event-cal').show();
		}

	// misc utils

		init_inline_tools();

		$('a.debug').click(function(){
			
			$('#codeigniter_profiler').toggle();
			return false;
		});

		$('select.jumper').change(function(){
			
			window.location = $(this).val();
		});

		$('p.more a').click(function(){
			
			$(this).closest(".more-set").find("p").show(); 
			$(this).remove(); 
			return false;
		});


	// clean up

		$('#place-detail-info a, .right-profile a').not('h3 a').not('#image-browse a').each(function(){
			
			$(this).html(str_replace('http://', '', $(this).html()));
			$(this).html(str_replace('www.', '', $(this).html()));
			$(this).html($(this).html().length > 32 ? $(this).html().substr(0, 32) + '...' : $(this).html());
			
			if ($(this).attr('href').indexOf('http://') < 0 && $(this).attr('href').substr(0, 1) != '/'){
				
				$(this).attr('href', 'http://' + $(this).attr('href'));
			}		 
		});
}); 

	// Utility functions

		function fit_slideshow_images(){
			
			$('#slideshow-in-post img').each(function(){
				
				if ($(this).width() > 470){
					
					$(this).css('width', '470px');
				}

			});
		}

		function init_voting(){
			
			$('span.comment-rater a').click(function(){
			
				$('div.success, div.warning, div.error').remove();

				$.post($(this).attr('href'), false, function(data){
					
					var message_class	= data.status == '1' ? 'success' : 'warning';
					var parent			= $('#comment-' + data.comment_id);

					parent.prepend('<div class="' + message_class + '">' + data.message + '</div>');
					parent.find('span.comment-rater a').remove();

					var rating_value = parent.find('span.rating-value');
					rating_value.removeClass('pos');
					rating_value.removeClass('neg');
					rating_value.addClass(data.rating_class);
					rating_value.html(data.new_rating);

				}, 'json');
				
				track_event('Comment', 'Vote', null, null);

				$(this).blur();
				return false;
			});
		}

		function init_inline_tools(){
			
			$('input.auto-selectable').click(function(){
				
				$(this).select();
			});

			$('a.to-comments').click(function(){
				
				$.scrollTo( '#comments-cont', 500, {easing:'easeInOutQuad'} );
				$(this).blur();
				return false;
			});
			
			if ($('#post-body').not('.clean').length > 0){

				$('#post-body').wysiwyg();
				$('#post-body').closest('form').submit(function(){
					
					//$(this).find('.wysiwyg a.html').closest('li').trigger('mousedown'); return false;
				});
			}
		}

		function track_page_view(page){
			
			if (env == 'prod'){

				pageTracker._trackPageview(page);
			}
		}

		function track_event(a, b, c, d){
			
			if (env == 'prod'){

				pageTracker._trackEvent(a, b, c, d);
			}
		}


		function str_replace (search, replace, subject, count) {

			// discuss at: http://phpjs.org/functions/str_replace

			var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
					f = [].concat(search),
					r = [].concat(replace),
					s = subject,
					ra = r instanceof Array, sa = s instanceof Array;
			s = [].concat(s);
			if (count) {
				this.window[count] = 0;
			}

			for (i=0, sl=s.length; i < sl; i++) {
				if (s[i] === '') {
					continue;
				}
				for (j=0, fl=f.length; j < fl; j++) {
					temp = s[i]+'';
					repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
					s[i] = (temp).split(f[j]).join(repl);
					if (count && s[i] !== temp) {
						this.window[count] += (temp.length-s[i].length)/f[j].length;}
				}
			}
			return sa ? s : s[0];
		}