﻿///<reference path="mootools.js" />

function MovieDownload() { }

MovieDownload.sceneId = 0;
MovieDownload.screenShotLength = 0;
MovieDownload.screenShotFileName = '';
MovieDownload.playSelectionWmvUrlFormat = '';
MovieDownload.playSelectionFlashUrlFormat = '';
MovieDownload.downloadSelectionUrlFormat = '';
MovieDownload.canOpenNewWindow = false;

MovieDownload.selectionStart = 0;
MovieDownload.selectionEnd = 0;
MovieDownload.selectionStartIndex = 0;
MovieDownload.selectionEndIndex = 0;

MovieDownload.cells = null;

MovieDownload.init = function() {

   var allCells = $$("#framesTable td");

   var cells = allCells.filter(function(item, index) {
      return index <= MovieDownload.selectionEndIndex;
   });

   //if (cells.length - 1 != MovieDownload.selectionEndIndex)
   //   alert("cells.length (" + cells.length + ") - 1 != MovieDownload.selectionEndIndex (" + MovieDownload.selectionEndIndex + ")");

   MovieDownload.cells = cells;

   MovieDownload.checkStart(0);
   MovieDownload.checkEnd(MovieDownload.selectionEndIndex);
};

MovieDownload.setStart = function(index, pos, screenShot) {

   //var start = ((index + 1) * MovieDownload.screenShotLength) - MovieDownload.screenShotLength;
   var start = pos;

   if (start >= MovieDownload.selectionEnd) {

      MovieDownload.uncheckStart(index);
      MovieDownload.checkStart(MovieDownload.selectionStartIndex);

      alert("Please select a valid time frame.");
      return;
   }

   MovieDownload.selectionStart = start;
   MovieDownload.selectionStartIndex = index;

   $("startThumb").src = MovieDownload.getScreenShotUrl(screenShot);
   MovieDownload.refreshUI();

   //alert('start: ' + MovieDownload.selectionStart + ' end: ' + MovieDownload.selectionEnd);
};

MovieDownload.setEnd = function(index, pos, screenShot) {

   //var end = (index + 1) * MovieDownload.screenShotLength;
   var end = pos;

   if (end <= MovieDownload.selectionStart) {

      MovieDownload.uncheckEnd(index);
      MovieDownload.checkEnd(MovieDownload.selectionEndIndex);

      alert("Please select a valid time frame.");
      return;
   }

   MovieDownload.selectionEnd = end;
   MovieDownload.selectionEndIndex = index;

   $("endThumb").src = MovieDownload.getScreenShotUrl(screenShot);
   MovieDownload.refreshUI();

   //alert('start: ' + MovieDownload.selectionStart + ' end: ' + MovieDownload.selectionEnd);
};

MovieDownload.getScreenShotUrl = function(pos) {

   var strPos = pos + "";
   var length = strPos.length;
   
   if (length < 4) {
      for (i = 1; i <= 4 - length; i++)
         strPos = "0" + strPos;
   }

   return "http://content.olderwomenvideos.com/ScreenShots/" + MovieDownload.screenShotFileName + "/" + MovieDownload.screenShotFileName + "_" + strPos + ".jpg";
};

MovieDownload.checkStart = function(index) {
   MovieDownload.cells[index].getElements('input[name=rStart]')[0].checked = true;
};

MovieDownload.checkEnd = function(index) {
   MovieDownload.cells[index].getElements('input[name=rEnd]')[0].checked = true;
};

MovieDownload.uncheckStart = function(index) {
   MovieDownload.cells[index].getElements('input[name=rStart]')[0].checked = false;
};

MovieDownload.uncheckEnd = function(index) {
   MovieDownload.cells[index].getElements('input[name=rEnd]')[0].checked = false;
};

MovieDownload.refreshUI = function() {

   MovieDownload.cells.each(function(item, i) {
      if (i < MovieDownload.selectionStartIndex) {
         item.removeClass("selected");
      } else if (i >= MovieDownload.selectionStartIndex && i <= MovieDownload.selectionEndIndex) {
         item.addClass("selected");
      } else if (i > MovieDownload.selectionEndIndex) {
         item.removeClass("selected");
      }
   });

   $("startTime").innerHTML = MovieDownload.formatTime(MovieDownload.selectionStart);
   $("endTime").innerHTML = MovieDownload.formatTime(MovieDownload.selectionEnd);
   $("selectionLength").innerHTML = MovieDownload.formatTime(MovieDownload.selectionEnd - MovieDownload.selectionStart);
   
   MovieDownload.refreshSelectionLinks();
};

MovieDownload.refreshSelectionLinks = function() {
   var settings = JSON.decode($('selectionFormat').value);

   var playUrl = '';
   var downloadUrl = '';
   var downloadVisibility = '';
   var target = '';

   if (settings.format == 'wmv') {
      playUrl = MovieDownload.playSelectionWmvUrlFormat.substitute({
         start: MovieDownload.selectionStart,
         end: MovieDownload.selectionEnd,
         quality: settings.quality
      });

      downloadUrl = MovieDownload.downloadSelectionUrlFormat.substitute({
         start: MovieDownload.selectionStart,
         length: (MovieDownload.selectionEnd - MovieDownload.selectionStart),
         quality: settings.quality
      });
      downloadVisibility = 'visible';
      target = '_blank';

   } else { // flash
      playUrl = MovieDownload.playSelectionFlashUrlFormat.substitute({
         start: MovieDownload.selectionStart,
         length: (MovieDownload.selectionEnd - MovieDownload.selectionStart),
         quality: settings.quality
      });

      downloadUrl = "javascript:MovieDownload.alertWrongDownloadFormat();";
      downloadVisibility = 'hidden';
      target = '_self';
   }

   $('playSelection')
      .set('href', playUrl)
      .set('target', MovieDownload.canOpenNewWindow ? target : '_self');

   var dlselbtn = $('downloadSelection');

   if (dlselbtn) {
      dlselbtn.set({
         'href': downloadUrl,
         'styles': {
            'visibility': downloadVisibility
         }
      });
   }
};

MovieDownload.alertWrongDownloadFormat = function() {
   alert("Download is only available in WMV format, please change your 'Format/Quality' option.");
   $('selectionFormat').focus();
};

MovieDownload.openFlashPlayer = function(url, quality) {

   $('scenethumbs').set('styles', { 'display': 'none' });

   var width = 0, height = 0;

   switch (quality) {
      case '256k':
      case '512k':
         width = 400;
         height = 300;
         break;
      case '768k':
      case '1300k':
         width = 640;
         height = 480;
         break;
      case '2000k':
         width = 720;
         height = 405;
         break;
   }

   // iframe mode

   $('playerIFrame').set({
      'src': url,
      'width': width,
      'height': height,
      'styles': {
         'display': ''
      }
   });

   $('flashPlayer').set({
      'styles': {
         'display': '',
         'width': width + 'px',
         'height': height + 'px'
      }
   });

   window.location.href = "#title";

   // swf mode

   /*swfobject.embedSWF(url, 'playerSwf', '320', '240', '9', null, {}, 
   { // params
   quality: 'high',
   bgcolor: '#869ca7',
   allowScriptAccess: 'sameDomain',
   allowFullscreen: 'true'
   }
   );*/

   // popup window mode

   //window.open(url, '', 'menubar=0,location=0,scrollbars=1,resizable=1');
};

MovieDownload.formatTime = function(seconds) {
   
   var minutes = Math.floor(seconds / 60);
   
   if (minutes == 0) {
      return "" + seconds + " seconds";
   } else {
      seconds = seconds % 60;
      minutesStr = "" + minutes;
      secondsStr = "" + seconds;
      if (seconds < 10)
         secondsStr = "0" + secondsStr;
      result = minutesStr + ":" + secondsStr + " minutes";
      return result;
   }
};