var lcm = {

	platform:{
		ie6: $.browser.msie && $.browser.version == 6,
		iPad: navigator.userAgent.match(/iPad/i) != null,
		oldIE: $.browser.msie && $.browser.version < 9
	},
	
	canHandle:{
		backgroundSize: function(){
			return Modernizr.prefixed('backgroundSize') !== false;
		},
		touch: function(){
			var tester = document.createElement('div');
			tester.setAttribute('ongesturestart', 'return;');
			return typeof tester.ongesturestart == "function";
		}
	},
	
	init:function init_lcm(){
		var that = this;
		if( lcm.canHandle.touch() ){
			lcm.events = "touchstart";
			document.addEventListener('orientationchange',function(e){
				that.scrollTo(0);
			},null);
			that.scrollTo(1000);
		}else{
			lcm.events = "click";
		}

		if( lcm.canHandle.backgroundSize() ){
			$('html').addClass('backgroundsize');
		}else{
			var flashvars = { image:'/dispatch/img/bg.jpg', interestX:0, interestY:0, interestW: window.innerWidth, interestH: window.innerHeight },
				params = { menu: "false", scale: "noScale", allowFullscreen: "true", allowScriptAccess: "always", bgcolor: "#000000", wmode: "transparent" },
				attributes = { id:"background" };
			swfobject.embedSWF("/dispatch/swf/DispatchPage.swf", "background", "100%", "100%", "9.0.0", "/dispatch/swf/expressInstall.swf", flashvars, params, attributes);
		}
		
		if(lcm.platform.iPad){
			$('html').addClass('ipad');
		}
		
		if(lcm.platform.ie6){
			$.getScript("/dispatch/js/dd_belated.js", function(){
				DD_belatedPNG.fix('.logo img, .primary li button, .ecom');
			});
		}
		
		if(lcm.platform.oldIE){
			$('li:last-child').each(function(){
				$(this).addClass('last');
			});
		}
		
		$('.primary > li > button').bind(lcm.events, function(e){
			if( $(this).parent('li').hasClass('active') ){
				return false;
			}
			var timer, $this = $(this);
			$('.secondary > li.selected').removeClass('selected');
			$('.primary > .active').removeClass('active fadeIn');
			$this.parent('li').addClass('active');
			timer = setTimeout(function(){
				$this.parent('li').addClass('fadeIn');
				delete timer;
			}, 100);
		});
		
		$('.secondary > li > a').bind(lcm.events, function(e){
			if( $(this).attr('href') == "#" || $(this).siblings('.tertiary').length > 0 ){
				e.preventDefault();
			}
			if( $(this).parent('li').hasClass('selected') ){
				$(this).parent('li').removeClass('selected');
				return false;
			}
			$('.secondary > li.selected').removeClass('selected');
			$(this).parent('li').addClass('selected');
		});
		
		$('.primary #international').addClass('fadeIn');
		
	},
	scrollTo: function(delay){
		/mobile/i.test(navigator.userAgent) && !window.location.hash && setTimeout(function () {
		  if (!window.pageYOffset) window.scrollTo(0, 1);
		},delay);
	}
	
};

$(function(){

	lcm.init();
	
});
