define(
	['scripts/knockout-3.3.0','scripts/act.js', 'scripts/CallWebObject.js', 'jqx/jqxcore', 'jqx/jqxwindow', 'scripts/ACTUtils'],
	function(ko, ACTSocket, InvokeCallback, jqxCore, jqxWindow, ACTUtils) {

    function GridPanelViewModel() {
		var self = this;

		this.hasComponent = ko.observable(false);
		this.mainComponent = ko.observable();
		this.componentModels = {};

		this.resize = function() {
			// we temporary hide the scrollbars cause the resize happen too late
			// the scrollbars are keeped and can change the real size of the window
			var $contentElements = $(".content");
			var oldOverflowStates = ACTUtils.GetOverflowsAndSetThemTo($contentElements, "hidden");

			for (var key in self.componentModels) {
				ACTUtils.Log("set dirty: "+key, 1);
				self.componentModels[key].setDirty();
				break;
			}
			for (var key in self.componentModels) {
				ACTUtils.Log("resize component: "+key, 1);
				self.componentModels[key].resize();
				break;
			}

			ACTUtils.SetOverflows($contentElements, oldOverflowStates);
		}

		this.lock = function(lockKey, noLockPaths) {
			for (var key in self.componentModels) {
				self.componentModels[key].lock(lockKey, noLockPaths);
			}
		}

		this.unlock = function(lockKey) {
			for (var key in self.componentModels) {
				self.componentModels[key].unlock(lockKey);
			}
		}

		this.registerComponentModel = function(name, component) {
			this.componentModels[name] = component;
		}

		this.componentReady = function(component, newVal) {
			if (newVal)
			{
				this.resize();
			}
		}

		var ignoreKeyAction = function(element) {
			var nodename = element.nodeName;
			return nodename == "INPUT" || nodename == "TEXTAREA";
		}

		$(window).on("keydown", function(event) {
			if (ignoreKeyAction(event.target))
				return;

			var obj = {
				$type: "Ansys.ACT.Core.UI.KeyUpDownWebObject, Ansys.ACT.Core, Culture=neutral, PublicKeyToken=null",
				KeyCode: event.keyCode,
				IsPressed: true,
			}
			ACTSocket.send("KeyUpDown", obj);
		});

		$(window).on("keyup", function(event) {
			if (ignoreKeyAction(event.target))
				return;

			var obj = {
				$type: "Ansys.ACT.Core.UI.KeyUpDownWebObject, Ansys.ACT.Core, Culture=neutral, PublicKeyToken=null",
				KeyCode: event.keyCode,
				IsPressed: false,
			}
			ACTSocket.send("KeyUpDown", obj);
		});

		this.onRefresh = function(obj) {
			// jqxtooltips and jqxwindows aren't automatically destroyed so we do it before loading a new page.
			var $jqxTooltips = $(".jqx-tooltip");
			if ($jqxTooltips.length > 0) {
				//jqxTooltips.jqxTooltip("destroy");  // can't do that cause we need the element on which one we got the tooltip
				$jqxTooltips.remove();                // so we do it... harder
			}
			var $jqxWindows = $('.jqxwindow');
			if ($jqxWindows.length > 0) {
				$jqxWindows.jqxWindow('destroy');
			}

			this.mainComponent(obj.MainComponent);
			obj.MainComponent.gridPanel = this;
			this.hasComponent(true);
		}.bind(this);

		ACTSocket.registerService("Container", this.onRefresh);	
		ACTSocket.registerService("Notifications", this.onReceiveNotification);

		var firstResize = true;
		$(window).resize(function(eventData, handler) {
			// resize only if we resize the window
			if (eventData.target == window) {
				this.resize();
			}
		}.bind(this));
	}
	
	GridPanelViewModel.prototype.dispose = function() {
		if ($('.jqxwindow').length>0) {
			$('.jqxwindow').jqxWindow('destroy'); 
		}
	};

    return GridPanelViewModel;

});

