/*
	webcentiv-fx.js - jh
*/
/*jslint strict:false, plusplus:false, nomen:false*/
/*global DT:false, window:false, DD_belatedPNG:false*/

// adds toggle functionality to the flyCategoryMenu
DT.CORE.register('fly-site-menu-panel toggle', function (io, $) {
	return {
		init: function () {
			$('.fly-site-menu-panel ul .active+ul').css('display', 'block');
			// toggle active
			$('.fly-site-menu-panel div+ul>li>a').click(function () {
				var a = $(this);
				if (a.hasClass('active')) {
					a.removeClass('active');
					a.next('ul').css('display', 'none');
				} else {
					a.addClass('active');
					a.next('ul').css('display', 'block');
				}
				io.notify({
					type: DT.CONSTANTS.LAYOUT_CHANGED
				});
				// goto page if it's the 2nd level menu
				if (a.closest('div').hasClass('fly-second-menu-panel')) {
					return true;
				}
				// merely expand if there are subs
				if (a.next('ul').length) {
					return false;
				}
				// goto that page otherwise
				return true;
			});
			io.notify(DT.CONSTANTS.EVENT_LAYOUT_CHANGED);
		}
	};
});
/* attaches fancybox to itemDetail */
DT.CORE.register('itemDetail fancybox', function (io, $) {
	return {
		init: function () {
			//if (!DT.TOOLKIT.anyOfThesePages('MixedItemDetail') || DT.TOOLKIT.isBE) {
			if (DT.TOOLKIT.isBE) {
				return;
			}
			var refresh, fancy;

			fancy = function () {
				var images = $('a.lightbox');
				images.attr('title', $('.headlinePanel h4:eq(0)').text().replace(/^\s+|\s+$/g, ''));
				images.fancybox({
					'titlePosition': 'inside',
					'titleFormat': function (title, currentArray, currentIndex, currentOpts) {
						var parts = [];
						if (title.length) {
							parts.push('<span class="title">' + title + '</span>');
						}
						//if (currentArray.length > 1) {
						//	parts.push('<span class="pictureNavigationPanel"><b id="fancy-custom-prev" class="button button-prev"></b><span class="position">' + (currentIndex + 1) + ' / ' + currentArray.length + '</span><b id="fancy-custom-next" class="button button-next"></b></span>');
						//}
						currentOpts.onComplete = function () {
							$('#fancy-custom-prev').click(function () {
								$.fancybox.prev();
							});
							$('#fancy-custom-next').click(function () {
								$.fancybox.next();
							});
						};
						return parts.join('<br/>');
					}
				});
			};

			fancy();

			if (window.itemDetailWebAsyncRefreshPanel_RefreshComplete) {
				refresh = window.itemDetailWebAsyncRefreshPanel_RefreshComplete;
				window.itemDetailWebAsyncRefreshPanel_RefreshComplete = function (oPanel) {
					fancy();
					refresh(oPanel);
					io.notify({
						type: DT.CONSTANTS.LAYOUT_CHANGED
					});
					io.notify({
						type: DT.CONSTANTS.PARTIAL_REFRESH
					});
				};
			}
		}
	};
});

// fixes PNGs for IE6
DT.CORE.register('fix PNGs for IE6', function (io, $) {
	return {
		init: function () {
			if (window.DD_belatedPNG) {
				io.listen([
					DT.CONSTANTS.EVENT_INIT_COMPLETE,
					DT.CONSTANTS.EVENT_PARTIAL_REFRESH
				], this.handleNotification, this);
			}
		},
		handleNotification: function (note) {
			if (DT.TOOLKIT.isIE6 && (note.type === DT.CONSTANTS.EVENT_INIT_COMPLETE || note.type === DT.CONSTANTS.EVENT_PARTIAL_REFRESH)) {
				//DD_belatedPNG.fix('.fly-site-menu-panel, .fly-site-menu-panel .headline-wrap h2, .fly-site-menu-panel ul, .leftCol .igpnl_StandardPanel, .rightCol .igpnl_StandardPanel, .ig_StandardHeader, .igpnl_StandardHeaderCollapsed, .itemDetailUltraWebTabPanel a');
				DD_belatedPNG.fix('.leftCol .igpnl_StandardPanel, .rightCol .igpnl_StandardPanel, .ig_StandardHeader, .igpnl_StandardHeaderCollapsed, .itemDetailUltraWebTabPanel a');
				$.each($('.itemDetailExperiencePanel img'), function () {
					var img;
					img = $(this);
					img.attr('src', img.attr('src') + '&DD=belated.png'); // DD_belatedPNG looks for ".png"
					DD_belatedPNG.fixPng(this);
				});
			}
		}
	};
});

// adds hover classes for IE6
DT.CORE.register('add ie6 hover classes', function (io, $) {
	return {
		init: function () {
			if (DT.TOOLKIT.isIE6) {
				$('.top-navi ul li, .top-navi ul').hover(
					function () {
						$(this).addClass('hover');
					},
					function () {
						$(this).removeClass('hover');
					}
				);
				$('.itemPreview').hover(
					function () {
						$(this).addClass('itemPreview-hover');
					},
					function () {
						$(this).removeClass('itemPreview-hover');
					}
				);
			}
		}
	};
});

// spawns error/info dialogs
DT.CORE.register('spawn dialogs', function (io, $) {
	return {
		init: function () {
			//return;
			if (DT.TOOLKIT.isBE) {
				return;
			}
			io.listen([
				DT.CONSTANTS.EVENT_PARTIAL_REFRESH
			], this.handleNotification, this);
			this.spawnAll();
		},
		handleNotification: function (note) {
			if (note.type === DT.CONSTANTS.EVENT_PARTIAL_REFRESH) {
				this.spawnAll();
			}
		},
		spawnAll: function () {
			var idp;
			idp = $('.itemDetailPanel');
			if (idp.length) {
				if (idp.data('spawned')) {
					return;
				}
				idp.data('spawned', this.spawn('.errorPanel', '.error'));
			} else {
				this.spawn('.errorPanel', '.error');
			}
			this.spawn('.infoPanel', '.info,.infoLabel');
		},
		spawn: function (selector, subSelector) {
			var panel, sub, id;
			panel = $(selector);
			if (panel.length > 0) {
				panel.css('display', 'none');
				if (panel.text().replace(/^\s+|\s+$/g, '').length) {
					sub = panel.find(subSelector);
					if (!sub.length) {
						return false;
					}
					id = sub.attr('id');
					if (id === '') {
						id = 'spawn-this-as-dialog';
						sub.attr('id', id);
					}
					id = '#' + id;
					$.fancybox({href: id});
					return true;
				}
			}
			return false;
		}
	};
});

// replicates the complete event of *WebAsyncRefreshPanel by attaching to window.InfraWebRefreshPanel.complete
DT.CORE.register('InfraWebRefreshPanel event replicator', function (io, $) {
	return {
		init: function () {
			if (window.InfraWebRefreshPanel) {
				var complete;
				complete = window.InfraWebRefreshPanel.complete;
				window.InfraWebRefreshPanel.complete = function () {
					var args = Array.prototype.slice.call(arguments);
					io.notify({
						type: DT.CONSTANTS.EVENT_LAYOUT_CHANGED
					});
					io.notify({
						type: DT.CONSTANTS.EVENT_PARTIAL_REFRESH
					});
					complete.apply(this, args);
				};
			}
		}
	};
});

// triggers the corresponding button if return is pressed inside a text or password field
DT.CORE.register('wire up buttons', function (io, $) {
	return {
		init: function () {
			var inputs, i, lastButton, current, type;
			function trigger(element, buttonId) {
				element.keydown(function (e) {
					if (e.keyCode === 13) {
						$('#' + buttonId).trigger('click');
						return false;
					}
					return true;
				});
			}
			inputs = $('input[type=text], input[type=password], input[type=submit]');
			for (i = inputs.length; i--;) {
				current = $(inputs[i]);
				type = current.attr('type');
				if (type === 'submit') {
					lastButton = current.attr('id');
				} else if (lastButton !== undefined && (type === 'text' || type === 'password')) {
					trigger(current, lastButton);
				}
			}
		}
	};
});

// kills ig toggles
DT.CORE.register('kill ig toggle', function (io, $) {
	return {
		init: function () {
			$.each($('.rightCol, .leftCol').find('.ig_StandardHeader'), function () {
				this.__isEventReady = false;
				$(this).css('cursor', 'default');
			});
		}
	};
});

// spawns modal dialogs
DT.CORE.register('spawn modal dialogs', function (io, $) {
	return {
		init: function () {
			var md, id;
			md = $('div.modal-dialog .inner');
			if (md.length) {
				md.css('display', 'block');
				if (DT.TOOLKIT.isBE) {
					$('div.modal-dialog').css('display', 'block');
					return;
				}
				id = md.attr('id');
				if (id === '') {
					id = 'spawn-this-as-dialog';
					md.attr('id', id);
				}
				id = '#' + id;
				$.fancybox({
					href: id,
					autoDimensions: false,
					width: 600
				});
			}
		}
	};
});
