/**
* @fileoverview This file contains the static main object.
* @author Sara Lin
* @version 0.1
*/

/** the google analytics tracking instance */
var pageTracker =  null;
	
/**
Main is the main object that is executed when the website first loads.  It initializes all other objects and attaches event handlers to footer links.
* @namespace
*/
var Main = {
	initialize: function(floor, location) {
		/** selected item's itemID */
		this.curItemID = null;
		/** tabs object */
		this.tabs = null;
		/** user object */
		this.user = null;
		/** an array of draggable divs.  This arra is iterated to find at a given moment, whether these divs are being dragged or clicked on. */
		this.draggables = [];
		
		var thisObj = this;
		
		//attach all the events here
		Event.observe(window, 'load', function() {
			Main.initTracking();
			Main.eventTracking();
			
			thisObj.tabs = new TabSystem();
			thisObj.user = new User($('admin'), "click");
			thisObj.tabs.loadTab('search','Search',true,true);
			
			thisObj.tabs.loadTab('item','Item Details',true,true);
			thisObj.tabs.loadTab('about','About SmartMap',true,true);
			thisObj.tabs.loadTab('feedback', 'Send us your feedback', true, true);
			thisObj.tabs.loadTab('help', 'Help', true, true);
			
			// initialize objects
			Help.initialize();
			ItemTab.initialize();
			Search.initialize();
			FloorManager.initialize(floor);
			Search.keyword._search(location);
			
			var drag = new Draggable('coordinateToolbox', {
				zindex: 1, 
				starteffect: null,
				endeffect: null});
			Main.draggables.push(drag);
			
			//register footer links
			Event.observe('help', "click", Footer.help);
			Event.observe('header-help', "click", Footer.help);
			Event.observe('feedback', "click", Footer.feedback);
			Event.observe('about', "click", Footer.about);		
		});
	},
	
	/**
	* attach event handlers to some click events for page tracking, such as floor switch.
	*/
	eventTracking: function() {
		// tracking the floor swtiches
		$$('.navFloor').each(function(el) {
			Event.observe($(el), "click", function(event) {
				var el = $(Event.element(event)).up('.navFloor');
				var floor = el.id.split('_')[1];
				Main.trackPage('/floor/'+floor+'.html');
			});
		});
		
		$$('#bottombox a').each(function(el) {
			Event.observe($(el), "click", function(event) {
				var el = Event.element(event);
				Main.trackPage('/'+el.id+'.html');
			});
		});
		
		$$('#header-opt a').each(function(el) {
			Event.observe($(el), "click", function(event) {
				var el = Event.element(event);
				Main.trackPage('/'+el.id+'.html');
			});
		});
	},
	
	/**
	* this function is called by item clicking and search to record the item clicked or searched keywords.  
	* @param {String} page the fake page link the event supposedly is directed to
	*/
	trackPage: function(page) {
		pageTracker._trackPageview(page);
	},
	
	/**
	* initTracking is invoked after page finished loading.  It initializes the pageTracker used to track page events.
	*/
	initTracking: function() {
		pageTracker = _gat._getTracker("UA-4339251-2");
		pageTracker._initData();
		pageTracker._trackPageview();
	}
}