var uastr = navigator.userAgent.toLowerCase();
// create the device object
var parts = uastr.split(/\s*[;)(]\s*/);
var gpVer = parts[3].replace(/[^\d._]/g, "");
//or if we dont need decimal (/[^\d]/g, "")
gpVer = gpVer.replace(/_/g, ".").substring(0, 3);
//remove _ from IOS - need to incorporate in above at some point. also wanted
// single deimal point precision.
var scrWidth = screen.width;
var scrHeight = screen.height;
if(uastr.indexOf('ipad') != -1) {
	mdeviceObj = {
		device : "Tablet",
		os : "IOS",
		type : "Desktop",
		osver : gpVer,
		width : scrWidth,
		height : scrHeight
	};
}
else if(uastr.indexOf('ipod') != -1) {
	mdeviceObj = {
		device : "MP3",
		os : "IOS",
		type : "Mobile",
		osver : gpVer,
		width : scrWidth,
		height : scrHeight
	};
}
else if(uastr.indexOf('iphone') != -1) {
	mdeviceObj = {
		device : "Phone",
		os : "IOS",
		type : "Mobile",
		osver : gpVer,
		width : scrWidth,
		height : scrHeight
	};
}
else if(uastr.indexOf('android') > -1 && uastr.indexOf('mobile') != -1) {
	mdeviceObj = {
		device : "Phone",
		os : "Android",
		type : "Mobile",
		osver : gpVer,
		width : scrWidth,
		height : scrHeight
	};
}
else if(uastr.indexOf('android') > -1 && uastr.indexOf('mobile') == -1) {
	mdeviceObj = {
		device : "Tablet",
		os : "Android",
		type : "Desktop",
		osver : gpVer,
		width : scrWidth,
		height : scrHeight
	};
}
else if(uastr.indexOf('windows') != -1 && uastr.indexOf('phone') != -1) {
	mdeviceObj = {
		device : "Phone",
		os : "WP7",
		type : "Mobile",
		osver : gpVer,
		width : scrWidth,
		height : scrHeight
	};
}
else if(uastr.indexOf('silk-accelerated') != -1) {
	mdeviceObj = {
		device : "Tablet",
		os : "Android",
		type : "Desktop",
		osver : gpVer,
		width : scrWidth,
		height : scrHeight
	};
}
else {
	mdeviceObj = {
		device : "Desktop",
		os : "Other",
		type : "Desktop",
		osver : gpVer,
		width : scrWidth,
		height : scrHeight
	};
}
if(mdeviceObj.device == "Tablet" && mdeviceObj.width < 600) {
	mdeviceObj.type = "Mobile";
}
if((mdeviceObj.os == "Android" && mdeviceObj.osver > 3) || (mdeviceObj.os == "IOS" && mdeviceObj.osver > 5)) {
	mdeviceObj.isFixed = true;
}
else {
	mdeviceObj.isFixed = false;
}
if(mdeviceObj.device == "Tablet" && window.location.href.indexOf('/checkout/') == -1) {
	$(document).ready(function() {
		$('#wrapper_columns').after('<div style="text-align:center;"><img src="http://www3.meijer.com/GP/backtotop.gif" alt="back to top" onclick="self.scrollTo(0,0);"/></div>');
	});
}
else if(mdeviceObj.type == "Mobile") {
	if(window.location.href.indexOf('/checkout/') == -1) {
		$(document).ready(function() {
			$('#wrapper_columns').after('<style type="text/css">#mobile_footer {border:1px solid #B4B4B4;list-style-type:none;margin:10px auto;padding:0;width:auto;} #mobile_footer li a {display:block;font:bold 17px "Helvetica Neue",Helvetica;color:#000;text-decoration:none;margin:-10px;padding: 12px 22px 12px 10px;} #mobile_footer li:first-child {border-top:none;} #mobile_footer li {border-top: 1px solid #B4B4B4;padding:10px;background-image: url("http://www.meijer.com/assets/site/mobile/css/../images/arrow_right.png");background-position: 98% 50%;background-repeat: no-repeat;}</style><div style="text-align:center;"><img src="http://www3.meijer.com/GP/backtotop.gif" alt="back to top" onclick="self.scrollTo(0,0);"/></div><ul id="mobile_footer"><li><a href="http://www.meijer.com">Home</a></li><li><a href="http://custhelp.meijer.com/">Help</a></li><li><a href="http://www.meijer.com/custserv/store_locator.jsp">Store Locator</a></li><li id="xfull"><a href="#" id="switch_fullsite">Full View</a></li></ul>');
			$('#switch_fullsite').live('click', function() {
				document.cookie = 'mjr_force_full=true; path=/';
				window.location.reload();
				return false;
			});
		});
	}
	if(document.cookie.indexOf('mjr_force_full=true') != -1 && window.location.href.indexOf('/checkout/') == -1) {
		$(document).ready(function() {
			$('#xfull').remove();
			$('#mobile_footer').append('<li id="xmob"><a href="#" id="switch_mobile">Mobile View</a></li>');
			$('#switch_mobile').live('click', function() {
				document.cookie = 'mjr_force_full=false; expires=-1; path=/';
				window.location.href = 'http://www.meijer.com/assets/site/mobile/index.html';
				return false;
			});
		});
	}
	else {
		var mOne = '<meta name="viewport" content="width=320, initial-scale=1">';
		$('head').append(mOne);
		var m2 = '<meta name="mobileoptimized" content="0" />';
		$('head').append(m2);
		if(window.location.href.indexOf('/checkout/') != -1 && window.location.href.indexOf('order_status.jsp') == -1 && window.location.href.indexOf('basket.jsp') == -1 && window.location.href.indexOf('update_items_in_order.cmd') == -1 && window.location.href.indexOf('move_item_to_wish_list.cmd') == -1) {
			var paths = ['/assets/site/mobile/css/mobilize_checkout.css'];
			var head = document.getElementsByTagName("head")[0];
			for(var a = 0; a < paths.length; a++) {
				var ref = document.createElement("link");
				ref.setAttribute("rel", "stylesheet");
				ref.setAttribute("type", "text/css");
				ref.setAttribute("href", paths[a]);
				head.appendChild(ref);
			}
			var $logo = $('#logo_meijer');
			$logo.attr('src', '/assets/site/mobile/images/mlogo.png');
			$logo.height(38);
			$logo.width(96);
			$(document).ready(function() {
				$('table.layout_2').attr('style', 'width: 95%');
				$('table.layout_2 input').attr('size', '40');
				$('#checkout #submitMsgBottom').attr('style', '');
			});
		}
		else {
			var paths = ['/assets/site/mobile/css/mobilize.css', '/assets/site/mobile/css/mobile.css'];
			var head = document.getElementsByTagName("head")[0];
			for(var a = 0; a < paths.length; a++) {
				var ref = document.createElement("link");
				ref.setAttribute("rel", "stylesheet");
				ref.setAttribute("type", "text/css");
				ref.setAttribute("href", paths[a]);
				head.appendChild(ref);
			}
			var toolbar = '<div id="toolbar" class="toolbar shared">' + '<a href="#" id="logo" class="logo"></a>' + '<div id="tools_container" class="tools_container">' + '<a id="cart" class="cart tool"></a>' + '</div>' + '<form id="searchMobile" action="http://www.meijer.com/catalog/search_command.cmd" method="POST" target="_self">' + '<div class="mform search">' + '<input class="text" type="input" id="keyword_mobile" name="keyword" />' + '</div>' + '</form>' + '</div>' + '<div id="toolbar_context" class="toolbar toolbar_sub shared">' + '<a class="button up" href="#">Back</a>' + '<h1 id="context_title"></h1>' + '</div>' + '<div style="display: none;">' + '<form id="sl_form" method="post" action="http://www.meijer.com/custserv/locate_store.cmd">' + '<input type="hidden" id="sl_latitude" name="latitude" />' + '<input type="hidden" id="sl_longitude" name="longitude" />' + '</form>' + '<form id="wa_form" method="post" action="http://www.meijer.com/custserv/locate_ad.cmd">' + '<input type="hidden" id="wa_latitude" name="latitude" />' + '<input type="hidden" id="wa_longitude" name="longitude" />' + '</form>' + '</div>';
			$('body').prepend($(toolbar));
			$('#logo').bind('click', function() {
				$('body').fadeOut(function() {
					window.location.href = '/assets/site/mobile/index.html';
				});
			});
			$('#mstores').click(function() {
				$('#sl_latitude').val(geoLat);
				$('#sl_longitude').val(geoLong);
				$('#sl_form').submit();
				return false;
			});
			$('#mads').click(function() {
				$('#wa_latitude').val(geoLat);
				$('#wa_longitude').val(geoLong);
				$('#wa_form').submit();
				return false;
			});
			$(document).ready(function() {
				if((window.location.href.split('-sale').length - 1) == 2) {
					$('ul.shop.tanBkg.tanRoundedTop').insertBefore($('div.pc_tc').first());
				}
				$('#main-prod-image').attr('height', '250').attr('width', '250');
				$('#bigsteals .head #timer').before($("<span style='font-size: 120%;'>Time Remaining for Today's Big Steal:</span>"));
				$('#checkout .product_description a.updateItemInOrder').unbind('click').bind('click', function() {
					return false;
				});
				$('#toolbar_context').after($('#persistent_cart'));
				$('.priceWrapper, .nameWrapper, .promoInfo').css('height', 'auto');
				$('select[id="endecaView"]:first').after($('<br/>'));
				$('select[id="endecaView"]:last').after($('<br/>'));
				$('#cart').bind('click', function() {
					window.location.href = 'http://www.meijer.com/checkout/basket.jsp';
					return false;
				});
				$('a.up').bind('click', function() {
					var bc = $('#wrapper_breadcrumb');
					if(window.location.href.indexOf('/big-steals') != -1) {
						window.location.href = _HOME;
					}
					else if(bc.length > 0) {
						var homeLink, site_sectionLink, categoryLink, subcategoryLink;
						var site_section, category;
						var $bcs = $('#wrapper_breadcrumb a').each(function(i, el) {
							var $el = $(el);
							var href = $el.attr('href');
							var label = $el.text();
							switch (i) {
								case 0:
									homeLink = href;
									break;
								case 1:
									site_sectionLink = href;
									site_section = label;
									break;
								case 2:
									categoryLink = href;
									category = label;
									break;
								case 3:
									subcategoryLink = href;
									break;
							}
						});
						if(subcategoryLink) {
							window.location.href = subcategoryLink;
						}
						else if(category) {
							window.location.href = '/assets/site/mobile/index.html?sess_started_at_page=page_tx_category&sess_started_at_txvalue=' + escape(category);
						}
					}
					else {
						window.location.href = _HOME;
					}
					return false;
				});
				var productName = $('#product-name').text();
				if(productName) {
					$('#context_title').hide().text(productName).fadeIn();
					var txt = '%20Prod:%20' + encodeURIComponent(productName);
					var img = '<img id="mTrk" class="trk" src="http://newmeijercom.122.2o7.net/b/ss/newmeijercom/5/?pccr=true&g=none&ev=event45&AQE=1&pageName=MjrMob:' + txt + '&AQB=1">';
					$('body').append(img);
				}
				else {
					if($('#wrapper_breadcrumb > h1:last').text()) {
						$('#context_title').hide().text($('#wrapper_breadcrumb > h1:last').text()).fadeIn();
					}
					else {
						$('#toolbar_context a.up').css('background-image', 'url("/assets/site/mobile/images/button_home.png")');
						if(window.location.href.indexOf('/store_locator.jsp') != -1 || window.location.href.indexOf('/locate_store.cmd') != -1 || window.location.href.indexOf('/locate_store_by_state.cmd') != -1) {
							$('#context_title').hide().text('Store Locator').fadeIn();
						}
						else if(window.location.href.indexOf('/show_ad.cmd') != -1 || window.location.href.indexOf('/locate_ad.cmd') != -1) {
							$('#context_title').hide().text('Weekly Ads').fadeIn();
						}
						else if(window.location.href.indexOf('/order_status.jsp') != -1) {
							$('#context_title').hide().text('Order Status').fadeIn();
						}
						else if(window.location.href.indexOf('/user/login.jsp') != -1) {
							$('#context_title').hide().text('Login').fadeIn();
						}
						else if(window.location.href.indexOf('/empty_basket.jsp') != -1 || window.location.href.indexOf('/basket.jsp') != -1) {
							$('#context_title').hide().text('Shopping Cart').fadeIn();
						}
						else if(window.location.href.indexOf('/search_command.cmd') != -1) {
							$('#context_title').hide().text('Search Results').fadeIn();
						}
						else if(window.location.href.indexOf('/Offer_Details.cms') != -1) {
							$('#context_title').hide().text('Special Offers').fadeIn();
						}
					}
					var txt = '%20SubCat:%20' + encodeURIComponent($('#context_title').text());
					var img = '<img id="mTrk" class="trk" src="http://newmeijercom.122.2o7.net/b/ss/newmeijercom/5/?pccr=true&g=none&ev=event45&AQE=1&pageName=MjrMob:' + txt + '&AQB=1">';
					$('body').append(img);
				}
				var $keywordMobile = $('#keyword_mobile');
				var searchValue = $('#header_search .SearchInputWrapper #keyword').val();
				if(searchValue) {
					$keywordMobile.val(searchValue);
				}
				$('#searchMobile').submit(function() {
					$('#header_search #keyword').val($keywordMobile.val());
					$('#header_search > form[name="endecaSearchForm"]').submit();
					return false;
				});
				var $searchResults2 = $('#second-col_search-results-mid');
				$('#first-col-search-results-mid').after($searchResults2);
				$searchResults2.show();
				$('#promo-sp-prod').insertAfter('#prod_price_outer').show();
			});
		}
	}
}

