﻿
//
// Common page events
//
$(document).ready(function ()
{

 // Change password
 $("#btnChangePassword").click(function ()
 {
  Popup.Show("pnlChangePasswordPopup");
 });
 $("#btnChangePasswordCancel").click(function ()
 {
  Popup.Hide("pnlChangePasswordPopup");
 });

 // forgot password
 $("#btnForgotPassword").click(function ()
 {
  Popup.Hide("pnlSigninPopup");
  Popup.Show("pnlForgotPasswordPopup");
 });
 $("#btnForgotPasswordCancel").click(function ()
 {
  Popup.Hide("pnlForgotPasswordPopup");
 });

 // signin
 $(".Signin").click(function ()
 {
  Popup.Show("pnlSigninPopup");
 });
 $("#btnSigninCancel").click(function ()
 {
  Popup.Hide("pnlSigninPopup");
 });

 SetupChangePasswordProcessing();
 SetupForgotPasswordProcessing();
 SetupSigninProcessing();

 // photo viewer
 SetupPhotoViewer();

 // change language
 $("#Language").change(function ()
 {
  $.cookie("Language", $(this).val(), { expires: 365, path: "/" });
  window.location.reload();
 });

 /**
 // checkboxlist events
 $(":checkbox").click(function ()
 {
 if ($(this).val() == "0")
 {
 $("input[name=" + $(this).attr("name") + "][value!=0]").removeAttr("checked");
 }
 else
 {
 var checked = true;

 $("input[name=" + $(this).attr("name") + "][value!=0]").each(function ()
 {
 if (checked) checked = $(this).is(':checked');
 });

 if (checked)
 {
 $("input[name=" + $(this).attr("name") + "][value!=0]").removeAttr("checked");
 $("input[name=" + $(this).attr("name") + "][value=0]").attr("checked", "checked");
 }
 else
 {
 $("input[name=" + $(this).attr("name") + "][value=0]").removeAttr("checked");
 }
 }
 });
 **/

 // accodian selector
 $(".AccordianHeader").click(function ()
 {
  var Name = $(this).attr("href").replace("#", "");

  if ($("#" + Name).css("display") == "none")
   $("#" + Name).css("display", "table-row-group");
  else
   $("#" + Name).css("display", "none");

 });

 // Send Message
 $(".SendMessage").click(function ()
 {
  alert("CLICK");

 });
 $("#btnSendMessageCancel").click(function ()
 {
  Popup.Hide("pnlSendMessagePopup");
 });

 // Send Signal
 $(".SendSignal").click(function ()
 {
  Popup.Show("pnlSendSignalPopup");
 });
 $("#btnSendSignalCancel").click(function ()
 {
  Popup.Hide("pnlSendSignalPopup");
 });

});

//
// Photo Viewer
//

function PhotoViewer()
{

}

PhotoViewer.Over = function (THIS, X, Y)
{
 var LoadingTop = null;
 var LoadingLeft = null;

 var LoadingTop = ($(window).scrollTop() + $(window).height() - $("#pnlLoading").height() / 2) - 40;
 var LoadingLeft = $(window).scrollLeft() + $(window).width() - $("#pnlLoading").width() / 2;

 //Popup.ShowWorking();

 var PhotoURL = $(THIS).attr("src").replace("thumbnails", "photos").trim()
 var Title = $(THIS).attr("title").trim();
 var Alt = $(THIS).attr("alt").trim();

 // mousing over the same image twice doesn't load the image, so we need to do this
 $("#pnlPhotoViewerPopupImage").attr("src", "");

 $("#pnlPhotoViewerPopupImage").attr("src", PhotoURL)
  .load(function ()
  {
   var Top = ($(window).scrollTop() + ($(window).height() - $("#pnlPhotoViewerPopupImage").height()) / 2) - 40;
   //   var Left = $(window).scrollLeft() + ($(window).width() - $("#pnlPhotoViewerPopupImage").width()) / 2;
   var Left;
   if (X < $(window).width() / 2)
    Left = X + 20;
   else
    Left = X - $("#pnlPhotoViewerPopupImage").width() - 20;

   var Overlay = "";

   if (Title != "") Overlay += "<span style=\"font-size: 12px; color: #ffffff;\">" + Title + "</span><br />";
   if (Alt != "") Overlay += Alt + "<br />";

   if (Overlay == "")
   {
    $("#pnlPhotoViewerPopupCaption").width(0);
    $("#pnlPhotoViewerPopupCaption").html("");
   }
   else
   {
    $("#pnlPhotoViewerPopupCaption").width($("#pnlPhotoViewerPopupImage").width());
    $("#pnlPhotoViewerPopupCaption").html("<div style='margin: 0px 8px 0px 8px;'>" + Overlay + "</div>");
   }

   //Popup.HideWorking();

   $("#pnlPhotoViewerPopup")
     .css("top", Top)
     .css("left", Left)
     .css("visibility", "visible");

  });


}

PhotoViewer.Out = function ()
{
// Popup.HideWorking();
 $("#pnlPhotoViewerPopup").css("visibility", "hidden");
}


//
// Popup
//

$(document).ready(function ()
{

 $(window).resize(function ()
 {
  $("#pnlCover").css("width", $(document).width());
  // $("#pnlCover").css("height", $(document).height());
 });

});

function Popup()
{


}

Popup.Show = function (PopupIdentifier)
{
 $("#pnlCover").css("height", $(document).height());
 $("#pnlCover").css("width", $(document).width());

 $("#" + PopupIdentifier).css("top", ($(window).scrollTop() + ($(window).height() - $("#" + PopupIdentifier).height()) / 2) - 40);
 $("#" + PopupIdentifier).css("left", ($(window).scrollLeft() + ($(window).width() - $("#" + PopupIdentifier).width()) / 2));

 $("#pnlCover").css("visibility", "visible");
 $("#" + PopupIdentifier).css("visibility", "visible");

}

Popup.Hide = function (PopupIdentifier)
{
 $("#pnlCover").css("visibility", "hidden");
 $("#" + PopupIdentifier).css("visibility", "hidden");

}

// list manager
function ListItems(Items, Required, SelectedItem)
{
 var OptionString = " ";

 if (Required == false)
 {
  OptionString += "<option></option>";
 }

 for (var Item = 0; Item < Items.length; Item++)
 {
  if (Items[Item] == SelectedItem)
   OptionString += "<option selected='selected'>" + Items[Item] + "</option>";
  else
   OptionString += "<option>" + Items[Item] + "</option>";
 }

 return OptionString;

}

function ListAttributes(Items, Required, SelectedValue)
{
 var OptionString = " ";

 if (Required == false)
 {
  OptionString += "<option value='0'></option>";
 }

 for (var Item = 0; Item < Items.length; Item++)
 {
  if (Item.Value == SelectedValue)
   OptionString += "<option value='" + Items[Item].Value + "' selected='selected'>" + Items[Item].Text + "</option>";
  else
   OptionString += "<option value='" + Items[Item].Value + "'>" + Items[Item].Text + "</option>";
 }

 return OptionString;

}

// validators
function Required(Name)
{
 var Status = $.trim($("#" + Name).val()).length > 0 ? true : false;
 DisplayError(Name, Status);
 return Status;
}

function Length(Name, MinimumLength, MaximumLength)
{
 var Value = $.trim ($("#" + Name).val());
 var Status = Value.length >= MinimumLength && Value.length <= MaximumLength ? true : false;
 DisplayError(Name, Status);
 return Status;
}

function Compare(Name, Operator, NameCompare)
{
 var Value = $.trim($("#" + Name).val());
 var ValueCompare = $.trim($("#" + NameCompare).val());
 var Status;

 if (Operator == "eq")
  Status = Value == ValueCompare ? true : false;
 else if (Operator == "gt")
  Status = Value > ValueCompare ? true : false;
 else if (Operator == "gte")
  Status = Value >= ValueCompare ? true : false;
 else if (Operator == "lt")
  Status = Value < ValueCompare ? true : false;
 else if (Operator == "lte")
  Status = Value <= ValueCompare ? true : false;
 else if (Operator == "ne")
  Status = Value != ValueCompare ? true : false;

 DisplayError(NameCompare, Status);
 return Status;
}

function Email(Name)
{
 var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
 var Status = emailPattern.test($.trim($("#" + Name).val()));
 DisplayError(Name, Status);
 return Status;
}

function Checked(Name)
{
 var Status = $('#checkBox').attr('checked') == "checked" ? true : false
 DisplayError(Name, Status);
 return Status;
}

function DisplayError(Name, Status)
{
 if (Status == true)
  $("#" + Name + "Message").css("display", "none");
 else
  $("#" + Name + "Message").css("display", "table-row");

}

// Identity Management

function SetupSigninProcessing()
{
 var options =
 {
  dataType: 'json',
  success: function (Result)
  {
   if (Result.Indicator == true)
    window.location = "http://" + window.location.host + "/Account/Home";
   else
    $("#SigninMessage").css("display", "block").html(Result.Message);
  }
 };

 $("#frmSignin").submit(function ()
 {
  var Status = true;

  if (Length("SigninMemberName", 6, 18) == false) Status = false;
  if (Length("SigninPassword", 6, 12) == false) Status = false;

  if (Status == true)
  {
   $(this).ajaxSubmit(options);
  }

  return false;

 });

}

function SetupChangePasswordProcessing()
{
 $("#ChangePasswordPassword").keyup(function ()
 {
  Length("ChangePasswordPassword", 6, 12);
 });
 $("#ChangePasswordPasswordConfirmation").keyup(function ()
 {
  Compare("ChangePasswordPassword", "eq", "PasswordConfirmation");
 });

 var options =
 {
  dataType: 'json',
  success: function (Result)
  {
   $("#ChangePasswordMessage").css("display", "block").html(Result.Message);
  }
 };

 $("#frmChangePassword").submit(function ()
 {
  var Status = true;

  if (Length("ChangePasswordPassword", 6, 12) == false) Status = false;
  if (Compare("ChangePasswordPassword", "eq", "PasswordConfirmation") == false) Status = false;

  if (Status == true)
  {
   $(this).ajaxSubmit(options);
  }

  return false;

 });

}

function SetupForgotPasswordProcessing()
{
 $("#ForgotPasswordMemberName").keyup(function ()
 {
  Length("ForgotPasswordMemberName", 6, 18);
 });

 var options =
 {
  dataType: 'json',
  success: function (Result)
  {
   $("#ForgotPasswordMessage").css("display", "block").html(Result.Message);
  }
 };

 $("#frmForgotPassword").submit(function ()
 {
  var Status = true;

  if (Length("ForgotPasswordMemberName", 6, 18) == false) Status = false;

  if (Status == true)
  {
   $(this).ajaxSubmit(options);
  }

  return false;

 });

}


function SetupGalleryActions ()
{

 $("#pnlSearchResults > div")
  .unbind("mouseenter")
  .unbind("mouseleave")
  .mouseenter(function (e)
   {
    var PanelIdentifier = $(this).attr("id");
    $("#" + PanelIdentifier).removeClass("ListItemStyle").addClass("ListItemHighlightStyle");
    $("#" + PanelIdentifier + "_ActionBar").css("visibility", "visible");
   })
  .mouseleave(function (e)
   {
    var PanelIdentifier = $(this).attr("id");
    $("#" + PanelIdentifier).removeClass("ListItemHighlightStyle").addClass("ListItemStyle");
    $("#" + PanelIdentifier + "_ActionBar").css("visibility", "hidden");
   })

}

function SetupPhotoViewer()
{
 $("img")
  .unbind("mouseenter")
  .unbind("mouseleave")
  .mouseenter(function (e)
   {
    PhotoViewer.Over(this, e.pageX, e.pageY);
   })
  .mouseleave(function (e)
  {
   PhotoViewer.Out();
  });

}

