//
// Copyright (c) 2008 Seesmic, llc
// All Rights Reserved
//
// License information is in the LICENSE.txt file
// 

/**
* javascripts for callbacks
*/
if (typeof $ == 'undefined') {
  var $ = function (s){
    return document.getElementById(s);
  }
}
if (typeof seesmic == 'undefined') {
  var seesmic = {}; 
}

seesmic.player = new function () {
  var _this = this;
  this.recorder;
  this.ready = false;
  this.getRecorder = function () {
    return $('sobject');
  };
  this.domLoaded = function () {
    return !!$('sobject');
  };
  this.setError = function (error) {
    this.getRecorder().setError(error);
  };
  this.playerLoaded = function () {
    this.ready = true;
    this.recorder = $('sobject');
  };

};




function setVars(data) {
	//recorderVersion = data.wprecorder;
	//playerVersion = data.wpplayer;				
}

wrapperVersion  = "http://seesmic.com/embeds/wrapper.swf"

function videoComment() {
	f = document.createElement('div');
	f.setAttribute('id', 'flashcontent');
	document.getElementById('comment-text').parentNode.insertBefore(f, document.getElementById('comment-text'));
	
	allow_anonymous = allow_anonymous == 'on' ? 1 : 0;
	
	var so = new SWFObject(wrapperVersion, "sobject", "300", "270", "9", "#000000");
	so.addVariable("title", '');
	so.addVariable("blog_id", blog_id);
	so.addVariable("version", "recorder");
	so.addVariable("allowAnonymous", allow_anonymous);
	so.addVariable("blog_name", blog_name);
	so.addParam("allowScriptAccess","always");
	so.write("flashcontent");
	document.getElementById('video_click').style.display = 'none';
	cancelButton = document.createElement('button');
	cancelButton.id = 'video_cancel_button';
	cancelButton.onclick = function() {
		cancelVideoComment();
		return false;
	}
	cancelButton.appendChild(document.createTextNode('Cancel'));
	document.getElementById('video_click').parentNode.insertBefore(cancelButton, document.getElementById('video_click'));
	if (document.getElementById('comments-open-footer')) {
		document.getElementById('comments-open-footer').style.display = 'none';
	}
	if (document.getElementById('comment-text')) {
		document.getElementById('comment-text').style.display = 'none';
	}
}

function cancelVideoComment() {
	document.getElementById('flashcontent').parentNode.removeChild(document.getElementById('flashcontent'));
	document.getElementById('video_cancel_button').parentNode.removeChild(document.getElementById('video_cancel_button'));
	document.getElementById('video_click').style.display = 'block';
	if (document.getElementById('comments-open-footer')) {
		document.getElementById('comments-open-footer').style.display = 'block';
	}
	if (document.getElementById('comment-text')) {
		document.getElementById('comment-text').style.display = 'block';
	}
}

	// this is a built in callback from flash
function videoFromRecorder(videoUri, title, url_thumbnail) {
 


var formExist = false;
if(document.all) formExist = document.getElementById('comment-form-name').style.display != 'none'
else formExist = document.getElementById('comment-form-name').style.getPropertyValue('display') != 'none'

 	if(formExist)
 	
 	{
 	
 	
 	 
    if(!! document.getElementById('comment-author'))
    {
    	if(document.getElementById('comment-author').value.length < 1)
    	{
    		 
    		seesmic.player.setError('please enter name and email address in the comments form and click ok')
    		return
    	}
    }
    
    if(!! document.getElementById('comment-email'))
    {
    	if(document.getElementById('comment-email').value.length < 1)
    	{
    		//alert('author is required')
    		seesmic.player.setError('please enter name and email address in the comments form and click ok')
    		return
    	}
    }
    }
    
	video_uri = document.createElement('input');
	video_uri.type = 'hidden';
	video_uri.name = 'seesmic_url';
	video_uri.value = videoUri;
	document.forms['comments_form'].appendChild(video_uri);
	
	video_title = document.createElement('input');
	video_title.type = 'hidden';
	video_title.name = 'seesmic_title';
	video_title.value = title;
	document.forms['comments_form'].appendChild(video_title);
	
	video_thumb = document.createElement('input');
	video_thumb.type = 'hidden';
	video_thumb.name = 'seesmic_thumb';
	video_thumb.value = url_thumbnail;
	document.forms['comments_form'].appendChild(video_thumb);
	if (document.getElementById('comment-text').value.length < 1) {
		document.getElementById('comment-text').value = "[seesmic placeholder]";
	}	
	document.forms['comments_form'].submit();
}

function see_play_video(videoID) {

this['st' + videoID]  = 0

	document.getElementById(videoID).removeChild(document.getElementById(videoID).firstChild);
	document.getElementById(videoID).style.width = "160px";
	document.getElementById(videoID).style.height = "120px";
	
	document.getElementById(videoID).innerHTML =  
	document.getElementById(videoID).oldHTML;
}

function videoPlay(targetNode, videoID) {

	
	if(this['st' + videoID] == 1)
	{
		see_play_video(videoID)
		return
	}
	this['st' + videoID]  = 1
	
	targetNode.oldHTML = targetNode.innerHTML;
 
	
	targetNode.style.width = "300px";
	targetNode.style.height = "270px";
	var so = new SWFObject(wrapperVersion, "sotester", "300", "270", "9", "#000000");
	so.addVariable("video", videoID);
	so.addVariable("version", "player");
	so.addVariable("closable", "true");
	so.addVariable("startVideo", "true");
	so.addParam("allowFullScreen", "true");
	so.addParam("allowScriptAccess","always");
	so.useExpressInstall('swfobject/expressinstall.swf');
	so.write(targetNode);
}



/**
* Cookies Management 
**/


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

