var baseurl;
function getparam() {
  playmode = "auto";
  if(window.location.search) {
    var temp=window.location.search.substring(1);
    var offset = temp.lastIndexOf('/');
    baseurl = temp.substring(0, offset + 1);
    callhtml = temp.substring(offset + 1);
//playmode=play.html→auto
//playmode=playlink.html→manual
    if(callhtml == "playlink.html") {
      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;
    }
    // _512.mov,_ISDN.mov,_384.mp4・・・more
    var temp = sVarLine.lastIndexOf('_');
    if(temp != -1) {
      var str = sVarLine.slice(temp);
      if(str.match(/^_([0-9a-zA-Z]+)\.([0-9a-zA-Z]+)\/?$/)) {
        sVarLine = sVarLine.slice(0, temp);
      }
    }
    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-telebee.jp/ユーザ名/ムービーセット名

//alert(baseurl);
  parseCallingURL(baseurl);
  if(0) {
    if(getMaxVars() != 6) {
      return;
    }
    re = new RegExp("^"+url+"\/users\/([0-9a-zA-Z-_]+)\/?([0-9a-zA-Z-_%]+)\/?$");
    if(!getVarString().match(re)) {
      return;
    }
  } else {
    if(getMaxVars() != 6) {
      if(getMaxVars() == 5) {
        if(getVarString().match(/^http:\/\/((pb|pb1)\.m-telebee\.jp|122\.208\.203\.52)\/vid\/([0-9]+)\/?$/)) {
          sValue = new getValueArray();
          LoadPlayer3(url + '/loadplayer/',mwidth,mheight,sValue[4], playmode);
        } else if(getVarString().match(/^http:\/\/(sompo)\.m-telebee\.jp\/vid\/([0-9]+)\/?$/)) {
          sValue = new getValueArray();
//use index2.html url = 'http://sompo.m-telebee.jp';
          LoadPlayerSompo(url + '/loadplayer/',mwidth,mheight,sValue[4], '1', playmode);
        } else {
//alert(getVarString());
          return;
        }
      } else if(getMaxVars() == 7) {
        if(!getVarString().match(/^http:\/\/((pb|pb1)\.m-telebee\.jp|122\.208\.203\.52)\/users\/rurubutv\/([0-9]+)\/?([0-9a-zA-Z.\-_%\(\)]+)\/?$/)) {
//alert(getVarString());
          return;
        }
        sValue = new getValueArray();
        LoadPlayer2(url + '/loadplayer/',mwidth,mheight,sValue[4]+'/'+sValue[5], URLencode(sValue[6]), playmode);
      }
      return;
    } else {
      if(getVarString().match(/^http:\/\/((pb|pb1)\.m-telebee\.jp|122\.208\.203\.52)\/vid\/([0-9]+)\/?([0-9]+)\/?$/)) {
        sValue = new getValueArray();
        LoadPlayer5(url + '/loadplayer/',mwidth,mheight,sValue[4], sValue[5], playmode);
      } else if(getVarString().match(/^http:\/\/((sompo)\.m-telebee\.jp|122\.208\.203\.52)\/vid\/([0-9]+)\/?([0-9]+)\/?$/)) {
        sValue = new getValueArray();
//use index2.html url = 'http://sompo.m-telebee.jp';
        LoadPlayerSompo(url + '/loadplayer/',mwidth,mheight,sValue[4], sValue[5], playmode);
      } else if(!getVarString().match(/^http:\/\/((pb|pb1)\.m-telebee\.jp|122\.208\.203\.52)\/users\/([0-9a-zA-Z\-_]+)\/?([0-9a-zA-Z.\-_%\(\)]+)\/?$/)) {
//alert(getVarString());
        return;
      } else {
        sValue = new getValueArray();
        //var productID = RegExp.$1;
        // movieSet(productID);
        //movieSet(sValue[4], sValue[5], sValue[5] + '_Thumbnail.jpg');
        //LoadPlayerJw('http://(pb|pb1).m-telebee.jp/loadplayer/',mwidth,mheight,sValue[4], URLencode(sValue[5]));
        if(sValue[4] == "okwave") {
          LoadPlayer4(url + '/loadplayer/',mwidth,mheight,sValue[4], URLencode(sValue[5]), playmode);
        } else {
          LoadPlayer2(url + '/loadplayer/',mwidth,mheight,sValue[4], URLencode(sValue[5]), playmode);
        }
      }
      return;
    }
  }
//  var obj = document.getElementById('movieArea');
//  obj.innerHTML = html;
}

function movieSet(name, mname, pname, num) {
/*
    var url = "http://pb.m-telebee.jp/"; // path
    var mwidth = 340; // iframeの幅
    var mheight= 320; // iframeの高さ
    var twidth = 320; // iframe非対応サムネイルの幅
    var theight= 240; // iframe非対応サムネイルの高さ
*/
    var movie = URLencode(mname);
    var html;

    html = "<iframe src=\"" + url + "users/"
      + name + "/"
      + movie + "/"
      + "play.html\" "
      + "marginwidth=\"0\" marginheight=\"0\" hspace=\"0\" "
      + "vspace=\"0\" allowtransparency=\"true\" bordercolor=\"#000000\" "
      + "frameborder=\"0\" scrolling=\"no\" "
      + "width=\"" + mwidth + "\" height=\"" + mheight + "\">"
      + "<a href=\"" + url + "users/"
      + name + "/"
      + movie + "/"
      + "play.html\">"
      + "<img src=\"" + url + "users/"
      + name + "/"
      + movie + "/"
      + movie + "_Thumbnail.jpg\" "
      + "alt=\""
      + mname + "\" border=\"0\" "
      + "width=\"" + twidth + "\" height=\"" + theight + "\" />"
      + "</a></iframe>"
      + "";

  var obj = document.getElementById('movieArea');
  obj.innerHTML = html;
  
  mainBodySet2(mname, pname, num);
  mainBodyTitleSet(num);
}

function mainBodySet(mname, pname, num) {
  //alert('mainBodySet');
  var txt = eval("mainBodyText" + num);

  var obj = document.getElementById('mainBody');
  obj.innerHTML = txt;
}

function mainBodyTitleSet(num) {
  //alert('mainBodySet');
  var ttl = eval("mainBodyTitle" + num);

  var obj = document.getElementById('mainBodyTitle');
  obj.innerHTML = ttl;
}

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;
}

