DC = window.DC || {};
DC.Chrome = (function() {
	// Base configuration.
	var itemsX = 4;
	var itemsY = 3;
	var startIndex = 1;
	var classes = HD.CSS_CLASSES;
	var dateFormat = "d mmm yyyy 'at' hh:MM tt";
	var bikeUrl = 'bikes-gallery.html';
	var loginCookie = comSiteName + '-login';
	
	return {
		// #navBar.
		headerEl: null,
		// #footer.
		footerEl: null,
		// #hdNavRSS.
		followEl: null,
		// Categories config.
		categories: [],
		// Whether a modify profile is queued.
		getProfile: false,
	
		// Initialize the page.
		init: function() {
			var self = this;
		
			// Preselect common elements.
			this.headerEl = HD.get('hdLogin');
			this.footerEl = HD.get('footer');
			this.followEl = HD.get('hdNavRSS');
			this.bikesEl = HD.get('hdNavBikes');			

			// Initialize the main engine.
			this.bikesEngine = new HD.CommunityEngine(this.config.bikes);
			
			// Attach widget observers.
			this.setObservers();
			
			// Attach DOM event listeners.
			this.setListeners();
			
			// Set global templates.
			this.setTemplates();
			
			// Get media.
			this.bikesEngine.dao.getPopular(8, HD.SORTS.NEW_TO_OLD);
		},
	
		// Engine/widget config.
		config: {
			bikes: {
				dao: {
					baseUrl : comSvcBaseUrl,
					siteName : comSiteName,
					contentType : 'both',
					categories : ['Bikes/Fat Bob', 'Bikes/Iron 883', 'Bikes/Nightster', 'Bikes/Street Bob', 'Bikes/Forty-Eight', 'Bikes/Seventy Two', 'Bikes/Other H-D bike', 'Bikes/Other bike'],
					startIndex : 1,
					pageSize : 8
				},
				cluster: {
					baseUrl : comMediaBaseUrl
				},
				view: {
					navigation : 'nav',
					canvas : 'canvas'
				},
				widgets: {
					latestBikes: {
						parent: 'latestBikes',
						type: 'hd_feature_stories_widget',
						contentType: 'both',
						pageSize: 8,
						previewLength: 0,
						templates: {
							featureStoriesLabel: '<em>LATEST</em> BIKE SUBMISSIONS',
							viewAllStoriesText: 'More Bike Submissions',
							html: '\
								<div class="${classes.FEATURE_STORIES} ${classes.WIDGET}">\
									<span class="${classes.TITLE}">${templates.getFeatureStoriesLabel()}</span>\
									<ul class="${classes.LIST}">\
										{for item in items}\
											<li>${item|facade}</li>\
										{/for}\
									</ul>\
									<a href="' + bikeUrl + '" class="${classes.VIEW_ALL} ${classes.CONTROL}">${templates.getViewAllStoriesText()}</a>\
								</div>'
						},
						photoTemplates: {
							html: '<div class="${classes.ITEM} ${classes.PHOTO}"><a href="' + bikeUrl + '#loc=detail/${photo.contentType}/${photo.mediaId}"><div class="${classes.SMALL_THUMB_WRAPPER}"><div class="${classes.SMALL_THUMB}" style="background: transparent url(${photo.thumbUrl}) no-repeat 50% 50%;"><div class="${classes.THUMB_OVERLAY}"></div></div></div></a></div>'
						},
						videoTemplates: {
							html: '<div class="${classes.ITEM} ${classes.VIDEO}"><a href="' + bikeUrl + '#loc=detail/${video.contentType}/${video.mediaId}"><div class="${classes.SMALL_THUMB_WRAPPER}"><div class="${classes.SMALL_THUMB}" style="background: transparent url(${video.thumbUrl}) no-repeat 50% 50%;"><div class="${classes.THUMB_OVERLAY}"></div></div></div></a></div>'
						}
					}
				}
			}
		},
		
		// Attach observer methods to widgets.
		setObservers: function() {
			var self = this;
			
			// Transform links function (for same page links).
			var transformLink = function(el) {
				var match = el.href.match(/#loc=detail\/(.+?)\/(\d+)/);
				if (match && match[1] && match[2]) {
					el.onclick = function() {
						DarkCustom.setNavigation('detail', match[1], match[2], null, false);
					};
				}
			};
			
			this.bikesEngine.addObserver({
				widgets: ['dao'],
				update: function(eventName, eventData) {
					if (eventName == "getPopular_Finish" && DarkCustom.analytics.section == 'Bikes') {
						setTimeout(function() {
							var linkEls = HD.get('latestBikes').getElementsByTagName('a');
							for (var i = 0, l = linkEls.length; i < l; i++) {
								transformLink(linkEls[i]);
							}
						}, 50);
					}
				}
			});
			
			// Attach login observer.
			this.bikesEngine.addObserver({
				widgets: [login.model],
				update: function(eventName, eventData) {
					if (eventName == "logged_In") {
						HD.addClass(self.headerEl, "hdLI");
						HD.removeClass(self.headerEl, "hdNLI");
						YAHOO.util.Cookie.setSubs(loginCookie, { login: 'yes' });
					}
					else if (eventName == "notLogged_In") {
						HD.addClass(self.headerEl, "hdNLI");
						HD.removeClass(self.headerEl, "hdLI");
						YAHOO.util.Cookie.remove(loginCookie);
					}
					else if (eventName == "loginInit_Finish") {
						var url = eventData.profile + '/cpapp/profile/indexPage.h-d?ret_uri=' + escape(eventData.returnUrl) + '&req_uri=' + escape(window.location) + '&hideTopNav=true';
						HD.get('getProfile').href = "javascript: DC.Chrome.openProfile('"+url+"')";
						if (self.getProfile) {
							window.location = url;
						}
					}
				}
			});
			// Update state if cookie is live.
			var loginState = YAHOO.util.Cookie.getSubs(loginCookie);
			if (loginState && loginState.login) {
				HD.addClass(self.headerEl, "hdLI");
				HD.removeClass(self.headerEl, "hdNLI");
				
				var detailFormEl = HD.get('detail');
				if (detailFormEl){
					HD.addClass(detailFormEl, "hdLI");
					HD.removeClass(detailFormEl, "hdNLI");
				}
			}
		},
		
		// Attach DOM event listeners.
		setListeners: function() {
			var self = this;
			
			// Attach footer link listeners.
			HD.get('dcLegal').onclick = function() {
				window.open('/wcm/Content/Pages/Utility/legal_notice.jsp?locale=en_US', '', 'width=598,height=540,scrollbars=yes,resizable=yes,toolbar=no,location=no,menubar=no,directories=no,status=no');
			};
			HD.get('dcPrivacy').onclick = function() {
				window.open('/wcm/Content/Pages/Utility/privacy_policy.jsp?locale=en_US', '', 'width=598,height=540,scrollbars=yes,resizable=yes,toolbar=no,location=no,menubar=no,directories=no,status=no');
			};

			// Attach Nav link listeners.
			self.followEl.onmouseover = function() {
				HD.addClass(self.followEl, 'showFollow');
			};
			self.followEl.onmouseout = function() {
				HD.removeClass(self.followEl, 'showFollow');
			};			
			self.bikesEl.onmouseover = function() {
				HD.addClass(self.bikesEl, 'showBikes');
			};
			self.bikesEl.onmouseout = function() {
				HD.removeClass(self.bikesEl, 'showBikes');
			};			
		},
		
		// Set custom templates.
		setTemplates: function() {
			// Custom templates as necessary.
		},
		
		// Redirect to profile page.
		queueProfile: function() {
			this.getProfile = true;
		},
		
		chooseLogin: function(){
			login.model.isUserLoggedIn(function(userResponse) {
				
				if(userResponse.loggedInLevel > 0){
					login.view.promptCreateScreenName();
				}
				else{
					login.view.promptAuthentication();
				}
				
			});
		},
		
		openProfile: function(url){
			var profileWindow = window.open(url, '', 'width=800,height=550,scrollbars=yes,resizable=yes,toolbar=no,location=no,menubar=no,directories=no,status=no');
		}
	};
})();

// Load the page.
HD.util.DOM.onDOMReady(function() {
	DC.Chrome.init();
});

