var bms_basepath = 'http://www.bmsri.com/wp-content/themes/bms/';

var global_nav = {
	init: function() {
		$('ul#menu-bms-global-nav > li > ul').hide();
		$('ul#menu-bms-global-nav > li.current-menu-ancestor > ul').show();
		
		$('#menu-bms-global-nav > li > a').click(function() {
			if ($(this).siblings('ul').length < 1) {
				return true;
			}
			
			if ($('#menu-bms-global-nav ul').is(':animated') || $(this).next().is(':visible')) {
				return false;
			}
			
			if ($('#menu-bms-global-nav > li > ul:visible').length < 1) {
				$('#menu-bms-global-nav > li').removeClass('current-menu-ancestor');
				$('#menu-bms-global-nav > li').removeClass('current-menu-item');
			}

			$('#menu-bms-global-nav > li > ul:visible').slideUp(750, function() {
				$(this).parents('li').removeClass('current-menu-ancestor');
				$(this).parents('li').removeClass('current-menu-item');
			});

			$heading = $(this);
			$heading.next().slideDown(750).parent().addClass('current-menu-ancestor');
			
			return false;	
		});
	}
}

var dropdown_headers = {
	init: function() {
		var startTime = 500;
		
		var items = $('.bg_overlay').not('.overlay .bg_overlay');
		items.sort(function() { return 0.5 - Math.random(); });
		
		$.each(items, function() {
			var el = this;
			
			$(el).css('top', -(el.clientHeight + 14) + 'px');
			
			setTimeout(function() {
				$(el).animate({
					'top': '0px'
				}, 750);
				// }, 750, "easeOutBounce");
			}, startTime);
			
			startTime += 250;
		});
	}
}

var home_heros = {
	init: function() {
		if ($('body.home').length < 1) return;
		
		$('#hero_content1').show();
		$('#hero_content2').hide();
		$('#hero_content3').hide();
		
		var count = 2;
		var timer = setInterval(function() {
			switch(count) {
				case 1:
				home_heros.hero1();
				break;
				
				case 2:
				home_heros.hero2();
				break;
				
				case 3:
				home_heros.hero3();
				break;
			}
			
			if (count == 3) {
				count = 1;
			} else {
				count++;
			}
		}, 12000);
		
		$('#hero_link1').click(function() {
			clearInterval(timer);
			if ($('#hero *').is(':animated')) return;
			
			home_heros.hero1();
			
			return false;
		});
		
		$('#hero_link2').click(function() {
			clearInterval(timer);
			if ($('#hero *').is(':animated')) return;
			
			home_heros.hero2();
			
			return false;
		});
		
		$('#hero_link3').click(function() {
			clearInterval(timer);
			if ($('#hero *').is(':animated')) return;
			
			home_heros.hero3();
			
			return false;
		});
	},
	hero1: function() {
		$('.hero_link').css('background', '#61625e');
		$('.hero_link:eq(0)').css('background', '#1b1b1d');

		var el = $('#hero .bg_overlay');
		el.animate({
			'top': -(el[0].clientHeight + 8) + 'px'
		}, 750, function() {
			$('<img src="'+bms_basepath+'images/home_hero1.jpg" alt="Biomedical Structures:  Biomedical textiles composed of permanent and absorbable materials for medical devices in orthopedic, cardiovascular, general surgery, and tissue engineering applications" title="Biomedical Structures:  Biomedical textiles composed of permanent and absorbable materials for medical devices in orthopedic, cardiovascular, general surgery, and tissue engineering applications" />').appendTo('#hero').hide().fadeIn(750, function() {
				$('#hero_content1').show();
				$('#hero_content2').hide();
				$('#hero_content3').hide();

				var el = $('#hero .bg_overlay');
				el.animate({
					'top': '0px'
				}, 750);
			});
		});
	}, 
	hero2: function() {
		$('.hero_link').css('background', '#61625e');
		$('.hero_link:eq(1)').css('background', '#1b1b1d');

		var el = $('#hero .bg_overlay');
		el.animate({
			'top': -(el[0].clientHeight + 8) + 'px'
		}, 750, function() {
			$('<img src="'+bms_basepath+'images/home_hero2.jpg" alt="Biomedical Structures:  Design, development and manufacturing of biomedical textiles for medical devices" title="Biomedical Structures:  Design, development and manufacturing of biomedical textiles for medical devices" />').appendTo('#hero').hide().fadeIn(750, function() {
				$('#hero_content1').hide();
				$('#hero_content2').show();
				$('#hero_content3').hide();

				var el = $('#hero .bg_overlay');
				el.animate({
					'top': '0px'
				}, 750);
			});
		});
	},
	hero3: function() {
		$('.hero_link').css('background', '#61625e');
		$('.hero_link:eq(2)').css('background', '#1b1b1d');

		var el = $('#hero .bg_overlay');
		el.animate({
			'top': -(el[0].clientHeight + 8) + 'px'
		}, 750, function() {
			$('<img src="'+bms_basepath+'images/home_hero3.jpg" alt="Biomedical Structures:  Customized biomedical textiles composed of permanent and absorbable fibers for breakthrough medical devices and emerging clinical applications" title="Biomedical Structures:  Customized biomedical textiles composed of permanent and absorbable fibers for breakthrough medical devices and emerging clinical applications" />').appendTo('#hero').hide().fadeIn(750, function() {	
				$('#hero_content1').hide();
				$('#hero_content2').hide();
				$('#hero_content3').show();

				var el = $('#hero .bg_overlay');
				el.animate({
					'top': '0px'
				}, 750);
			});
		});
	}
}

var structure_overlays = {
	init: function() {
		if ($('body.structures').length < 1) return;
		
		$('body.structures .overlay').hide();
		
		// redirect based on url hash if needed
		switch(window.location.hash) {
			case '#knitted':
			$('#knitting_overlay').show();
			$('body').css('background', 'url('+bms_basepath+'images/backgrounds/green_bg.jpg) no-repeat');
			$('#menu-bms-global-nav > li:eq(1) li').removeClass('current-menu-item');
			$('#menu-bms-global-nav > li:eq(1) li:eq(1)').addClass('current-menu-item');
			break;
			
			case '#braided':
			$('#braiding_overlay').show();
			$('body').css('background', 'url('+bms_basepath+'images/backgrounds/green_bg2.jpg) no-repeat');
			$('#menu-bms-global-nav > li:eq(1) li').removeClass('current-menu-item');
			$('#menu-bms-global-nav > li:eq(1) li:eq(2)').addClass('current-menu-item');
			break;
			
			case '#woven':
			$('#woven_overlay').show();
			$('body').css('background', 'url('+bms_basepath+'images/backgrounds/red_bg.jpg) no-repeat');
			$('#menu-bms-global-nav > li:eq(1) li').removeClass('current-menu-item');
			$('#menu-bms-global-nav > li:eq(1) li:eq(3)').addClass('current-menu-item');
			break;
			
			case '#nonwoven':
			$('#nonwoven_overlay').show();
			$('body').css('background', 'url('+bms_basepath+'images/backgrounds/orange_bg.jpg) no-repeat');
			$('#menu-bms-global-nav > li:eq(1) li').removeClass('current-menu-item');
			$('#menu-bms-global-nav > li:eq(1) li:eq(4)').addClass('current-menu-item');
			break;
		}
		
		$('.structures_strip:eq(0), #menu-bms-global-nav > li:eq(1) li:eq(1)').click(function() {
			if ($('#knitting_overlay').is(':visible')) return false;
			
			if ($('.overlay').is(':visible')) {
				$('.overlay:visible').fadeOut(750, function() {
					$('#knitting_overlay').fadeIn(750);
				});
			} else {
				$('#knitting_overlay').fadeIn(750);
			}
			
			$('#menu-bms-global-nav > li:eq(1) li').removeClass('current-menu-item');
			$('#menu-bms-global-nav > li:eq(1) li:eq(1)').addClass('current-menu-item');
			
			$('body').css('background', 'url('+bms_basepath+'images/backgrounds/green_bg.jpg) no-repeat');
			
			return false;
		});
		
		$('.structures_strip:eq(1), #menu-bms-global-nav > li:eq(1) li:eq(2)').click(function() {
			if ($('#braiding_overlay').is(':visible')) return false;
			
			if ($('.overlay').is(':visible')) {
				$('.overlay:visible').fadeOut(750, function() {
					$('#braiding_overlay').fadeIn(750);
				});
			} else {
				$('#braiding_overlay').fadeIn(750);
			}
			
			$('#menu-bms-global-nav > li:eq(1) li').removeClass('current-menu-item');
			$('#menu-bms-global-nav > li:eq(1) li:eq(2)').addClass('current-menu-item');
			
			$('body').css('background', 'url('+bms_basepath+'images/backgrounds/green_bg2.jpg) no-repeat');
			
			return false;
		});
		
		$('.structures_strip:eq(2), #menu-bms-global-nav > li:eq(1) li:eq(3)').click(function() {
			if ($('#woven_overlay').is(':visible')) return false;
			
			if ($('.overlay').is(':visible')) {
				$('.overlay:visible').fadeOut(750, function() {
					$('#woven_overlay').fadeIn(750);
				});
			} else {
				$('#woven_overlay').fadeIn(750);
			}
			
			$('#menu-bms-global-nav > li:eq(1) li').removeClass('current-menu-item');
			$('#menu-bms-global-nav > li:eq(1) li:eq(3)').addClass('current-menu-item');
			
			$('body').css('background', 'url('+bms_basepath+'images/backgrounds/red_bg.jpg) no-repeat');
			
			return false;
		});
		
		$('.structures_strip:eq(3), #menu-bms-global-nav > li:eq(1) li:eq(4)').click(function() {
			if ($('#nonwoven_overlay').is(':visible')) return false;
			
			if ($('.overlay').is(':visible')) {
				$('.overlay:visible').fadeOut(750, function() {
					$('#nonwoven_overlay').fadeIn(750);
				});
			} else {
				$('#nonwoven_overlay').fadeIn(750);
			}
			
			$('#menu-bms-global-nav > li:eq(1) li').removeClass('current-menu-item');
			$('#menu-bms-global-nav > li:eq(1) li:eq(4)').addClass('current-menu-item');
			
			$('body').css('background', 'url('+bms_basepath+'images/backgrounds/orange_bg.jpg) no-repeat');
			
			return false;
		});
		
		structure_overlays.close_overlay();
	},
	close_overlay: function() {
		$('.close_overlay').click(function() {
			$('.overlay').fadeOut(750);
			
			$('#menu-bms-global-nav > li:eq(1) li').removeClass('current');
			$('#menu-bms-global-nav > li:eq(1) li:eq(0)').addClass('current');
			
			$('body').css('background', 'url('+bms_basepath+'images/backgrounds/blue_bg2.jpg) no-repeat');
			
			return false;
		});
	}
}

var application_overlays = {
	init: function() {
		if ($('body.applications').length < 1) return;
		
		$('body.applications .overlay').hide();
		
		var els = {
			cardiac: {
				item1: $('#cardiac'),
				item2: $('#cardiac_overlay')
			},
			surgery: {
				item1: $('#surgery'),
				item2: $('#surgery_overlay')
			},
			ortho: {
				item1: $('#ortho'),
				item2: $('#ortho_overlay')
			},
			bariatric: {
				item1: $('#bariatric'),
				item2: $('#bariatric_overlay')
			},
			tissue: {
				item1: $('#tissue'),
				item2: $('#tissue_overlay')
			},
			cosmetic: {
				item1: $('#cosmetic'),
				item2: $('#cosmetic_overlay')
			},
			vet: {
				item1: $('#vet'),
				item2: $('#vet_overlay')
			},
			emerging: {
				item1: $('#emerging'),
				item2: $('#emerging_overlay')
			}
		}
		
		$.each(els, function() {
			var el = this;

			$(el.item1).click(function() {
				if ($('.overlay').is(':visible')) {
					$('.overlay:visible').fadeOut(750, function() {
						$(el.item2).fadeIn(750);
					});
				} else {
					$(el.item2).fadeIn(750);
				}

				return false;
			});
		});
		
		application_overlays.close_overlay();
	},
	close_overlay: function() {
		$('.close_overlay').click(function() {
			$('.overlay').fadeOut(750);
			
			return false;
		});
	}
}

var brochure = {
	validate: function() {
		$('#brochure_form').submit(function() {
			var valid = true;
			
			if ($('#brochure_form input#firstname').val() == '') {
				$('#brochure_form input#firstname').css('border-color', 'red');
				valid = false;
			}
			
			if ($('#brochure_form input#lastname').val() == '') {
				$('#brochure_form input#lastname').css('border-color', 'red');
				valid = false;
			}
			
			if ($('#brochure_form input#email').val() == '') {
				$('#brochure_form input#email').css('border-color', 'red');
				valid = false;
			}
			
			return valid;
		});
		
		$('#brochure_form input:text').focus(function() {
			$(this).css('border-color', '#C9C9C9');
		})
	}
}

$(function() {
	global_nav.init();
	dropdown_headers.init();
	home_heros.init();
	structure_overlays.init();
	application_overlays.init();
	brochure.validate();
	
	// preload images
	var imgs = [bms_basepath+'images/backgrounds/green_bg.jpg', bms_basepath+'images/backgrounds/green_bg2.jpg', bms_basepath+'images/backgrounds/red_bg.jpg', bms_basepath+'images/backgrounds/orange_bg.jpg'];
	
	$.each(imgs, function() {
		var img = document.createElement('img');
		img.src = this;
	});
	
	$(document).keyup(function(e) {
        if (e.keyCode == 27) {
			if ($('.overlay').is(':visible')) {
				$('.overlay').fadeOut(750);
			}
        }
   		
        return false;
    });
});
