///////////////////////////////////////////////////////////////////////////////
//
//  StageObjects.js   			version 1.0
//  2007 Julia Yu, Lenny Burdette
//
//  These objects appear on the stage and are usually singletons
//  they call creation on XamlObjects
//
//  FUNCTIONS:
//			Loc.preloader
//			Loc.nav
//			Loc.navButton
//			Loc.stage
//			
//	DEPENDENCIES:
//			Utils.js
//			XamlObjects.js
//
///////////////////////////////////////////////////////////////////////////////

if ( !window.Loc) { window.Loc = {}; }

/**
 * The generic preloader
 */
Loc.Preloader = function(params){
	this.name = params.name;
	this.host = params.parent || Loc.root;
	this.top = params.top || 0;
	this.left = params.left || 0;
	this.bar = params.bar || true;
	this.fragment = Loc.loaderFragment

	try {
		this.host.children.add(this.fragment);
	}catch(e) {
		return; 
	}
	this.self = this.fragment.findName('preloader');
	this.spinWheel = this.fragment.findName("rotate");
	if (this.spinWheel) {
		this.spinWheel.Begin();	
	}
	
	this.position(this.top, this.left);
}

Loc.Preloader.prototype.position = function(top, left) {
	if (top) {
		this.self['Canvas.Top'] = top;
	}
	if (left) {
		this.self['Canvas.Left'] = left;
	}
}

Loc.Preloader.prototype.stop = function() {
	var preloadXaml = this.fragment.findName("preloader");
    if (preloadXaml != null){
        this.host.children.remove(preloadXaml); // removing from root
    }
}

/*
 * nav
 */

Loc.nav = function(params) {
	this.name = params.name || "maps_nav";
	this.host = params.parent || Loc.root; 		
	this.selfContent = this.host.findName('nav');
	
	Loc.make('navButton', {name: 'map1507', overState: '1507_over', hitState: '1507_hit', page: 'map1507'});
	Loc.make('navButton', {name: 'map1516', overState: '1516_over', hitState: '1516_hit', page: 'map1516'});
	Loc.make('navButton', {name: 'book', overState: 'book_over', hitState: 'book_hit', page: 'book'});
}

/**
 * Nav button area
 */
Loc.navButton = function(params) {
	this.name = params.name || "navbutton";
	this.selfContent = params.parent || Loc.root;
	this.state = "off";
	
	this.page = params.page;
	this.overBG = this.selfContent.findName(params.overState);
	this.hitArea = this.selfContent.findName(params.hitState);
	
	this.hitArea.addEventListener('MouseLeftButtonDown', this.onClick.bind(this));
	
	Loc.dispatch.subscribe("pageLoad", this.name, this.listenPageLoad.bind(this));
}
Loc.navButton.prototype.onClick = function() {
	this.state = "on";
	this.overBG.Visibility = "Visible"

	Loc.dispatch.broadcast("pageLoad", this.page);
}

Loc.navButton.prototype.listenPageLoad = function(pageName) {
	if (pageName != this.name) {
		this.state = "off";
		this.overBG.Visibility = "Collapsed";
	} else {
		this.state = "on";	
		this.overBG.Visibility = "Visible";
	}
}

/**
 * Stage content area
 */
Loc.stage = function(params) {
	this.name = params.name; 	
	this.host = params.parent || Loc.root; 	
	this.selfContent = this.host.findName('stage_content');

	Loc.dispatch.subscribe("pageLoad", this.name, this.listenPageLoad.bind(this));
}

Loc.extend(Loc.stage, Loc.clearable);
	
Loc.stage.prototype.listenPageLoad = function(pageName) {
	var myPage = Loc.page[pageName];	
	if (myPage.stageContent) {
		this.makeNewChildren(myPage.stageContent, "stage");
	} else {
		this.clearChildren();	// always remove children on page load
	}
	
	if ((pageName == "map1507") || (pageName == "map1516")) {
		this.addChild('button', { 
			name: 'btn_explore', 
			type: 'image', 
			imageSource: 'cmn/img/btn_explore.png', 
			w : 133, h : 32, top: 350, left: 495,
			click: function() { Loc.subPage(pageName); }
		});
	}
}

/**
 * Load a sub page of the current asset
 */
Loc.subPage = function(pageType) {	
	Loc.tracker.stage.stage_content.clearChildren();
	Loc.tracker.panel.panel_intro.clearChildren();
	Loc.tracker.panel.panel_intro.hide();
	
	var thisStage = Loc.tracker.stage.stage_content;
	thisStage.addChild('Zoomer', { parent : thisStage.selfContent, page: pageType, top: 0 });

}
 