﻿var LF = LF || {};

LF.global = LF.global || {};

LF.global.didYouKnowSetup = function () {
	if ($('#did-you-know-widget').length > 0) {
		var intActive = Math.floor(Math.random()*$('#did-you-know-widget ul li').length);

		$('#did-you-know-widget ul li').removeClass('active');
		$('#did-you-know-widget ul li:eq(' + intActive + ')').addClass('active');

		$('#did-you-know-widget ul').swapElement({
			'viewTimer': 10000,
			'fadeSpeed': 1000
		});
	}
};

LF.global.init = function () {
	LF.global.setupUi();
	LF.global.didYouKnowSetup();
};

LF.global.setupUi = function () {	
	$('.popup_close a').click(function() {
		LF.contact.resetContactForm();
		$('#email_us_span select').val('');
		$('#email_us_span span').text('Choose an option');
		LF.util.removePopupOverlay();

		return false;
	});
	
	$('#contact_popup').click(function() {
		LF.util.openThenAdjustPopup($('#contact_modal'));
		return false;
	});
	
	$('#main_nav_history, #nav_history').click(function() {
		LF.util.openThenAdjustPopup($('#timeline_modal'));
		return false;
	});
	
	$('#main_nav li').hover(function() {
		$(this).children('ul').hide().fadeIn('medium');
	}, function() {
		$(this).children('ul').fadeOut('fast');
	});
	
	// Setup select wrapper
	$('span.select_wrapper select').each(function() {
		$(this).siblings('span').html($(this).children('option:selected').text());
	});
	
	$('span.select_wrapper select').change(function() {
		$(this).siblings('span').html($(this).children('option:selected').text());
	});
	
	// Clear radio buttons then add click event to handle styled radio buttons
	$('span.radio_option input').attr('checked', false);
	$('span.radio_option').click(function() {
		$(this).closest('fieldset').find('span.radio_option').removeClass('checked');
		$(this).closest('fieldset').find('span.radio_option input').attr('checked', false);
		if ($(this).children('input').is(':checked')) {
			$(this).children('input').attr('checked', false);
			$(this).removeClass('checked');
		} else {
			$(this).children('input').attr('checked', true);
			$(this).addClass('checked');		
		}
	});
	
	// handle labels that sit over their inputs
	$('input.over').val('');
	$('textarea.t-over').val('');
	$('input.over,textarea.t-over').focus(function() {
		$(this).prev('label').fadeOut('fast');
	});
	$('label.over').click(function() {
		$(this).fadeOut('fast');
		$(this).next('input.over').focus();
	});
	$('label.t-over').click(function() {
		$(this).fadeOut('fast');
		$(this).next('textarea.t-over').focus();
	});
	$('input.over,textarea.t-over').blur(function() {
		if ($(this).val() == '') {
			$(this).prev('label').fadeIn('fast');
		}
	});
	
};

// global handler for document ready functions
LF.global.domReadyLoad = function (f) {
	this.queue = this.queue || [];

	// add functions to the queue
	if (arguments.length > 0) {
		var loaded = this.queue.length;
		for (var i = 0; i < arguments.length; i++) {
			if (typeof arguments[i] == "function") {
				this.queue[i + loaded] = arguments[i];
			} else {
				throw ('Global Page Load - Argument must be a function: ' + arguments[i]);
			}
		}

	// run all functions in the queue
	} else if (this.queue.length > 0) {
		for (var i = 0; i < this.queue.length; i++) {
			this.queue[i]();
		}
	} else {
		return false;
	}
};

LF.global.domReadyLoad(LF.global.init);

