if(!window['skoodat']) {
	window['skoodat'] = {};
	var skoodat = window['skoodat'];
}

(function() {

	$(document).ready(function() {
		skoodat.menu();
		skoodat.subnav();
		skoodat.ticker();
		skoodat.tickersecondary();
		skoodat.pdfs();
		skoodat.hero();
		skoodat.forms();
		$('input:text,textarea').each(function() { var elem = $(this); var defaultText = elem.val(); elem.focus(function(){ if(elem.val() == defaultText) { elem.val(''); } }); elem.blur(function(){ if(elem.val() == '') { elem.val(defaultText); } }); })
		if($.browser.msie && $.browser.version < 7) {
			$('input').each(function() {
				$(this).addClass($(this).attr('type'));
			});
		}
		$('li.tab').hover(function() {
			$(this).addClass('tab-hover');
		}, function() {
			$(this).removeClass('tab-hover');
		});
		$('#nav li').hover(function() {
			$(this).addClass('hover');
		}, function() {
			$(this).removeClass('hover');
		});
	    $('a[href$=swf]').each(function() {
	    	var link = $(this);
			link.addClass('swf');
			link.media({'width': link.attr('width'), 'height': link.attr('height'), 'bgColor': '#ddd'});
	    });
	});
	
	$(window).load(function() {
		if($.browser.msie) {
             $('.box-contact').corners("8px")
        }

	});
	
	skoodat.menu = function() {
		var links = $('#nav li');
		links.each(function() {
			var link = $(this);
			link.hover(function() {
				link.addClass('sfhover');
			}, function() {
				link.removeClass('sfhover');
			});
		});
	}
	
	skoodat.subnav = function() {
		$('ul.menu ul').hide();
		$('ul.menu').each(function(){
			$('#' + this.id + '.expandfirst ul:first').show();
		});
		$('ul.menu > li > a').each(function() {
			var link = $(this);
			link.bind(
				link.parents('.accordion-nav').size() ? 'mouseover' : 'click',
				function(c) {
					if($(this).parents('ul.menu').find('ul:animated').size()) { return; }
					try{ c.preventDefault(); link.blur(); } catch(err) { }
					if(!link.siblings('ul').hasClass('open')) {
						link.parents('ul.menu').find('ul.open').removeClass('open').slideUp().siblings('a.open').removeClass('open');
						link.addClass('open');
						if($.browser.msie) {
							link.siblings('ul').addClass('open').show('normal');
						} else {
							link.siblings('ul').addClass('open').slideDown('normal');
						}
					}
				}
			);
		});
		
		$('a.accordionhead').click(function(c) { c.preventDefault(); });
	}
	
	skoodat.ticker = function() {
		var duration = 4 /* how long, in seconds, each item should be displayed */
		var fadetime = 1 /* how long, in seconds, the transition should last */

		var list = $('ul.newsticker');
		var items = list.find('li');
		if(!list.size() || items.size() < 2) { return; }
		
		var tallest = 0;
		items.each(function() {
			tallest = tallest < $(this).outerHeight() ? $(this).outerHeight() : tallest;
		});
		
		list.css({'position':'relative'}).height(tallest);
		
		items.css({'display':'none','position':'absolute','top': 0, 'left': 0});
		items.slice(0,1).show();
		
		setInterval(function(){
			var current = items.filter(':visible');
			var next = current.next().size() ? current.next() : items.slice(0,1);
			
			if(current.size() && next.size()) {
				current.fadeOut(fadetime * 1000);
				next.fadeIn(fadetime * 1000);
			}
		},(duration + fadetime) * 1000);
	}
	
	skoodat.tickersecondary = function() {
		var duration = 3 /* how long, in seconds, each item should be displayed */
		var fadetime = 1 /* how long, in seconds, the transition should last */

		var list = $('ul.newstickersecondary');
		var items = list.find('li');
		if(!list.size() || items.size() < 2) { return; }
		
		var tallest = 0;
		items.each(function() {
			tallest = tallest < $(this).outerHeight() ? $(this).outerHeight() : tallest;
		});
		
		list.css({'position':'relative'}).height(tallest);
		
		items.css({'display':'none','position':'absolute','top': 0, 'left': 0});
		items.slice(0,1).show();
		
		setInterval(function(){
			var current = items.filter(':visible');
			var next = current.next().size() ? current.next() : items.slice(0,1);
			
			if(current.size() && next.size()) {
				current.fadeOut(fadetime * 1000);
				next.fadeIn(fadetime * 1000);
			}
		},(duration + fadetime) * 1000);
	}
	
	skoodat.pdfs = function() {
		var links = $('a[href$=pdf]');

		if(!links.size() || $.cookie('skoodat-pdfs') != null) { return; }
		
		skoodat.overlay = $('#overlay').size() ? $('#overlay') : $(jQuery('<div id="overlay" style="display: none;"></div>')).appendTo($('body'));
		skoodat.popup = $('#lightbox').size() ? $('#lightbox') : $(jQuery('<div id="popup" style="display: none;"></div>')).appendTo($('body'));
		skoodat.popup.close = $(jQuery('<a href="#close" class="close" title="close">× Close</a>')).appendTo(skoodat.popup).click(function(c){ c.preventDefault(); if(!$.browser.msie) { skoodat.overlay.fadeOut(); } skoodat.popup.fadeOut('normal', function() { if($.browser.msie) { skoodat.overlay.hide(); } }) });
		skoodat.pdfs.link = window.location;
		
		skoodat.pdfs.registerForm = $(jQuery('<form action="https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" target="_blank" method="post"></form>'));
		skoodat.pdfs.registerForm.appendTo(skoodat.popup)
		skoodat.pdfs.registerForm.append(
			$(jQuery('<h2>Required</h2>'))
		).append(
			$(jQuery('<p>Please provide the folowing information to download the requested document.</p>'))
		).append(
			$(jQuery('<input type="hidden" name="oid" value="00D80000000LnXw">'))
		).append(
			$(jQuery('<label for="first_name">First Name</label>'))
		).append(
			$(jQuery('<input type="text" name="first_name" class="text">'))
		).append(
			$(jQuery('<label for="last_name">Last Name</label>'))
		).append(
			$(jQuery('<input type="text" name="last_name" class="text">'))
		).append(
			$(jQuery('<label for="company">Company</label>'))
		).append(
			$(jQuery('<input type="text" class="text" name="company">'))
		).append(
			$(jQuery('<label for="email">Email</label>'))
		).append(
			$(jQuery('<input type="text" class="text" name="email">'))
		).append(
			$(jQuery('<input type="image" name="submit" src="/images/submit.png">'))
		).submit(function(s) {
			var requirementsMet = true;
			skoodat.pdfs.registerForm.find('input[type=text]').each(function() {
				if($(this).val() == '') {
					alert($(this).prev('label').text() + ' is a required field');
					requirementsMet = false;
				}
			});
			if(requirementsMet) {
				skoodat.popup.close.click();
				$.cookie('skoodat-pdf', 'registered', {'expires': 10000, 'path': '/'});
			}
			return requirementsMet;
		});

		links.each(function() {
			var link = $(this);
			link.click(function(c) {
				if($.cookie('skoodat-pdf') == null) { 
					c.preventDefault();
					skoodat.overlay.show();
					skoodat.popup.fadeIn();
					skoodat.pdfs.registerForm.append($(jQuery('<input type="hidden" name="retURL" value="http://' + document.domain + '/' + link.attr('href') + '">')));
					skoodat.pdfs.registerForm.append($(jQuery('<input type="hidden" name="lead_source" value="Web-' + link.attr('href') + '">')));
					skoodat.pdfs.registerForm.find('input[name=first_name]').focus();
					skoodat.pdfs.link = link.attr('href');
				}
			});
		});
	}
	
	skoodat.hero = function() {
		skoodat.hero.intervals = [];

		if(!$('.heroshot').size()) { return; }

		skoodat.hero.interval = null

		skoodat.hero.containers = $('.heroshot')

		skoodat.hero.containers.each(function(i) {
			var container = $(this);

			var fade = container.find('input[name=fadevalue]')
			fade = fade.val() * 1000;

			var dur = container.find('input[name=showvalue]')
			dur = dur.val() * 1000;
			
			container.find('input').remove();

			if(container.children('a,img').size() < 2) { // Less than two images, we don't need to xfade or add controls
				return
			}

			container.children('a:first-child,img:first-child').attr({current: 'current'})
			container.children('a:not(:first-child),img:not(:first-child)').hide()

			if(jQuery.browser.safari) {
				container.children('a:not(:first-child),img:not(:first-child)').css({display: 'none'})
			}

			skoodat.hero.intervals[i] = setInterval(function() {
				var images = container.children('a,img');
				var current = container.children('a[current],img[current]');
				if(current.next('a,img').size()) {
					var to = current.next('a,img');
				} else {
					var to = $(images[0]);
				}
				current.removeAttr('current').fadeOut(fade);
				to.attr({current: 'current'}).fadeIn(fade);
			}, dur + fade)
		});

	}
	
	skoodat.forms = function() {
		$('form[name=contact-form]').submit(function(s) {
			var requirementsMet = true;
			$('form[name=contact-form] input').filter(function(){ var name = $(this).attr('name'); return name == 'first_name' || name == 'last_name' || name == 'company' || name == 'email' }).each(function(index) {
				if($(this).val() == '') {
					alert($(this).parent('td').prev('th').find('label').text() + ' is a required field');
					requirementsMet = false;
				}
			});
			return requirementsMet;
		});
	}
	

})();
