/** 
 **  Copyright (c) 2008 Snapvine, LLC. All rights reserved.
 **
 ** THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF Snapvine, LLC
 **    The copyright notice above does not evidence any
 **    actual or intended publication of such source code.
 **/


/** JavaScript for a Voice Drops
 **/



var $g_session_id = null;
var $g_box_guid = null;
var $g_vd_flow = new String();
var $g_vd_ref = new String();
var $g_vd_action = new String();
var $g_vd_audio_recorded = false;
var $g_vd_codes_copied = false;
var $g_vd_codes_posted = false;
var $g_vd_secure_api = false;

function vd_init(referrer, flow, action)
{
    $g_vd_ref = referrer;
    if (referrer == null || referrer == 'undefined' || referrer.empty())
        $g_vd_ref = "no_referrer";
    
    $g_vd_flow = flow;
    $g_vd_action = action;
}

function vd_partner_security_init(appserver, service_path)
{
    $g_vd_appserver = appserver;
    $g_vd_service_path = service_path;
    $g_vd_secure_api = true;
}

function safe_vd_track(state)
{
    var loc = "/voice_drop"
    loc += "/ref_" + $g_vd_ref;
    loc += "_flow_" + $g_vd_flow;
    loc += "/" + $g_vd_action + '_state_' + state;
    safeUrchinTracker(loc);
}

function vd_track_post_codes(size,site)
{
  safe_vd_track('get_codes_inserted?size='+size+'&site='+site);
  if (!$g_vd_codes_posted) {
    safe_vd_track('get_codes_posted?size='+size+'&site='+site);
    $g_vd_codes_posted = true;
  }
}
function vd_track_copy_codes(size,site)
{
  if (!$g_vd_codes_posted) {
    safe_vd_track('get_codes_copied?size='+size+'&site='+site);
    safe_vd_track('get_codes_posted?size='+size+'&site='+site);
    $g_vd_codes_posted = true;
  }
}

function vd_set_pimpsite_img(img_url)
{
    $pimp_image = new Image();
    $pimp_image.src = img_url;
}

function vd_set_sessionid(id)
{
    $g_session_id = id;
}

function vd_updatedata(title, photo, on_complete, account_id, image_url)
{
    if (title.length == 0)
        title = " ";
    if (photo == "")
        photo = 'none';

    var data = { title: title, photo: photo, session: $g_session_id,
                 account_id: account_id, image_url: image_url };
    var urn = '/voicedrop/create_data';
    
    if ($g_box_guid != null)
        urn = "/voicedrop/update/" + $g_box_guid;
    $.get(urn, data, function(data){ on_vdupdate(data, on_complete); }, "json");
}

function on_vdupdate(json_data, on_complete)
{
  if (json_data.status == "failed")
      return;
    
  if ($g_box_guid == null) 
      $g_box_guid = json_data.guid;
  
  on_complete = on_complete || Prototype.emptyFunction;
  on_complete(json_data);
}


function vd_save_partner_voice_drop(account_id, external_guid, image_width, callback_url, align, theme, show_share, on_complete)
{
    
    if ($g_vd_secure_api)
    {
        save_callback();
    }
    else
    {
        var data = {
          account_id: account_id,
          external_guid: external_guid,
          w: image_width,
          align: align,
          callback_url: callback_url,
          theme: theme,
          show_share: show_share
        };
        var urn = '/talking_photo_api/save/' + $g_box_guid;
        var callback = on_complete;
        $.post(urn, data, function(data){ on_vd_save_partner_voice_drop(data, callback); }, "json");
    }
}

function on_vd_save_partner_voice_drop(json_data, on_complete)
{
  if (json_data.status == "failed") {
      return;
  }
    
  on_complete = on_complete || Prototype.emptyFunction;
  on_complete(json_data);
}

function vd_delete_partner_voice_drop(account_id, external_guid, on_complete)
{
    
    if ($g_vd_secure_api)
    {
        delete_callback();
    }
    else
    {
        var data = { account_id: account_id, external_guid: external_guid };
        var urn = '/talking_photo_api/delete/' + $g_box_guid;
        var callback = on_complete;
        
        $.post(urn, data, function(data){ on_vd_delete_partner_voice_drop(data, callback); }, "json");
    }

}

function on_vd_delete_partner_voice_drop(json_data, on_complete)
{
    if (json_data.status == "failed") {
        return;
    }
    on_complete = on_complete || Prototype.emptyFunction;
    on_complete(json_data);
}


function vd_loadpreview(img_url, img_width, create_url, on_complete)
{
    on_complete = on_complete || Prototype.emptyFunction;
    $.getJSON("/voicedrop/get_preview_codes/" + $g_box_guid,
              { img_url: img_url,
                img_width: img_width,
                create_url: create_url
              },
              function(data) { on_complete(data); }
           );
}
