$(document).ready(function() {
	$('.tblank').attr('target','_blank');
	modalActivatorClick();
});


// Simple Accordion
function simpleAccordion (activator,firstOpen,limitOne) {
	$(activator).click(function() {
		$(this).toggleClass('closed').next().toggle(500);
		// if this is true, only one item will be permitted to be open at a time
		if (limitOne === true) {
			$(activator).not($(this)).addClass('closed').next().hide(500);
		}
		return false;
	});
	// if firstOpen is true, the first item will be open on load
	if (firstOpen === true) {
		$(activator).filter(':not(:first)').addClass('closed').next().hide();
	// if firstOpen is an integer, then it will select an item by index (1-based) to be open on load
	} else if (firstOpen === parseInt(firstOpen)) {
		$(activator+':not(:eq('+(firstOpen-1)+'))').addClass('closed').next().hide();
	// if there is a hash (#) in the url, tests to see if it points to one of the activators, and opens that activator if it is
	// then tests to see whether the url hash points to an ID inside one of the accordion content areas, and opens that one if it does
	} else if (window.location.hash) {
		var activatorHasId = 0;
		$(activator).each(function(){
			if (('#'+$(this).attr('id')) === window.location.hash){
				$(activator).not($(window.location.hash)).addClass('closed').next().hide();
				activatorHasId = 1;
				return;
			}
		});
		if (activatorHasId != 1){
			$(activator).not($(window.location.hash).parents().prev(activator)).addClass('closed').next().hide();
		}
	// otherwise firstOpen will be accepted as a css selector for the item (or items) to be open on load, and where none are present, all items will be closed on load
	} else {
		$(activator+':not('+firstOpen+')').addClass('closed').next().hide();
	}
}

// Expiration Date
function expirationDate (el,year,month,day) {
	var expirationDate = new Date();
	expirationDate.setFullYear(year,(month-1),day);
	var currentDate = new Date();
	if(expirationDate<currentDate){
		$(el).hide();
	}
}

function modalActivatorClick() {
	$('.modal-activator').click(function(e){
		e.preventDefault();
		modalWindow($(this).attr('href'));
	});
}
var removedVideo;
function modalWindow(modalID,source) {
	if ($(modalID).hasClass('modal')) {
		if ($('.modal-opaque').length < 1) {
			$('.modals').append('<div class="modal-opaque"></div>');
		}
		if (removedVideo) {
			$(modalID +' .modal-wrapper').append(removedVideo);
		}
		$('.modal-opaque').fadeIn(source=='url'?1:400).click(function(e) {
			modalClose(e,modalID);
		});
		if ($(modalID).outerHeight() > $(window).height()) {
			$(modalID).css({
				'position':'absolute',
				'top':'30px',
				'margin-top':'0',
				'margin-left':(($(modalID).outerWidth()/2)*-1)
			});
		} else {
			$(modalID).css({
				'margin-top':(($(modalID).outerHeight()/2)*-1),
				'margin-left':(($(modalID).outerWidth()/2)*-1)
			});
		}
		$(modalID).fadeIn(source=='url'?1:400,function(){
			if (source=='url') {
				$('html,body').scrollTop(0);
			}
		});
		if ($(modalID).find('.close').length < 1) {
			$(modalID).append('<a class="close">Close</a>');
		}
		$(modalID+' .close').click(function(e){
			modalClose(e,modalID);
		});
	}
}

function modalClose(e,modalID){
	e.preventDefault();
	$('.modal-opaque').fadeOut();
	$(modalID).fadeOut(function(){
		$(modalID).css({
			'position':'fixed',
			'top':'',
			'margin-top':'',
			'margin-left':''
		});
	});
	if ($(modalID).hasClass('video')) {
		removedVideo = $('.video .modal-wrapper object').remove();
	}
}
