/// <reference name="MicrosoftAjax.js"/>
Type.registerNamespace('WindowsSite.Ajax.Controls');

WindowsSite.Ajax.Controls.ContentListPanelBehavior = function(element) {
	WindowsSite.Ajax.Controls.ContentListPanelBehavior.initializeBase(this, [element]);

	this.currentlySelectedIndex = 0;
	this._updateSectionsDelegate = Function.createDelegate(this, this._onCallbackCompleted);
}

WindowsSite.Ajax.Controls.ContentListPanelBehavior.prototype =
{
	initialize: function() {
		WindowsSite.Ajax.Controls.ContentListPanelBehavior.callBaseMethod(this, 'initialize');

		var contentDiv = document.createElement('div');
		contentDiv.innerHTML = this.get_ContentList()[0];
		contentDiv.setAttribute("class", this.get_ContentCssStyleClass());
		contentDiv.setAttribute("className", this.get_ContentCssStyleClass());
		this.get_element().appendChild(contentDiv);
	},

	dispose: function() {
		WindowsSite.Ajax.Controls.ContentListPanelBehavior.callBaseMethod(this, 'dispose');
		this._updateSectionsDelegate = null;
	},

	_onControlCommand: function(sender, args) {
		if (args != null) {
			this._commandName = args.get_CommandName();
			this._commandArgument = args.get_CommandArgument();
		}
		switch (this._commandName) {
			case "LeftArrowClicked":
				this.currentlySelectedIndex -= 1;
				this.SetThisElementInnerHtml(this.currentlySelectedIndex);
				break;
			case "RightArrowClicked":
				this.currentlySelectedIndex += 1;
				this.SetThisElementInnerHtml(this.currentlySelectedIndex);
				break;
			case "ContentClicked":
				this.RegisterWebTrendsClick();
				break;
			case "GetNewListItems":
				this.GetNewListItems(this._commandArgument);
				break;
			default:
				WindowsSite.Ajax.Controls.ContentListPanelBehavior.callBaseMethod(this, '_onControlCommand');
				break;
		}
	},

	SetThisElementInnerHtml: function(currentlySelectedIndex) {
		if (currentlySelectedIndex < 0) {
			this.currentlySelectedIndex = this.get_ContentList().length - 1;
		}
		else if (currentlySelectedIndex >= this.get_ContentList().length) {
			this.currentlySelectedIndex = 0;
		}

		var newContentDiv = document.createElement('div');
		newContentDiv.setAttribute("class", this.get_ContentCssStyleClass());
		newContentDiv.setAttribute("className", this.get_ContentCssStyleClass());
		newContentDiv.innerHTML = this.get_ContentList()[this.currentlySelectedIndex];
		this.get_element().removeChild(this.get_element().firstChild);
		this.get_element().appendChild(newContentDiv);

		this.set_CurrentWebTrendsName(this.get_WebTrendsNameList()[this.currentlySelectedIndex])
		if (this.get_WebTrendsNameList()[this.currentlySelectedIndex] != null) {
			if (this.get_OmnitureModuleName() != "") {
				omniModTracking(this, this.get_OmnitureModuleName(), '', this.get_OmnitureNameList()[this.currentlySelectedIndex], 0, this.get_OmnitureNameList()[this.currentlySelectedIndex], '');
			}
			else {
				omniModTracking(this, 'Certified', '', this.get_OmnitureNameList()[this.currentlySelectedIndex], 0, this.get_OmnitureNameList()[this.currentlySelectedIndex], '');
			}
		}
	},

	RegisterWebTrendsClick: function() {
		wtClearAllVariables();
		dcsSetVar('DCSext.win_vcp', this.get_WebTrendsNameList()[this.currentlySelectedIndex], 'DCSext.win_evttype', '1');
	},

	GetNewListItems: function(args) {
		var request = new Sys.Net.WebRequest();
		request.set_url(this._servicePath + args);
		request.add_completed(this._updateSectionsDelegate);
		var executor = new Sys.Net.XMLHttpExecutor();
		request.set_executor(executor);
		executor.executeRequest();
	},

	_onCallbackCompleted: function(response, args) {
		if (response.get_responseAvailable()) {
			var root = response.get_xml().documentElement;
			if (root.nodeName == "response") {
				Array.clear(this.get_ContentList());
				Array.clear(this.get_WebTrendsNameList());
				Array.clear(this.get_OmnitureNameList());
				for (var i = 0; i < root.childNodes.length; i++) {
					this.get_ContentList().push(root.childNodes[i].firstChild.nodeValue);
					if (root.childNodes[i].attributes) {
						for (var j = 0; j < root.childNodes[i].attributes.length; j++) {
							if (root.childNodes[i].attributes[j].nodeName == "wt") this.get_WebTrendsNameList().push(root.childNodes[i].attributes[j].nodeValue);
							if (root.childNodes[i].attributes[j].nodeName == "omni") this.get_OmnitureNameList().push(root.childNodes[i].attributes[j].nodeValue);
						}
					}
				}
			}
		}
		this.currentlySelectedIndex = 0;
		this.SetThisElementInnerHtml(this.currentlySelectedIndex);
	},

	//Properties
	get_CurrentWebTrendsName: function() {
		return this._curWebTrendsName;
	},
	set_CurrentWebTrendsName: function(value) {
		if (this._curWebTrendsName != value) {
			this._curWebTrendsName = value;
			this.raisePropertyChanged('CurrentWebTrendsName');
		}

		//HACK: i'm piggy backing webtrends name property changes.. TODO: refactor this to its own:
		this.this_curOmnitureName = this.get_OmnitureNameList()[this.currentlySelectedIndex];
	},

	get_ContentList: function() {
		return this._contentList;
	},
	set_ContentList: function(value) {
		this._contentList = value ? Array.parse(value) : new Array();
		this.raisePropertyChanged('ContentList');
	},
	get_ContentCssStyleClass: function() {
		return this._contentCssStyleClass;

	},
	set_ContentCssStyleClass: function(value) {
		this._contentCssStyleClass = value;
		this.raisePropertyChanged('ContentCssStyleClass');
	},
	get_WebTrendsNameList: function() {
		return this._webTrendsNameList;
	},
	set_WebTrendsNameList: function(value) {
		this._webTrendsNameList = value ? Array.parse(value) : new Array();
		this.raisePropertyChanged('WebTrendsNameList');
	},
	get_OmnitureNameList: function() {
		return this._omnitureNameList;
	},
	set_OmnitureNameList: function(value) {
		this._omnitureNameList = value ? Array.parse(value) : new Array();
		this.raisePropertyChanged('OmnitureNameList');
	},
	get_OmnitureModuleName: function() {
		return this._omnitureModuleName;
	},
	set_OmnitureModuleName: function(value) {
		this._omnitureModuleName = value;
		this.raisePropertyChanged('OmnitureModuleName');
	},

	get_ServicePath: function() {
		return this._servicePath;
	},
	set_ServicePath: function(value) {
		this._servicePath = value;
		this.raisePropertyChanged('ServicePath');
	}
}

WindowsSite.Ajax.Controls.ContentListPanelBehavior.registerClass('WindowsSite.Ajax.Controls.ContentListPanelBehavior', WindowsSite.Ajax.Controls.CommandControlBehavior );
