
function WriteCookie(name, value) {
  var argv = WriteCookie.arguments;
  var argc = WriteCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
  
  var expireDate = new Date();
  expireDate.setTime(expireDate.getTime() + 2 * 24 * 3600 * 1000); // Expire dans 2 jours
  
  document.cookie = name + "=" + escape(value)+ "; expires=" + expireDate.toGMTString() +
  						((path == null) ? "" : ("; path="+path)) +
  						((domain == null) ? "" : ("; domain="+domain)) +
  						((secure == true) ? "; secure" : "");
}

function getCookieVal(offset) {
  var endstr = document.cookie.indexOf(";", offset);
  if (endstr == -1) {
    endstr = document.cookie.length;
  }
  return unescape(document.cookie.substring(offset, endstr));
}

function ReadCookie(name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg) {
      return getCookieVal(j);
    }
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) {
      break;
    }
  }
  
  return null;
}

function mobileCheckIsMobile() {
	if (!(navigator.userAgent.match(/ipad/i))) {
		if (navigator.userAgent.match(/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|vodafone|o2|pocket|kindle|mobile|treo|ipaq|palm|nokia|blackberry|opera mini|lg|htc|mot|psp|sonyericsson|sgh|android|iphone|ipod)/i)) {
			return true;
		}
	}
	return false;
}

function mobileIsSpecificMobile() {
	if (navigator.userAgent.match(/ipad|iphone/i)) {
		return true;
	}
	return false;
}

function mobileRedirectIfMobile() {
	if (mobileCheckIsMobile()) {
		if (ReadCookie("XVIDEOS_MOBILE") != "NO") {
			location.replace("http://mobile." + master_url + "/");
		}
	}
}

function mobileGoBackOnNormalSite() {
	WriteCookie("XVIDEOS_MOBILE", "NO", null, "/", master_url);
	location.replace("http://www." + master_url + "/");
}

function mobileVideoChangeForMobile() {
	if (mobileIsSpecificMobile()) {
		var player = document.getElementById("player");
		if (player) {
			var txt = player.innerHTML;
			var index = txt.indexOf("3GP||");
			if (index) {
				var index2 = txt.indexOf("||", index + 5);
				if (index2) {
					var url = txt.substring(index + 5, index2);
					player.innerHTML = "<h2>Mobile version</h2>";
					player.innerHTML += "<video src=\"" + url + "\" width=\"600\" height=\"400\" autobuffer controls /><br />";
					player.innerHTML += "<a href=\"" + url + "\">Download / View video</a><br />";
				}
			}
		}
	}
}
