/**
 * Copyright 2009 Crisp Wireless Inc.
 */
function mobileRedirect() {
  var mobiles = [];
  mobiles[0] = "iphone";
  mobiles[1] = "ipod";
 /* mobiles[2] = "playstation";
  mobiles[3] = "wap";
  mobiles[4] = "windows ce";
  mobiles[5] = "wm5 pie";
  mobiles[6] = "iemobile";
  mobiles[7] = "series60";
  mobiles[8] = "symbian";
  mobiles[9] = "series60";
  mobiles[10] = "series70";
  mobiles[11] = "series80";
  mobiles[12] = "series90";
  mobiles[13] = "blackberry";*/
  mobiles[2] = "android";
  /*mobiles[15] = "midp";
  mobiles[16] = "wml";
  mobiles[17] = "brew";
  mobiles[18] = "palm";
  mobiles[19] = "xiino";
  mobiles[20] = "blazer";
  mobiles[21] = "nitro";
  mobiles[22] = "netfront";
  mobiles[23] = "sonyericsson";
  mobiles[24] = "ericsson";
  mobiles[25] = "sec-sgh";
  mobiles[26] = "docomo";
  mobiles[27] = "kddi";
  mobiles[28] = "vodafone";
  mobiles[29] = "opera mini";
  mobiles[30] = "motorola";
  mobiles[31] = "nokia";
  mobiles[32] = "smartphone";
  mobiles[33] = "hiptop";*/
  var uagent = navigator.userAgent.toLowerCase();
  for(i=0; i<mobiles.length; i++) {
     if(uagent.search(mobiles[i]) > -1) {
      //window.location = mobileURL;
	mobrediropt();
      break;
     }
  }
}

$(document).ready(function(){

	if (typeof(incid) === 'undefined') {
		incid = 0;
	}

	processmobpop = function(val) {

		switch (val) {
			case 0:
				mobredirhide();
				location.href = 'http://m.inc.com?incid=' + incid;
				break;
			case 1:
				mobredirhide();
				break;
			case 2:
				mobredirhide();	
				setCookie('mobrediropt','1', {"expires": 20000000, "path" : "/", "domain" : ".inc.com" });
				break;
			default:
				mobredirhide();
				location.href = 'http://m.inc.com?incid=' + incid;
				break;
		}
	}



	mobrediropt = function() {

		if (incid > 0) {
			try {
				$('.inctv-player-wrap').addClass('displaynone');
			}
			catch(error) {}

			$('html').append('<div id="mobpop" style="display: none;">' +
						'<div id="mobpopheader"><img src="http://www.inc.com/images/mobile-popup-header.png"</div>' + 
						'<div id="mobpoptext">Would you like to view this article in Inc.com Mobile?</div>' +
						'<div id="mobpopbcontainer">' +
							'<button class="mobpopbuttons" onclick="processmobpop(0);">Yes</button>' +
							'<button class="mobpopbuttons" onclick="processmobpop(1);">No</button>' +
							'<button class="mobpopbuttons" onclick="processmobpop(2);">Never</button>' +
						'</div>');
			$('body').fadeTo(500, 0.2);
			$('#mobpop').fadeIn(500);
		}
		else {
			location.href = 'http://m.inc.com';
		}
	}

	mobredirhide = function() {
			$('body').fadeTo(500, 1.0);
			$('#mobpop').fadeOut(500, function() {
				$('html').remove('#mobpop');
			});

			try {
				$('.inctv-player-wrap').removeClass('displaynone');
			}
			catch(error) {}
	}

	try {
		if ((location.search.indexOf('mobile=override')==-1) && ((!getCookie('mobrediropt') && incid > 0) || incid == 0)) {
			     mobileRedirect();
		}
	}
	catch(error) {}
});

