if(jQuery.fn.tooltip) {
jQuery.fn.tooltip = jQuery.fn.tooltip.noConflict();
}

/* if(jQuery.fn.tooltip) {
jQuery.fn.tlp = jQuery.fn.tooltip.noConflict();
jQuery(function($){
    $(".hasTooltip").tlp({"html": true,"container": "body"});
});
} */

if(jQuery.fn.button) {
jQuery.fn.button = jQuery.fn.button.noConflict();
}
function getURLVar(key) {
	var value = [];

	var query = String(document.location).split('?');

	if (query[1]) {
		var part = query[1].split('&');

		for (i = 0; i < part.length; i++) {
			var data = part[i].split('=');

			if (data[0] && data[1]) {
				value[data[0]] = data[1];
			}
		}

		if (value[key]) {
			return value[key];
		} else {
			return '';
		}
	}
}
function ocFileManager(field_name, url, type, win) {
	//alert(field_name + ':' + url + ':' + type + ':' + win);
	jQuery('#modal-image').remove();	
	jQuery.ajax({
		url: 'index.php?page=joocart&tmpl=component&route=common/filemanager&user_token=' + getURLVar('user_token') +  '&target='+field_name,
		dataType: 'html',
		beforeSend: function() {
			jQuery('#button-image i').replaceWith('<i class="fa fa-circle-o-notch fa-spin"></i>');
			jQuery('#button-image').prop('disabled', true);
		},
		complete: function() {
			jQuery('#button-image i').replaceWith('<i class="fa fa-upload"></i>');
			jQuery('#button-image').prop('disabled', false);
		},
		success: function(html) {
			jQuery('body').append('<div id="modal-image" class="modal-oc" style="z-index:99999;">' + html + '</div>');

			jQuery('#modal-image').modal('show');
		}
	});	
	return false;
}
jQuery(document).ready(function() {
	//Form Submit for IE Browser
	jQuery('button[type=\'submit\']').on('click', function() {
		jQuery("form[id*='form-']").submit();
	});

	// Highlight any found errors
	jQuery('.text-danger').each(function() {
		var element = jQuery(this).parent().parent();

		if (element.hasClass('form-group')) {
			element.addClass('has-error');
		}
	});

	// tooltips on hover
	if(jQuery('[data-toggle=\'tooltip\']').tooltip) {
	jQuery('[data-toggle=\'tooltip\']').tooltip({container: '.body-oc', html: true});
	}
	// dropdown on click
	if(jQuery('[data-toggle=\'dropdown\']').dropdown) {
	jQuery('[data-toggle=\'dropdown\']').dropdown();
	}
	jQuery('[data-target=\'.nav-collapse\']').click(function(e) {
		jQuery(this).removeAttr("data-toggle");
		var collapsable_div = jQuery(".nav-collapse");
		if(collapsable_div && collapsable_div.css('height')=="0px")
		collapsable_div.attr( "style", "height: auto !important;" );
		else
		collapsable_div.attr( "style", "height: 0px !important;" );
	});
	jQuery('#menu-oc [data-toggle=\'collapse\']').click(function(e) {
		if(jQuery(this).attr("aria-expanded")== "true") {
			var collapsable_div = jQuery(jQuery(this).attr("href"));
			if(collapsable_div.attr("style") == "display: block;" || collapsable_div.attr("style") == "display:block;"){
				collapsable_div.attr("style", "");
			}
		}
	});
	
	// Makes tooltips work on ajax generated content
	jQuery(document).ajaxStop(function() {
		if(jQuery('[data-toggle=\'tooltip\']').tooltip) {
		jQuery('[data-toggle=\'tooltip\']').tooltip({container: '.body-oc'});
		}
	});

	// https://github.com/opencart/opencart/issues/2595
	jQuery.event.special.remove = {
		remove: function(o) {
			if (o.handler) {
				o.handler.apply(this, arguments);
			}
		}
	}
	
	// tooltip remove
	jQuery('[data-toggle=\'tooltip\']').on('remove', function() {
		jQuery(this).tooltip('destroy');
	});

	// Tooltip remove fixed
	jQuery(document).on('click', '[data-toggle=\'tooltip\']', function(e) {
		jQuery('body > .tooltip').remove();
	});
	
	jQuery('#button-menu').on('click', function(e) {
		e.preventDefault();
		
		jQuery('#column-left').toggleClass('active');
	});

	// Set last page opened on the menu
	jQuery('#menu-oc a[href]').on('click', function() {
		sessionStorage.setItem('menu', jQuery(this).attr('href'));
	});

	if (!sessionStorage.getItem('menu')) {
		jQuery('#menu-oc #dashboard').addClass('active');
	} else {
		// Sets active and open to selected page in the left column menu.
		jQuery('#menu-oc a[href=\'' + sessionStorage.getItem('menu') + '\']').parent().addClass('active');
	}
	
	jQuery('#menu-oc a[href=\'' + sessionStorage.getItem('menu') + '\']').parents('li > a').removeClass('collapsed');
	
	jQuery('#menu-oc a[href=\'' + sessionStorage.getItem('menu') + '\']').parents('ul').addClass('in');
	
	jQuery('#menu-oc a[href=\'' + sessionStorage.getItem('menu') + '\']').parents('li').addClass('active');
	
	// Image Manager
	jQuery(document).on('click', 'a[data-toggle=\'image\']', function(e) {
		var $element = jQuery(this);
		var $popover = $element.data('bs.popover'); // element has bs popover?

		e.preventDefault();

		// destroy all image popovers
		jQuery('a[data-toggle="image"]').popover('destroy');

		// remove flickering (do not re-add popover when clicking for removal)
		if ($popover) {
			return;
		}

		$element.popover({
			html: true,
			sanitize: false,
			placement: 'right',
			trigger: 'manual',
			content: function() {
				return '<button type="button" id="button-image" class="btn btn-primary"><i class="fa fa-pencil"></i></button> <button type="button" id="button-clear" class="btn btn-danger"><i class="fa fa-trash-o"></i></button>';
			}
		});

		$element.popover('show');

		setTimeout(function(){ // fix bind events on new popover when 

			jQuery('#button-image').on('click', function() {
				var $button = jQuery(this);
				var $icon   = $button.find('> i');

				jQuery('#modal-image').remove();

				jQuery.ajax({
					url: 'index.php?page=joocart&tmpl=component&route=common/filemanager&user_token=' + getURLVar('user_token') + '&target=' + $element.parent().find('input').attr('id') + '&thumb=' + $element.attr('id'),
					dataType: 'html',
					beforeSend: function() {
						$button.prop('disabled', true);
						if ($icon.length) {
							$icon.attr('class', 'fa fa-circle-o-notch fa-spin');
						}
					},
					complete: function() {
						$button.prop('disabled', false);

						if ($icon.length) {
							$icon.attr('class', 'fa fa-pencil');
						}
					},
					success: function(html) {
						jQuery('body').append('<div id="modal-image" class="modal-oc">' + html + '</div>');

						jQuery('#modal-image').modal('show');
					}
				});

				$element.popover('destroy');
			});

			jQuery('#button-clear').on('click', function() {
				$element.find('img').attr('src', $element.find('img').attr('data-placeholder'));

				$element.parent().find('input').val('');

				$element.popover('destroy');
			});
			
		}, 250); // end timeout fix
			
	});
});

// Autocomplete */
(function($) {
	$.fn.autocomplete = function(option) {
		return this.each(function() {
			var $this = $(this);
			var $dropdown = $('<ul class="dropdown-menu" />');
			
			this.timer = null;
			this.items = [];

			$.extend(this, option);

			$this.attr('autocomplete', 'off');

			// Focus
			$this.on('focus', function() {
				this.request();
			});

			// Blur
			$this.on('blur', function() {
				setTimeout(function(object) {
					object.hide();
				}, 200, this);
			});

			// Keydown
			$this.on('keydown', function(event) {
				switch(event.keyCode) {
					case 27: // escape
						this.hide();
						break;
					default:
						this.request();
						break;
				}
			});

			// Click
			this.click = function(event) {
				event.preventDefault();

				var value = $(event.target).parent().attr('data-value');

				if (value && this.items[value]) {
					this.select(this.items[value]);
					this.hide();
				}
			}

			// Show
			this.show = function() {
				var pos = $this.position();

				$dropdown.css({
					top: pos.top + $this.outerHeight(),
					left: pos.left
				});

				$dropdown.show();
			}

			// Hide
			this.hide = function() {
				$dropdown.hide();
			}

			// Request
			this.request = function() {
				clearTimeout(this.timer);

				this.timer = setTimeout(function(object) {
					object.source($(object).val(), $.proxy(object.response, object));
				}, 200, this);
			}

			// Response
			this.response = function(json) {
				var html = '';
				var category = {};
				var name;
				var i = 0, j = 0;

				if (json.length) {
					for (i = 0; i < json.length; i++) {
						// update element items
						this.items[json[i]['value']] = json[i];

						if (!json[i]['category']) {
							// ungrouped items
							html += '<li data-value="' + json[i]['value'] + '"><a href="#">' + json[i]['label'] + '</a></li>';
						} else {
							// grouped items
							name = json[i]['category'];
							if (!category[name]) {
								category[name] = [];
							}

							category[name].push(json[i]);
						}
					}

					for (name in category) {
						if(category[name]) {
						html += '<li class="dropdown-header">' + name + '</li>';

						for (j = 0; j < category[name].length; j++) {
							html += '<li data-value="' + category[name][j]['value'] + '"><a href="#">&nbsp;&nbsp;&nbsp;' + category[name][j]['label'] + '</a></li>';
						}
						}
					}
				}

				if (html) {
					this.show();
				} else {
					this.hide();
				}

				$dropdown.html(html);
			}

			$dropdown.on('mousedown', '> li > a', $.proxy(this.click, this));
			$this.after($dropdown);
		});
	}
})(window.jQuery);
// If you have MooTools issue then add comments for the following lines
if(typeof MooTools != 'undefined' ) {
	window.addEvent('domready',function() {
		Element.prototype.hide = function() {       
			// Do nothing
		};
	});
}