//jQuery on-load function
$(function() {

	$('.main-content').css('min-height', $('.sidebar').height());
	$('[placeholder]').defaultValue();

	$('.slider').bxSlider({
		pager: false,
		touchEnabled: false,
		auto: true,
		pause: 7000,
		autoHover: true
	});

	$(window).resize(function() {resize_events();});

	resize_events();
	function resize_events() {
		var w = $(window).width();

		if (w <= 660) {
			$('header nav li ul').each(function() {$(this).parent().addClass('dropdown');});
		}
		else if (w > 660) {
			$('header .fr li').removeClass('open');
			$('header .fr ul').attr('style', '');
		}
	}

	$('.mobile-menu-button').click(function() {
		$(this).toggleClass('close');
		$('header .fr').toggle();
		$('header .fr li').removeClass('open');
		$('header .fr ul').attr('style', '');
	});

	$('header nav .dropdown > a').click(function(e) {
		var parent = $(this).parent();
		if (!parent.hasClass('open')) {
			e.preventDefault();
			$('header nav li').removeClass('open');
			parent.addClass('open');
			$('header nav li ul').slideUp();
			$(this).siblings('ul').slideDown();
		}
	});

	$('.gallery .thumbnail.entry').each(function() {
		var image = '<img src="'+$(this).attr('href')+'" alt="">';
		var title = '<p>'+$(this).children('h3').html()+'</p>';
		$('.gallery-lightbox .images').append('<div class="image">'+image+title+'</div>');
	});

	$('.gallery .thumbnail.entry').click(function(e) {
		e.preventDefault();
		var index = $(this).index()-1;
		changeImage(index);
	});

	$('.gallery-lightbox, .mask').click(function(e) {
		e.preventDefault();
		e.stopPropagation();
		if (!$(e.target).is('img')) {
		$('.gallery-lightbox, .mask').fadeOut(400, function() {
			$('.gallery-lightbox').attr('style', '');
		});
		}
	});

	$('.gallery-lightbox .controls div').click(function(e) {
		e.stopPropagation();
		var index = $('.gallery-lightbox .image:visible').index() + 1;
		index = ($(this).hasClass('previous')) ? index - 1 : index + 1;
		if (index <= 0) {
			index = $('.gallery-lightbox .image').length;
		}
		if (index > ($('.gallery-lightbox .image').length)) {
			index = 1;
		}
		changeImage(index);
	});

	function changeImage(index) {
		var parent = $('.gallery-lightbox .image:nth-child('+index+')');
		setDimensions(parent);
		$('.gallery-lightbox .image').hide();
		parent.show();
		$('.gallery-lightbox').css({
			'display': 'none',
			'visibility': 'visible'
		});
		$('.gallery-lightbox, .mask').fadeIn();
	}

	function setDimensions(parent) {
		parent.show();
		var parent_width = parent.width();
		var parent_height = parent.height();
		var image = parent.children('img');
		var image_width = image.outerWidth();
		var image_height = image.outerHeight();
		var offset = (parent_height - image_height)/2;

		image.css('margin-top', offset);
		parent.children('p').css('margin-top', offset + 15);
		$('.gallery-lightbox .close').css({
			'top': offset + 15,
			'margin-left': (image_width/2) - 59
		});
		$('.gallery-lightbox .controls').css('top', offset + image_height - 42);
		$('.gallery-lightbox .previous').css('margin-left', (image_width/-2) + 7.5);
		$('.gallery-lightbox .next').css('margin-left', (image_width/2) - 135);
	}
});