var baseurl;
function getparam() {
  playmode = "auto";
  if(window.location.search) {
    var temp = window.location.search.substring(1);
    var offset = temp.lastIndexOf('/');
    playmode = temp.substring(offset + 1);
    if(!playmode) {
      temp = temp.substring(0, offset);
      var offset = temp.lastIndexOf('/');
      playmode = temp.substring(offset + 1);
    }
    baseurl = temp.substring(0, offset + 1);
//playmode=/auto/→auto
//playmode=/manual/→manual
//alert(playmode);
    if(playmode == "manual") {
      playmode = "manual";
    } else {
      playmode = "auto";
    }
  }
  url2cmd(playmode);
}
window.onload=getparam;

function url2cmd(playmode) {
  var nMaxVars = 0;
  var sVarLine = "";
  var sName = new Array();
  var sValue = new Array();
  
  function getMaxVars() { return nMaxVars; }
  function getVarString() { return sVarLine; }
  function getNameArray() { return sName; }
  function getValueArray() { return sValue; }
    
  function parseCallingURL(sCallingURL) {
    //Get the calling URL and parse out variables
    sCallingURL = String(sCallingURL);
    
    if(sCallingURL.length == 0)
      return;
      
    // Check if there are any variables
    if(sCallingURL.indexOf('/') != -1) {
      sVarLine = String(sCallingURL);
    } else {
      // No variables
      return;
    }
    var nPos = 0;
    var sChar = "";
    var sWord = "";
    nMaxVars = 0;
    
    // Parse
    while (nPos < sVarLine.length) {
      sChar = sVarLine.substring(nPos, nPos + 1);
      
      if(sChar == "/") {
        sValue[nMaxVars] = sWord;
        sWord = "";
        sName[nMaxVars] = nPos;
        nMaxVars++;
        
      } else if(nPos == sVarLine.length - 1) {
        sWord += sVarLine.substring(nPos, nPos + 1);
        sValue[nMaxVars] = sWord;
        sWord = "";
        nMaxVars++;
        
      } else {
        sWord += sChar;
      }
      nPos++;
    }
  }
  //http://pb1.m-booth.com/サイズ[s:320,m:640,l:-]/fileid
  parseCallingURL(baseurl);
//alert(baseurl);
//alert(getMaxVars());
  switch(getMaxVars()) {
  case 5:
    if(!getVarString().match(/^http:\/\/(pb|pb1|sotokoto)\.m-booth\.com\/([smpl]+)\/?([0-9a-zA-Z]+)\/?$/)) {
    //alert(getVarString());
      return;
    } else {
      sValue = new getValueArray();
      switch(sValue[2]) {
      case "sotokoto.m-booth.com":
        url = "http://sotokoto.m-booth.com";
        volume = 0;
        break;
      default:
        volume = 50;
        break;
      }
  //alert(sValue[3]);
      switch(sValue[3]) {
      case 's':
        mwidth = 320;
        mheight = 240;
        vmode = 1;
        break;
      case 'p':
        mwidth = 320;
        mheight = 184;
        vmode = 2;
        break;
      case 'm':
        mwidth = 640;
        mheight = 368;
        vmode = 2;
        break;
      case 'l':
        vmode = 2;
        break;
      default:
        mwidth = 320;
        mheight = 240;
        vmode = 1;
        break;
      }
  //function LoadPlayer(path,w,h,vmode,eid,playmode,swfid)
      var dateObj = (new Date()).getTime();
      LoadPlayer(url + '/playlist/',mwidth,mheight,vmode,URLencode(sValue[4]), '',playmode,dateObj,volume);
    }
    break;
  case 6:
    if(!getVarString().match(/^http:\/\/(pb|pb1)\.m-booth\.com\/([smpl]+)\/?([0-9a-zA-Z]+)\/?([0-9a-zA-Z]+)\/?$/)) {
    //alert(getVarString());
      return;
    } else {
      sValue = new getValueArray();
      volume = 50;
  //alert(sValue[3]);
      switch(sValue[3]) {
      case 'p':
        mwidth = 320;
        mheight = 184;
        vmode = 2;
        break;
      default:
        mwidth = 320;
        mheight = 184;
        vmode = 2;
        break;
      }
  //function LoadPlayer(path,w,h,vmode,eid,playmode,swfid)
      var dateObj = (new Date()).getTime();
      LoadPlayer(url + '/playlist/',mwidth,mheight,vmode,URLencode(sValue[4]), '%26previewkey=' + URLencode(sValue[5]),playmode,dateObj,volume);
    }
    break;
  default:
    break;
  }
  return;
}

function URLencode(str){
// Unicode to URL encoded UTF-8
var i, encoded_str, char_code, padded_str;
encoded_str = "";
for (i = 0; i < str.length; i++){
  char_code = str.charCodeAt(i);
  if(char_code == 0x20){
    // space -> "+"
    encoded_str += "+";
  } else { // else 1
    if(((0x30 <= char_code) && (char_code <= 0x39)) || ((0x41 <= char_code) && (char_code <= 0x5a)) 
      || ((0x61 <= char_code) && (char_code <= 0x7a))){
      // [0-9a-z-A-Z]
      // no escape
      encoded_str += str.charAt(i);
    } else if((char_code == 0x2a) || (char_code == 0x2e) || (char_code == 0x2d) || (char_code == 0x5f)) {
      // [.-_]
      // no escape
      encoded_str += str.charAt(i);
    } else { // else 2
      // for internal unicode to UTF-8
      if( char_code > 0xffff ) {
        encoded_str += "%" + ((char_code >> 18) | 0xf0).toString(16).toUpperCase();
        encoded_str += "%" + (((char_code >> 12) & 0x3f) | 0x80).toString(16).toUpperCase();
        encoded_str += "%" + (((char_code >> 6) & 0x3f) | 0x80).toString(16).toUpperCase();
        encoded_str += "%" + ((char_code & 0x3f) | 0x80).toString(16).toUpperCase();
      } else if( char_code > 0x7ff ) {
        encoded_str += "%" + ((char_code >> 12) | 0xe0).toString(16).toUpperCase();
        encoded_str += "%" + (((char_code >> 6) & 0x3f) | 0x80).toString(16).toUpperCase();
        encoded_str += "%" + ((char_code & 0x3f) | 0x80).toString(16).toUpperCase();
      } else if( char_code > 0x7f ) {
        encoded_str += "%" + (((char_code >> 6) & 0x1f) | 0xc0).toString(16).toUpperCase();
        encoded_str += "%" + ((char_code & 0x3f) | 0x80).toString(16).toUpperCase();
      } else {
        // for ascii
        padded_str = "0" + char_code.toString(16).toUpperCase();
        encoded_str += "%" + padded_str.substr(padded_str.length - 2, 2);
      }
    } // else 2
  } // else 1
} // for
return encoded_str;
}
