jQuery(function($) {
	"use strict";

	if($('.page_load').length){
		$(window).load(function(){
			$('.body').css({opacity: 1});
			$('.page_load').css({display: 'none'});
		});
	}

	//Mobile menu toggle
 	$('header .logo-menu nav .menu-icon i').on('click', function(){
 		$('header .logo-menu nav .menu').slideDown('slow');
 	});

	//Isotope
	$( window ).load(function() {
		if($('.grid-system').length){
			$('.grid-system').isotope({
				itemSelector: '.grid-item',
				layoutMode: 'packery'
			});
		}
	});
	
	//Pop up video
	$('.home-2-news .video-block .image .overlay').on('click', function(){
		$('.home-2-news .video-block .video-show').fadeIn('slow');
	});
	//Scroll
	if($(".scroll-pane").length){
		$('.scroll-pane').jScrollPane();
	}

	//Custom list category
	$('.library-category-list > li > a').on('click', function(e){
		$('.children').slideUp('slow');
		$('.library-category-list > li').removeClass('active');
		var parent = $(this).parent().addClass('active').find('.children');
		parent.slideDown('slow');
		e.preventDefault();
	});
	//Registration form
	if($(".registration-form, .tabs").length){
		$(".registration-form .inner, .tabs").tabs({
			collapsible: true
		});
	}

	if($(".exit").length){
		$(".exit").on('click', function(){
			$('.pop-up').fadeOut(300);
		});
	}
	if($(".registration").length){
		$(".registration span,.registration a.profile").on('click', function(e){
			$('.registration-form').fadeIn(300);
			e.preventDefault();
		});
	}

	//Select
	$('.dropdown select').easyDropDown();

	//Menu
	$('.menu-icon').toggle(function(){
		$('.menu').slideDown(300);
	}, function(){
		$('.menu').slideUp(300);
	});

	//Geolocation
	if($('#GeoAPI').length) {
		if (!navigator.geolocation) return;
		navigator.geolocation.getCurrentPosition(function (pos) {
			var geocoder = new google.maps.Geocoder();
			var latlng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
			geocoder.geocode({'latLng': latlng}, function (results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
					//Check result 0
					var result = results[0];
					//look for locality tag and administrative_area_level_1
					var city = "";
					var state = "";
					for (var i = 0, len = result.address_components.length; i < len; i++) {
						var ac = result.address_components[i];
						if (ac.types.indexOf("locality") >= 0) city = ac.long_name;
						if (ac.types.indexOf("administrative_area_level_1") >= 0) state = ac.long_name;
					}
					//only report if we got Good Stuff
					if (city != '' && state != '') {
						$('#GeoAPI').html(city);
					}
				}
			});
		});
	}

	//Slider
	if($('.home-3-people .slider').length){
		$('.home-3-people .slider').flexslider({
			animation: "slide",
			animationLoop: true,
			itemWidth: 371,
			maxItems: 5,
			minItems: 1,
			move: 1,
			pauseOnHover: true,
			controlNav: false
		});
	}
	$('.home-4-slider .carousel').flexslider({
		animation: "slide",
		controlNav: false,
		animationLoop: false,
		itemWidth: 210,
		itemMargin: 5,
		asNavFor: '.home-4-slider .slider'
	});

	$('.home-4-slider .slider').flexslider({
		animation: "slide",
		controlNav: false,
		smoothHeight: true,
		animationLoop: false,
		sync: ".home-4-slider .carousel"
	});


	$("a[rel=example_group]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});

	if($('.flexslider,.slider,.home-2-events,.home-5-slider .slider').length){
		$('.flexslider,.slider,.home-2-events,.home-5-slider .slider').flexslider({
			pauseOnHover: true,
			smoothHeight: true,
			slideshow:false,
			controlNav: false
		});
	}
	
	//Pop up
	if($('.college-box .our-college .video .image').length){
		$('.college-box .our-college .video .image').on('click', function(){
			$('.college-box .our-college .video .popup').fadeIn(300);
			$('.close').on('click', function(){
				$('.college-box .our-college .video .popup').fadeOut(300);
			});
		});
	}

	if($(".about-page section .video .item").length){
		$('.about-page section .video .item').on('click', function(){
			$('.popup').fadeIn(300);
			$('.close').on('click', function(){
				$('.popup').fadeOut(300);
			});
			return false;
		});
	}

	if($('.single .article .image span').length){
		$('.single .article .image span').on('click', function(){
			$('.popup-slider').fadeIn(300);
			$('.close').on('click', function(){
				$('.popup-slider').fadeOut(300);
			});
			return false;
		});
	}

	//Accordion
	if($('.accordion').length){
		$(document).ready(function(){
			$( ".accordion" ).accordion({
				collapsible: true
			});
		});
	}

	if($('.about-accordion').length){
		$( ".about-accordion" ).accordion({
			collapsible: true
		});
	}
});

Modernizr.addTest('ipad', function () {
	return !!navigator.userAgent.match(/iPad/i);
});

Modernizr.addTest('iphone', function () {
	return !!navigator.userAgent.match(/iPhone/i);
});

Modernizr.addTest('ipod', function () {
	return !!navigator.userAgent.match(/iPod/i);
});

Modernizr.addTest('appleios', function () {
	return (Modernizr.ipad || Modernizr.ipod || Modernizr.iphone);
});

Modernizr.addTest('positionfixed', function () {
	var test    = document.createElement('div'),
		control = test.cloneNode(false),
		fake = false,
		root = document.body || (function () {
			fake = true;
			return document.documentElement.appendChild(document.createElement('body'));
		}());

	var oldCssText = root.style.cssText;
	root.style.cssText = 'padding:0;margin:0';
	test.style.cssText = 'position:fixed;top:42px';
	root.appendChild(test);
	root.appendChild(control);

	var ret = test.offsetTop !== control.offsetTop;

	root.removeChild(test);
	root.removeChild(control);
	root.style.cssText = oldCssText;

	if (fake) {
		document.documentElement.removeChild(root);
	}

	/* Uh-oh. iOS would return a false positive here.
	 * If it's about to return true, we'll explicitly test for known iOS User Agent strings.
	 * "UA Sniffing is bad practice" you say. Agreeable, but sadly this feature has made it to
	 * Modernizr's list of undectables, so we're reduced to having to use this. */
	return ret && !Modernizr.appleios;
});


// Modernizr.load loading the right scripts only if you need them
Modernizr.load([
	{
		// Let's see if we need to load selectivizr
		test : Modernizr.borderradius,
		// Modernizr.load loads selectivizr and Respond.js for IE6-8
		nope : [ale.template_dir + '/js/libs/selectivizr.min.js', ale.template_dir + '/js/libs/respond.min.js']
	},{
		test: Modernizr.touch,
		yep:ale.template_dir + '/css/touch.css'
	}
]);



jQuery(function($) {
	var is_single = $('#post').length;
	var posts = $('article.post');
	var is_mobile = parseInt(ale.is_mobile);

	var slider_settings = {
		animation: "slide",
		slideshow: false,
		controlNav: false
	}

	$(document).ajaxComplete(function(){
		try{
			if (!posts.length) {
				return;
			}
			FB.XFBML.parse();
			gapi.plusone.go();
			twttr.widgets.load();
			pin_load();
		}catch(ex){}
	});

	// open external links in new window
	$("a[rel$=external]").each(function(){
		$(this).attr('target', '_blank');
	});

	$.fn.init_posts = function() {
		var init_post = function(data) {
			// close other posts
			data.post.siblings('.open-post').find('a.toggle').trigger('click', {
				hide:true
			});

			var loading = data.post.find('span.loading');

			if (data.more.is(':empty')) {
				data.post.addClass('post-loading');
				loading.css('visibility', 'visible');
				data.more.load(ale.ajax_load_url, {
					'action':'aletheme_load_post',
					'id':data.post.data('post-id')
				}, function(){
					loading.remove();
					data.more.slideDown(400, function(){
						data.post.addClass('open-post');
						data.toggler.text('Close Post');
						$('.video', data.more).fitVids();
						if (data.scroll) {
							data.scroll.scrollTo('fast');
						}
					});
					init_comments(data.post);
				});
			} else {
				data.more.slideDown(400, function(){
					data.post.addClass('open-post');
					data.toggler.text('Close Post');
					if (data.scroll) {
						data.scroll.scrollTo('fast');
					}
				});
			}
		}

		var load_post = function(e, _opts) {
			e.preventDefault();
			var data  = {
				toggler:$(this),
				scroll:false
			};
			var opts = $.extend({
				comments:false,
				hide:false,
				add_comment:false
			}, _opts);
			data.post = data.toggler.parents('article.post');
			data.more = data.post.find('.full');

			if (data.more.is(':visible')) {
				if (opts.hide == true) {
					// quick hide for multiple posts
					data.more.hide();
				} else {
					data.more.slideUp(400);
				}
				data.toggler.text('Open Post');
				data.post.removeClass('open-post');
			} else {
				if (typeof(e.originalEvent) != 'undefined' ) {
					data.scroll = data.post;
				}
				init_post(data);
			}
		}

		var init_comments = function(post) {
			var respond = $('section.respond', post);
			var respond_form = $('form', respond);
			var respond_form_error = $('p.error', respond_form);
			//var respond_cancel = $('.cancel-comment-reply a', respond);
			var comments = $('section.comments', post);

			/*$('a.comment-reply-link', post).on('click', function(e){
			 e.preventDefault();
			 var comment = $(this).parents('li.comment');
			 comment.find('>div').append(respond);
			 respond_cancel.show();
			 respond.find('input[name=comment_post_ID]').val(post.data('post-id'));
			 respond.find('input[name=comment_parent]').val(comment.data('comment-id'));
			 respond.find('input:first').focus();
			 }).attr('onclick', '');

			 respond_cancel.on('click', function(e){
			 e.preventDefault();
			 comments.after(respond);
			 respond.find('input[name=comment_post_ID]').val(post.data('post-id'));
			 respond.find('input[name=comment_parent]').val(0);
			 $(this).hide();
			 });
			 */
			respond_form.ajaxForm({
				'beforeSubmit':function(){
					respond_form_error.text('').hide();
				},
				'success':function(_data){
					var data = $.parseJSON(_data);
					if (data.error) {
						respond_form_error.html(data.msg).slideDown('fast');
						return;
					}
					var comment_parent_id = respond.find('input[name=comment_parent]').val();
					var _comment = $(data.html);
					var list;
					_comment.hide();

					if (comment_parent_id == 0) {
						list = comments.find('ol');
						if (!list.length) {
							list = $('<ol class="commentlist "></ol>');
							comments.find('.scrollbox .jspContainer .jspPane').append(list);
						}
					} else {
						list = $('#comment-' + comment_parent_id).parent().find('ul');
						if (!list.length) {
							list = $('<ul class="children"></ul>');
							$('#comment-' + comment_parent_id).parent().append(list);
						}
						respond_cancel.trigger('click');
					}
					list.append(_comment);
					_comment.fadeIn('fast');
					//.scrollTo();

					respond.find('textarea').clearFields();
				},
				'error':function(response){
					var error = response.responseText.match(/\<p\>(.*)<\/p\>/)[1];
					if (typeof(error) == 'undefined') {
						error = 'Something went wrong. Please reload the page and try again.';
					}
					respond_form_error.html(error).slideDown('fast');
				}
			});
		}
		$(this).each(function(){
			var post = $(this);
			// init post galleries
			$(window).load(function(){
				$('.preview .flexslider', post).flexslider(slider_settings);
			});
			// do not init ajax posts & comments for mobile
			if (!is_mobile) {
				// ajax posts enabled
				if (ale.ajax_posts) {
					$('a.toggle', post).click(load_post);
					$('.video', post).fitVids();
					$('.preview figure a', post).click(function(e){
						e.preventDefault();
						$(this).parents('article.post').find('a.toggle').trigger('click');
					});
				}
			}
		});
		// init ajax comments on a single post if ajax comments are enabled
		if (is_single && parseInt(ale.ajax_comments)) {
			init_comments(posts);
		}
		// open single post on page
		if (parseInt(ale.ajax_open_single) && !is_single && posts.length == 1) {
			posts.find('a.toggle').trigger('click');
		}
	}
	posts.init_posts();

	$.fn.init_gallery = function() {
		$(this).each(function(){
			var gallery = $(this);
			$(window).load(function(){
				$('.flexslider', gallery).flexslider(slider_settings);
			});

		})
	}
	$('#gallery').init_gallery();

	$.fn.init_archives = function()
	{
		$(this).each(function(){
			var archives = $(this);
			var year = $('#archives-active-year');
			var months = $('div.months div.year-months', archives);
			var arrows = $('a.up, a.down', archives);
			var activeMonth;
			var current, active;
			var animated = false;
			if (months.length == 1) {
				arrows.remove();
				activeMonth = months.filter(':first').addClass('year-active').show();
				year.text(activeMonth.attr('id').replace(/[^0-9]*/, ''));
				return;
			}
			arrows.click(function(e){
				e.preventDefault();
				if (animated) {
					return;
				}
				var fn = $(this);
				animated = true;
				arrows.css('visibility', 'visible');
				var current = months.filter('.year-active');
				if (fn.hasClass('up')) {
					active = current.prev();
					if (!active.length) {
						active = months.filter(':last');
					}
				} else {
					active = current.next();
					if (!active.length) {
						active = months.filter(':first');
					}
				}
				current.removeClass('year-active').fadeOut(150, function(){
					active.addClass('year-active').fadeIn(150, function(){
						animated = false;
					});
					year.text(active.attr('id').replace(/[^0-9]*/, ''));

					if (fn.hasClass('up')) {
						if (!active.prev().length) {
							arrows.filter('.up').css('visibility', 'hidden');
						}
					} else {
						if (!active.next().length) {
							arrows.filter('.down').css('visibility', 'hidden');
						}
					}
				});
			});
			activeMonth = months.filter(':first').addClass('year-active').show();
			year.text(activeMonth.attr('id').replace(/[^0-9]*/, ''));
			arrows.filter('.up').css('visibility', 'hidden');
		});
	}
	$('#archives .ale-archives').init_archives();






});

// HTML5 Fallbacks for older browsers
jQuery(function($) {
	// check placeholder browser support
	if (!Modernizr.input.placeholder) {
		// set placeholder values
		$(this).find('[placeholder]').each(function() {
			$(this).val( $(this).attr('placeholder') );
		});

		// focus and blur of placeholders
		$('[placeholder]').focus(function() {
			if ($(this).val() == $(this).attr('placeholder')) {
				$(this).val('');
				$(this).removeClass('placeholder');
			}
		}).blur(function() {
				if ($(this).val() == '' || $(this).val() == $(this).attr('placeholder')) {
					$(this).val($(this).attr('placeholder'));
					$(this).addClass('placeholder');
				}
			});

		// remove placeholders on submit
		$('[placeholder]').closest('form').submit(function() {
			$(this).find('[placeholder]').each(function() {
				if ($(this).val() == $(this).attr('placeholder')) {
					$(this).val('');
				}
			});
		});
	}
});

