/**
 * JavaScript tip.js
 * @version 1.0
 * @Created: 2011-02-14
 * @Author: Oscar Engström, http://www.engstream.se/
 *
 * Comment to this document:
 * For sending tip and information to site owner
 */

$(document).ready(function(){
  //Initialize
  performerTip.init();
});

var performerTip = {
  /**
   * Run this function once for initializing and binding
   */
  init: function(){
    $("#tipSubmit").live('click', function(ev){
      ev.preventDefault();
      $("#sendTipForm").submit();
    });

    $("#sendTipForm").submit(function(ev){
      ev.preventDefault();
      if($("#tipMessage").val().length == 0){
        performerShadowbox.showError('Inget meddelande', 'Du behöver skriva något i meddelandefältet, övriga fält är frivilliga.', false, 150);
        return false;
      }

      performerTip.confirmSendTip();
    });

    $("#tipConfirmSendYes").live('click', function(ev){
      ev.preventDefault();
      performerTip.performSendTip();
    });
  },

  confirmSendTip: function(){
    performerShadowbox.stdOpen($("#loading").html(), 155, 555);
    performerShadowbox.showDialogue('tip.confirmSend', false, 155);
  },

  performSendTip: function(){
    var data = new Object();
        data.action = 'sendTip';
        data.name = $("#tipName").val();
        data.phone = $("#tipPhone").val();
        data.email = $("#tipEmail").val();
        data.message = $("#tipMessage").val();

        var dataString = $.toJSON(data);

        $.post('ajax/tip.php', {data: dataString}, function(res){
            var obj = $.evalJSON(res);
            if(obj.success == 1){
              //Tip successfully sent
              performerShadowbox.stdOpen(obj.tpl, 155, 555);
              $("#tipName").val('');
              $("#tipPhone").val('');
              $("#tipEmail").val('');
              $("#tipMessage").val('');
            } else {
              //Could not get send the tip
              performerShadowbox.stdOpen(obj.tpl, 155, 555);
            }
        });
  }
};
