


function intelligenceCore()
{
	var theCore = this;
	this.loadStartTime;
	this.intelligenceWindow;
	this.intelligenceScreen;
	this.isMobile;

	this.init = function() 
	{
	
		this.isMobile = (/iPhone|iPod|iPad|Android|BlackBerry/).test(navigator.userAgent);

		jQuery.fn.center = function () {
			this.css("position","absolute");
			this.css("top", (($(window).height() - this.outerHeight()) / 2) + 
														$(window).scrollTop() + "px");
			this.css("left", (($(window).width() - this.outerWidth()) / 2) + 
														$(window).scrollLeft() + "px");
			return this;
		}

		jQuery.fn.move = function (x,y) {
			
			var bodyWidth = $('body').width();
			var bodyHeight = $('body').height();
			
			var elementBottom = y + $(this).height() + 2*(parseInt($(this).css("padding-bottom").replace("px",""),10));
			if (elementBottom >= bodyHeight) {
				y = y - (elementBottom-bodyHeight);
			}

			var elementRight = x + $(this).width() + 2*(parseInt($(this).css("padding-right").replace("px",""),10));
			if (elementRight >= bodyWidth) {
				x = x - (elementRight-bodyWidth);
			}

			this.css("position","absolute");
			this.css("top", y + "px");
			this.css("left", x + "px");
			
			return this;
		}

		jQuery.fn.center = function () {
			this.css("position","absolute");
			this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
			this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
			return this;
		}	

		$('[title]').attr('title', function(i, title) {
			$(this).data('title', title).removeAttr('title');
		});

		this.loadStartTime = (new Date()).getTime();
		
		this.oformPanel = new formPanel();
		this.oformPanel.init();

		this.oindicatorPanel = new indicatorPanel();
		this.oindicatorPanel.init();
	
		if (!(this.isMobile)) {
			$(document).on('mouseover mouseout', '.taskbutton, .menubutton, .search_artist, .search_song, .note:data(title)', function () {
				var hoverBox;
				var thisEl = this;
				if (event.type == 'mouseover') {
					if ($('#tool_hoverbox').length != 0) {
						$('#tool_hoverbox').remove();
					}
					if (!theCore.oformPanel.isOpen) {
						hoverBox = $("<div id='tool_hoverbox'>" + unescape($(this).data("title")) + "</div>");
						$('body').append($(hoverBox));


						$(hoverBox).move(event.pageX,event.pageY+25).fadeIn('slow', function() {});


					}
				} else {
					hoverBox = $('#tool_hoverbox');
					$(hoverBox).fadeOut('slow', function() { $(hoverBox).remove() });
				}
			});
		}
	};

	this.fnLoadTitles = function()
	{
		$('[title]').attr('title', function(i, title) {
			$(this).data('title', title).removeAttr('title');
		});
	}

	this.fnCoreAddCommas = function(nStr) 
	{
		nStr = nStr + '';
		var x = nStr.split('.');
		var x1 = x[0];
		var x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	};

	this.fnCoreGetOppositeDirection = function(direction) 
	{
		if(direction == "right") return "left";
		if(direction == "left") return "right";
		if(direction == "up") return "down";
		if(direction == "down") return "up";
	};

	this.fnCoreTransitionPage = function(thisPage, msg) 
	{
		var thisPageTransition, thisPageElement;
		if($intelligenceWindow) {
			thisPageTransition = thisPage.settings.transition2;
			thisPageElement = $intelligenceWindow;
		} else {
			thisPageTransition = thisPage.settings.transition;
			thisPageElement = $(thisPage.innerDiv);
		} 
		switch(thisPageTransition) {
			case "fade":
				thisPageElement.fadeOut(thisPage.settings.speed, function() {
					$(thisPage).html(msg);
					if($intelligenceWindow) {
						$intelligenceWindow.center();
					}
					$(thisPage).fadeIn(thisPage.settings.speed);
				});
				break;
			case "slide":
				thisPageElement.hide("slide", { direction: thisPage.settings.direction }, thisPage.settings.speed, function() {
					$(thisPage).html(msg);
				}).show("slide", { direction: fnCoreGetOppositeDirection(thisPage.settings.direction) }, thisPage.settings.speed);
				break;
			case "popup":
				$intelligenceWindow = $("<div id='intelligence_tool_window' class='intelligence_tool'><div id='intelligence_tool_close'>Close</div>" + msg + "</div>");
				$intelligenceScreen = $("<div id='intelligence_tool_screen'>&nbsp;</div>");
				$intelligenceScreen.css({ opacity: 0.7, 'width':$(document).width(),'height':$(document).height()});
				$('body').css({'overflow':'hidden'});
				$intelligenceWindow.hide();
				$('body').append($intelligenceScreen);
				$('body').append($intelligenceWindow);
				$intelligenceWindow.center();
				$intelligenceWindow.fadeIn(thisPage.settings.speed);

				$(document).on('click', '#intelligence_tool_close', function () {
					$intelligenceWindow.remove();
					$intelligenceScreen.remove();
					$intelligenceWindow = null;
					$intelligenceScreen = null;
					return false;
				})
				break;
			default:
				$(thisPageElment).html(msg);
		}
	};

	// Return a helper with preserved width of cells
	this.fnCoreFixHelper = function(e, ui) {
		ui.children().each(function() {
			$(this).width($(this).width());
		});
		return ui;
	};
}

function formPanel()
{
	this.height;
	this.button;
	this.panel;
	this.isOpen;
	this.msg;
	this.btnPreVal;

	this.init = function()
	{
		this.height = 0;
		this.isOpen = false;

		$(document).on('click', '#slide_form_panel input.cancel', function () {
			plugin.oCore.oformPanel.close();
			return false;
		});
	}

	this.open = function(pButton, msg) 
	{
		this.button = pButton;
		this.msg = msg;
		
		if ($(this.button).hasClass('taskbutton')) {
			var colspan = $(this.button).parents('table').find('tr')[0].cells.length;
			this.panel = $("<tr id='slide_form_panel' class='slide_form_panel'><td colspan='" + colspan + "'><div>" + this.msg + "</div></td></tr>");
			$(this.button).parent().parent().parent().after(this.panel);
			height = $(this.panel).height();
			$(this.panel).find('td > div').hide().css({ height : 0 });
			$(this.panel).find('td > div').show().animate({ height : height }, { duration: 'slow' });
		} 
		else {
			this.btnPreVal = $(this.button).val();
			$(this.button).val("Cancel");


			this.panel = $("<div id='slide_form_panel' class='slide_form_panel'></div>");
			$(this.panel).html(this.msg);

			var $parentDiv = $(this.button).parents('.intelligence_tool_gradient');
			var offset = $parentDiv.offset();
			var height = $parentDiv.height();
			var width = $parentDiv.width();

			var top = offset.top + height + "px";
			var left = offset.left - (1);

			$parentDiv.after(this.panel);
			$(this.panel).css("top",top).css("left",left);
                
			if ($(this.panel).height() > $('#intelligence_tool').height()) 
			{
				height = $('#intelligence_tool').height() - 100;
			}
			else 
			{
				height = $(this.panel).height();
			}
                
			$(this.panel).hide().css({ height : 0 });
			$(this.panel).show().animate({ height : height }, 'slow', function(){ 
				var firstInput = $(":input:not(input[type=button],input[type=submit],button):visible:first", $(this).find('form'));
				firstInput.focus();
			});
		}  

		this.isOpen = true;
	};

	this.close = function() {
		var plugin = this;
		if ($(plugin.button).hasClass('taskbutton')) {
			$(plugin.panel).find('td > div').slideUp('slow', function() {    
				$(plugin.panel).remove();               
				plugin.isOpen = false;
			});
		}
		else {
			$(plugin.panel).slideUp('slow', function() {    
				$(plugin.button).val(plugin.btnPreVal);
				$(plugin.panel).remove();               
				plugin.isOpen = false;
			});
		}
	};

};

function indicatorPanel()
{
	this.panel; 

	this.init = function() {
		this.panel = $("<div id='tool_indicator_panel'></div>");
		$('body').append($(this.panel.hide()));
	}
	
	this.show = function(msg) {
		var plugin = this;
		$(plugin.panel).html(msg);
		$(plugin.panel).center().fadeIn('slow', function() {
			$(plugin.panel).fadeOut('slow', function() {});
		});
	}
}

