(function(){
					
var jQuery = window.top.jQuery;

if (!jQuery) return;

//Iframe DomReady event.
if (document.addEventListener){
	var DOMContentLoaded = function(){
		document.removeEventListener('DOMContentLoaded', DOMContentLoaded, false);
		jQuery(window).trigger('iframeready').unbind('iframeready');
	}
	document.addEventListener('DOMContentLoaded', DOMContentLoaded, false)
}
else if (document.attachEvent){
	var DOMContentLoaded = function(){
		if (document.readyState === "complete"){
			document.detachEvent("onreadystatechange", DOMContentLoaded);
			jQuery(window).trigger('iframeready').unbind('iframeready');
		}
	}
	document.attachEvent('onreadystatechange', DOMContentLoaded, false)
}

//Fallback to load event.
jQuery(window).bind('load', function(){
	jQuery(window).trigger('iframeready').unbind('iframeready');
});

jQuery(window).bind('iframeready', function(){
	//Menu/
	jQuery(document).find('a.ekm-edit-button[data-actions]').each(function(){
		var element = jQuery(this);
		element.addClass('ekm-hasactions');
		var actions = element.attr('data-actions').split('[SPLIT]');
		var actionNames = element.attr('data-actionnames').split('[SPLIT]');
		var actionTitles = element.attr('data-titles').split('[SPLIT]');
		var menu = jQuery('<div class="ekm-actionmenu" />');
		var icons = {
			'edit|modificar': 'ekm-editico',
			'delete|eliminar': 'ekm-delico',
			'order|posici\u00F3n': 'ekm-orderico',
			'approve|aprobar': 'ekm-approveico',
			'yes|s\u00CD': 'ekm-yesico',
			'up|arriba': 'ekm-upico',
			'down|abajo': 'ekm-downico'
		};
		jQuery.each(actionNames, function(i, v){
			if (v.indexOf('|') > 0) {
				var vArray = v.split('|');
				var actionsArray = actions[i].split('|');
				var titleArray = actionTitles[i].split('|');
				var a = jQuery('<a class="ekm-actionmenuitem">' + vArray[0] + '</a>');
				a.attr('title', titleArray[0]);
				var subMenu = jQuery('<div class="ekm-actionsubmenu" />');
				var newFunc = function(e){
					var d = e.data.d;
					if (d) eval(d);
					e.stopImmediatePropagation();
					e.preventDefault();
				};
				for (var n = 1; n < vArray.length && n < actionsArray.length; n++) {
					var subItem;
					if (actionsArray[n].indexOf(";") != -1) {
						subItem = jQuery('<a href="#" class="ekm-actionsubmenuitem">' + vArray[n] + '</a>');
						subItem.bind('click', {d: actionsArray[n]}, newFunc);
						rCounter = 0;
					} else {
						subItem = jQuery('<a class="ekm-actionsubmenuitem">' + vArray[n] + '</a>');
						subItem.attr('href', actionsArray[n]);
					}
					jQuery.each(icons, function(k, value){
						var re = new RegExp(k, 'i');
						if (re.test(vArray[n])) subItem.addClass(value);
					});					
					subMenu.append(subItem);
				}
				var subMenuTimeout;
				a.bind({
					'mouseenter': function(){
						clearTimeout(subMenuTimeout);
					},
					'mouseleave': function(){
						subMenuTimeout = setTimeout(function(){
							subMenu.stop().fadeOut(250, function() { subMenu.detach().show();});
						}, 250);
					}
				});	
				subMenu.bind({
					'mouseenter': function(){
						clearTimeout(timeout);
						clearTimeout(subMenuTimeout);
					},
					'mouseleave': function(){
						subMenuTimeout = setTimeout(function(){
							subMenu.stop().fadeOut(250, function() { subMenu.detach().show();});
						}, 250);
						timeout = setTimeout(function(){
							menu.detach();
						}, 500);
					},
					'click': function() {
						subMenu.detach();
						menu.detach();
						clearTimeout(timeout);
						clearTimeout(subMenuTimeout);
					}
				});					
				a.bind('click', function(event){
					var btn = jQuery(this);
					jQuery(document.body).append(subMenu);
					var offset = btn.offset();
					var width = subMenu.outerWidth();
					var height = subMenu.outerHeight();
					var x = offset.left + btn.outerWidth() - width;
					var y = (offset.top+Math.floor(btn.outerHeight()/2)) - Math.floor(height/2);
					var btnParent = btn.offsetParent();
					if ((y + height) > (btnParent.offset().top + btnParent.outerHeight())) y = (btnParent.offset().top + btnParent.outerHeight()) - height; 
					if (y < btnParent.offset().top) y = btnParent.offset().top; 
					var win = jQuery(window)
					var left = win.scrollLeft();
					var top = win.scrollTop();
					if (x < left) x = left;
					if (y < top) y = top;
					var right = win.width() + left;
					var bottom = win.height() + top;
					if ((x + width) > right) x = right - width;
					if ((y + height) > bottom) y = bottom - height;
					//Work around for jQuery on mobile safari.
					var left = 0;
					if (/CPU.+OS ([0-9_]{3}).*AppleWebkit.*Mobile/i.test(navigator.userAgent)){
						left = window.top.scrollX;
						x -= left;
						y -= window.top.scrollY
					}
					subMenu.css({
						'left': x,
						'top': y
					});
					if ((offset.left + btn.outerWidth() + width) < right) {
						subMenu.stop().animate({'left': (offset.left + btn.outerWidth()) - left });
					} else {
						subMenu.stop().animate({'left': (offset.left - width) - left });
					}
					event.preventDefault();
					event.stopPropagation();
				});
				a.attr('href', '#');
				menu.append(a);
				jQuery.each(icons, function(k, value){
					var re = new RegExp(k, 'i');
					if (re.test(vArray[0])) a.addClass(value);
				});
			} else {
				var a = jQuery('<a class="ekm-actionmenuitem">' + v + '</a>');
				a.attr('href', actions[i]);
				a.attr('title', actionTitles[i]);
				menu.append(a);
				jQuery.each(icons, function(k, value){
					var re = new RegExp(k, 'i');
					if (re.test(v)) a.addClass(value);
				});				
			}
		});
		var timeout;
		menu.bind({
			'mouseenter': function(){
				clearTimeout(timeout);
			},
			'mouseleave': function(){
				timeout = setTimeout(function(){
					menu.detach();
				}, 500);
			}
		});
		element.bind('click', function(event){
			var btn = jQuery(this);
			jQuery(document.body).append(menu);
			var offset = btn.offset();
			var width = menu.outerWidth();
			var height = menu.outerHeight();
			var x = (offset.left + Math.floor(btn.outerWidth() / 2)) - Math.floor(width / 2);
			var y = offset.top;
			var win = jQuery(window)
			var left = win.scrollLeft();
			var top = win.scrollTop();
			if (x < left) x = left;
			if (y < top) y = top;
			var right = win.width() + left;
			var bottom = win.height() + top;
			if ((x + width) > right) x = right - width;
			if ((y + height) > bottom) y = bottom - height;
			//Work around for jQuery on mobile safari.
			if (/CPU.+OS ([0-9_]{3}).*AppleWebkit.*Mobile/i.test(navigator.userAgent)){
				x -= window.top.scrollX;
				y -= window.top.scrollY;
			}
			menu.css({
				'left': x,
				'top': y
			});
			event.preventDefault();
			event.stopPropagation();
		});
	});
	
	//Button position.
	var positions = {
		'left': 'ekm-edit-button-float-left',
		'right': 'ekm-edit-button-float-right',
		'bottom': 'ekm-edit-button-float-bottom'
	};
	jQuery(document).find('a.ekm-edit-button[data-float]').each(function(){
		var element = jQuery(this);
		var d = element.attr('data-float').split(' ');
		for (var i=0; i<d.length; i++){
			var c = positions[d[i]];
			if (c) element.addClass(c);
		}
	});
});

var ekmReorderCount = 0;
var ekmReorderCounterTimeOut;
var ekmReorderRedirectDoOnce = true;

function ekmReorderCounter(href, eventB) {	
	clearTimeout(ekmReorderCounterTimeOut);
	ekmReorderCount++;
	var textContent = eventB.target.innerHTML;
	if (textContent.lastIndexOf(" ") != -1) { textContent = textContent.substring(0,textContent.lastIndexOf(" ")); }
	if (ekmReorderCount != 1) { eventB.target.innerHTML = textContent + " " + ekmReorderCount; }
	ekmReorderCounterTimeOut = setTimeout(function(){ ekmReorderRedirect(href); }, 700);
}

function ekmReorderRedirect(href) {
	clearTimeout(ekmReorderCounterTimeOut);
	jQuery(document).find('.ekm-actionmenu').detach();
	jQuery(document).find('.ekm-actionsubmenu').detach();
	if (ekmReorderRedirectDoOnce) {
		ekmReorderRedirectDoOnce = false;
		window.location = href + "&amount="+ekmReorderCount;
	}
}

})();

