var comSvcBaseUrl = "/Community/services";
var comMediaBaseUrl = "/Community/media";
var comSiteName = "Dark Custom";
var comPageUrl = "downloads.html";

var login = null;

var DarkCustom = (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 services = HD.ShareWidget.SERVICES;
	
	return {
		// #darkCustom (the parent container).
		containerEl: null,
		// Categories config.
		categories: [],
		// Analytics settings.
		analytics: {
		},
	
		// Initialize the page.
		init: function() {
			var self = this;
		
			// Preselect common elements.
			this.containerEl = HD.get('darkCustom');
			
			// Initialize the session.
			HD.util.DWR.initSession();
			HD.util.DWR.setErrorHandler();
			login = HD.Profile.profile;
			
			//Override copy
			HD.SendFriendWidget.templates.sendFriendInstructionalText = this.templates.sendFriend.sendFriendTemplates.sendFriendInstructionalText;
			
			// Initialize the main engine.
			this.engine = new HD.CommunityEngine(this.config);
			
			// Set analytics
			if (this.setAnalytics) {
				this.setAnalytics();
			}
			
			// Attach DOM event listeners.
			this.setListeners();
			
			// Create the object to be shared here
			var sharedItem = {
					author : "Dark Custom",
					category : "Dark Custom",
					description : 'Dark Custom | Downloads',
					thumbUrl : '',
					title : 'Dark Custom | Downloads'
			};
			
			this.engine.dao.notifyObservers.call(this.engine.dao, "loadShare_Start", sharedItem);
			this.engine.dao.notifyObservers.call(this.engine.dao, "loadSTAF_Start", sharedItem);
		},
	
		// Engine/widget config.
		config: {
			dao: {
				baseUrl : comSvcBaseUrl,
				siteName : comSiteName,
				contentType : 'both',
				categories : [],
				startIndex : startIndex,
				pageSize : itemsX * itemsY
			},
			cluster: {
				baseUrl : comMediaBaseUrl
			},
			view: {
				navigation : 'nav',
				canvas : 'canvas'
			},
			widgets: {
				share: {
					parent: 'share',
					type: 'hd_share_widget',
					getPermalink: function(media) {
						return window.location.href;
					},
					services: [services.FACEBOOK, services.TWITTER, services.MYSPACE, services.BLOGGER, services.WORDPRESS],
					callback: function(service, title, description, thumbnail, permalink) {
						
					},
					standaloneMode : true
				},
				sendFriend: HD.merge({
					parent: 'sendFriend',
					type: 'hd_send_friend_widget',
					standaloneMode : true
				}, sendFriendConfig)
			}
		},
		
		// Retrieve the current navigation location.
		getNavigation: function() {
			return window.location.href;
		},
		
		// Attach DOM event listeners.
		setListeners: function() {
			
			HD.getByClass('size6', '*', 'darkCustom', function(el) {
				el.onclick = function() {
					var trackingPath = '/Products/Motorcycles/Dark+Custom/Wallpapers/Download/iPhone/' + el.id;
					_hbPageView('downloads.html', trackingPath); 
				};
			});
			
		}
	};
})();

// Load the page.
HD.util.DOM.onDOMReady(function() {
	DarkCustom.init();
});

//Profile callback to prompt for login
if(typeof HD == "undefined" || !HD){
	var HD = {};
}
if(typeof HD.Config == "undefined" || !HD.Config){
	HD.Config = {};
}

HD.Config.profile = {
		showSoftProfile: false,
		requireScreenName: true,
		cssFile: "/en_US/Media/css/dark_custom_v3/dark_custom_downloadsLogin.css",
		initCallback: function() {

	HD.Profile.profile.model.getUser(function(userResponse) {	

		var observer = {
			update : function(eventName, eventData) {
				if (eventName == HD.Profile.profile.model.getEventName('LOGGED_IN') || eventName == HD.Profile.profile.model.getEventName('SCREENNAME_CREATE_SUCCESSFUL')) {				
					UserService.getLoggedInStatus({ token: HD.Profile.token, siteName : comSiteName });
					var el = HD.get('hdLogin');
					HD.addClass(el, "hdLI");
					HD.removeClass(el, "hdNLI");
				}
				else if (eventName == "notLogged_In") {
					var el = HD.get('hdLogin');
					HD.addClass(el, "hdNLI");
					HD.removeClass(el, "hdLI");
				}
			}

		};

		HD.util.Common.getObservable().addObserver(observer);
		HD.Profile.profile.model.addObserver(observer);

		HD.Profile.Content.AUTHENTICATE.contentURL = "/en_US/Content/Pages/dark-custom/authenticate.htm";
		HD.Profile.Content.REGISTER.contentURL = "/en_US/Content/Pages/dark-custom/create_profile.htm";
		
	});

}};

