

var CRLF = "\r\n";

function TPictURL() {
	this.initialized = false;
	return this;
}

// pass in the json data, the id names of the url, html, bbcode, and preview image
TPictURL.prototype.initialize = function(aPictId, aInviteID, aIsVideo, aOrigWidth, aOrigHeight, aWebWidth, aWebHeight, aHostName, aExtension,
		aWmvExists, aX264Exists, aURLFieldName, aHTMLFieldName, aBBCodeFieldName, aPreviewImageName) {
	
  this.pictureId = aPictId;
	this.inviteId = aInviteID;
	this.isVideo = aIsVideo;
	this.origWidth = aOrigWidth;
	this.origHeight = aOrigHeight;	
	this.webWidth = aWebWidth;
	this.webHeight = aWebHeight;
	this.hostName = aHostName;
	this.origExtension = aExtension;
	this.wmvExists = aWmvExists;
        this.x264Exists = aX264Exists;
	
	this.systemURL = "";
	
	this.URLFieldName = aURLFieldName;
	this.HTMLFieldName = aHTMLFieldName;
	this.BBCodeFieldName = aBBCodeFieldName;
	this.URLField = document.getElementById(this.URLFieldName);
	this.HTMLField = document.getElementById(this.HTMLFieldName);
	this.BBCodeField = document.getElementById(this.BBCodeFieldName);	
	
	if (aPreviewImageName != "") {
    var ImgElem = document.getElementById(aPreviewImageName);
		if (ImgElem) ImgElem.src = this.previewThumbnail();		
	}
	
	
	this.initialized = true;
}


// pass in the json data, the id names of the url, html, bbcode, and preview image
TPictURL.prototype.initializeFromJSON = function(metadatastr, aURLFieldName, aHTMLFieldName, aBBCodeFieldName, aPreviewImageName) {
	// parse it
  var metadata = metadatastr.parseJSON();
	
  this.pictureId = metadata.PictId;
	this.inviteId = metadata.InviteId;
	this.isVideo = metadata.IsVideoB;
	this.origWidth = metadata.OrigWidth;
	this.origHeight = metadata.OrigHeight;	
	this.webWidth = metadata.StdWidth;
	this.webHeight = metadata.StdHeight;
	this.hostName = metadata.HostName;
	this.origExtension = metadata.Ext;
	this.wmvExists = metadata.wmvExists;
        this.x264Exists = metadata.x264Exists;
	this.systemURL = metadata.Url;
	
	this.URLFieldName = aURLFieldName;
	this.HTMLFieldName = aHTMLFieldName;
	this.BBCodeFieldName = aBBCodeFieldName;
	this.URLField = document.getElementById(this.URLFieldName);
	this.HTMLField = document.getElementById(this.HTMLFieldName);
	this.BBCodeField = document.getElementById(this.BBCodeFieldName);	
	
	if (aPreviewImageName != "") {
    var ImgElem = document.getElementById(aPreviewImageName);
		if (ImgElem) ImgElem.src = this.previewThumbnail();		
	}
	
	
	this.initialized = true;
}

// this refreshes the edit fields with the right values 
// based upon the selected size
TPictURL.prototype.refreshFields = function() {
	if (this.URLField) this.URLField.value = this.getURL();
	if (this.HTMLField) this.HTMLField.value = this.getHTML();
	if (this.BBCodeField) this.BBCodeField.value = this.getBBCode();	
}

TPictURL.prototype.getCurrentSizeURL = function() {
	var sizeCode = this.parent.getCurrentSizeCode();
	if (sizeCode == "S") {
		
		if (this.isVideo) {
                  if (this.wmvExists) 
                    return("conv")
                  else 
                    return("orig");				
                 } else {
  		return("standard");
		}
		
	}

	if (sizeCode == "X") {
		
		if (this.isVideo) {
                        if (this.x264Exists) 
                          return("convx264")
                        else
                        {
			  if (this.wmvExists) 
                            return("conv")
                          else 
                            return("orig");				
                        }
		} else {
  		return("standard");
		}
		
	}

	if (sizeCode == "O") return("orig");
	if (sizeCode == "T") return("medium");
}

TPictURL.prototype.getCurrentExtension = function() {
	var sizeCode = this.parent.getCurrentSizeCode();
	if (sizeCode == "S") {
		if (this.isVideo) {
                  if (this.wmvExists) return(".wmv")
                  else return(this.origExtension);
		} else {
  		return(".jpg");
		}
	}

	if (sizeCode == "X") {
		if (this.isVideo) {
                  if (this.x264Exists) return(".mp4")
                  else return(this.origExtension);
		} else {
  		return(".jpg");
		}
	}

	if (sizeCode == "O") {
           return(this.origExtension);
        }
	if (sizeCode == "T") return(".jpg");
}


TPictURL.prototype.getURL = function() {
  return("http://" + this.hostName + "/" + this.inviteId + "/" + this.getCurrentSizeURL() + this.getCurrentExtension());
}

TPictURL.prototype.getX264URL = function() {
  return("http://" + this.hostName + "/" + this.inviteId + "/convx264.mp4");
}



TPictURL.prototype.getThumbHref = function() {
 return("http://" + this.hostName + "/pictinv/" + this.inviteId);
}

TPictURL.prototype.getHTML = function() {
  var html = "";
  var sizeCode = this.parent.getCurrentSizeCode();
    if (this.isVideo)  {
          var width = 320;
	  var height = 240;
	  if (this.useWindowsMediaPlayer()) {			
            if (sizeCode == "O") {
	  		width = this.origWidth;
		  	height = this.origHeight;
  		} else {
	  		width = this.webWidth;
		  	height = this.webHeight;			
  		}
          }
		
          // add on space for the controller
         if (this.useQuickTime()) height += 20;
         if (this.useWindowsMediaPlayer()) height += 100;

         // check which player to use by video extension
         if (sizeCode == 'X')
         {
           html += this.getx264Html()
         
         }
         
         if (sizeCode == 'O')
         {
           html += '<a href="' + this.getURL() + '"><img src="http://' + this.hostName + '/' + this.inviteId + '/thumb.jpg"></a>';
         }

         if (sizeCode == 'S')
         {
             if (this.useQuickTime()) {
               html += '<embed type="video/quicktime" controller="true" scale="aspect" autoplay="false" src="' + this.getURL() + '" width="' + width + '" height="' + height + '">';			
             } else {
                html += '<EMBED TYPE="application/x-mplayer2" SRC="' + this.getURL() + '" WIDTH="' + width + '" HEIGHT="' + height + '" AUTOSTART="0" SHOWCONTROLS="1"></EMBED>';
             }
         }
  }
  else {
    if (sizeCode == "T")
      html += '<a href="' + this.getThumbHref() + '"><img src="' + this.getURL() + '" border=0 alt="Hosting provided by FotoTime"></a>'
    else
      html += '<img src="' + this.getURL() + '" border=0 alt="Hosting provided by FotoTime">';
   }
   return(html);
}


TPictURL.prototype.useQuickTime = function() {
	var uurl = this.getURL().toUpperCase();
	var isqt = ((uurl.indexOf(".MOV") > 0) || (uurl.indexOf(".MP4") > 0));	
	return(isqt);
}

TPictURL.prototype.useWindowsMediaPlayer = function() {
	var uurl = this.getURL().toUpperCase();
	var isqt = ((uurl.indexOf(".MOV") > 0) || (uurl.indexOf(".MP4") > 0));	
	return(!isqt);	
}

TPictURL.prototype.usex264Player = function() {
    if (this.x264Exists) 
      return(true)
    else
      return(false);
}

TPictURL.prototype.getx264Html = function() {
  var html = '<div id=playerDiv style="width:640px;height:480px"></div>';
  html += '<script src="http://' + this.hostName + '/ftweb/scripts/jw_player/swfobject.js"></script>';
  html += '<script>';
  html += '   var so = new SWFObject("http://' + this.hostName + '/ftweb/scripts/jw_player/player-licensed.swf", "ftvideo", "640px", "480px", "9", "#000000");';
  html += '   so.addVariable("file","' + this.getX264URL() + '");';
  html += '   so.addVariable("stretching", "none");';
  html += '   so.addVariable("smoothing", "true");';
  html += '   so.addVariable("quality", "true");';
  html += '   so.addVariable("bufferlength", "4");';
  html += '   so.addParam("allowfullscreen", "true");';
  html += '   so.addParam("wmode", "opaque");';
  html += '   so.addParam("allowscriptaccess", "always");';
  html += 'so.write("playerDiv");';
  html += '</script>';
  return(html);
}  



TPictURL.prototype.getBBCode = function() {
	var html = "";
	var sizeCode = this.parent.getCurrentSizeCode();
	var origOrStd = ((sizeCode == "O") || (sizeCode == "S") || (sizeCode == "X"));
	if ((this.isVideo) && (origOrStd)) {

    html += '[url=' + this.getURL() + '][img]http://' + this.hostName + '/' + this.inviteId + '/thumb.jpg[/img][/url]';
		return(html);
		
    var width = 320;
		var height = 240;
		if (this.useWindowsMediaPlayer()) {			
      if (sizeCode == "O") {
	  		width = this.origWidth;
		  	height = this.origHeight;
  		} else {
	  		width = this.webWidth;
		  	height = this.webHeight;			
  		}
		}
		
		// add on space for the controller
		if (this.useQuickTime()) height += 20;
		if (this.useWindowsMediaPlayer()) height += 100;

		html += '[MEDIA="' + width + ',' + height + ',false,0"]' + this.getURL() + '[/MEDIA]';
		
	} else {
    if (sizeCode == "T")
      html += '[url="' + this.getThumbHref() + '"][img]' + this.getURL() + '[/img][/url]'
    else
      html += '[img]' + this.getURL() + '[/img]';
	}
	return(html);
}

TPictURL.prototype.previewThumbnail = function() {
  return('/ftweb/bin/ft.dll/thumb?size=sm&pictid=' + this.pictureId);
}


TPictURL.prototype.debug = function() {
	var txt = "";
	txt += "Host:" + this.hostName + CRLF;	
	txt += "InviteId:" + this.inviteId + CRLF;
  txt += "PictureId:" + this.pictureId + CRLF;
	txt += "Orig Ext:" + this.origExtension + CRLF;
	txt += "isVideo:" + this.isVideo + CRLF;
	txt += "Orig Width:" + this.origWidth + CRLF;
	txt += "Orig Height:" + this.origHeight + CRLF;	
	txt += "Web Width:" + this.webWidth + CRLF;
	txt += "Web Height:" + this.webHeight + CRLF;	
	txt += "Radio Selection:" + this.parent.getCurrentSizeValue() + CRLF;
	txt += "Size Code:" + this.parent.getCurrentSizeCode() + CRLF;
	txt += "System Generated URL:" + this.systemURL + CRLF;
	txt += "URL:" + this.getURL() + CRLF;
	txt += "HTML:" + this.getHTML() + CRLF;
	txt += "BBCode:" + this.getBBCode() + CRLF;	
	alert(txt);
}


// this is the array of TPictURL objects
// it is a container so that it can handle
// more than one url per screen
function TPictURLs() {
	this.items = new Array();
	this.initialized = false;
	return this;
}

TPictURLs.prototype.clear = function() {
	this.items = null;
	this.items = new Array();	
}


TPictURLs.prototype.add = function(picturl) {
  picturl.parent = this;
	picturl.ndx = this.items.length;
	this.items[this.items.length] = picturl;
  return picturl;
}

TPictURLs.prototype.newURL = function() {
	var item = new TPictURL();
	this.add(item);
	return(item);
}


TPictURLs.prototype.setRadioButton = function(sizefield) {
	this.sizeField = sizefield;
}

// this looks at radio button and returns a size code
// S = standard ,  O = orig,  T = thumb
TPictURLs.prototype.getCurrentSizeCode = function() {
      if (this.sizeField)
      {
	if (this.sizeField[0].checked) {
          if (this.sizeField[0].value == "x264") return("X");
	  if (this.sizeField[0].value == "Standard") return("S");
	  if (this.sizeField[0].value == "Orig") return("O");
	  if (this.sizeField[0].value == "Thumb") return("T");		
	}
	if (this.sizeField[1].checked) {
          if (this.sizeField[1].value == "x264") return("X");
	  if (this.sizeField[1].value == "Standard") return("S");
	  if (this.sizeField[1].value == "Orig") return("O");
	  if (this.sizeField[1].value == "Thumb") return("T");				
	}
	if (this.sizeField[2].checked) {
          if (this.sizeField[2].value == "x264") return("X");
	  if (this.sizeField[2].value == "Standard") return("S");
	  if (this.sizeField[2].value == "Orig") return("O");
	  if (this.sizeField[2].value == "Thumb") return("T");						
	}
	if (this.sizeField[3].checked) {
          if (this.sizeField[3].value == "x264") return("X");
	  if (this.sizeField[3].value == "Standard") return("S");
	  if (this.sizeField[3].value == "Orig") return("O");
	  if (this.sizeField[3].value == "Thumb") return("T");						
	}

      }
      else
       return("T");
}


TPictURLs.prototype.getCurrentSizeValue = function() {
	if (this.sizeField[0].checked) return(this.sizeField[0].value);
	if (this.sizeField[1].checked) return(this.sizeField[1].value);
	if (this.sizeField[2].checked) return(this.sizeField[2].value);	
}

TPictURLs.prototype.initializeLabels = function(stdname,origname,thumbname,stdspanname) {
	var stdfld = document.getElementById(stdname);
	var origfld = document.getElementById(origname);
	var thumbfld = document.getElementById(thumbname);
	var stdspan = document.getElementById(stdspanname);
        var x264span = document.getElementById("x264span");
        var thumbspan = document.getElementById("thumbspan");
	
	if (this.items.length != 1) return;
	
	var item = this.items[0];

        if (stdspan) {
	  if (item.isVideo) {
			if (item.wmvExists)  {
				stdspan.style.display = "inline"; 
			} else {
				stdspan.style.display = "none";
				this.sizeField[1].checked = true;
			}
		} else {
			stdspan.style.display = "inline";
		}
	}
       
        if (x264span) {
          if (item.isVideo) {
            if (item.x264Exists) {
              x264span.style.display = "inline";
              thumbspan.style.display = "none";
              this.sizeField[0].checked = true;
            }
            else {
              x264span.style.display = "none";
              thumbspan.style.display = "inline";
              this.sizeField[1].checked = true;
            }  
          }
          else {
            x264span.style.display = "none";
            thumbspan.style.display = "inline";
          }
        }
           

  if (item.isVideo) {
  	if (stdfld) stdfld.innerHTML = "Standard Quality - WMV";
  	if (origfld) origfld.innerHTML = "Original Uploaded Video";
  	if (thumbfld) thumbfld.innerHTML = "Thumbnail";	
	} else {
  	if (stdfld) stdfld.innerHTML = "Standard Size <font color=#88888888>- " + item.webWidth + "x" + item.webHeight;
  	if (origfld) origfld.innerHTML = "Original Size <font color=#88888888>- " + item.origWidth + "x" + item.origHeight ;
  	if (thumbfld) thumbfld.innerHTML = "Thumbnail <font color=#88888888>- 100x100";		
	}
}

TPictURLs.prototype.refreshFields = function() {
	var i = 0;
	while (i < this.items.length) {
		var a = this.items[i];
		a.refreshFields();
		i++;
	}
}

var pictURLs = new TPictURLs();

