
/*
 * LTV User
 */

//Global Event Namespace
var LTV_USER_ACCESS_DENIED = 0;
var LTV_USER_SUCCESS = 1;
var LTV_USER_LOGGED_IN = 2;
var LTV_USER_PASSWORD_MAILED = 3;

// Global Action Namespace
var LTV_USER_LOG_IN = 101;
var LTV_USER_SEND_PASSWORD = 102;
var LTV_USER_COMMENT_SEND_PASSWORD = 103;
var LTV_USER_TOPIC_SEND_PASSWORD = 104;

var LTV_USER_FRONT_PAGE;
var LTV_USER_COMMENT_PAGE;
var LTV_USER_SWEEP_PAGE;
var LTV_USER_SWEEP_PRIZE_PAGE;
var LTV_USER_PLANET_FORECAST_PAGE;
var LTV_USER_SWEEP_ENTRY_PAGE;

var LTV_BC_VIDEO_PAGE;

var LTV_AW_PAGE;

// handle remote login
if (!remoteDomain) {
  var remoteDomain = '';
}

Drupal.flagThisCommentAttach = function() {
  $('.comment-flag .flag').click( function(e) {
    var offset = $(this).offset( {
      scroll :false
    });
    var url = $(this).attr('href');
    var tmparr = url.split('/');
    var id = 0;
    for (idx = 0; idx < tmparr.length; idx++) {
      if (!isNaN(tmparr[idx])) {
        id = tmparr[idx];
      }
    }
    ltv_user_comment_flag_form(offset.left, offset.top, id, url);
    return false;
  });
}

var baseUrl = '/';

$(document).ready( function() {
  //baseUrl = 'http://' +  Drupal.settings.ltv_user.host + Drupal.settings.ltv_user.basePath;
  
  $('#button-signin').click( function(e) {
    var offset = $(this).offset( {
      scroll :false
    });
    ltv_user_login_form(offset.left, offset.top);
    return false;
  });
  $('.comment-signin').click( function(e) {
    var offset = $(this).offset( {
      scroll :false
    });
    ltv_user_comment_login_form(offset.left, offset.top);
    return false;
  });
  Drupal.flagThisCommentAttach();

  //init_form(LTV_USER_LOG_IN);
  // Test to see if we are on the front page
  LTV_USER_FRONT_PAGE = $('.homepage_spotlight_scroller').html() != undefined ? 1 : 0;
  LTV_USER_COMMENT_PAGE = $('.comment-signin').size() > 0 ? 1 : 0;
  LTV_USER_SWEEP_PAGE = $('#user-sweep-specs').html() != undefined ? 3: 0;
  LTV_USER_SWEEP_PRIZE_PAGE = $('#user-sweep-prize-specs').html() != undefined ? 4: 0;
  if (LTV_USER_SWEEP_PRIZE_PAGE == 4) {
    LTV_USER_SWEEP_PRIZE_PAGE = $('#swp-signup-block').html() != undefined ? 4: 0;
  }
  LTV_USER_PLANET_FORECAST_PAGE = $('.forecast-week-header').html() != undefined ? 5: 0;
  LTV_USER_SWEEP_ENTRY_PAGE = $('#sweep-entry-form').html() != undefined ? 6: 0;
  
  if($('.video-comment-box').size() > 0) {
    //if it's a video page, then convert to comment page
    LTV_USER_COMMENT_PAGE = 1;
    LTV_BC_VIDEO_PAGE = 1;
  }
  LTV_AW_PAGE = $('iframe.community-iframe').length > 0? 7: 0;
});

function init_form(event) {
  switch (event) {
    case LTV_USER_SEND_PASSWORD:
    case LTV_USER_COMMENT_SEND_PASSWORD:
    case LTV_USER_TOPIC_SEND_PASSWORD:
      $("input[@name=email]").click( function() {
        ltv_user_empty_input(this);
      });
      $("input[@name=email]").keypress( function(data) {
        ltv_user_get_action(data, event);
      });
      break;
    case LTV_USER_LOG_IN:
    default:
      $("input[@name=email], input[@name=username], input[@name=name]").focus(function() {
        $(this).val('').get(0).focus(); 
      }).keypress(function(data) { 
        ltv_user_get_action(data, event); 
      });

      $("input[@name=userpass], input[@name=pass]").focus(function() { 
        var old_pass = $(this);
        var new_pass = $(
          '<input type="password" '
          + 'id="' + old_pass.attr('id') + '" '
          + 'class="' + old_pass.attr('class') + '" '
          + 'name="' + old_pass.attr('name') + '" '
          + 'size="' + old_pass.attr('size') + '" '
          + 'tabindex="' + old_pass.attr('tabindex') + '" '
          + 'value="" />');
        old_pass.replaceWith(new_pass);
        new_pass.get(0).focus();
      }).keypress(function (data) {
        ltv_user_get_action(data, event);
      });
      
      aetn.user.init();
      
      break;
  }
}

// added by srini on 26 Nov
function ltv_user_get_action_member(e, action) {
  var character_code;
  if (e && e.which) {
    character_code = e.which;
  } else {
    character_code = e.keyCode;
  }

  if (character_code == 13) {
    switch (action) {
      case LTV_USER_LOG_IN:
        if (LTV_USER_FRONT_PAGE) {
          ltv_user_member(LTV_USER_FRONT_PAGE);
        } else if (LTV_USER_SWEEP_PAGE) {
          ltv_user_member(LTV_USER_SWEEP_PAGE);
        } else if (LTV_USER_SWEEP_PRIZE_PAGE) {
          ltv_user_member(LTV_USER_SWEEP_PRIZE_PAGE);
        } else {
          ltv_user_member(LTV_USER_COMMENT_PAGE);
        }
        break;
      case LTV_USER_SEND_PASSWORD:
        ltv_user_forgot_password();
        break;
      case LTV_USER_COMMENT_SEND_PASSWORD:
        ltv_user_forgot_password('#user-comment');
        break;
      case LTV_USER_TOPIC_SEND_PASSWORD:
        ltv_user_forgot_password('#user-topic');
        break;
    }
    return false;
  } else {
    return true;
  }
}

function ltv_user_member(location) {
  $("p.welcome").text("").css('color', '');
  $.ajax( {
    type :'POST',
    url : baseUrl + 'ltv_user/login/' + location,
    data :'name=' + $("input[@name=name]").val() 
        + '&pass=' + $("input[@name=pass]").val() ,
    dataType :'json',
    success : function(data) {
      switch (data.status) {
        case LTV_USER_SUCCESS:
          window.location = remoteDomain + '/user';
          break;
        case LTV_USER_ACCESS_DENIED:
          $("p.welcome").text("Wrong Username / Password").css('color', 'red');
          break;
      }
      return false;
    }
  });
}
// End

function browser_msie_post_load(event) {
  $('input[@name=userpass]').keypress( function(data) {
    ltv_user_get_action(data, LTV_USER_LOG_IN);
  });
  // $('input[@name=userpass]')[0].focus();
}

// added by srini
function browser_msie_post_load_member(event) {
  $('input[@name=pass]').keypress( function(data) {
    ltv_user_get_action(data, LTV_USER_LOG_IN);
  });
  $('input[@name=pass]')[0].focus();
}
// End

function ltv_user_get_action(e, action) {
  var character_code;

  if (e && e.which) {
    character_code = e.which;
  } else {
    character_code = e.keyCode;
  }

  if (character_code == 13) {
    switch (action) {
      case LTV_USER_LOG_IN:
        if (LTV_USER_FRONT_PAGE) {
          ltv_user_sign_in(LTV_USER_FRONT_PAGE);
        } else if (LTV_USER_SWEEP_PAGE) {
          ltv_user_sign_in(LTV_USER_SWEEP_PAGE);
        } else if (LTV_USER_SWEEP_PRIZE_PAGE) {
          ltv_user_sign_in(LTV_USER_SWEEP_PRIZE_PAGE);
        } else {
          ltv_user_sign_in(LTV_USER_COMMENT_PAGE);
        }
        break;
      case LTV_USER_SEND_PASSWORD:
        ltv_user_forgot_password();
        break;
      case LTV_USER_COMMENT_SEND_PASSWORD:
        ltv_user_forgot_password('#user-comment');
        break;
      case LTV_USER_TOPIC_SEND_PASSWORD:
        ltv_user_forgot_password('#user-topic');
        break;
    }
    return false;
  } else {
    return true;
  }
}

function ltv_user_toggle_login(obj) {
  toggle = $('.user-block-bottom');
  
  if ($(obj).attr('class') == 'btn-expand') {
    // show login form
    if($('> div', toggle).attr('class').indexOf('login-form') == -1) {
      $.get(baseUrl + 'ltv_user/content/1', function(data) {
        if (data != '') {
          $(toggle).html('');
          $(toggle).html(data);
          init_form();
        }
      });
    }
  }

  if (toggle.css('display') == 'none') {
    toggle.show();
  } else {
    toggle.hide();
  }
}

function ltv_user_load_login_form() {
  // user is logged in lets grab their custom login box
  if(getcookie('loginstatus') && $('#block-ltv_user-0 .logged-in').length == 0) {
    
    $('#block-ltv_user-0 .content').load(baseUrl + 'ltv_user/content/0', function(event) {
      init_form();
      $('#block-ltv_user-0').css('display', 'block');
    });
    
    //load astrology info
    $.getJSON(baseUrl + 'api/astrology/homepage/get_sign_info_for_user', function(data){
      $('.horoscope_box').html(data.html);
    });
    
    
  } else {
    $('#block-ltv_user-0').css('display', 'block');
  }
}
/*
// as soon as the html is available, alter it
var ltv_user_load_login_form_timer = setInterval(function() {
  if(document.getElementById('block-ltv_user-0')) {
    ltv_user_load_login_form();
    clearInterval(ltv_user_load_login_form_timer);
  }
}, 300);
*/
function ltv_user_login_form(x, y) {
  if ($('#user_log_in_form').html() == undefined) {
    $("body").prepend(
        "<div id=\"user_log_in_form\" class=\"user_log_in_form\"></div>");
  }
  $.get(baseUrl + 'ltv_user/content/2', function(data) {
    $("#user_log_in_form").html(data);
    $("#user_log_in_form #user-wrap").css("position", "absolute");
    $("#user_log_in_form #user-wrap").css("right", "0px");
    $("#user_log_in_form #user-wrap").css("top", y + "px");
    $("#user_log_in_form #user-wrap").css("left", x + "px");
    $("#user-wrap").css("z-index", "60");
    $("#user-wrap").fadeIn(400);
    init_form(LTV_USER_LOG_IN);
  });
}

function ltv_user_comment_login_form(x, y) {
  if ($('#user_log_in_form-2').html() == undefined) {
    $("body").prepend(
        "<div id=\"user_log_in_form-2\" class=\"user_log_in_form-2\"></div>");
  }
  $.get(baseUrl + 'ltv_user/content/4', function(data) {
    $("#user_log_in_form-2").html(data);
    $("#user_log_in_form-2 #user-wrap-2").css("position", "absolute");
    $("#user_log_in_form-2 #user-wrap-2").css("right", "0px");
    $("#user_log_in_form-2 #user-wrap-2").css("top", y + "px");
    $("#user_log_in_form-2 #user-wrap-2").css("left", x + "px");
    $("#user_log_in_form-2 #user-wrap-2").css("width", "265px");
    $("#user-wrap-2").css("z-index", "60");
    $("#user-wrap-2").fadeIn(400);
    init_form(LTV_USER_LOG_IN);
  });
}

function ltv_user_comment_flag_form(x, y, id, url) {
  x = x - 240; // / for moving the pop up to right align with the text - for
                // overlapping with flash player problem
  var get_url = baseUrl + 'ltv_user/content/5/' + id;
  var qstr = '';
  if (url.indexOf('?') != -1) {
    qstr = url.split('?')[1];
    if (qstr.indexOf('destination') != -1) {
      get_url += '?' + qstr;
    }
  }
  
  if ($('#comment_flag_form').html() == undefined) {
    $("body").prepend(
        "<div id=\"comment_flag_form\" class=\"comment_flag_form\"></div>");
  }
  $.get(get_url,
  // note: using $.get explicit params, ie {name:value} blows up stuff because
  // of encoding here
      function(data) {
        $("#comment_flag_form").html(data);
        $("#comment_flag_form #comment_flag-wrap").css("position", "absolute");
        $("#comment_flag_form #comment_flag-wrap").css("right", "0px");
        $("#comment_flag_form #comment_flag-wrap").css("top", y + "px");
        $("#comment_flag_form #comment_flag-wrap").css("left", x + "px");
        $("#comment_flag-wrap").css("z-index", "60");
        $("#comment_flag-wrap").fadeIn(400);
        // init_form(LTV_USER_LOG_IN);
    });
}

function ltv_user_comment_flag_submit() {
  var submit_page = $('#flag-content-add').attr('action');
  show_response = "Thank You!";
  $("#comment_flag .comment_flag_form_wrapper").html(show_response);

  $.post(submit_page, {
    eid :$("#flag-content-add input[@name=eid]").val(),
    type :'comment',
    confirm :$("#flag-content-add input[@name=confirm]").val(),
    form_id :$("#flag-content-add input[@name=form_id]").val(),
    form_token :$("#flag-content-add input[@name=form_token]").val(),
    op :'Flag'
  }, function(data) {
    $('#comment_flag-wrap').fadeOut(400, function() {
      $('#comment_flag-wrap').hide();
      $("#comment_flag_form").html('');
    })
  });
}

function ltv_user_sign_in(location) {

  $.ajax( {
    type :'POST',
    url :baseUrl + 'ltv_user/login/' + location,
    data :'name=' + $("input[@name=username]").val() + '&pass='
        + $("input[@name=userpass]").val(),
    dataType :'json',
    success : function(data) {
      switch (data.status) {
        case LTV_USER_SUCCESS:
          if (data.destination) {
            window.location = data.destination;
          } else {
            if(LTV_BC_VIDEO_PAGE) {
              window.location = window.location.pathname + "#";
              window.location.reload(true);
            } else if (LTV_USER_COMMENT_PAGE) {
              window.location.reload(true);
            } else if (LTV_USER_FRONT_PAGE) {
              window.location.reload(true);
            } else if (LTV_USER_SWEEP_PAGE) {
              window.location.reload(true);
            } else if (LTV_USER_SWEEP_PRIZE_PAGE) {
              // Pull destination location from href source
              window.location = $('#button-signin').attr('href');
          
            } else if (LTV_USER_PLANET_FORECAST_PAGE) {
              var loc_planet_current = new String(window.location);
              var loc_planet_home = loc_planet_current.replace(/\/ruling-planets\/forecast\/(.*$)/, "/ruling-planets/forecast");
              window.location = loc_planet_home;
            } else if (LTV_USER_SWEEP_ENTRY_PAGE) {
              window.location.reload(true);
            } else if (LTV_AW_PAGE) {
              window.location.reload(true);
            } else {
              $(".ltv-user-block").html($(data.content).html());
            }
          }
          break;
        case LTV_USER_LOGGED_IN:
            $(".ltv-user-block").html($(data.content).html());
          break;
        case LTV_USER_ACCESS_DENIED:
          $(".login-status").text("Invalid Username / Password.").css('color', 'red').css('font-size', '11px');
          break;
      }
      return false;
    }
  });
}

function ltv_user_forgot_password(id) {
  ltv_form_id = '.user-block-bottom';
  $.ajax( {
    type :'POST',
    url : baseUrl + 'ltv_user/password',
    data :'name=' + $("input[@name=email]").val(),
    dataType :'json',
    success : function(data, id) {
      switch (data.status) {
        case LTV_USER_SUCCESS:
          $(ltv_form_id).html(data.content);
          break;
        case LTV_USER_ACCESS_DENIED:
          $(ltv_form_id).html(data.content);
          break;
      }
    }
  });
}

function ltv_user_password_form() {
  $.get(baseUrl + 'ltv_user/content/3', function(data) {
    $('.user-block-bottom').html(data).show();

    init_form(LTV_USER_SEND_PASSWORD);
  });
}

function ltv_user_comment_password_form() {
  $.get(baseUrl + 'ltv_user/content/3', function(data) {
    $('#user-comment').html(data);
    init_form(LTV_USER_COMMENT_SEND_PASSWORD);
  });
}

function ltv_user_topic_password_form() {
  $.get(baseUrl + 'ltv_user/content/3', function(data) {
    $('#user-topic').html(data);
    init_form(LTV_USER_TOPIC_SEND_PASSWORD);
  });
}

function ltv_user_empty_input(input) {
  $(input).val('');
}

function ltv_user_make_password(input) {
  $(input).attr( { type: 'password' }).val('');
}