/******************************************
 * The Screen Manager class
 ******************************************/

onepica.ScreenManager = Class.create({
	initialize: function() {
		this.initPrototips();
		this.initModals();
		this.initModalLinks();
	},

	/**
	 * Initializes the Prototip styles and open links around the site.
	 */
	initPrototips: function () {
		Prototip.Styles.preserve = {
			className: 'preserve-tip',
			border: 6,
			borderColor: '#0081c6',
			radius: 8,
			stem: { position: 'bottomRight', height: 26, width: 50 },
			hook: { target: 'topMiddle', tip: 'bottomRight', mouse: false },
			offset: { x: 55, y: 5 }
		};
		Prototip.Styles.preserveBot = {
			className: 'preserve-tip',
			border: 6,
			borderColor: '#0081c6',
			radius: 8,
			stem: { position: 'topRight', height: 26, width: 50 },
			hook: { target: 'bottomMiddle', tip: 'topRight', mouse: false },
			offset: { x: 55, y: -5 }
		};
		Prototip.Styles.preserveRight = {
			className: 'preserve-tip',
			border: 6,
			borderColor: '#0081c6',
			radius: 8,
			stem: { position: 'leftMiddle', height: 16, width: 50 },
			hook: { target: 'rightMiddle', tip: 'leftMiddle', mouse: false },
			offset: { x: 10, y: 25 }
		};
		
		// add prototips to links with class 'open-tip'
		$$('a.open-tip').each(function (el) {
			if (el.rel) {
				new Tip(el, el.rel, { style: 'preserve' });
			}
		});
	},
	
	/**
	 * Initializes the sitewide modals.
	 */
	initModals: function () {
		this.contentModal = new onepica.Modal.Content({destroyOnClose: false});
		this.contactModal = new onepica.Modal.Contact({destroyOnClose: false});
	},

	/**
	 * Initializes links to that open content modals.
	 */
	initModalLinks: function () {
		this.modalLinks = $$('a.open-modal');
		for (var i = 0; i < this.modalLinks.length; i++) {
			this.modalLinks[i].observe('click', this.eventOpenContentModal.bindAsEventListener(this));
		}
	},

	/**
	 * Returns the content modal object.
	 */
	getContentModal: function() {
		return this.contentModal;
	},
	
	getContactModal: function() {
		return this.contactModal;
	},

	/**
	 * Event triggered to open a content modal.
	 */
	eventOpenContentModal: function (event) {
		var link = event.findElement('a');
		var modalType = link.getAttribute('modaltype') ? link.getAttribute('modaltype') : 'content';
		var modal;
		switch (modalType.toLowerCase()) {  // suppose here to add other modal types
			case 'contact':
				modal = this.getContactModal();
				break;
			case 'content':
			default:
				modal = this.getContentModal();
		}
		modal.setUrl(link.getAttribute('url'));
		modal.load();
	},

	/**
	 * Logs an event described by the input parameters to Google Analytics.
	 *
	 * String   category The general event category (e.g. "Videos").
	 * String   actn The action for the event (e.g. "Play").
	 * String   opt_label An optional descriptor for the event.
	 * Int      opt_value An optional value to be aggregated with 
	 */
	trackEvent: function (category, action, label, value) {
		if (typeof(pageTracker) != 'undefined') {
			try {
				var success = pageTracker._trackEvent(category, action, label, value);
				/*
				if (success) 
					console.log("Event Logged: " + category + " - " + action + " / " + label);
				else
					console.log("Event logging FAILED");
				*/
			}
			catch (e) {}
		}
	},

	/**
	 * Tracks a page view to Google Analytics.
	 *
	 * String   opt_pageURL Optional parameter to indicate what page URL to track metrics under. 
	 *			When using this option, use a beginning slash (/) to indicate the page URL. 
	 */
	trackPageview: function (pageUrl) {
		if (typeof pageTracker != 'undefined') {
			var host = window.location.protocol + "//" + window.location.host;
			if (pageUrl && pageUrl.length > host.length && pageUrl.substr(0, host.length) == host) {
				pageUrl = pageUrl.substr(host.length+1);
			}
			try {
				pageTracker._trackPageview(pageUrl);
				//console.log("Page tracked: " + pageUrl);
			}
			catch (e) {}
		}
	}
});