/*
 * dialogs.js
 * Dialog objects for the common services controller.
 * By Ryan Sandor Richards
 * Copyright 2009 FreeCause, Inc.
 */

/** 
 * Dialog object for the sign-in dialog.
 */
var dialog_signin = {
  title: 'Sign-in',
  modal: true,
  width: 320,
  height: 220,
  buttons: {
    "Sign In": function() {
      var username = $('.dialog_signin #fusername').val();
      var password = $('.dialog_signin #fpassword').val();
      var this_dialog = this;

      $.ajax({
        type: "POST",
        url: Site.baseurl+"account/signin",
        data: "username=" + username + "&password=" + password,
        error: function(XMLHttpRequest, textStatus, errorThrown){
          $('#msg_signin').text('Failed: ' + textStatus);
        },
        success: function(msg){
          if(msg){
            $('#msg_signin').text(msg);
          } else {
            $(this_dialog).dialog('close');
            
            var redirect_url_login = ($('#redirect_url_login').length > 0 ? $('#redirect_url_login').val() : '');
            
            //console.log(redirect_url_login);
            
            if(redirect_url_login)
            {
              //console.log('redirecting to ' + redirect_url_login);
              location.href = redirect_url_login;
            }
            else
            {
            location.reload();
          }
        }
        }
      });
    },
    "Cancel": function() {
      $(this).dialog('close');
    }
  },
  close: function() {
    $('#signin_link').click(signin_click);
  },
  open: function() {
    $('.dialog_signin #fpassword, .dialog_signin #fusername').keydown(function(event) {
      if (event.keyCode == 13) {
        dialog_signin.buttons['Sign In']();
      }
    });
  }
};

// Admin Manage Dialogs 
function dialog_close(){
  window.dialog_active = false;
  $('#dialog_admin').parent().parent().remove();
  $(".ui-dialog").remove()
}

function dialog_cancel(){
  window.dialog_active = false;
  $('#dialog_admin').parent().remove();
  $(this).dialog('close');
  $(".ui-dialog").remove()
}

function showResponse(responseText, statusText){
    var new_id = null;
    var new_page_id = null;
    if (responseText == ""){
      //we don't want the message to show up, it is repetitive and unnecessary
      //responseText = "Page Updated! Click OK to continue.";
    } else if(responseText.substr(0, 3) == 'id:')
    {
      new_page_id = responseText.substr(3).split('|')[0];
      site_id = responseText.substr(3).split('|')[1];
      
      //we don't want the message to show up, it is repetitive and unnecessary
      //responseText = "Page Updated! Click OK to continue.";
      responseText = "";
    } else if (responseText == "Removed!"){
      
    } else {
      window.fc_engine_reload_diff_location = false;
    }
  
    if(responseText)
    {
    Dialog.alert("Alert:", responseText, function(){
      if (window.fc_engine_reload_diff_location === false){
        // don't reload
        delete window.fc_engine_reload_diff_location;
      } else if(new_page_id  != null)
      {
        location.href = Site.baseurl + Site.lowercase_controller_name + "/site/" + site_id + "/manage/" + new_page_id;
      } else if (window.fc_engine_reload_diff_location == undefined){
      dialog_close(); 
        location.reload();
      } else {
        // alert(window.fc_engine_reload_diff_location);
        dialog_close();
        window.location = window.fc_engine_reload_diff_location;
      }
    });
    }
    else
    {
      if (window.fc_engine_reload_diff_location === false){
        // don't reload
        delete window.fc_engine_reload_diff_location;
      } else if(new_page_id  != null)
      {
        location.href = Site.baseurl + Site.lowercase_controller_name + "/site/" + site_id + "/manage/" + new_page_id;
      }else if (window.fc_engine_reload_diff_location == undefined){
        dialog_close();
        location.reload();
      } else {
        // alert(window.fc_engine_reload_diff_location);
        dialog_close();
        window.location = window.fc_engine_reload_diff_location;
      }
    }
    
    // setTimeout("dialog_close();location.reload();", 1500);
}

function page_remove_ajax(){
  var controller_name = (Site.lowercase_controller_name ? Site.lowercase_controller_name : 'admin');
  var answer = confirm("Are you sure you want to remove this page?");
  var page_id = $('#dialog_page_id').val();
  if(answer) {
    $.ajax({
      type: "GET",
      url: Site.baseurl + controller_name + "/ws_remove_page_from_site/" + page_id,
      error: function(XMLHttpRequest, textStatus, errorThrown){
        alert('Failed: ' + textStatus);
      },
      success: function(msg){
        if(msg && msg != 'Removed!'){
          alert(msg);
        } else {
          //alert('Removed!');
          dialog_close();
          /*var url = window.location.href.split("/");
          x = url.pop();
          // alert(x);
          if (x.match(/manage/) != null){
            url.push("manage");
          } else if (x == ""){
            url.pop();
        }
          // alert(url);
          window.location = url.join("/");*/
          location.reload();
      }
      }
    });
  } else {
    // Do nothing...
  }
}

var dialog_module_add = {
  title: "Module Properties",
  modal: true,
  height: 400,
  width: 860,
  close: dialog_close,
  open: function() {
    $('#module_library').tabs({
      cache: false
    });
  },
  buttons: {
    "Cancel": dialog_cancel
  }
}

var dialog_mall_edit = {
    title: "Mall Properties",
    modal: true,
    height: 400,
    width: 860,
    close: dialog_close,
    open: function() {
      $('#module_library').tabs({
        cache: false
      });
    },
    buttons: {
      "Cancel": dialog_cancel
    }
}

var dialog_page_add = {
  title: "Page Properties",
  modal: true,
  height: 445,
  width: 800,
  success: showResponse,
  close: dialog_close,
  buttons: {
    "Cancel": function(){ dialog_cancel(); },
    "Add Page": function() { $("#page_add_form").submit();}
  }
}

var dialog_page_edit = {  
  title: "Page Properties",
  modal: true,
  height: 445,
  width: 800,
  success: showResponse,
  close: dialog_close,
  buttons: {
    "Cancel": dialog_cancel,
    "Update Page": function() {
      $('#page_update_form').ajaxSubmit(dialog_page_edit);
    },
    "Delete Page": function() {page_remove_ajax();}
  }
}

var dialog_publish_fb_embed = {
  title: "Publishing FB Embed Code...",
  modal: true,
  height: 120,
  width: 320,
  success: function(){
    window['publish_fb_index.clicked'] = false;
    alert("Page Updated! " + responseText);
    dialog_close();
  },
  close: function(){ window['publish_fb_index.clicked'] = false; $('.dialog_publish_fb_embed').parent().dialog('close'); },
  buttons: {
    "Cancel": function(){ window['publish_fb_index.clicked'] = false; $('.dialog_publish_fb_embed').parent().dialog('close'); }
  }
}

var dialog_publish_downloads = {
  title: "Select your download format:",
  modal: true,
  height: 320,
  width: 480,
  success: function(){
    window['publish_downloads.clicked'] = false;
    alert("Page Updated! " + responseText);
    dialog_close();
  },
  close: function(){ window['publish_downloads.clicked'] = false; dialog_close(); },
  buttons: {
    "Cancel": function(){ window['publish_downloads.clicked'] = false; dialog_close(); }
  }
}

var dialog_preview_normal_embed = {
  title: "Loading your preview:",
  modal: true,
  height: 320,
  width: 480,
  success: function(responseText){
    window['publish_normal.clicked'] = false;
    alert(responseText);
    dialog_close();
  },
  close: function(){ window['publish_normal.clicked'] = false;  $('.dialog_preview_normal_embed').parent().dialog('close'); },
  buttons: {
    "Cancel": function(){ window['publish_normal.clicked'] = false;  $('.dialog_preview_normal_embed').parent().dialog('close'); }
  }
}