AUI().add('monator-theme-main',function(A) {
	var Lang = A.Lang,
		isArray = Lang.isArray,
		isFunction = Lang.isFunction,
		isNull = Lang.isNull,
		isObject = Lang.isObject,
		isString = Lang.isString,
		isUndefined = Lang.isUndefined,
		getClassName = A.ClassNameManager.getClassName,
		concat = function() {
			return Array.prototype.slice.call(arguments).join(SPACE);
		},
		
		BANNER_BOX = 'bannerBox',
		EVENT_MONATOR_READY = 'monator-ready',
		FOOTER_SITEMAP = 'footerSitemap',
		
		KONAMI_CODE_STANDARD = '38384040373937396665',
		KONAMI_CODE_WITH_ENTER = '3838404037393739666513',
		
		NAME = 'monator-theme-main',
		NS = 'monator-theme-main',
		URL_SITEMAP = 'urlSitemap'
	;

	var TPL_FOOTER_SITEMAP_ROOT = '';
		TPL_FOOTER_SITEMAP_ROOT += '<ul class="sitemap-tree-root">';
				TPL_FOOTER_SITEMAP_ROOT += '<li><a href="{urlSitemap}">{textSitemap}</a></li>';
		TPL_FOOTER_SITEMAP_ROOT += '</ul>';

	var MonatorThemeMain = A.Component.create(
			{
				ATTRS: {
					bannerBox: {
						setter: A.one,
						value: '.banner-box'
					},
					footerSitemap: {
						setter: A.one,
						value: '#footer .portlet-site-map'
					},
					urlSitemap: {
						value: '/sitemap'
					}
				},
				EXTENDS: A.Component,
				NAME: NAME,
				NS: NS,
				prototype: {
					referencesTooltip: null,
					bannerCarousel: null,
					footer: null,
					footerSitemapTree: null,
					initializer: function(config) {
						var instance = this;
						
						instance.footer = A.one('#footer');
					},
					
					renderUI: function() {
						var instance = this;
						
						instance._initBannerCarousel();
						instance._initFooterSitemap();
						instance._initMTabs();
						instance._initOfficeMaps();
						instance._initKonami();
						
					},
	
					bindUI: function() {
						var instance = this;
					},
					
					_afterFooterSitemapRender: function(e)  {
						var instance = this;
						
						// Make footer fixed
						instance.footer.addClass('footer-fix');

						// Show footer (if hidden)
						instance.footer.show();
					},
					
					_initBannerCarousel: function() {
						var instance = this;
						
						var bannerBox = instance.get(BANNER_BOX);
						
						if(A.UA.ie) {return;}
						if(isNull(bannerBox)) {return;}
						
						var bannerBoxContent = bannerBox.one('.banner-box-content');
						
						if(isNull(bannerBoxContent)) {return;}
						
						instance.bannerCarousel = new A.Carousel({
							intervalTime: 8,
							contentBox: bannerBoxContent,
							activeIndex: 'rand',
							height: 240,
							width: 930
						}).render();

						bannerBoxContent.all('a.banner-box-link').removeClass('aui-helper-hidden');
						bannerBox.addClass('banner-box-js');
					},
					
					_initFooterSitemap: function() {
						var instance = this;
						
						var footerSitemap = instance.get(FOOTER_SITEMAP);
						
						var boundingBox = footerSitemap.one('.portlet-body');
						var contentBox = footerSitemap.one('.portlet-body>ul');
						
						if(boundingBox) {
							
							// Append new root node
							var sitemapRootData = {
								urlSitemap: instance.get(URL_SITEMAP),
								textSitemap: '\u0053\u0069\u0074\u0065\u006d\u0061\u0070'
							};	    		
							var sitemapRoot = A.substitute(TPL_FOOTER_SITEMAP_ROOT, sitemapRootData);
							boundingBox.prepend(sitemapRoot);
							
							// Move old contentBox to new root,
							var rootListItem = boundingBox.one('ul.sitemap-tree-root>li');
							rootListItem.append(contentBox);
							
							// Update reference to contentBox
							contentBox = footerSitemap.one('ul.sitemap-tree-root');
							
							// Create and render tree view
							if(contentBox) {
								
								instance.footerSitemapTree = new A.TreeView({
									boundingBox: boundingBox,
									contentBox: contentBox,
									type: 'normal'
								});
								
								instance.footerSitemapTree.after('render', instance._afterFooterSitemapRender, instance);
								
								instance.footerSitemapTree.render();
							}
						}
					},
					
					_initKonami: function() {
						var instance = this;
						
						konami = new Konami();
						konami.pattern = KONAMI_CODE_STANDARD;
						konami.code = instance._onKonamiCode;

						konami.load();
					},
					
					_initMTabs: function() {
						
						var instance = this;
						
						var mTabsWrapNodes = A.all('.m-tabs-wrap');
						
						mTabsWrapNodes.each(function(mTabsWrapNode, index, mTabsWrapNodes) {
							var instance = this;
							
							mTabsWrapNode.addClass('m-tabs-wrap-tabbed');
							
							var contentNode = mTabsWrapNode.one('.m-tabs-content');
							var titleNodes = mTabsWrapNode.all('.m-tabs-title');
							
							mTabsWrapNode.prepend('<ul class="aui-tabview-list aui-widget-hd"></ul>');
							var listNode = mTabsWrapNode.one('ul.aui-tabview-list');
							
							titleNodes.each(function(node, index, list) {
								var listNode = this;
								listNode.append('<li class="aui-tab"><a class="aui-tab-label" href="javascript:;">' + node.html() + '</a></li>');
								node.hide();
							}, listNode);
							
							var mTabs = new A.TabView({
								boundingBox: mTabsWrapNode,
								listNode: listNode,
								contentNode: contentNode
							});
							mTabs.render();
							
						}, instance);
					},
					
					_initOfficeMaps: function() {
						var instance = this;
						
						var officeListNode = A.one('ul.offices');
						
						if(officeListNode) {
							var geocoder = new google.maps.Geocoder();
							var officeInfoNodes = officeListNode.all('.office-info-address');
							
							officeInfoNodes.each(function(node, index, list) {
								var instance = this;

								var streetNode = node.one('.office-info-street');
								var postalNode = node.one('.office-info-postal');
								
								var streetStr = streetNode.html();
								var postalStr = postalNode.html();
								
								var officeAddress = streetStr + ', ' + postalStr;
								
								geocoder.geocode({ 'address': officeAddress}, function(results, status) {
									if (status == google.maps.GeocoderStatus.OK) {
										A.log('GeoCoder succeeded.');
										instance._renderOfficeMap(node, results[0].geometry.location);
									}
									else {
										A.log('GeoCoder failed.');
									}
								});
								
							}, instance);
							
						}
						
					},
					
					_renderOfficeMap: function(officeAddressNode, latLng) {
						var instance = this;
						
						var officeListItem = officeAddressNode.ancestor('li');
						var officeMapNode = officeListItem.one('.office-map');
						var officeMapDOM = officeMapNode.getDOM();
						
						var addressHtml = officeAddressNode.html();
						var titleHtml = officeListItem.one('h2').html();
						
				    	var map = new google.maps.Map(officeMapDOM, {
					        center: latLng,
					        mapTypeControl: true,
					        mapTypeId: google.maps.MapTypeId.ROADMAP,
					        navigationControl: true,
					        scaleControl: true,
					        streetViewControl: true,
					        zoom: 12
				  		});
				  		
						var contentString = '<div class="office-map-info">' +
								'<div><strong>' + titleHtml + '</strong></div>' +
								'<div>' + addressHtml + '</div>' +
							'</div>';
						
						var infoWindow = new google.maps.InfoWindow({
							content: contentString
						});
						
					  	var marker = new google.maps.Marker({
					  		position: latLng, 
					  		map: map, 
					  		title: titleHtml
						});
						
						google.maps.event.addListener(marker, 'click', function() {
							infoWindow.open(map, marker);
						});
				
						// Launch info window
						google.maps.event.trigger(marker, 'click');
						//infoWindow.open(map, marker);
						
						
					},
					
					_onKonamiCode: function() {
						var instance = this;
						
						if(instance.konamiCallbackRunning) {
							return;
						}
						
						// Having some problems with ie
						if(A.UA.ie > 0) {
							return;
						}
						
						instance.konamiCallbackRunning = true;
							
						var dialog = new A.Dialog({
							bodyContent: 'You have entered the Konami Code.',
							centered: true,
							constrain2view: true,
							cssClass: 'konami-dialog',
							destroyOnClose: true,
							draggable: true,
							group: 'default',
							height: 660,
							modal: true,
							stack: true,
							title: 'Konami Code',
							width: 1000
						});
						
						// On before render listener
						dialog.before('render', function(e, params) {
							var instance = this;
							
							var dialog = params[0];
							
							var bodyContent = '';
							
							var TPL_KONAMI_DIALOG_TREAT = '' +
								'<div class="konami-dialog-body-content">' +
									'<h1>You have entered the Konami Code!</h1>' +
									'<p>And you\'re using a browser that supports web standards. Awesome. You\'re just as geeky as we are. Why don\'t you treat yourself with a nice game of Angry Birds.</p>' +
									'<iframe class="konami-iframe3" title="" frameborder="0" src="http://chrome.angrybirds.com/" width="954" height="550" scrolling="no"></iframe>' +
								'</div>'
							;
							
							var TPL_KONAMI_DIALOG_SMALL_SCREEN = '' +
								'<div class="konami-dialog-body-content">' +
									'<h1>You have entered the Konami Code!</h1>' +
									'<p>And you\'re using a browser that supports web standards. Awesome. You\'re just as geeky as we are.</p>' +
									'<p>Please come back and enter the Konami Code when your screen size is 1000x600 or more for an additional treat.</p>' +
								'</div>'
							;
							
							var TPL_KONAMI_DIALOG_IE = '' +
								'<div class="konami-dialog-body-content">' +
									'<h1>You have entered the Konami Code!</h1>' +
									'<p>Too bad you\'re not using a modern browser that supports web standards.</p>' +
									'<p>Download a standards based browser like <a href="http://www.mozilla.org/firefox" target="_BLANK">Firefox</a> or <a href="http://www.google.com/chrome" target="_BLANK">Chrome</a> and come back for an additional Konami Code treat.</p>' +
								'</div>'
							;
							
							if(A.UA.ie > 0) {
								bodyContent = TPL_KONAMI_DIALOG_IE;
								dialog.set('height', 200);
								dialog.set('width', 450);
							}
							else {
								var bodyNode = A.one('body');
								var winHeight = bodyNode.get('winHeight');
								var winWidth = bodyNode.get('winWidth');
								
								if(winHeight < 660 || winWidth < 1000) {
									bodyContent = TPL_KONAMI_DIALOG_SMALL_SCREEN;
									
									dialog.set('height', 200);
									dialog.set('width', 450);
								}
								
								else {
									bodyContent = TPL_KONAMI_DIALOG_TREAT;	
								}
							}
							
							dialog.set('bodyContent', bodyContent);
						}, instance, [dialog]);
						
						
						// On before close listener
						dialog.before('close', function(e, params) {
							var instance = this;
							
							instance.konamiCallbackRunning = false;
							
						}, instance, [dialog]);
						

						dialog.render();
					},
					
				    test: function() {
				    	instance = this;
				    	A.log('Test Method');
				    }
				}
			}
	);

	A.MonatorThemeMain = MonatorThemeMain;
		
	},1, {
		requires: [
			'aui-base',
			'aui-carousel',
			'aui-dialog',
			'aui-tabs',
			'aui-tooltip',
			'aui-tree-view',
			'event-mouseenter',
			'substitute'
      ]
	}
);
