function addPageContentGetter(js_method){
  $.post('/page_contents/show_page_toolbar', function(data){
    $('#cms-build_page_toolbar').append(data);    
    $('.cms-toolbar_option').each(function(){
      $(this).addClass('component').draggable({revert: true});
    });
  });

  $('#cms-build_page_toolbar').draggable();		 

  $('.cms-editable').addClass('edidable').each(function(i){
    var iteration = i;
    var page_type = $('#page_type').val();
    var page_id = $('#page_id').val();
    var page_content_id = $(this).attr('id');    

    $(this).droppable({
      accept: ".component",
      drop: function(event,ui) {
        var section_name = ui.helper.attr('href');
	$.post('/'+section_name+'_section_holders/'+section_name+'_form', {page_content_id: page_content_id, page_type: page_type, page_id: page_id}, function(data){
          makeDivModal('cms-manage_content_form');
          $('#cms-manage_content_form').html(data);
        })
      }
    });
    $.post('/page_contents/add_getter', {page_content_id: page_content_id, page_id: page_id, page_type: page_type}, function(data){
      $('#'+page_content_id).html(data.content);
      if (iteration == ($('.cms-editable').length -1)){
        eval(js_method);
      }
    },'json');
  });
}


function addGetter(js_method){
  $('.cms-editable').each(function(i){
    var iteration = i;
    var page_type = $('#page_type').val();
    var page_id = $('#page_id').val();
    var page_content_id = $(this).attr('id');    

    $.post('/page_contents/add_getter', {page_content_id: page_content_id, page_id: page_id, page_type: page_type, options: "live"}, function(data){  
      $('#'+page_content_id).html(data);
      if (iteration == ($('.cms-editable').length -1)){
        eval(js_method);
      }
    });
  });
}

function toggleAndShowContent(current_link, menu_section_holder_id, article_id, section_id, effect_type){
  $(current_link).addClass('current_page_item');
  if (effect_type){
    runEffect(effect_type,section_id);
  }

  $.post('/content_text_section_holders/fill_content_text_holders', {menu_section_holder_id: menu_section_holder_id, article_id: article_id}, function(data){  
   for(i=0;i<data.content_text_holders.length;i++){
     $('#'+data.content_text_holders[i]).html(data.article_content);
   }
  },"json");

}

			
function runEffect(selectedEffect,sectionId){
  //if (!selectedEffect){
  //  selectedEffect = 'blind';
  //}

  //most effect types need no options passed by default
  var options = {};
  //check if it's scale, transfer, or size - they need options explicitly set
  if(selectedEffect == 'scale'){  options = {percent: 0}; }
  else if(selectedEffect == 'size'){ options = { to: {width: 200,height: 60} }; }

  $('#'+sectionId).toggle(selectedEffect,options,500);      
}
			

function editMenu(menu_id,html_keeping_element){
  $.post('/menu_section_holders/edit_form', {id: menu_id}, function(data){
    makeDivModal('cms-manage_content_form');
    $('#cms-manage_content_form').html(data);

  }); 
}

function removeMenu(menu_id,html_keeping_element){
  $.post('/menu_section_holders/destroy', {id: menu_id}, function(data){
    $('#'+html_keeping_element).remove();
  }); 
}

function editSubMenu(sub_menu_id,html_keeping_element){
  $.post('/sub_menu_section_holders/edit_form', {id: sub_menu_id}, function(data){
    makeDivModal('cms-manage_content_form');
    $('#cms-manage_content_form').html(data);

  }); 
}

function removeSubMenu(sub_menu_id,html_keeping_element){
  $.post('/sub_menu_section_holders/destroy', {id: sub_menu_id}, function(data){
    $('#'+html_keeping_element).remove();
  }); 
}

function referencesPage(menu_section_holder_id, article_id, section_id){
  $.post('/article_menu_section_holders/references_page_form', {menu_section_holder_id: menu_section_holder_id, article_id: article_id, section_id: section_id}, function(data){
    makeDivModal('cms-manage_content_form');
    $('#cms-manage_content_form').html(data);
  })
}


function removeReferencesPage(article_menu_section_holder_id){
  $.post('/article_menu_section_holders/remove_references', {id: article_menu_section_holder_id}, function(data){
    window.location.reload();
  },'json')
}

function editSnippet(snippet_section_holder_id,html_keeping_element){
  $.post('/snippet_section_holders/edit_form', {id: snippet_section_holder_id}, function(data){
    makeDivModal('cms-manage_content_form');
    $('#cms-manage_content_form').html(data);
  }); 
}

function removeSnippet(snippet_section_id,html_keeping_element){
  $.post('/snippet_section_holders/destroy', {id: snippet_section_id}, function(data){
    $('#'+html_keeping_element).remove();
  }); 
}

function editFreeText(free_text_id,html_keeping_element){
  $.post('/free_text_section_holders/edit_form', {id: free_text_id}, function(data){
    makeDivModal('cms-manage_content_form');
    $('#cms-manage_content_form').html(data);
  }); 
}

function removeFreeText(free_text_id,html_keeping_element){
  $.post('/free_text_section_holders/destroy', {id: free_text_id}, function(data){
    $('#'+html_keeping_element).remove();
  }); 
}

function editContentText(content_text_id,html_keeping_element){
  $.post('/content_text_section_holders/edit_form', {id: content_text_id}, function(data){
    makeDivModal('cms-manage_content_form');
    $('#cms-manage_content_form').html(data);
  }); 
}

function removeContentText(content_text_id,html_keeping_element){
  $.post('/content_text_section_holders/destroy', {id: content_text_id}, function(data){
    $('#'+html_keeping_element).remove();
  }); 
}

function editPoll(poll_id,html_keeping_element){
  $.post('/poll_section_holders/edit_form', {id: poll_id}, function(data){
    makeDivModal('cms-manage_content_form');
    $('#cms-manage_content_form').html(data);

  }); 
}

function removePoll(poll_id,html_keeping_element){
  $.post('/poll_section_holders/destroy', {id: poll_id}, function(data){
    $('#'+html_keeping_element).remove();
  }); 
}



function editNewsletter(newsletter_id,html_keeping_element){
  $.post('/newsletter_section_holders/edit_form', {id: newsletter_id}, function(data){
    makeDivModal('cms-manage_content_form');
    $('#cms-manage_content_form').html(data);

  }); 
}

function removeNewsletter(newsletter_id,html_keeping_element){
  $.post('/newsletter_section_holders/destroy', {id: newsletter_id}, function(data){
    $('#'+html_keeping_element).remove();
  }); 
}


function editSearch(search_id,html_keeping_element){
  $.post('/search_section_holders/edit_form', {id: search_id}, function(data){
    makeDivModal('cms-manage_content_form');
    $('#cms-manage_content_form').html(data);

  }); 
}

function removeSearch(search_id,html_keeping_element){
  $.post('/search_section_holders/destroy', {id: search_id}, function(data){
    $('#'+html_keeping_element).remove();
  }); 
}


function showCategoryArticles(menu_section_holder_id, category_id){
  $.post('/content_text_section_holders/get_content_text_holder', {menu_section_holder_id: menu_section_holder_id}, function(result){  
	  $.post('/categories/get_content', {id: category_id, menu_section_holder_id: menu_section_holder_id, content_text_holder: result.content_text_holder}, function(response){
      $('#'+result.content_text_holder).css('background-image','').append(response);
    });
  },"json");
}

function showArticleContent(menu_section_holder_id, article_id){
  $.post('/content_text_section_holders/get_content_text_holder', {menu_section_holder_id: menu_section_holder_id}, function(result){  
    $.post('/articles/get_content', {id: article_id}, function(response){
      $('#'+result.content_text_holder).html(response);
    })
  },"json"); 
}


function editForm(form_id, html_keeping_element){
  $.post('/form_section_holders/edit_form', {id: form_id}, function(data){
    makeDivModal('cms-manage_content_form');
    $('#cms-manage_content_form').html(data);
  }); 
}

function removeForm(form_id,html_keeping_element){
  $.post('/form_section_holders/destroy', {id: form_id}, function(data){
    $('#'+html_keeping_element).remove();
  }); 
}


function editLanguagesSection(language_section_id,html_keeping_element){
  $.post('/languages_section_holders/edit_form', {id: language_section_id}, function(data){
    makeDivModal('cms-manage_content_form');
    $('#cms-manage_content_form').html(data);

  }); 
}

function removeLanguagesSection(language_section_id,html_keeping_element){
  $.post('/languages_section_holders/destroy', {id: language_section_id}, function(data){
    $('#'+html_keeping_element).remove();
  }); 
}

function displayArticleContent(article_id,menu_section_holder_id){
  var page_type = $('#page_type').val();
  var page_id = $('#page_id').val();

  $.post('/articles/display_article_content', {page_type: page_type, page_id: page_id, menu_section_holder_id: menu_section_holder_id, article_id: article_id}, function(data){
    if (data.success){
      $('li#article_'+article_id+menu_section_holder_id).addClass('current_page_item');
      $('#'+data.content_text_holder).css('background-image','');
      $('#'+data.content_text_holder).append(data.content);
    }
  },'json');  
}

function displayGalleryContent(gallery_id,menu_section_holder_id){
  var page_type = $('#page_type').val();
  var page_id = $('#page_id').val();

  $.post('/galleries/display_gallery_content', {page_type: page_type, page_id: page_id, menu_section_holder_id: menu_section_holder_id, gallery_id: gallery_id}, function(data){
    if (data.success){
      $('li#article_'+gallery_id+menu_section_holder_id).addClass('current_page_item');
      $('#'+data.content_text_holder).css('background-image','');
      $('#'+data.content_text_holder).append(data.content);
    }
  },'json');  
}


function onlyOneMainForPage(element){  
    if (element.checked == true){
      element.checked = true;
    } else {
      element.checked = false;
    }

    $('.cms-main_on_page').each(function(){
      if (this != element){
        this.checked = false;
      }
    });
    
}
 
function displayMainContent(){
  var page_type = $('#page_type').val();
  var page_id = $('#page_id').val(); 
  
  var location = window.location.toString();

  if (!location.match('article')){

    $.post('/page_contents/display_main_content', {page_type: page_type, page_id: page_id}, function(data){
      if (data.success){
        var content_text_holder = data.content_text_holder
	$.post('/'+data.content_type+'/get_content', {id: data.content_id, content_text_holder: content_text_holder, display_main_content: true, menu_section_holder_id: data.menu_section_holder_id}, function(response){
         $('#'+content_text_holder).css('background-image','').append(response);
        })
      }
  },"json");
  }
}


function makeMenuSuperfish(menu_section_holder_html_keeping_element,menu_section_holder_effect_type){
    $("."+menu_section_holder_html_keeping_element+"-"+menu_section_holder_effect_type+"-menu").supersubs({minWidth: 12,maxWidth: 30,extraWidth: 1,autoArrows: true}).superfish();
}


function addSubMenu(menu_id,html_keeping_element){
  $.post('/menu_section_holders/add_submenu_form', {id: menu_id}, function(data){
    makeDivModal('cms-manage_content_form');
    $('#cms-manage_content_form').html(data);
  }); 
}


function checkMenuName(element){
  var menu_name = $(element).val();

  $.post('/menu_section_holders/check_menu_name', {menu_name: menu_name}, function(data){
    if (data.success) {
      $("#menu_section_holder_name").val(data.menu_name);    	  	        
    }
  },'json');  

  return false;
}
