﻿/// <reference path="jquery-1.5.1.min.js" />
/// <reference path="jquery.hoverIntent.minified.js" />
/// <reference path="jCarouselLite.js" />
/// <reference path="SnT.Utils.js" />
/// <reference path="fancybox/jquery.fancybox-1.3.2.js" />

SnTUtils.RegisterNameSpace("ITU.Common");

ITU.Common = {

	Config: {

		PopupWidth: 320,
		PopupHeight: 400,
		PopupScrolling: 'auto',
		PopupShowCloseButton: false,
		WebResourcesUrl: '/WebResources',
		ResourceManagerUrl: '/ResourceManager',
		ButtonCustomSubmitState: true
	},
	RMDocumentType: $.extend({}, SnTUtils.BaseEnum, {
		Undefined: 0,
		Image: 1,
		Document: 2,
		FlashAnimation: 4,
		Video: 8,
		Audio: 16
	}),
	Popup: {
		Data: {},
		Open: function (url, width, height, scrolling, showCloseButton, inCurrentWindow) {

			var popup = ITU.Common.Popup;

			try {
				width = typeof width == 'undefined' ? ITU.Common.Config.PopupWidth : width;
				height = typeof height == 'undefined' ? ITU.Common.Config.PopupHeight : height;
				scrolling = typeof scrolling == 'undefined' ? ITU.Common.Config.PopupScrolling : scrolling;
				showCloseButton = typeof showCloseButton == 'undefined' ? ITU.Common.Config.PopupShowCloseButton : showCloseButton;
				inCurrentWindow = typeof inCurrentWindow == 'undefined' ? true : inCurrentWindow;

				var config = {
					url: url,
					type: 'iframe',
					scrolling: scrolling,
					autoDimensions: false,
					width: width,
					height: height,
					showCloseButton: showCloseButton
				};
				popup.Data.CurrentConfig = config;

				if (!inCurrentWindow && self.parent) {

					self.parent.ITU.Common.Popup.Data.OldConfig = self.parent.ITU.Common.Popup.Data.CurrentConfig;
					self.parent.$.fancybox(url, config);
				}
				else {

					self.parent.ITU.Common.Popup.Data.OldConfig = null;
					$.fancybox(url, config);
				}
			}
			catch (e) {

				SnTUtils.HandleException(e, "ITU.Common.Popup.Open");
			}
		},
		Close: function (redirectUrl) {

			try {

				var config = self.parent.ITU.Common.Popup.Data.OldConfig;
				self.parent.ITU.Common.Popup.Data.OldConfig = null;

				if (config) {

					self.parent.ITU.Common.Popup.Open(config.url, config.width, config.height, config.scrolling, config.showCloseButton, true);
				}
				else {

					self.parent.$.fancybox.close();

					$.fancybox.close();

					//window.close();

					if (redirectUrl) {

						self.parent.location.href = redirectUrl;
					}
				}
			}
			catch (e) {

				SnTUtils.HandleException(e, "ITU.Common.Popup.Close");
			}
		},
		OpenWindow: function (url) {


			window.open(url, "_blank");
		}
	},
	List: {

		AppendItems: function (sourceUrl, container, itemClass) {

			container = $(container);

			if (container.length) {

				var rowCount = container.find(itemClass).length;

				$.get(String.Format("{0}?RowCount={1}", sourceUrl, rowCount), function (responseHtml) {

					if (!String.IsNullOrWhiteSpace(responseHtml)) {

						var lastRow = container.find(itemClass + ":last");

						lastRow.after(responseHtml);
					}
				});
			}
		}
	},
	Tools:
	{
		ToSubmitState: function (control, disableControl, text) {

			if (ITU.Common.Config.ButtonCustomSubmitState) {

				var me = $(control);
				var html = !String.IsNullOrWhiteSpace(text) ? text : me.text();

				if (disableControl) {

					setTimeout(function () {

						me.attr("onclick", "return false;")
						  .attr("href", "javascript:;");
					}, 100);
				}

				var span = me.find("span");

				setTimeout(function () {

					span.html('<img width="16" height="16" style="padding-right:2px; float:left;" src="/webresources/images/preloader-' + me.attr("class") + '.gif" />' + html);

				}, 100);
			}
		}
	}
};


