$(document).ready(function() {

    addEventListenerToWidgetLinks();

    initTinyMce();

    initAjaxLoading();

    initSelectBoxChange();

    initEditNextButton();

});

function editProfileContent(profileId, elementId, act, type, position, order, event)
{

    cleanInterface();

    switch(act) {

        case 'n':

            $("#act").val('n');
            $("#element_type").val('paragraph');
            createElement(profileId, elementId, 'new', position, order);

            break;

        case 'e':

            $("#act").val('e');
            editElement(profileId, elementId, type, position, order);

            break;

        case 'b':

            $("#act").val('down');
            moveElement('down', elementId, profileId, type);

            break;

        case 'f':

            $("#act").val("up");
            moveElement('up', elementId, profileId, type);

            break;

        case 'd':

            $("#act").val('delete');
            if(confirm('Er du sikker du vil slette dette element?')) {
                 deleteElement(elementId, profileId, type);    
            }else   {
                return false;
            }

            break;
    }

}

function addEventListenerToWidgetLinks()
{

    $(".widgets").click(function(e) {

       var profileId = $(this).closest('div').attr('rel').replace("p_", '');
       var elementId = $(this).closest('div').attr('id').replace("e_", '');
       var act       = $(this).attr('class').replace('widgets ', '');
       var type      = $(this).closest('div').attr('class').replace('widgetLinks', '');
       
       var regExp = new RegExp(/order_[0-9]*/);

       //Get the order
       var order = regExp.exec(type);
       order = order[0].replace("order_", "");

       //Get the type
       type = type.replace(regExp, "");

       var position  = $(this).position();

       editProfileContent(profileId,elementId, act, type, position, order, e);

    });

    $("#cancel").click(function() {

       $("#editor_container").hide();

    });

    $(".new_widget").click(function(e){

       var profileId = $(this).attr('id').replace('p_', '');
       var elementId = 0
       var act       = 'n'
       var type      = 'paragraph';
       var order     = '1';
       var position  = $(this).position();

       editProfileContent(profileId,elementId, act, type, position, order, e);

    });

}

function createElement(profileId, elementId, type, position, order)
{

    $(".element").hide();

    type = jQuery.trim(type);

    $("#element_id").val(null);
    $("#profile_id").val(profileId);

    $("#editor_container").show();
    $("#newtexteditor").show();

    $("#order").val(order);

}

function editElement(profileId, elementId, type, position, order)
{

    //Show the main container
    $(".element").hide();

    type = jQuery.trim(type);

    //Set the select box to the right value and trigger the event
    var selectbox = $("#element_type").val(type);

    //Insert the ids into the hidden elements
    $("#element_id").val(elementId);
    $("#profile_id").val(profileId);

    $.ajax({
       type     : "POST",
       url      : "fileadmin/template/profile/getElementViaAjax.php",
       data     : "element_id=" + elementId + "&type=" + type,
       dataType : 'json',
       success  : function(json){

                      if(json.status == 'true') {

                          populateTinyMCE(json, type);

                      }else {

                          alert(json.error);

                      }

                  },
       error    : function(msg) {

                        alert(msg);

                }
   });

    //Scrape the content from the DOM
    //var contentElement = $("div.#e_" + elementId + " > *:first");
    //contentElement.children('span').remove();
    //var content = contentElement.html();

    //var description = $("div.#e_" + elementId + " > p.description").html();

    //Populate the form with the scraped content
    

    selectbox.change();

    var container = $("#editor_container").show();

}

function moveElement(direction, elementId, profileId, type)
{

    $("#element_id").val(elementId);
    $("#profile_id").val(profileId);
    $("#type").val(type);

    $("form[name='profile_element_form']").attr('action', 'fileadmin/template/profile/moveElement.php')

    $("#submit_form").click();


}

function deleteElement(elementId, profileId, type)
{

    $("#element_id").val(elementId);
    $("#profile_id").val(profileId);
    $("#type").val(type);

    $("form[name='profile_element_form']").attr('action', 'fileadmin/template/profile/deleteElement.php')

    $("#submit_form").click();

}

function initTinyMce()
{

    

}

function initAjaxLoading()
{

    $("#youtube-loading").hide();
    $("#text-loading").hide();
    $("#file-description-loading").hide();
    $("#heading-loading").hide();

    $("#youtube-loading").ajaxStart(function() {

       $(this).show();

    });

    $("#youtube-loading").ajaxStop(function() {

       $(this).hide();

    });

    $("#text-loading").ajaxStart(function() {

       $(this).show();

    });

    $("#text-loading").ajaxStop(function() {

       $(this).hide();

    });

    $("#file-description-loading").ajaxStart(function() {

       $(this).show();

    });

    $("#file-description-loading").ajaxStop(function() {

       $(this).hide();

    });

    $("#heading-loading").ajaxStart(function() {

       $(this).show();

    });

    $("#heading-loading").ajaxStop(function() {

       $(this).hide();

    });

}

function initSelectBoxChange()
{

    $("#element_type").change(function() {

      $(".element").hide();

      //Show the correct editor
      $("." + $(this).val()).show();

   });

}

function initEditNextButton()
{

   $("#edit_next_button").click(function() {

      $("#edit_next").val('true');

   });

}

function cleanInterface()
{

    $("#texteditor textarea").val('');

    $("#file_description").val('');

}

function populateTinyMCE(json, type)
{

switch(type){

        case 'paragraph':

            tinyMCE.execInstanceCommand('text_content', 'mceSetContent', false, json.paragraph);

            break;
        case 'heading':

            tinyMCE.execInstanceCommand('heading_content', 'mceSetContent', false, json.heading);

            break;

        case 'video':

             $("#content-youtube").val('http://www.youtube.com/watch?v=' + json.code);
             tinyMCE.execInstanceCommand('youtube_description', 'mceSetContent', false, json.description);

            break;

        case 'file':

            tinyMCE.execInstanceCommand('file_description', 'mceSetContent', false, json.description);

            break;

    }

}



