/* custompage owner scripts */
function custompageinit(currentroot,currentpage,currentprofilename){
$('document').ready(function(){
/* custompage menu ajax */
function refreshServices(){
  var serv = new Array();
  $('#cp_container_topwide,#cp_container_left,#cp_container_middle,#cp_container_right').each(function(){
    var columnservices = new Array();
    $(this).children('.cp_menu').children('.cp_content').each(function(){
      columnservices.push($(this).attr('id').replace('cps',''));
    });
    serv[$(this).attr('id')]=columnservices.join();
  });
  $.cookie('cp_'+currentpage+'_'+currentprofilename,
    serv['cp_container_topwide'] + '|' +
    serv['cp_container_left'] + '|' +
    serv['cp_container_middle'] + '|' +
    serv['cp_container_right'] + '|' +
    $('#cp_container_left').width()+','+$('#cp_container_middle').width()+','+$('#cp_container_right').width()
    );
}

/* custompage menu drag and drop */
$('.cp_menu').each(function(){
  $(this).children('.cp_content').find('.cp_dragger').css('cursor','move');
  $(this).sortable({
    appendTo:'body',
    connectWith:[$('.cp_menu')],
    handle:'.cp_dragger',
    helper:function(e,el){
      return '<div class="cp_content"><div><div class="cp_dragger"></div><div class="cp_title" style="padding-left:30px;white-space:nowrap;">'+$(el).find('.cp_title').html()+'</div></div>';
    },
    scroll:true,
    start:function(e,ui){
      $('.cp_menu .cp_inner').find('script').remove();
      $('.cp_menu').addClass('cp_menu_able');
      $('#cp_container_right')
      .prev('.cp_container_resizer').andSelf()
        .removeClass('cp_container_hidden');
    },
    stop:function(e,ui){
      $('.cp_menu').removeClass('cp_menu_able');
      if($('#cp_container_right .cp_content').length > 0){
        $('#cp_container_right').prev('.cp_container_resizer').andSelf().removeClass('cp_container_hidden');
      } else {
        $('#cp_container_right').prev('.cp_container_resizer').andSelf().addClass('cp_container_hidden');
      }
      refreshServices();
    },
    zIndex:2000
  });
});

/* custompage left,middle,right container resizer */
$('.cp_container_resizer').each(function(){
  var mousex;
  var resize_left = $(this).prev('.cp_container');
  var resize_right = $(this).next('.cp_container');

  $(this)
    .css('cursor','w-resize')
    .hover(
      function(){
        $(this).addClass('cp_container_resizer_hover');
      },
      function(){
        $(this).removeClass('cp_container_resizer_hover');
      }
    )
    .mouseInteraction({
      executor: this,
      delay: 0,
      distance: 0,
      dragPrevention:['input','textarea','button','select','option'],
      start:function(e){
        mousex = e.pageX;
        $('.cp_menu').addClass('cp_menu_able');
        $(this).addClass('cp_container_resizer_active');
      },
      stop:function(e){
        $('.cp_menu').removeClass('cp_menu_able');
        $(this).removeClass('cp_container_resizer_active');
        refreshServices();
      },
      drag:function(e){
        var dx = mousex-e.pageX;
        var tempwidth = 0;
        if(dx<0){
          tempwidth = resize_right.width();
          resize_right.width(tempwidth+dx);
          tempwidth = - tempwidth + resize_right.width();
          resize_left.width(resize_left.width() - tempwidth);
        } else if(dx>0){
          tempwidth = resize_left.width();
          resize_left.width(tempwidth-dx);
          tempwidth = tempwidth - resize_left.width();
          resize_right.width(resize_right.width() + tempwidth);
        }
        mousex = e.pageX - tempwidth + dx;
      }
    });
});
});
}