// Function to remove fields and mark the destroy field to 1
//as seen in question editor
function remove_fields(link) {
  $(link).prev("input[type=hidden]").attr('value', '1');
  $(link).parent(".fields").hide();
  return false;
}

// Function to add fields for a given association and generate a new id for it
//as seen in question editor
function add_fields(link, association, content) {
  var new_id = new Date().getTime();
  var regexp = new RegExp("new_" + association, "g");
  $(link).before(content.replace(regexp, new_id));
  $(".newWysiwyg").tinymce({
        // Location of TinyMCE script
        script_url : '/javascripts/ams/tiny_mce/tiny_mce.js',
  
        // General options
        theme : "advanced",
          skin : "cirkuit",
        plugins : "pagebreak,style,table,advhr,advlink,iespell,inlinepopups,insertdatetime,searchreplace,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras",
  
        // Theme options
        theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,formatselect,fontselect,fontsizeselect",
        theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,cleanup,code,|,insertdate,inserttime",
        theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,iespell,advhr,ltr,rtl,|,fullscreen",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_statusbar_location : "bottom",
        theme_advanced_resizing : false,
  
        // Example content CSS (should be your site CSS)
        //content_css : "/stylesheets/ams/style.css",
  });
  return false;
}

// Function to remove fields and mark the id field to "" thus negating the association
//as seen in question editor
function remove_associate_fields(link) {
  $(link).prev("input[type=hidden]").value = "";
  $(link).parent(".fields").hide();
  return false;
}

function toggleMe(div,t) {
  if (t == null) {
    t = 0;
  }
  $(div).toggle(t);
  return false;
}
$(document).ready(function() {
  // wysywig config function
  $('.wysiwygBasic').tinymce({
      // Location of TinyMCE script
      script_url : '/javascripts/ams/tiny_mce/tiny_mce.js',
    
      // General options
      theme : "advanced",
      skin : "cirkuit",
      plugins : "pagebreak,style,table,advhr,advlink,iespell,inlinepopups,insertdatetime,searchreplace,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras",
    
      // Theme options
      theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,formatselect,fontselect,fontsizeselect,charmap",
      theme_advanced_buttons2 : "",
      theme_advanced_buttons3 : "",
      theme_advanced_toolbar_location : "top",
      theme_advanced_toolbar_align : "left",
      theme_advanced_statusbar_location : "",
      theme_advanced_resizing : false,
    
      // Example content CSS (should be your site CSS)
      //content_css : "/stylesheets/ams/style.css",
  });
});



function goToByScroll(id){
  var target_offset = $("#"+id).offset();
  var target_top = target_offset.top;
 
        //goto that anchor by setting the body scroll top to anchor top
  $('html, body').animate({scrollTop:target_top}, 500);
  //$('html,body').animate({scrollTop: $("#"+id).offset.top},'slow');
}



//Ready all link type JS
function readylinks() {
  // set facebox_iframe_link links to open their pages in facebox_iframe div
  $( "#facebox_iframe" ).dialog({
    autoOpen: false,
    show: "blind",
    hide: "fade",
    modal: "true",
    minWidth: "720",
    minHeight: "500"
  });
  $('.facebox_iframe_link').click(function() {
    var src = $(this).attr("href");
    $("#facebox_iframe_content").attr('src', src);
    $('#facebox_iframe').dialog('open');
    return false;
  });
  
  $(".hsPop").click(function() {
    return hs.htmlExpand(this, { objectType: 'iframe', width: 850, height: 520 } );
  });
  $(".hsPopSmall").click(function() {
    return hs.htmlExpand(this, { objectType: 'iframe', width: 450, height: 200 } );
  });
  $(".hsPopMedium").click(function() {
    return hs.htmlExpand(this, { objectType: 'iframe', width: 500, height: 350 } );
  });
  $(".treeBranchLink").click(function() {
    $(this).parent().find(".treeBranch:first").toggle(1000);
    return false;
  });

    $(".toolTipImage[title]").tooltip({
    effect: 'slide',
    fadeOutSpeed: 100,
    predelay: 100,
    opacity: 0.7,

    // tweak the position
    position: "bottom center"
  });
  $(".toolTipInput[title]").tooltip({
    effect: 'fade',
    fadeOutSpeed: 100,
    predelay: 100,
    opacity: 0.7,

    // tweak the position
    position: "center right",
    offset: [-2, 10]
  });
  $(".toolTipInfo").tooltip({
    effect: 'fade',
    fadeInSpeed: 300,
    fadeOutSpeed: 100,
    predelay: 300,
    direction: 'right',

    // tweak the position
    offset: [0, -80]
  });
  var messageCenter = $(".toolTipMessageCenter").tooltip({
    effect: 'fade',
    fadeInSpeed: 0,
    fadeOutSpeed: 300,
    predelay: 300,
    position: 'bottom right',
      events: { def: 'click, blur' },

    // tweak the position
    offset: [-2, -53],
    api: true
  });
  $('.toolTipMessageCenter').click(function() {
     if (messageCenter.isShown()) {
        messageCenter.hide();
        return false;
     }
  });
  
  $('.toggleLink').click(function() { //toggle content with a link
    $(this).siblings().slideToggle();
    return false;
  });
  $('.newFormLink').click(function() { //show content with a link and remove the link
    $(this).siblings().show(600);
    $(this).hide();
    return false;
  });
  
  // for alert and confirmations
  $(".confirmBox").click( function() {
    var message = this.getAttribute("data-message");
    jConfirm(this.getAttribute("data-message"));
  });
  $(".alertBox").click( function() {
    var message = this.getAttribute("data-message");
    jAlert(this.getAttribute("data-message"));
  });
};

$(document).ready(function(){
  readylinks();
});


// ajax display and hide loading image
$(function() {
  $('a, form').live('ajax:before', function() {
	  $('#loading_image').show();
  });

  $('a, form').live('ajax:complete', function(event, request) {
	  $('#loading_image').hide();

	  var msg = request.getResponseHeader('x-flash');
	  var msgType = request.getResponseHeader('x-flash-type');
	  if (msg) {
		//alert(msgType + ": " + msg);
    };
      
    //ready the links on any rendered content
    readylinks();
  });
});

