/*
 * Pilots core, requires Scriptaculous
 * 
 * Copyright (c) 2008. Elements Interactive.
 * All rights reserved.
 */

/*
 * TODO:
 * 
 * Zoomout in gallery ?
 * Bar is clickable, gallery will move, but fader check is not working properly.
 */

// Start the pilots object
var pilots = {
	version: '1.0.0',
	REQUIRED_PROTOTYPE: '1.6.0',
	debugManager: null,
	panel: 'home',
	doneFading: true,
	
	init: function() {
		
		// Loading xml.php via HTTP and parse it
		var xmlLocation = 'xml.xml';
		var xotree = new XML.ObjTree();
		xotree.attr_prefix = '@';
		pilots.debug('PILOTS: Parsing xml file "'+xmlLocation+'"...');
		xotree.parseHTTP( xmlLocation, null, pilots.loadXml );
		
		// Create the sliding panels for the id pilotsViewer
		if($("pilotsViewer") && Spry)
			pilots.ps = new Spry.Widget.SlidingPanels('pilotsViewer');
		
		$('content').setStyle({
			marginTop: ((document.documentElement.clientHeight/2)-($('content').getHeight()/2) - (Prototype.Browser.IE ? '0' : '15')) + 'px'
		});
		
	},
	
	require: function(libraryName) {
		// inserting via DOM fails in Safari 2.0, so brute force approach
		document.write('<script type="text/javascript" src="'+libraryName+'"><\/script>');
	},
	
	load: function() {
	    $A(document.getElementsByTagName("script")).findAll( function(s) {
	      return (s.src && s.src.match(/pilots\.js(\?.*)?$/))
	    }).each( function(s) {
	      var path = s.src.replace(/pilots\.js(\?.*)?$/,'');
	      var includes = s.src.match(/\?.*load=([a-z,]*)/);
	      (includes ? includes[1] : 'lib/lightbox,debug,scroller,xml_lib,xml,gallery,process,clients').split(',').each(
	       function(include) { Scriptaculous.require(path+include+'.js') });
	    });
	}
}

// Start loading all modules asap...
pilots.load();

/**
 * Triggered by pilots.init as callback.
 * Pass the parsed xml data to the modules that need it 
 * 
 * @param {Object} xml
 */
pilots.loadXml = function(xml) {
	pilots.debug('PILOTS: Xml data parsed, passing it on...');
	pilots.xml = new pilots.content(xml);
	pilots.process = new pilots.processPage(xml.data.process);
	pilots.clients = new pilots.clientsPage(xml.data.clients);
	pilots.gallery.init(xml.data.gallery);
	pilots.xmlData = xml.data;
}

/**
 * Switch the main pilots panel to another page.
 * 
 * @param {Object} panel	The panel index to navigate to.
 * @param {Object} obj		The object to add class "active" like a menu item.
 */
pilots.switchPanel = function(panel, obj) {
	
	if(pilots.panel == panel && panel == 'gallery' && pilots.gallery.status.zoom == true) {
		var galleryWrapper = $('gallery-wrapper');
		new Effect.Opacity(galleryWrapper, {
			to: 0,
			from: 1,
			duration: .5,
			afterFinish: function(){
				galleryWrapper.update();
				pilots.gallery.init(pilots.xmlData.gallery);
				new Effect.Opacity(galleryWrapper, {
					to: 1,
					from: 0,
					duration: .5,
					afterFinish: function(){
						// f00						
					}
				});
			}
		});
	}
	
//	if(pilots.panel == panel && panel == 'process' && pilots.process.zoomed == true) {
//		var processWrapper = $('process');
//		new Effect.Opacity(processWrapper, {
//			to: 0,
//			from: 1,
//			duration: .5,
//			afterFinish: function(){
//				processWrapper.update();
//				pilots.process = new pilots.processPage(pilots.xmlData.process);
//				new Effect.Opacity(processWrapper, {
//					to: 1,
//					from: 0,
//					duration: .5,
//					afterFinish: function(){
//						// f00						
//					}
//				});
//			}
//		});
//	}
	
	if(pilots.panel == panel)
		return;
	
	pilots.debug('<hr /> --> Switch to panel "'+panel+'"', 2);
	
	pilots.xml.scrollBars.each(function(scroller) {
		scroller.reset();
	});
	
	pilots.xml.switchPanel(panel);
	pilots.ps.showPanel('page-'+panel);

	var navigator = $('navigator');
	navigator.className = "navigator " + panel.replace(/page\-/, '');
	
	var currentActive = $$('#header a.active');
	var currentActive = currentActive[0];
	currentActive.removeClassName("active");
	
	obj = $(obj);

	obj.addClassName("active");
	
	pilots.panel = panel;
}

/**
 * Make the background resizeable.
 */
pilots.resizeBackground = function() {
	var self = this;
	this.body = $("body"); 
	this.container = $("pilots-background");
	this.image = $("pilots-background").adjacent('#pilots-background img');
	this.image = this.image[0];	
	this.image.setStyle({ position: "absolute", left: "-10000px" });
	this.alternate = false;
	this.check = function() {	
		if(this.alternate && this.image.getWidth() < this.container.getWidth()) {
			this.image.removeClassName("alternate");
			this.alternate = false;
		}
		if(!this.alternate && this.image.getHeight() < this.container.getHeight()) {
			this.image.addClassName("alternate");
			this.alternate = true;
		}
		var margin = -(this.image.getHeight() - this.container.getHeight())/2 + "px 0 0 " + -(this.image.getWidth() - this.container.getWidth())/2 + "px";
		this.image.setStyle({ margin: margin });
	}
	Event.observe( window, 'resize', function() { self.check(); });
	self.check(); 
	this.image.setStyle({ position: "static" });
	
}

window.onload=function() {
	new pilots.debugManager();
	pilots.init();

	if(typeof alphaBackgrounds != 'undefined')
		alphaBackgrounds();
}

/**
 * Google maps part....
 */
function load() {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map"));
    map.setCenter(new GLatLng(52.367464,4.917197), 16);
	
	map.addOverlay(new GMarker(new GLatLng(52.367464,4.917197)));

	
  }
}