(function ($) {
	$.each(['show', 'hide'], function (i, ev) {
		var el = $.fn[ev];
		$.fn[ev] = function () {
			this.trigger(ev);
			return el.apply(this, arguments);
		};
	});
})(jQuery);
	
$.fn.rotate = function(fromangle, toangle, type, duration, easing, complete) {
	var args = $.speed(duration, easing, complete);
	var step = args.step;
	return this.each(function(i, e) {
		args.step = function(now) {
			if(type == 'X') {
				$.style(e, 'transform', 'rotateX(' + now + 'deg)');
			} else if (type == 'Y') {
				$.style(e, 'transform', 'rotateY(' + now + 'deg)');
			} else if (type == 'Z') {
				$.style(e, 'transform', 'rotateZ(' + now + 'deg)');
			} else if (type == 'XY' || type == 'YX') {
				$.style(e, 'transform', 'rotateX(' + now + 'deg) rotateY(' + now + 'deg)');
			} else if (type == 'YZ' || type == 'ZY') {
				$.style(e, 'transform', 'rotateZ(' + now + 'deg) rotateY(' + now + 'deg)');
			} else if (type == 'XZ' || type == 'ZX') {
				$.style(e, 'transform', 'rotateX(' + now + 'deg) rotateZ(' + now + 'deg)');
			} else {
				$.style(e, 'transform', 'rotate(' + now + 'deg)');
			}
			if (step) return step.apply(this, arguments);
		};

		$({deg: fromangle}).animate({deg: toangle}, args);
	});
};


var fm = null;
$(document).ready(function() {
	PNotify.prototype.options.delay = 3000;

	$('#closeAlert').off('click').on('click',hideAlert);
	$('#alert').off('click').on('click',hideAlert);
	
	$('.flashData').off('click').on('click',function() { $(this).remove(); }).slideDown(function() {
		setTimeout(function() {
			$('.flashData').slideUp(function() {
				$(this).remove();
			});
		},5000);
	});
	
  $('.dropdown-submenu a').on("click", function(e){
		var $ul = $(this).next('ul');
			
    $ul.toggle();
		if(	$ul.is(':visible') ) {
			var ul = Math.round($ul.offset().left);
			var uw = $ul.outerWidth();
			var dw = $(document).outerWidth();
			console.log(ul,uw,dw);
			
			if((ul+uw)+dw) {
				$ul.addClass('left');
			}
		} else {
			$ul.removeClass('left');
		}
    e.stopPropagation();
    e.preventDefault();
  });
	
	/**
	* LAYOUT TOGGLER
	*/
	var $trigger = $('.hamburger'), $overlay = $('.overlay'), _is_layout_closed = false;
	$trigger.click(function () {
		hamburger_cross();      
	});

	function hamburger_cross() {
		if (_is_layout_closed == true) {          
			$overlay.hide();
			$trigger.removeClass('is-open');
			$trigger.addClass('is-closed');
			_is_layout_closed = false;
		} else {   
			$overlay.show();
			$trigger.removeClass('is-closed');
			$trigger.addClass('is-open');
			_is_layout_closed = true;
		}
	}

	$('[data-toggle="offcanvas"]').click(function () {
		$('#wrapper').toggleClass('toggled');
	});
});

function LoadingOpen() {
	new PNotify({
		title: "Please wait...",
		type: 'info',
		icon: 'fa fa-spinner fa-spin',
		animate: {
			animate: true,
			in_class: 'flipInX',
			out_class: 'flipOutX'
		}
	});
}	

function LoadingClose() {
	PNotify.removeAll();
}

function Notify(message) {	
	var s = (message.substring(0,7).toUpperCase() == 'SUCCESS') ? true : false;
	
	var $f = $('<div />', { 'class' : 'col-xl-2 col-lg-4 col-md-6 col-sm-8 col-xs-10 flashData '+((s) ? "success" : null), 'id' : 'flash-data' }).css('display','none');
	var $m = $('<div />', { 'class' : 'flashMsg col-xs-10' });
	var $i = $('<div />', { 'class' : 'flashItem col-xs-12' });
				$i.html(message);
				$i.appendTo($m);
			$m.appendTo($f);
	var $c = $('<div />', { 'class' : 'flashClose col-xs-2' });
				$c.html('<span class=\"fa fa-times fa-2x\"></span>');
			$c.appendTo($f);
	
	$f.appendTo('body');
	
	$f.off('click').on('click',function() { $(this).remove(); }).slideDown(function() {
		setTimeout(function() {
			$('.flashData').slideUp(function() {
				$(this).remove();
			});
		},5000);
	});
}	

function showAlert(msg) {
	$('#alert').find('span').html(msg);
	$('#alert').removeClass('hidden');
	$('#alert').show();
}

function hideAlert() {
	$('#alert').hide();
	$('#alert').addClass('hidden');
	$('#alert').find('span').html('');
	
	$('.form-group').removeClass('has-error');
}