﻿/// <reference name="MicrosoftAjax.js" />

Type.registerNamespace('WIS.Web.Server');

WIS.Web.Server.TabManager = function() {
	this._path = null;
	this._servicePath = null;
	this._container = null;
	this._contextData = null;
}

WIS.Web.Server.TabManager.prototype = {

	Initialize: function(path, servicePath, container, contextData) {
		this._path = path;
		this._servicePath = servicePath;
		this._container = container;
		this._contextData = contextData;
	},

	EnableLoadAnimation: function() {
		var overlay = $("#activetab_overlay")[0];
		if (overlay != null) {
			overlay.style.display = '';
			$("#activetab_loader")[0].style.display = '';
		}
	},

	DisableLoadAnimation: function() {
		var overlay = $("#activetab_overlay")[0];
		if (overlay != null) {
			overlay.style.display = 'none';
			$("#activetab_loader")[0].style.display = 'none';
		}
	},

	SelectPath: function(source, path) {
		this.EnableLoadAnimation();
		this.SelectItem(source);
		var jsonData = {
			"path": path
		}
		this._invokeCall("SelectPath", jsonData, this._onSelectPathComplete);
	},

	_onSelectPathComplete: function(result, userContext, methodName) {
		var actualResult = eval("(" + result + ")");
		this._path = actualResult.path;
		$get(this._container).innerHTML = actualResult.rawHTML;
		if (actualResult.loadScript && actualResult.loadScript != null && actualResult.loadScript !== '') {
			eval(actualResult.loadScript);
		}
		this.DisableLoadAnimation();
	},

	HandleActiveClick: function(id) {
		var data = {
			"path": this._path, "id": id
		}
		this._invokeCall("ActiveClick", data, this._onSelectPathComplete);
	},

	HandleActiveSubmit: function(sourceID, values) {
		var postValues = {};
		for (var i = 0; i < values.length; i++) {
			eval("postValues." + values[i] + " = '" + $("#"+values[i]).val() + "';");
		}
		var data = {
			"path": this._path,
			"sourceID": sourceID,
			"postValues": postValues
		}
		this._invokeCall("ActiveSubmit", data, this._onSelectPathComplete);
	},

	More: function(source, start, count) {
		source.style.display = 'none';
		var jsonData = {
			"path": this._path,
			"cursor": start,
			"count": count
		}
		this._invokeCall("More", jsonData, this._onMoreComplete);
	},

	_onMoreComplete: function(result, userContext, methodName) {
		var actualResult = eval("(" + result + ")");
		$get(this._container + "_inner_content").innerHTML += actualResult.rawHTML;
	},

	Refresh: function() {
		var jsonData = {
			"path": this._path
		}
		this._invokeCall("SelectPath", jsonData, this._onSelectPathComplete);
	},

	SelectItem: function(source) {
		source.setAttribute("class", "selected");
		source.setAttribute("className", "selected");

		var parent = source.parentNode;
		var children = parent.getElementsByTagName('li');
		for (var i = 0; i < children.length; i++) {
			if (children[i] != source) {
				children[i].setAttribute("class", "");
				children[i].setAttribute("className", "");
			}
		}
	},

	get_Path: function() {
		return this._path;
	},

	_invokeCall: function(methodname, data, formDelegate) {
		if (this._contextData != null) {
			data["contextData"] = this._contextData;
		}
		Sys.Net.WebServiceProxy.invoke(
            this._servicePath, methodname, false, data, Function.createDelegate(this, formDelegate)
        );
	}
}

WIS.Web.Server.TabManager.registerClass('WIS.Web.Server.TabManager', null);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();