﻿$(document).ready(function() 
{ 
	//Set a var that tracks whether or not a dialog is already open
	this.isDialogOpen = false;
	
	$('#lnkEditPage').bind('click',function(event)
	{
		event.preventDefault();
		SetupPageEditing();	
	});
});

function openPagePropertiesDialog(pageId)
{
  if (!document.isDialogOpen)
  {
    document.isDialogOpen = true;
    createPagePropertiesDialog().Open(pageId);
  }
  return false;
}

function openNewPagePropertiesDialog(parentPageId)
{
  if (!document.isDialogOpen)
  {
    document.isDialogOpen = true;
    createPagePropertiesDialog().OpenForNew(parentPageId);
  }
  return false;
}

//Create dialog with a callback to reload on save and close and to reset the isDialogOpen flag
function createPagePropertiesDialog()
{
  return new PagePropertiesDialog(
					function () { window.location.reload(true); },
					function () { document.isDialogOpen = false; });
}

function SetupPageEditing()
{
	$('#lnkEditPage').hide();
	//set up page properties button
	$('#lnkEditPage').after(' <a id="lnkAddNewSegment" href="#">Add New Segment</a>');
	if (canAdministrate)
	{
	  $('#lnkAddNewSegment').after(' <a id="lnkPageProperties" href="/page/' + pageId + '/edit">Page Properties</a>');
	  $('#lnkPageProperties').bind('click', function (event)
	  { return  openPagePropertiesDialog(pageId); });
	}

  $('.segment>:first-child')
    .before('<div class="segmentEditControls"><a href="#" class="editSegmentLink">Edit</a> <a href="#" class="segmentUpLink">↑</a> <a href="#" class="segmentDownLink">↓</a> <a href="#" class="segmentDeleteLink" alt="Delete segment">X</a></div>');

	//bind events to the controls
	//Edit and Add new segment ops
  var editSegmentHandler = function (event)
  {
    if (!document.isDialogOpen)
    {
      document.isDialogOpen = true;

      //Create dialog with a callback to reload on save and close and to reset the isDialogOpen flag
      var segmentProps = new SegmentPropertiesDialog(
				function () { window.location.reload(true); },
				function () { document.isDialogOpen = false; }
			);

      if (event.data.editSegment)
      {
        var segmentId = getSegmentId(this);
        segmentProps.OpenForEdit(segmentId);
      }
      else
      { segmentProps.OpenForNew(pageId); }
    }
    event.preventDefault();
  };
			
	$('.editSegmentLink').bind('click', { editSegment: true }, editSegmentHandler);
	$('#lnkAddNewSegment').bind('click', { newSegment: true }, editSegmentHandler);
	//End Edit and Add new segment ops
		
	//Move Segment ops
	var moveSegmentHandler = function (event)
	{
	  if (!document.isDialogOpen)
	  {
	    var segmentId = getSegmentId(this);

	    $.ajax({
	      url: '/ContentPage/MoveSegmentUpOrDown',
	      type: 'POST',
	      data: { 'segmentId': segmentId, 'direction': event.data.direction },
	      success: function (data)
	      {
	        window.location.reload(true);
	      }
	    });

	    return false;
	  }
	};

	$('.segmentUpLink').bind('click', {direction: "up"}, moveSegmentHandler);	
	$('.segmentDownLink').bind('click', {direction: "down"}, moveSegmentHandler);
	//End Move Segment ops	
	
	//Delete ops
	$('.segmentDeleteLink').bind('click', function (event)
	{
	  if (!document.isDialogOpen)
	  {
	    if (confirm('Are you sure want to delete this segment? This cannot be undone.'))
	    {
	      var segmentId = getSegmentId(this);
	      console.log('Delete segment ' + segmentId);

	      $.ajax({
	        url: '/ContentPage/DeleteSegment',
	        type: 'POST',
	        data: { 'segmentId': segmentId },
	        success: function (data)
	        {
	          window.location.reload(true);
	        }
	      });
	    }

	    event.preventDefault();
	  }
	});

  function getSegmentId(link)
  {
    var idString = link.parentNode.parentNode.id.replace('segment', '');
    return parseInt(idString);
  }
}
