﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("WindowsSite.Ajax.Controls");

WindowsSite.Ajax.Controls.ListPanelDetailsBehavior = function(element)
{
    this._onListItemChangedDelegate = Function.createDelegate(this, this._onListItemChanged);
    this._targetPropertyName = "innerHTML";

    WindowsSite.Ajax.Controls.ListPanelDetailsBehavior.initializeBase(this, [element]);
}

WindowsSite.Ajax.Controls.ListPanelDetailsBehavior.prototype =
{
	initialize: function() {
		this.get_ListPanel().add_SelectedItemChanged(this._onListItemChangedDelegate);
		WindowsSite.Ajax.Controls.ListPanelDetailsBehavior.callBaseMethod(this, 'initialize');
		if (this.get_EventName() && this.get_ScriptFormatString()) {
			$addHandler(this.get_element(), this.get_EventName(), Function.createDelegate(this, this._executeScript));
		}
	},

	_onListItemChanged: function(sender, args) {
		var item = sender.get_SelectedDataItem(),
			defaultValue = this.get_DefaultValue();
		$common.setVisible(this.get_element(), item || defaultValue);

		if (this.get_EventName() && this.get_ScriptFormatString()) {
			this._scriptTarget = item ? item[this.get_DataMember()] : null;
		}
		else {
			var itemHtml = item ? item[this.get_DataMember()] : defaultValue;
			if (item && item.IsGenuine && this.get_GenuineIconUrl()) itemHtml += "<img alt=\"Genuine\" src=\"" + this.get_GenuineIconUrl() + "\"/>";
			this.get_element()[this.get_TargetPropertyName()] = itemHtml;
		}
	},

	_executeScript: function() {
		if (this.get_ScriptFormatString() == "OmnitureTracking") {
			if (this.get_ListPanel() && this.get_ListPanel().get_ParentPanel()) {
				var category = this.get_ListPanel().get_ParentPanel().get_SelectedDataItem().Title;
				var destUrl = this.get_ListPanel().get_SelectedDataItem().Url;
				var nameOfDownload = this.get_ListPanel().get_SelectedDataItem().Title;
				eval(String.format("omniModTracking(this, 'Downloads', '{0}', '{1}', 1, '{2}','')", category, destUrl, nameOfDownload));
			}
		}
		else {
			eval(String.format(this.get_ScriptFormatString(), this._scriptTarget)); //execute webtrends tracking
		}
	},

	get_ScriptFormatString: function() { return this._scriptFormatString; },
	set_ScriptFormatString: function(value) {
		if (this._scriptFormatString != value) {
			this._scriptFormatString = value;
			this.raisePropertyChanged("ScriptFormatString");
		}
	},

	get_EventName: function() { return this._eventName; },
	set_EventName: function(value) {
		if (this._eventName != value) {
			this._eventName = value;
			this.raisePropertyChanged("EventName");
		}
	},

	get_DataMember: function() { return this._dataMember; },
	set_DataMember: function(value) {
		if (this._dataMember != value) {
			this._dataMember = value;
			this.raisePropertyChanged("DataMember");
		}
	},

	get_DefaultValue: function() { return this._defaultValue; },
	set_DefaultValue: function(value) {
		if (this._defaultValue != value) {
			this._defaultValue = value;
			this.raisePropertyChanged("DefaultValue");
		}
	},

	get_TargetPropertyName: function() { return this._targetPropertyName; },
	set_TargetPropertyName: function(value) {
		if (this._targetPropertyName != value) {
			this._targetPropertyName = value;
			this.raisePropertyChanged("TargetPropertyName");
		}
	},

	get_ListPanel: function() { return this._listPanel; },
	set_ListPanel: function(value) {
		if (this._listPanel != value) {
			this._listPanel = value;
			this.raisePropertyChanged("ListPanel");
		}
	},

	get_GenuineIconUrl: function() { return this._genuineIconUrl; },
	set_GenuineIconUrl: function(value) {
		if (this._genuineIconUrl != value) {
			this._genuineIconUrl = value;
			this.raisePropertyChanged("GenuineIconUrl");
		}
	},

	dispose: function() {
		//Add custom dispose actions here
		WindowsSite.Ajax.Controls.ListPanelDetailsBehavior.callBaseMethod(this, 'dispose');
	}
}
WindowsSite.Ajax.Controls.ListPanelDetailsBehavior.registerClass('WindowsSite.Ajax.Controls.ListPanelDetailsBehavior', AjaxControlToolkit.BehaviorBase);
