var authLogin = false;

var twUser;

var twPass;

var modal;

var $x1, $y1, $x2, $y2, $w, $h;

 

$(document).ready(function(){

  $.addwatermarks();

  $('#cancelLogin').click(function(){

    modal.hide(function(){$(document).queue('actions',[]);});

  });

  $('#goAuth').click(function(){

    TestTwitter($('#user').val(),$('#pass').val());

  });

  // links

  //$('#test').click(function(){

  //  authRequired(function(){Boxy.alert('hell yeah!');});

  //});

  $('#Autobots div.badge').click(function(){

    $('#Decepticons div.badge').slideUp();

    $('#Decepticons div.mission').slideDown();

  });

  $('#Decepticons div.badge').click(function(){

    $('#Autobots div.badge').slideUp();

    $('#Autobots div.mission').slideDown();

  });

  $('#dec_cancel').click(function(){

    $('#Autobots div.badge').slideDown();

    $('#Autobots div.mission').slideUp();

  });

  $('#aut_cancel').click(function(){

    $('#Decepticons div.badge').slideDown();

    $('#Decepticons div.mission').slideUp();

  });

  $('#bg1').click(function(){

    authRequired(function(){applybg(twUser,twPass,'media/autobot.jpg');});

  });

  $('#bg2').click(function(){

    authRequired(function(){applybg(twUser,twPass,'media/decepticon.jpg');});

  });

  //$('#testav_a').click(function(){

  //  authRequired(function(){applyavatar(twUser,twPass,'images/aut_mask_100.jpg');});

  //});

  //$('#testav_d').click(function(){

  //  authRequired(function(){applyavatar(twUser,twPass,'images/dec_mask_100.jpg');});

  //});

  $('#follow1').click(function(){

    authRequired(function(){follow(twUser,twPass,faction1);});

  });

  $('#follow2').click(function(){

    authRequired(function(){follow(twUser,twPass,faction2);});

  });

});



function authRequired(action){

  $(document).queue('actions',action);

  if(!authLogin){

    modal = new Boxy($('#login'), {title: "Twitter Login Required", modal: true, closeable: false});

  }else{

    $(document).dequeue('actions');

  }

}



function applyavatar(user,pass,img){

  //alert(user + "," + pass + "," + img);

  $.post('ajax.php', { action: 'setAvatar', twUser: user, twPass: pass, img: img },

  function(data){

    //$('#debug').text(data);

    if(!data){

      Boxy.alert('MALFUNCTION!<br/>A glitch in the trans-perambulation of pseudo-cosmic antimatter has caused your request to fail. Please try again.');

    }else{

      Boxy.alert('AFFIRMATIVE!<br/>Your new profile image has been applied, there may be a 10-15 minute delay before it appears on your Twitter profile.');

    }

  }, 'text');

}

function applybg(user,pass,img){
	
  $.post('ajax.php', { action: 'setBGImage', twUser: user, twPass: pass, img: img },

  function(data){

    $('#debug').text(data);

    if(!data){

      Boxy.alert('MALFUNCTION!<br/>There was a problem applying the background image. Please try again later.');

    }else{

      Boxy.alert('AFFIRMATIVE!<br/>Your background has been applied, there may be a 10-15 minute delay before it appears on your Twitter profile.');

    }

  }, 'text');

}

function follow(user,pass,faction){
if(!Boxy.isModalVisible()){
	modal = modal = new Boxy($('<div class="loading">loading...</div>'), {title: 'Following @' + faction, modal: true, closeable: false});
}else{
	modal.setTitle('Following @' + faction);
	modal.setContent('<div class="loading">Negotiating protocols...<br/><img src="img/ajax-loader.gif"/></div>');
}
  $.post('ajax.php', { action: 'followFaction', twUser: user, twPass: pass, faction: faction },

  function(data){

    if(!data){

//      Boxy.alert('MALFUNCTION!<br/>A glitch in the trans-perambulation of pseudo-cosmic antimatter has caused your request to fail. Please try again.');
	modal.setTitle('MALFUNCTION!');
	modal.setContent('<p>A glitch in the trans-perambulation of pseudo-cosmic antimatter has caused your request to fail. Please try again.</p><p><a href="javascript:modal.hide();">close</a></p>');

    }else{

//      Boxy.alert('AFFIRMATIVE!<br/>You are now following '+faction+'<br/>We appreciate your support.');
	modal.setTitle('AFFIRMATIVE!');
	modal.setContent('<p>You are now following '+faction+'<br/>We appreciate your support.</p><p><a href="javascript:modal.hide();">close</a></p>');

    }

  }, 'text');

}



function TestTwitter(user,pass){

  $.post('ajax.php', { action: 'validateLogin', twUser: user, twPass: pass },

  function(data){

    if(!data){

      $('#authErr').text('login failed');

    }else{

      authLogin = true;

      twUser = user;

      twPass = pass;

      //login.hide(function(){$(document).dequeue('actions');});
      $(document).dequeue('actions');

    }

  }, 'text');

}



 

 function selectChange(img, selection)

 {

   $x1.text(selection.x1);

   $y1.text(selection.y1);

   $x2.text(selection.x2);

   $y2.text(selection.y2);

   $w.text(selection.width);

   $h.text(selection.height);

 }

 