var sxn_state = "p";
var sxn_first = 0;
var sxn_last = 0;
var layer_open = 0;
var sxn_lang = "en";
var sxn_maxposts = 25;
var sxn_maxchar = 140;
var sxn_timer;
var sxn_semaphore = true;


function Sxn_CheckSemaphore()
{
   return sxn_semaphore;
}

function Sxn_StartUpdate()
{
   sxn_semaphore = false;
   $("#updating").html("<img src=img/ajax-loader.gif>");
}

function Sxn_EndUpdate()
{
   $("#updating").html("");
   sxn_semaphore = true;
}

function Sxn_UpdatePosts(response, status)
{
   $("body").append(response);
   Sxn_GetPosts(sxn_state + "update");
   $("#f_to").attr("value","");
   $("#f_post").attr("value","");
   Sxn_CountChar();
   Sxn_EndUpdate();
}

function Sxn_WritePosts(data)
{
   $("#posts").html(data);
   if (sxn_state === "i")
      $("#newinbox").html("");
   Sxn_EndUpdate();
}

function Sxn_AppendPostsTop(data)
{
   $("#posts ul").prepend(data);
   if (sxn_state === "i")
      $("#newinbox").html("");
   while ($("#posts li").length > sxn_maxposts){
      $("#posts li:last").remove();   
   }
   pid = $("#posts li:last").attr("id");
   sxn_first = pid.substr(5);
   Sxn_EndUpdate();
}

function Sxn_AppendPostsBottom(data)
{
   $("#posts ul").append(data);
   np = $("#posts li").length;
   if (np > sxn_maxposts)
      sxn_maxposts = np;
   Sxn_EndUpdate();
}

function Sxn_GetPosts(s_action)
{
   if (sxn_semaphore){
      Sxn_StartUpdate();
      if ((s_action === "pshow") || (s_action === "ishow") || (s_action === "sshow") || (s_action === "fshow"))
         $.get("objserv.php?sid=" + sxn_sid + "&opt=post&action=" + s_action + "&first=" +sxn_first + "&last=" + sxn_last,Sxn_WritePosts);
      else
      if ((s_action === "pupdate") || (s_action === "iupdate") || (s_action === "supdate"))
         $.get("objserv.php?sid=" + sxn_sid + "&opt=post&action=" + s_action + "&first=" +sxn_first + "&last=" + sxn_last,Sxn_AppendPostsTop);
      else
      if ((s_action === "pmore") || (s_action === "imore") || (s_action === "smore") || (s_action == "fmore")) 
         $.get("objserv.php?sid=" + sxn_sid + "&opt=post&action=" + s_action + "&first=" +sxn_first + "&last=" + sxn_last,Sxn_AppendPostsBottom);
      }   
}

function Sxn_GotoHome()
{
   if (sxn_state === 'p')
      Sxn_GetPosts('pupdate');
   else{
      sxn_state = 'p';
      Sxn_GetPosts('pshow');
   }
}

function Sxn_GotoInbox()
{
   if (sxn_state === 'i')
      Sxn_GetPosts('iupdate');
   else{
      sxn_state = 'i';
      Sxn_GetPosts('ishow');
   }
}

function Sxn_GotoSent()
{
   if (sxn_state === 's')
      Sxn_GetPosts('supdate');
   else{
      sxn_state = 's';
      Sxn_GetPosts('sshow');
   }
}

function Sxn_GotoFav()
{
   if (sxn_state === 'f')
      return;
   else{
      sxn_state = 'f';
      Sxn_GetPosts('fshow');
   }
}

function Sxn_MorePosts()
{
   Sxn_GetPosts(sxn_state + "more");
}

function Sxn_Reply(id,poster)
{
   $("#f_to").attr("value",poster);
   $("#f_inresponse").attr("value",id);
   $("#f_post").focus();
   
}

function Sxn_EndAction(data)
{
   $("body").append(data);
   Sxn_EndUpdate();
}

function Sxn_SetFav(id)
{
   if (sxn_semaphore){
      Sxn_StartUpdate();
      $.get("objserv.php?sid=" + sxn_sid + "&opt=post&action=setfav&post=" + id,Sxn_EndAction);
   }
}

function Sxn_RmFav(id)
{
   if (sxn_semaphore){
      if (confirm(sxn_rmconf_msg)){
         Sxn_StartUpdate();
         $.get("objserv.php?sid=" + sxn_sid + "&opt=post&action=rmfav&post=" + id,Sxn_EndAction);
      }
   }
}

function Sxn_RmSent(id)
{
   if (sxn_semaphore){
      if (confirm(sxn_rmconf_msg)){
         Sxn_StartUpdate();
         $.get("objserv.php?sid=" + sxn_sid + "&opt=post&action=rmsent&post=" + id,Sxn_EndAction);
      }
   }
}

function Sxn_RmInbox(id)
{
   if (sxn_semaphore){
      if (confirm(sxn_rmconf_msg)){
         Sxn_StartUpdate();
         $.get("objserv.php?sid=" + sxn_sid + "&opt=post&action=rminbox&post=" + id,Sxn_EndAction);
      }
   }
}

function Sxn_SetLayer(data)
{
   tlayer = $("#targetlayer",data).attr("value");
   $("body").append("<div id=" + tlayer + " class=" +tlayer +"></div>");
   py = 130 + $(window).scrollTop();
   $("div#" + tlayer).append(data).css("top",py + "px").draggable({handle: '#layer-head'}).show();
   Sxn_EndUpdate();
}

function Sxn_UpdateLayer(data)
{
   tlayer = $("#targetlayer",data).attr("value");
   $("div#" + tlayer).html(data);
   $("div#" + tlayer).draggable({handle: '#layer-head'});
   Sxn_EndUpdate();
}

function Sxn_OpenLayer(option)
{
   if (layer_open)
      return;
      
   if (sxn_semaphore){
      layer_open = 1;
      Sxn_StartUpdate();
      $.get("objserv.php?sid=" + sxn_sid + "&opt=" + option + "&action=show",Sxn_SetLayer);
   }
}

function Sxn_ShowProfile(id,user)
{
   if (layer_open)
      return;
      
   if (sxn_semaphore){
      layer_open = 1;
      Sxn_StartUpdate();
      $.get("objserv.php?sid=" + sxn_sid + "&opt=userprofile&action=show&id=" + id + "&user=" + user,Sxn_SetLayer);
   }
}

function Sxn_CloseLayer(id)
{
   $("#" + id).remove();
   layer_open = 0;
}

function Sxn_SetStates(data)
{
   $("#state").replaceWith(data);
   $("#state").focus();
   Sxn_EndUpdate();
}

function Sxn_GetStates()
{
   if (sxn_semaphore){
      country = $("#country").attr("value");
      Sxn_StartUpdate();
      $.get("getstate.php?class=enduser&lang=" + sxn_lang +"&country=" + country,Sxn_SetStates);
   }
}

function Sxn_GetStatesFilter()
{
   if (sxn_semaphore){
      country = $("#country").attr("value");
      Sxn_StartUpdate();
      $.get("getstate.php?all=1&size=8&class=filter&lang=" + sxn_lang + "&country=" + country,Sxn_SetStates);
   }
}

function Sxn_EditProfile(id)
{
   if (sxn_semaphore){
      Sxn_StartUpdate();
      $.get("objserv.php?sid=" + sxn_sid + "&opt=profile&action=edit&id=" + id,Sxn_UpdateLayer);
   }
}

function Sxn_AddFriend(id,user)
{
   if (sxn_semaphore){
      Sxn_StartUpdate();
      $.get("objserv.php?sid=" + sxn_sid + "&opt=userprofile&action=addfriend&id=" + id + "&user=" + user,Sxn_UpdateLayer);
   }
}

function Sxn_BlockUser(id,user)
{
   if (sxn_semaphore){
      Sxn_StartUpdate();
      $.get("objserv.php?sid=" + sxn_sid + "&opt=userprofile&action=block&id=" + id + "&user=" + user,Sxn_UpdateLayer);
   }
}

function Sxn_RmFriend(id)
{
   if (sxn_semaphore){
      Sxn_StartUpdate();
      $.get("objserv.php?sid=" + sxn_sid + "&opt=friends&action=rmfriend&friend=" + id,Sxn_UpdateLayer);
   }
}

function Sxn_RmBlocked(id)
{
   if (sxn_semaphore){
      Sxn_StartUpdate();
      $.get("objserv.php?sid=" + sxn_sid + "&opt=blocked&action=rmblocked&blocked=" + id,Sxn_UpdateLayer);
   }
}

function Sxn_AddLocation()
{
   cv = $("#country").val();
   ct = $("#country :selected").text();
   sv = $("#state").val();
   st = $("#state :selected").text();
   
   val = cv + "-" + sv;
   
   if ((sv.length > 0) && ($("#tlocation :option[value='" + val + "']").length == 0))
      $("#tlocation").append("<option value='" + val + "'>" + ct + " - " + st);
}

function Sxn_RmLocation()
{
   $("#tlocation option:selected").remove();
}

function Sxn_AddSexOrient()
{
  so = $("#sexorient").val();
  st = $("#sexorient :selected").text();
  
  if ($("#tsexorient :option[value='" + so + "']").length == 0)
     $("#tsexorient").append("<option value='" + so + "'>" + st);
  
}

function Sxn_RmSexOrient()
{
   $("#tsexorient option:selected").remove();
}

function Sxn_SaveFilter()
{
   
   if ($("#tsexorient option").size() == 0){
      alert("At least one gender & orientation must be selected");
      return;
   }
   if ($("#tlocation option").size() == 0){
      alert("At least one location must be selected");
      return;
   }
   $("#tsexorient option").attr("selected","1");
   s = $("#tsexorient").serialize();

   $("#tlocation option").attr("selected","1");
   l = $("#tlocation").serialize();

   if (sxn_semaphore){
      Sxn_StartUpdate();
      $.post("objserv.php?sid=" + sxn_sid + "&opt=filter&action=update", s + "&" + l,Sxn_UpdateLayer);
   }
   
}

function Sxn_StartTimer()
{
   sxn_timer = setTimeout("Sxn_Refresh()",300000);
}

function Sxn_Refresh(){

   if (sxn_state === 'p')
      Sxn_GetPosts('pupdate');
   else
   if (sxn_state === 'i')
      Sxn_GetPosts('iupdate');
   Sxn_StartTimer();
}

function Sxn_CountChar()
{
   left = sxn_maxchar - $("#f_post").val().length;
   if (left < 0)
      $("#char_counter").addClass("in_negative");
   else
      $("#char_counter").removeClass("in_negative");
   $("#char_counter").html(left);
}

function Sxn_OpenStatic(page)
{
   if (layer_open)
      return;
      
   if (sxn_semaphore){
      layer_open = 1;
      Sxn_StartUpdate();
      $.get("objserv.php?opt=static&action=show&id=" + page,Sxn_SetLayer);
   }
}

function Sxn_OpenChat()
{
   window.open("chat.php?sid=" + sxn_sid + "&opt=chat&action=open","sxn_chat_window","width=600,height=600,location=no,menubar=no,toolbar=no,status=no");
}



