/*! LayerSlider 5.1.2
* layerslider.webshopworks.com
* Copyright 2015 WebshopWorks */

imgpath = "";

function lsInsertImage(path) {
	$imgInput.val(path);
	$imgNode.attr('src', path);
	LayerSlider.willGeneratePreview( jQuery('.ls-box.active').index() );
	lsCloseMediaWindow();
}

function lsCloseMediaWindow() {
	var $m = jQuery('#ls-media-window');
	$m.prev().remove();
	$m.remove();
}

jQuery(document).on('click', '.ls-upload', function(e) {
	$imgInput = jQuery(this).prev();
	$imgNode = jQuery(this).children().children();
	// Create window
	jQuery('body').prepend( jQuery('<div>', { 'id' : 'ls-media-window', 'class' : 'ls-modal ls-box' })
		.append( jQuery('<h1>', { 'class' : 'header', 'text' : 'Image Manager' })
			.append( jQuery('<a>', { 'text' : 'x' }).click(lsCloseMediaWindow))
		)
		.append( jQuery('<div>')
			.css('overflow', 'hidden')
			.append( jQuery('<iframe>').attr({
				width: '100%',
				height: '531',
				src: location.href.split('controller')[0] + 'controller=AdminLayerSliderMedia'
			}).css('border', 'none'))
		)
	);

	// Create overlay
	jQuery('body').prepend( jQuery('<div>', { 'class' : 'ls-overlay'}).click(lsCloseMediaWindow));
});

(function($, undefined) {

  $.fn.inputsToObj = function() {
    var obj = {};
    this.find(':input').each(function() {
      obj[this.name] = this.type == 'checkbox' ? this.checked : this.value;
    });
    return JSON.stringify(obj);
  };

  $.fn.objToInputs = function(obj) {
    if (!obj) return alert("There is nothing to paste!");
    obj = JSON.parse(obj);
    var $input, prop;
    for (prop in obj) {
      $input = this.find('[name="'+prop+'"]');
      if (typeof obj[prop] == 'boolean') {
        if ($input[0].checked != obj[prop]) $input.next().click();
      }
      else $input.val(obj[prop]);
    }
    $input.trigger("keyup");
  };

  $.ui.draggable.prototype.options.delay = 100;
  $.ui.sortable.prototype.options.delay = 100;

})(jQuery);


jQuery(function($) {
  // init selectable
  var selected = [],
      activeTab = 0,
      $activeTab = $('.ls-layer-box.active');
  $('#ls-pages')
    .on('mousedown', '.ls-preview .ui-draggable', function(e) {
      e.stopPropagation();
      $activeTab.find('.ls-selected').removeClass('ls-selected');
      selected[activeTab] = $(this).addClass('ls-selected').index();
    })
    .on("mousedown", ".ls-preview-td", function(e) {
      e.stopPropagation();
      $activeTab.find('.ls-selected').removeClass('ls-selected');
      selected[activeTab] = undefined;
    })
    .on('mouseup', '.ls-sublayers > tr', function(e) {
      var i = $(this).index();
      $activeTab.find('.ls-layer > :eq('+i+')').trigger('mousedown');
    })
    // handle tab changing
    .on('click', '#ls-layer-tabs a', function(e) {
      activeTab = $(this).index();
      $activeTab = $('.ls-layer-box.active');
      var i = selected[activeTab];
      if (i) $activeTab.find('.ls-layer > :eq('+i+')').trigger('mousedown');
    });

  $(document.body).on('keypress', function(e) {
    if ($(e.target).is(':input')) return;
    var $layer = $('.ls-layer-box.active .ls-selected');
    if (!$layer.length) return;
    var i = 0, j = 0;
    switch (e.keyCode) {
      case 37: i--; break; // left
      case 38: j--; break; // up
      case 39: i++; break; // right
      case 40: j++; break; // down
      case 46: // delete
        return $activeTab.find('tr.active .remove').click();
    }
    if (i) {
      var x = parseInt($layer.css('left'));
      x += e.shiftKey? i*10 : i;
      $layer.css('left', x);
      var i = selected[activeTab];
      if (i) $activeTab.find('input[name=left]:eq('+i+')').val(x + 'px');
      return e.preventDefault();
    }
    if (j) {
      var y = parseInt($layer.css('top'));
      y += e.shiftKey? j*10 : j;
      $layer.css('top', y);
      var i = selected[activeTab];
      if (i) $activeTab.find('input[name=top]:eq('+i+')').val(y + 'px');
      return e.preventDefault();
    }
  });

	// refresh module position on listing page
	var $listPage = $('#ls-list-page');
	$listPage.find('.ls-hook').one('mousedown', function() {this.origVal = this.value});
	$listPage.on('change', '.ls-hook', function() {
		$(this).next().css('visibility', this.value == this.origVal ? 'hidden' : 'visible');
	});
	$listPage.on('click', '.ls-hook-update', function() {
		var $select = $(this).prev();
		location.href = location.href + '&action=modulePosition&id='+$select.data('id')+'&hook='+$select.val();
	});

  // INIT HELP
  $('#screen-meta').on('click', '.contextual-help-tabs a', function(e) {
    var $this = $(this);
    $('#screen-meta .active').removeClass('active');
    $this.addClass('active');
    $($this.attr('href')).addClass('active');
    e.preventDefault();
  });
  // init first tab
  $('.contextual-help-tabs a:first, .help-tab-content:first').addClass('active');
  $('.show-settings').on("click", function(e) {
    $($(this).attr('href')).slideToggle(250);
    e.preventDefault();
  });
});