// ================================================================
// Configuration needs to be changed
// to the one provided by Moberia
var SIP_REALM = "88.198.37.58";

var customer_key = "93274663986302";
var destination = "skype-tlc-polska" + "@" + SIP_REALM;


// please contact info@moberia.com for more info
// ================================================================





// ================================================================
// Configuration parameters below should not be changed !!
// provided by Moberia

var creds = { 
  "phoneno": customer_key,
  "username": "webphone",
  "password": "webphone",
  "realm": SIP_REALM,
  "server": "88.198.37.58:5061",
  "host": "moberia.com",
  "path": "/vphone"
};

// this is for example purposes only
// in real deployment you need to insert proper number
var call_num = destination;

// ================================================================

function loginPhone(creds)
{
  $("#vphone").login(
    creds["phoneno"], 
    creds["username"], 
    creds["password"], 
    creds["realm"], 
    creds["server"],
    creds["host"], 
    creds["path"]
  );
}

function skypeFix()
{
  $("#__skype_highlight_id").html("");
  $("#__skype_highlight_originaltext_id").removeClass("__skype_highlight_originaltext_id");
  $(".skype_pnh_print_container").removeClass("skype_pnh_print_container");
  $(".skype_pnh_container").html("");
}

function showCallLink()
{
  $("#callLink").addClass("clickable");
  skypeFix();
  $("#callLink").click(call);
}

function hideCallLink()
{
  $("#callLink").removeClass("clickable");
  skypeFix();
  $("#callLink").unbind('click', call);
}

function call(e)
{
  $("#vphone").call(call_num);
}

function disconnect(e)
{
  $("#vphone").hangup(); 
}

function connected()
{
  hideCallLink();
  $("#callLink").text("Rozłącz").addClass("clickable").click(disconnect);
  
}

function disconnected()
{
  $("#callLink").text("Zadzwoń teraz").removeClass("clickable").unbind('click', disconnect);
  showCallLink();
}

function showSettings()
{
  var left = $("#phone").offset().left;
  $("#vphone").settings();
  left = parseInt(left, 10);
  $("#vphone").css("left", left);

  try{
    if ($.browser.mozilla) {
      $(window).resize(function() { $("#vphone").css('margin-left', window.outerWidth % 2 ? '0' : '0.5px') } ).resize();
    }
  }catch(ex){}

}

function hideSettings()
{
  $("#vphone").css("left", "-9999px");
}

function setupPhone()
{
  skypeFix();

  $("#vphone").vPhone({
    position: "absolute",
    swfPath: "flash",
    ready: function() {
      loginPhone(creds);
      skypeFix();
    },
    reg: function() {
      showCallLink();
    },
    noServer: function(msg) {
      hideCallLink();
      window.setTimeout(function() { loginPhone(creds) }, 5000);
    },
    status: function(msg) {
      skypeFix();
      $("#log").append(msg+"<br>");
	  //console.log(msg);
    },
    incoming: function(src) {
    },
    disconnect: function() {
      disconnected();
    },
    connect: function() {
      connected();
    },
    micStatus: function(muted) {
      if (muted)
        window.setTimeout(function() { showSettings() }, 3000);
      else
        window.setTimeout(function() { hideSettings() }, 3000);
    }
  });

  skypeFix();
}

function setupPhones()
{
  var playerVersion = swfobject.getFlashPlayerVersion();

  if (playerVersion.major < 9 || playerVersion.release > 990)
  {
    return;
  }

  setupPhone();
}

$(document).ready(setupPhones);

