if (typeof seesmic == 'undefined') {
  var seesmic = {}; 
}

var swfUrl =  "http://seesmic.com/embeds/wrapper.swf"

 
seesmic.player = new function () {
  var _this = this;
  this.recorder;
  this.ready = false;
  this.getRecorder = function () {
    return document.getElementById('seesmicrecorder');
  };
  this.domLoaded = function () {
    return !!document.getElementById('seesmicrecorder');
  };
  this.setError = function (error) {
    this.getRecorder().setError(error);
  };
  this.playerLoaded = function () {
    this.ready = true;
    this.recorder = document.getElementById('seesmicrecorder');
  };
  this.createSWF = function(type, id, containerNode, 
    urlName, video, closable, startVideo, path) {
    var html = '';

    // Player obj settings
    var w = 300;
    var h = 270;
      var params = {
        'quality': 'high',
        'bgcolor': '#000000',
        'allowFullScreen': 'true',
        'allowScriptAccess': 'always',
        'wmode': 'transparent',
        'flashVars': 'version=' + type + '&urlName=' + urlName + '&video=' + video +
          '&closable=' + closable + '&startVideo=' + startVideo + '&allowAnonymous=1'
          
      };

    // Create the markup for the player
    if (document.all) {
      html = '<object' +
      ' id="' + id + '"' +
      ' classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"' +
      ' width="' + w + '"' +
      ' height="' + h + '"' +
      ' codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">' +
      '  <param name="movie" value="' + swfUrl + '" />';
      for (var i in params) {
        html += '  <param name="' + i + '" value="' + params[i] + '" />';
      }
      html += '</object>';
    }
    else {
      html = '<embed' +
        ' id="' + id + '"' +
        ' src="' + swfUrl + '"' +
        ' width="' + w + '"' +
        ' height="' + h + '"' +
        ' play="true"' +
        ' loop="false"' +
        ' type="application/x-shockwave-flash"' +
        ' pluginspage="http://www.adobe.com/go/getflashplayer"';
      for (i in params) {
        html += ' ' + i + '="' + params[i] + '"';
      }
      html +='></embed>';
    }
    containerNode.innerHTML = html;
  }
  this.createPlayer = function (containerNode, video,
    closable, startVideo) {
    seesmic.player.createSWF('player',
      'seesmicplayer' + video, containerNode, '', video,
      closable, startVideo);
  }
  this.createRecorder = function (containerNode, urlName, video) {
      seesmic.player.createSWF('recorder', 'seesmicrecorder',
      containerNode, urlName, video, '', '')
  };
};

function seePlayVideo(videoUri, add) {
  var flashcontentDiv = document.getElementById(videoUri+"_content");
  var previewDiv = document.getElementById(videoUri+"_preview");
  var hideDiv = document.getElementById(videoUri+"_hide");
  var showDiv = document.getElementById(videoUri+"_show");
  if(add) {
    // push flash widget
    /*var so = new SWFObject(  swf, "sotester", "300", "270", "9", "#000000");
    so.addVariable("video", videoUri);
    so.addVariable("blog_id", blog_id);
    so.addVariable("blog_name", blog_name);
    so.addParam("allowFullScreen", "true");
    so.addParam("allowScriptAccess","always");
    so.useExpressInstall('swfobject/expressinstall.swf');
    so.write(videoUri+"_content");*/
    seesmic.player.createPlayer(flashcontentDiv,
      videoUri, true, true)
    previewDiv.style.display="none";
    hideDiv.style.display="block";
    showDiv.style.display="none";
  }
  else {
    flashcontentDiv.innerHTML="";
    previewDiv.style.display="block";
    hideDiv.style.display="none";
    showDiv.style.display="block";
  }
}

// =================================
// A bunch of basic cookie functions
// =================================
function setCookieStrig (cookie_string) {
  document.cookie = cookie_string;
  //alert("c:" + document.cookie + " " + cookie_string);
}

// SET COOKIE
function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure )
{
  var cookie_string = name + "=" + escape ( value );

  if ( exp_y )
  {
    var expires = new Date ( exp_y, exp_m, exp_d );
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if ( path )
        cookie_string += "; path=" + escape ( path );

  if ( domain )
        cookie_string += "; domain=" + escape ( domain );
  
  if ( secure )
        cookie_string += "; secure";
  
  document.cookie = cookie_string;
}


// GET COOKIE
function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );

  if ( results )
    return ( unescape ( results[1] ) );
  else
    return null;
}


// DELETE COOKIE
function delete_cookie ( cookie_name )
{
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}



// When the video is uploaded:
// 1.- Hide the recorder
// 2.- Send the information to the application, so the comment is recorded
function videoFromRecorder(videoUri, title, url_thumbnail, recState, hasTitle)
{
  document.getElementById('recorderHolder').style.display = 'none';
  document.getElementById("video_comment_video_uri").value = videoUri;
  document.getElementById("video_comment_thumbnail").value = url_thumbnail;
  document.getElementById("video_comment_text").value = title;
  if (is_logged_in()) {
    document.getElementById("new_video_comment").submit();
  } else {
    window.log_in_successful = function() {
      document.getElementById("new_video_comment").submit();
    }
    tb_show("Login for video", '#TB_inline?height=210&width=640&inlineId=loginBox');
  }
}
