(function() {
	if (navigator.appName != 'Microsoft Internet Explorer') {
		var metas = document.querySelectorAll('meta[name="viewport"]'),
		    forEach = [].forEach;
		function fixMetas(firstTime) {
			forEach.call(metas, function(el) {
				el.content = firstTime
					? 'width=device-width,minimum-scale=1.0,maximum-scale=1.0'
					: 'width=device-width,minimum-scale=0.25,maximum-scale=1.6';
			});
		}
		fixMetas(true);
		document.body.addEventListener('gesturestart', fixMetas, false);
	}
}());

$(function() {
	var ie = 1000;
	if ($.browser.msie) {
		ie = parseInt($.browser.version, 10);
	}
	
	$finder = $('#finder');
	
	if (navigator.userAgent.match(/iPad/i) != null) {
		$finder.hide();
		$('#footer').css('padding', '0');
	} else {
		$finder.find('h2').click(function() {
			if ($finder.attr('class') == 'show') {
				$finder.attr('class', 'hide');
				$finder.find('.content').animate({top: '-70'}, 700);
			} else {
				$finder.attr('class', 'show');
				$finder.find('.content').animate({top: '-155'}, 700);
			}
		});
		$('.ie7 #finder [type=button]').val('');
	}

	var width = $(window).width();
	if (width > 979 || (width < 980 && navigator.userAgent.match(/iPad/i) != null)) {
		var over = false;
		$('li.flyout').hover(
			function() {
				if (ie < 9) {
					$(this).find('ul').show();
				} else {
					$(this).find('ul').fadeIn(400);
				}
			},
			function() {
				if (ie < 9) {
					$(this).find('ul').hide();				
				} else {
					$(this).find('ul').fadeOut(200);
				}
			});
	}

	$(window)
		.resize(function() { fix(); })
		.scroll(function() { fix(); });

	$(window).scrollTop(1).scrollTop(0);
	
	fix();
	
	function fix() {
		var content_height = $('#container').height(),
			window_height = $(window).height(),
			offset = $(window).scrollTop(),
			$footer = $('#footer');

		if ((content_height - offset - 200) > window_height) {
			$footer.find('#finder').css({
				'position': 'fixed',
				'top': (window_height) + 'px'
				});
		} else {
			$footer.find('#finder').css({
				'position': 'absolute',
				'top': ''
				});
		}
		
		if (parseInt($(window).width()) < 1160) {
			$('#slides').removeClass('large');
		} else {
			$('#slides').addClass('large');
		}
	}
	
	if ($.browser.msie && parseInt($.browser.version, 10) < 10) {
		var $h2 = $('#header h2');
		html = $h2.html();
		
		$h2.html(
			'<span class="text">' + html + '</span>' +
			'<span class="lt">' + html + '</span>' +
			'<span class="rt">' + html + '</span>' +
			'<span class="lb">' + html + '</span>' +
			'<span class="rb">' + html + '</span>'
			);
	}
});
