/*!
 * All the cms functions go here
 */



// turn on or off a tinymce editor  
function toggle_tinymce(text_area_id) 
    {
    if (!tinyMCE.get(text_area_id))
        {
        //alert('gotta create the tinymce!');
        tinyMCE.execCommand('mceAddControl', false, text_area_id);
        }
    else 
        {
        //alert('gotta destroy the tinymce!');
        tinyMCE.execCommand('mceRemoveControl', false, text_area_id);
        }
    }
      
function toggle_tinymce_instance(element)
  {
  tinyMCE.execCommand('mceAddControl',false, 'textarea'+$(element).html());
  }

    
function reload_content(old_id, new_id, stream_id)
          {
          //alert(stream_id);
            $.ajax
            ({
              type: 'POST',
              url: 'display_content_item.php',
              data: "id=" + new_id + "&stream_id=" + stream_id,
              success:   function(data) 
                            {
                              $("#content_item_container"+old_id).replaceWith(data);
                              
                            }
            });
          }



  
function edit_content(id, stream_id)
        {
        $.ajax
            ({
              type: 'POST',
              url: 'edit_content.php',
              data: "id=" + id + "&content=" + escape(tinyMCE.get('textarea'+id).getContent()) ,
              success:   function(data) 
                            {
                            toggle_tinymce('textarea'+id);
                            reload_content(id, data, stream_id);
                            }
            });
        }
    
    
    
function delete_content(id)
        {
          $.ajax
            ({
              type: 'POST',
              url: 'delete_content.php',
              data: "id=" + id ,
              success:   function(data) 
                            {
                            alert("Content deleted.");
                            tinyMCE.execCommand('mceRemoveControl',false, 'textarea'+id);
                            ContentStack.splice(query_array('content_item_container'+id, ContentStack), 1);
                            }
            });
        }
        

    
function cancel(id)
        {
         //alert(id);
         tinyMCE.get('textarea'+id).setContent($("#content_item"+id).html());
         hide('#content_editor'+id); 
         hide('#content_item'+id); 
         hide('.edit_button'); 
         toggle_edit_mode(); 
         toggle_tinymce('textarea'+id); 
        }
        
        
// create a new content item
function create_content(stream_id)
        {
          $.ajax
            ({
              type: 'POST',
              url: 'create_content.php',
              data: "content=" + escape(tinyMCE.get('tinymce_new_content').getContent()) +"&"+ "stream_id=" +stream_id,
              success:   function(data) 
                            {
                              $("#content_container").append(data);
                              tinyMCE.get('tinymce_new_content').setContent("");
                              $('#new_id').remove();
                              toggle_tinymce('tinymce_new_content');
                            } 
            });
        }
        

  
// hide block elements        
function hide(element) 
      {
      if ($(element).css("display") != "none")
        {
        $(element).css("display", "none");
        }
      else
        {
        $(element).css("display", "block");
        }
      }
      

// hide inline elements      
function hide_inline(element) 
      {
      if ($(element).css("visibility") != "hidden")
        {
        $(element).css("visibility", "hidden");
        }
      else
        {
        $(element).css("visibility", "visible");
        }
      }


// toggle the edit mode      
var edit_mode = false;
function toggle_edit_mode()
        {
        if (edit_mode == false)
            {
            edit_mode = true;
            $('.content_item_container').css('background', '');
            $('.edit_button').css('display', 'none');
            }
         else
            {
            edit_mode = false;
            $('.content_item_container').css('background', '');
            $('.edit_button').css('display', 'none');
            }
        }
     

function toggle_content_item_controls(id, mouse)
       {
       if (edit_mode != true)
          {
          if (mouse == 'on')
              {
              $('#content_item_container'+id).css('background', '#ccc'); 
              $('#edit_button'+id).css('display','inline');
              $('#item_up'+id).css('display','inline');
              $('#item_down'+id).css('display','inline');
              }
            else
              {
              $('#content_item_container'+id).css('background', ''); 
              $('#edit_button'+id).css('display','none');
              $('#item_up'+id).css('display','none');
              $('#item_down'+id).css('display','none');
              }
           //$(this).css('background', ''); hide('#edit_button$id')
          }
       }


//toggle controls for the page title       
function page_title_controls(id, mouse)
       {
       if (edit_mode != true)
          {
          if (mouse == 'on')
              {
              $('#content_item_container'+id).css('background', '#cbfecb'); 
              $('#edit_button'+id).css('display','inline');
              $('#title_toggle_button'+id).css('display','inline');
              $('#item_down'+id).css('display','inline');
              }
            else
              {
              $('#content_item_container'+id).css('background', ''); 
              $('#edit_button'+id).css('display','none');
              $('#title_toggle_button'+id).css('display','none');
              $('#item_down'+id).css('display','none');
              }
           //$(this).css('background', ''); hide('#edit_button$id')
          }
       }
       

    
// update content order in the database
function edit_content_order(item_id, direction, stream_id)
        {
         $.ajax
            ({
              type: 'POST',
              url: 'edit_content_order.php',
              data: "content_id=" + item_id +"&"+ "direction=" +direction +"&"+ "stream_id=" +stream_id,
              success:   function(data) 
                            {
                              //alert(data);
                            } 
            });
        }
 
 
// create an array of all of the content items in a specific stream
function get_stream_content_order(stream)
    {
    //alert(stream);
    var ContentStack = new Array();
    var i = 0;
    $(stream + ' .content_item_container').each(function()
        { 
        ContentStack[i] = this.id;
        i++;
        })
    return ContentStack;
    }
    
    
            
// Swap content       
function swap_content(content1, direction) 
    {
    var ContentStack = get_stream_content_order('#content_container')
    content1_location = query_array(content1 , ContentStack);
    if (direction == "up")
        {
        var content2_location = content1_location - 1;
        //alert('hi');
        }
    else
        {
        content2_location = content1_location + 1;
        }  
    content2 = ContentStack[content2_location];
    
    if ((content2_location >= 0) && (content2_location < ContentStack.length))
        {
        /*$("#"+content1).swap({  
            target: content2, // Mandatory. The ID of the element we want to swap with  
            opacity: "0.5", // Optional. If set will give the swapping elements a translucent effect while in motion  
            speed: 750 // Optional. The time taken in milliseconds for the animation to occur  
        });  
        */
        //alert("hi"+$('.content_item_container').margin());
        var position1 = $('#'+ContentStack[content1_location]).position();
        var height1   = $('#'+ContentStack[content1_location]).height();
        var height2   = $('#'+ContentStack[content2_location]).height();
        var position2 = $('#'+ContentStack[content2_location]).position();
        //alert((position2.top + height2) + (position1.top + height1));
        //alert(position2.top);
                
        if (direction == "down")
          {          
              $('#'+ContentStack[content1_location]).animate({
                
                top: '-=' + ((position1.top + height1) - (position2.top + height2)),
                //top: '-=' + (0)
                
              }, 600, function() {
                $('#'+ContentStack[content1_location]).insertBefore($('#'+ContentStack[content2_location]));
              });
                    
              $('#'+ContentStack[content2_location]).animate({
                
                top: '-=' + (position2.top - position1.top)
                //top: '-=' + (0)
                
              }, 600, function() 
                {
                $('#'+ContentStack[content1_location]).css('top', '0px');
                $('#'+ContentStack[content2_location]).css('top', '0px');
                });        
         
          }
        else
          {
          $('#'+ContentStack[content2_location]).animate({
                
                //top: '+=' + ($('#'+ContentStack[content1_location]).height() + 21),
                top: '+=' + ((position1.top + height1) - (position2.top + height2)),
                
              }, 600, function() {
                $('#'+ContentStack[content1_location]).insertAfter($('#'+ContentStack[content2_location]));
              });
                    
              $('#'+ContentStack[content1_location]).animate({
                
                //top: '-=' + ($('#'+ContentStack[content2_location]).height() + 21),
                top: '+=' + (position2.top - position1.top)
                
              }, 600, function() 
                {                
                $('#'+ContentStack[content2_location]).css('top', '0px');
                $('#'+ContentStack[content1_location]).css('top', '0px');
                });        
         
          }
          
        var temp_var = ContentStack[content1_location];
        ContentStack[content1_location] = ContentStack[content2_location];
        ContentStack[content2_location] = temp_var;
        
        //alert (ContentStack[0] + " " + ContentStack[1] + " " + ContentStack[2] + " " + ContentStack[3]);
        }
    }

 
//find location of a content item in an array
function query_array(content_item, arr1)
    {
    for (i=0 ; i<arr1.length ; i++)
      {
      if (arr1[i] == content_item)
          {
         // alert(i);
          return i;
          }
      }
    }
   

// hide controls for all nav items       
function hide_nav_items_controls()
        {
        $('.edit_button').css('display','none');
        $('.subpage_button').css('display','none');
        }
        
        
        
// Drag and sort for navigation menus
	$(function() {
    // Primary navigation drag and sort
		$( "#primary_nav_tabs .navlist").sortable({ 
		axis: "x", containment: "#primary_nav_tabs .navlist", scroll: false, 
		items: 'li:not(.non-sortable)',
		update: 
		function(event, ui) { 
		var order = $('#primary_nav_tabs .navlist').sortable('toArray'); 
		$.ajax
            ({
              type: 'POST',
              url: 'update_page_order.php',
              data: {order : order},
              success:   function(data) {}
            });
    }});
		$( "#primary_nav_tabs .navlist").disableSelection();
		
// Secondary navigation drag and sort
		$( "#sidebar_secondary_nav .navlist").sortable({ 
		axis: "y", containment: "#sidebar_secondary_nav .navlist", scroll: false, 
		items: 'li:not(.non-sortable)',
		update: 
		function(event, ui) { 
		var order = $('#sidebar_secondary_nav .navlist').sortable('toArray'); 
		$.ajax
            ({
              type: 'POST',
              url: 'update_page_order.php',
              data: {order : order},
              success:   function(data) {}
            });
    }});
		$( "#sidebar_secondary_nav .navlist").disableSelection();
		
// Third-level navigation drag and sort
		$( "#third_nav_sequence .navlist").sortable({ 
		axis: "x", containment: "#third_nav_sequence .navlist", scroll: false, 
		items: 'li:not(.non-sortable)',
		update: 
		function(event, ui) { 
		var order = $('#third_nav_sequence .navlist').sortable('toArray'); 
		$.ajax
            ({
              type: 'POST',
              url: 'update_page_order.php',
              data: {order : order},
              success:   function(data) {}
            });
    }});
		$( "#third_nav_sequence .navlist").disableSelection();
		
	});
