(function($){
	var carousel_items = 0;
	// Carousel
	$(document).ready (function(){
		$('#header .navigation > ul > li:last').addClass('last');
		$('#header .navigation > ul > li').hover(function(){
			if ( $(this).hasClass('current-menu-item') || $(this).hasClass('current_page_ancestor') || $(this).hasClass('current_menu_parent') ) {
				return;
			};
			if ( $(this).find('ul.sub-menu').length != 0 ) {
				$(this).toggleClass('current-menu-item-hover nmargin');
			};
		}, function(){
			if ( $(this).hasClass('current-menu-item') || $(this).hasClass('current_page_ancestor') || $(this).hasClass('current_menu_parent') ) {
				return;
			};
			if ( $(this).find('ul.sub-menu').length != 0 ) {
				$(this).toggleClass('current-menu-item-hover nmargin');
			};
		})

		$('.navigation a.link-disabled').click(function(){
			return false;
		})

		var men_count = $('.footer .navigation > ul > li').length;
		$('.footer .navigation > ul > li:eq(' + ( men_count - 1 ) + '), .footer .navigation > ul > li:eq(' + ( men_count - 2 ) + ')').addClass('last');

		$('.field, textarea').focus(function() {
			if(this.title==this.value) {
				this.value = '';
			}
		}).blur(function(){
			if(this.value=='') {
				this.value = this.title;
			}
		});

		// General Info page 

		var sl_timeout = 0;
		$('.general-info .title .shell p').click(function() {
			var th = $(this);
			if(th.parents('.title:eq(0)').next().is(':visible')) {  
				th.parents('.title:eq(0)').next().slideUp();
				th.removeClass('active');
			} else {
				sl_timeout = ( $('.hidden:visible').length )? 410 : 10;
				$('.hidden').slideUp(400);
				setTimeout(function(){
					$('.general-info .title .shell p').removeClass('active');
					th.addClass('active');
					th.parents('.title:eq(0)').next().slideDown('fast', function(){
						$.scrollTo(th, {
							axis: 'y',
							duration: 550,
							offset: -10
						});
					});
				}, sl_timeout);
			}
		});

		if ( $(".slider-carousel ul:eq(0) > li").length > 1 ) {
			carousel_items = $(".slider-carousel ul:eq(0)").find('> li').length;
			$(".slider-carousel ul:eq(0)").jcarousel({
				scroll:1,
				auto: 6,
				wrap:"circular",
				itemFirstInCallback: mycarousel_itemFirstInCallback,
				initCallback: mycarousel_initCallback,
				start: 1
			});
		};

		$('#main #sidebar ul li form p').each(function() {
			if ($(this).find('label')) {
				$(this).find('label').after('<span class="cl">&nbsp;</span>');
			};
		});

		// Slider Arrows
		var browserWidth = $(window).width();
		window_dimensions_changed(browserWidth);
		if ( $('.hero .slider-nav').length != 0 ) {
			$('.hero .slider-nav').remove().appendTo($('.header-shell #header'));
		};

		$(window).resize(function() {
			browserWidth = $(window).width();
			window_dimensions_changed(browserWidth);
		});

		$('.load-more a').live('click', function(){
			var more = $(this).parent();
			var url = $(this).attr('href');
			url += '&is_events_publ_ajax=1';
			$.get(url, function(data){
				if ( typeof(data) != 'undefined' && data != '' ) {
					more.remove();
					$('.ep-container').append(data);
				};
			}, 'html');
			return false;
		});

		// People filtering
		var filters = $('.filters input[type="checkbox"]');
		var entries = $('.content .entry, .blog-content .post');
		
		filters.change(function() {
			var filteredEntries = $('.content .entry, .blog-content .post');
			var author_class = get_author_class();

			$('.filters').each(function() {
				var listFilters = $(this).find('input:checked');
				
				if (listFilters.length) {
					var listEntries = [];
					
					listFilters.each(function() {
						$.merge(listEntries, $('.' + $(this).attr('data-taxonomy') + '-' + $(this).attr('data-term')));
					});

					filteredEntries = $.grep(filteredEntries, function(n, i) {
						if ( author_class != '' ) {
							return ( $.inArray(n, listEntries) > -1 && $(n).hasClass(author_class))? true : false;
						} else {
							return $.inArray(n, listEntries) > -1;
						};
					});
				}
			});

			entries.hide();
			$(filteredEntries).show();
			maybe_show_noposts_msg();
		});

		$('#entries-search-form').submit(function(){return false;});

		$('#entries-search').keyup(function() {
			var filters = $('.filters input[type="checkbox"]').removeAttr('checked');
			var author_class = get_author_class();

			var query = $(this).val().toLowerCase();
			var events = $('.content .entry, .blog-content .post').each(function() {
				if ( author_class != '' ) {
					if ($(this).find('.text,.post-entry').text().toLowerCase().indexOf(query) > -1 && $(this).hasClass(author_class)) {
						$(this).show();
					} else {
						$(this).hide();
					}
				} else {
					if ($(this).find('.text,.post-entry').text().toLowerCase().indexOf(query) > -1) {
						$(this).show();
					} else {
						$(this).hide();
					}
				};
			});
			maybe_show_noposts_msg();
		});

		$('#author_select').change(function(){
			$('#entries-search').trigger('keyup');
		})
		
	});

	function window_dimensions_changed(browserWidth) {
		var diff = 1420 - browserWidth;
		if (browserWidth >= 1420) {
			$('.hero').css('position','static');
			$('.nav-holder').addClass('outer');
			$('.slider-hero-holder .jcarousel-clip').css('margin-left', '0');
			// $('.hero .slider-nav').css('right', '176px');
		} else {
			if ( browserWidth > 1200 ) {
				// $('.hero .slider-nav').css('right', ((diff / 2) + 176) + 'px');
				$('.slider-hero-holder .jcarousel-clip').css('margin-left', ((diff / 2) * -1) + 'px');
				// this is for the slider navigation
			} else {
				// $('.hero .slider-nav').css('right', (parseInt(diff / 1.65) + 176) + 'px');
				// console.log(diff);
				// $('.hero .slider-nav').css('right', ((diff / 1.9) + 136) + 'px');
			};
			$('.hero').css('position','relative');
			$('.nav-holder').removeClass('outer');
		};
		if ( browserWidth <= 1200 ) {
			$('#header').parent('.shell').addClass('fluid-shell');
			$('.slider-hero-holder .jcarousel-clip').addClass('narrow');
		} else {
			$('#header').parent('.shell').removeClass('fluid-shell');
			$('.slider-hero-holder .jcarousel-clip').removeClass('narrow');
		};
	}

	function get_author_class () {
		var author_class;
		if ( $('select#author_select').length != 0 ) {
			author_class = $('select#author_select').val();
			author_class = ( author_class != -1 )? 'author-' + author_class : '';
		} else {
			author_class = '';
		};

		return author_class
	}

	function maybe_show_noposts_msg() {
		if ( $('.content .entry:visible:not(.entry-noposts), .blog-content .post:visible').length == 0 && $('.entry-noposts').length != 0 ) {
			$('.entry-noposts').show();
		} else if ( $('.content .entry:visible:not(.entry-noposts), .blog-content .post:visible').length != 0 && $('.entry-noposts').length != 0 ) {
			$('.entry-noposts').hide();
		};
	}

	function mycarousel_initCallback(carousel) {
		$('.slider-nav a').live('click', function() {
			carousel.scroll($.jcarousel.intval($(this).text()));
			return false;
		});  
		
		 $('a.j-next').bind('click', function() {
			carousel.next();
			return false;
		});

		$('a.j-prev').bind('click', function() {
			carousel.prev();
			return false;
		});       
	};
		
	function mycarousel_itemFirstInCallback(carousel, item, idx, state) {
		idx = ( idx > carousel_items )? idx % carousel_items : idx;
		$('.slider-nav a').removeClass('active');
		$('.slider-nav a').eq(idx-1).addClass('active');
	};
})(jQuery)
