
var flash_video_id;
var video_area_id;
var video_title_id;
var sel_area_id;
var info_area_id;
var autozoom_on_id;
var autozoom_off_id;
var zoom_on_id;
var zoom_off_id;
var mAutozoom = false;
var mZoom = false;
var playing = false;
var mViewNumber;

function playFlashVideo() {
  if (!flash_video_id) {
    flash_video_id = document.getElementById('flash_video');
  }
  if (flash_video_id) {
    flash_video_id.playRequest();
  }
}
function pauseFlashVideo() {
  if (!flash_video_id) {
    flash_video_id = document.getElementById('flash_video');
  }
  if (flash_video_id) {
    flash_video_id.pauseRequest();
  }
}
function changeWebisodeFlashVideo(num) {
  if (!flash_video_id) {
    flash_video_id = document.getElementById('flash_video');
  }
  if (flash_video_id) {
    flash_video_id.webisodeChangeRequest(num);
  }
}

function flashVideoState(webisode_number, transport_state, max_webisodes) {
// alert('WN: ' + webisode_number + '; TS: ' + transport_state + '; Max: ' + max_webisodes);
//  new Ajax.Request('/movie/change_webisode/0000000000020101', {asynchronous:true, evalScripts:true, onComplete:function(request){setPlayState(transport_state)}}); return false;

  if (!video_area_id) {
    video_area_id = document.getElementById('div_video_area');
  }
  if (!video_title_id) {
    video_title_id = document.getElementById('div_video_title');
  }
  if (!flash_video_id) {
    flash_video_id = document.getElementById('flash_video');
  }
  if (!sel_area_id) {
    sel_area_id = document.getElementById('div_sel_area');
  }
  if (!info_area_id) {
    info_area_id = document.getElementById('div_info_area');
  }
  if (!autozoom_on_id) {
    autozoom_on_id = document.getElementById('autozoom_on');
  }
  if (!autozoom_off_id) {
    autozoom_off_id = document.getElementById('autozoom_off');
  }
  for (i=0; i<max_webisodes; i++) {
    var wn;
    if (i<10) {
      wn = '0' + i;
    } else {
      wn = i;
    }
    if (i == webisode_number) {
      document.getElementById('webisode' + wn + '_nonsel').style.display = 'none';
      if (transport_state == '0') {
        document.getElementById('webisode' + wn + '_playing').style.display = 'none';
        document.getElementById('webisode' + wn + '_paused').style.display = 'block';
      } else if (transport_state == '1') {
        document.getElementById('webisode' + wn + '_playing').style.display = 'block';
        document.getElementById('webisode' + wn + '_paused').style.display = 'none';
      }
    } else {
      document.getElementById('webisode' + wn + '_nonsel').style.display = 'block';
      document.getElementById('webisode' + wn + '_playing').style.display = 'none';
      document.getElementById('webisode' + wn + '_paused').style.display = 'none';
    }
  }
  if (video_area_id && flash_video_id) {
    if (transport_state == '0') {
      cssjs('remove',video_area_id,'video_area_big');
      cssjs('add',video_area_id,'video_area');
      cssjs('remove',video_title_id,'video_title_big');
      cssjs('add',video_title_id,'video_title');
      flash_video_id.height = 310;
      flash_video_id.width = 480;
      sel_area_id.style.display = 'block';
      info_area_id.style.display = 'block';
      playing = false;
    } else {
      if (getAutozoom()) {
        cssjs('add',video_area_id,'video_area_big');
        cssjs('remove',video_area_id,'video_area');
        cssjs('add',video_title_id,'video_title_big');
        cssjs('remove',video_title_id,'video_title');
        flash_video_id.height = 600;
        flash_video_id.width = 930;
        sel_area_id.style.display = 'none';
        info_area_id.style.display = 'none';
      }
      playing = true;
    }
  }
}

function autozoom(aAz) {
//   alert('autozoom arg = ' + aAz);
  if (!video_area_id) {
    video_area_id = document.getElementById('div_video_area');
  }
  if (!video_title_id) {
    video_title_id = document.getElementById('div_video_title');
  }
  if (!flash_video_id) {
    flash_video_id = document.getElementById('flash_video');
  }
  if (!sel_area_id) {
    sel_area_id = document.getElementById('div_sel_area');
  }
  if (!info_area_id) {
    info_area_id = document.getElementById('div_info_area');
  }
  if (!autozoom_on_id) {
    autozoom_on_id = document.getElementById('autozoom_on');
  }
  if (!autozoom_off_id) {
    autozoom_off_id = document.getElementById('autozoom_off');
  }

  if (playing && aAz) {
    cssjs('add',video_area_id,'video_area_big');
    cssjs('remove',video_area_id,'video_area');
    cssjs('add',video_title_id,'video_title_big');
    cssjs('remove',video_title_id,'video_title');
    flash_video_id.height = 600;
    flash_video_id.width = 930;
    sel_area_id.style.display = 'none';
    info_area_id.style.display = 'none';
  } else {
    cssjs('remove',video_area_id,'video_area_big');
    cssjs('add',video_area_id,'video_area');
    cssjs('remove',video_title_id,'video_title_big');
    cssjs('add',video_title_id,'video_title');
    video_title_id.width = 470;
    flash_video_id.height = 310;
    flash_video_id.width = 480;
    sel_area_id.style.display = 'block';
    info_area_id.style.display = 'block';
  }

  if (aAz) {
    mAutozoom = true;
    autozoom_on_id.style.display = 'block';
    autozoom_off_id.style.display = 'none';
    new Ajax.Request('/movie/autozoom/true', {asynchronous:true, evalScripts:true }); return false;

  } else {
    mAutozoom = false;
    autozoom_on_id.style.display = 'none';
    autozoom_off_id.style.display = 'block';
    new Ajax.Request('/movie/autozoom/false', {asynchronous:true, evalScripts:true }); return false;
  }

}

function getAutozoom() {
  if (!mAutozoom) {
    if (!autozoom_on_id) {
      autozoom_on_id = document.getElementById('autozoom_on');
    }
    mAutozoom = (autozoom_on_id.style.display != 'none');
// alert('mAutozoom = ' + mAutozoom);
  }
  return mAutozoom;
}

function notifyVideoHasBeenSeen( aCode, aWN ) {
  var wn = (aWN >9)?aWN :'0'+aWN;
  var code = aCode.substring(0,4) + wn + getViewNumberString(aCode);
// alert('Code = ' + code);
  new Ajax.Request('/movie/seen/' + code, {asynchronous:true, evalScripts:true}); return false;
}

function setViewNumber( aVN ) {
  mViewNumber = aVN;
//alert('VN = ' + aVN );
}

function getViewNumberString(aCode) {
  if (!mViewNumber) {
    mViewNumber = parseInt(aCode.substring(6,8));
  }
  var vn = (mViewNumber >9)?mViewNumber :'0'+mViewNumber;
  return vn;
}













function zoom(aZ) {
// alert('zoom arg = ' + aZ);
  if (!video_area_id) {
    video_area_id = document.getElementById('div_video_area');
  }
  if (!video_title_id) {
    video_title_id = document.getElementById('div_video_title');
  }
  if (!flash_video_id) {
    flash_video_id = document.getElementById('flash_video');
  }
  if (!sel_area_id) {
    sel_area_id = document.getElementById('div_sel_area');
  }
  if (!info_area_id) {
    info_area_id = document.getElementById('div_info_area');
  }
  if (!zoom_on_id) {
    zoom_on_id = document.getElementById('zoom_on');
  }
  if (!zoom_off_id) {
    zoom_off_id = document.getElementById('zoom_off');
  }

  if (aZ) {
    cssjs('add',video_area_id,'video_area_big');
    cssjs('remove',video_area_id,'video_area');
    cssjs('add',video_title_id,'video_title_big');
    cssjs('remove',video_title_id,'video_title');
    flash_video_id.height = 600;
    flash_video_id.width = 930;
    sel_area_id.style.display = 'none';
    info_area_id.style.display = 'none';
    zoom_on_id.style.display = 'block';
    zoom_off_id.style.display = 'none';
    mZoom = true;
  } else {
    cssjs('remove',video_area_id,'video_area_big');
    cssjs('add',video_area_id,'video_area');
    cssjs('remove',video_title_id,'video_title_big');
    cssjs('add',video_title_id,'video_title');
    video_title_id.width = 470;
    flash_video_id.height = 310;
    flash_video_id.width = 480;
    sel_area_id.style.display = 'block';
    info_area_id.style.display = 'block';
    zoom_on_id.style.display = 'none';
    zoom_off_id.style.display = 'block';
    mZoom = false;
  }
}

function getZoom() {
  if (!mZoom) {
    if (!zoom_on_id) {
      zoom_on_id = document.getElementById('zoom_on');
    }
    mZoom = (zoom_on_id.style.display != 'none');
// alert('mZoom = ' + mZoom);
  }
  return mZoom;
}