 /***************************************************************************\
 * ThyAPI - Thyamad Javascript API - HomePage Main Javascript                *
 * http://www.thyamad.com                                                    *
 *                                                                           *
 * Copyright (C) 2005 - Raphael Derosso Pereira                              *
 * Based on DynAPI v3.0b1                                                    *
 * ------------------------------------------------------------------------- *
 *  This library is free software; you can redistribute it and/or modify it  *
 *  under the terms of the GNU Lesser General Public License as published    *
 *  by the Free Software Foundation; either version 2.1 of the License,      *
 *  or any later version.                                                    *
 *  This library is distributed in the hope that it will be useful, but      *
 *  WITHOUT ANY WARRANTY; without even the implied warranty of               *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     *
 *  See the GNU Lesser General Public License for more details.              *
 \***************************************************************************/

	/**
	 * thyAPI Home Page JS File
	 *
	 *	This file constructs the interface on thyAPI Home Page.
	 *
	 *	Althought thyAPI was not meant to be used directly as a page creator,
	 *	this page was done this way to show some of its powers.
	 *
	 */

	/**
	 * The method below is used to populate the internal elements
	 *
	 */
	var general_populate = function (data)
	{
		var i;

		if (data.title)
		{
			this.setTitle(data.title);
		}
		
		for (i in data)
		{
			if (this[i] && this[i].populate)
			{
				this[i].populate(data[i]);
			}
		}
	}
	
	/*************************************************************************\
	 *                           Left Panel                                  *
	\*************************************************************************/
	function leftPanel()
	{
		this.thyWindow = thyWindow;
		this.thyWindow('left_panel');

		this.docBtn     = new thyButton(this.name+'_doc');
		this.downBtn    = new thyButton(this.name+'_down');
		this.contactBtn = new thyButton(this.name+'_contact');
		this.clientBtn  = new thyButton(this.name+'_client');
		this.serverBtn  = new thyButton(this.name+'_server');
		
		this.addChild(this.docBtn);
		this.addChild(this.downBtn);
		this.addChild(this.contactBtn);
		this.addChild(this.clientBtn);
		this.addChild(this.serverBtn);

		// Data Source \\
		var dataSrc = new thyDataSource(this.name+'_name');
		dataSrc.setServerClass('thyAPILeftPanel');
		dataSrc.useSync = true;
		
		//* Interface components *\\
		dataSrc.link(this, 'leftPanel', ['startup','read','write']);
		dataSrc.startup();

		// These are the event listeners for the buttons \\

		this.docBtn.addEventListener({
			onclick: function () {window.location = 'http://doc.thyamad.com/wiki/index.php/ThyAPI';}
		});
		this.downBtn.addEventListener({
			onclick: function () {window.location = 'http://www.sf.net/projects/thyapi';}
		});
		this.contactBtn.addEventListener({
			onclick: function () {window.location = 'http://www.thyamad.com/';}
		});
		this.clientBtn.addEventListener({
			onclick: function () {window.location = 'thyAPIHome.js';}
		});
		this.serverBtn.addEventListener({
			onclick: function () {window.location = 'server.php';}
		});
	}

	p = dynapi.setPrototype('leftPanel','thyWindow');

	p.populate = general_populate;
	


	
	/*************************************************************************\
	 *                           Right Panel                                 *
	\*************************************************************************/
	function rightPanel()
	{
		this.thyWindow = thyWindow;
		this.thyWindow('right_panel');

		var dataSrc = new thyDataSource(this.name+'_name');
		dataSrc.setServerClass('thyAPIRightPanel');
		dataSrc.useSync = true;
		
		//* Interface components *\\
		dataSrc.link(this, 'rightPanel', ['startup','read','write']);
		dataSrc.startup();
	}

	p = dynapi.setPrototype('rightPanel','thyWindow');

	p.populate = general_populate;
	


	
	/*************************************************************************\
	 *                            Main Panel                                 *
	\*************************************************************************/
	function mainPanel()
	{
		this.thyWindow = thyWindow;
		this.thyWindow('main_panel');

		var dataSrc = new thyDataSource(this.name+'_name');
		dataSrc.setServerClass('thyAPIMainPanel');
		dataSrc.useSync = true;
		
		//* Interface components *\\
		dataSrc.link(this, 'mainPanel', ['startup','read','write']);
		dataSrc.startup();
	}

	p = dynapi.setPrototype('mainPanel','thyWindow');

	p.populate = general_populate;




	/*************************************************************************\
	 *                           Page Back Panel                             *
	\*************************************************************************/
	function MainPage()
	{
		this.thyPanel = thyPanel;
		this.thyPanel('main_page');

		//* Interfaces *\\
		this.left  = new leftPanel();
		this.right = new rightPanel();
		this.main  = new mainPanel();

		this.ltBar = new thyPanel(this.name+'_ltBar');
		this.logo  = new thyPanel(this.name+'_logo');
		this.rtBar = new thyPanel(this.name+'_rtBar');
		this.bBar  = new thyPanel(this.name+'_bBar');
		this.copy  = new thyPanel(this.name+'_copy');

		var dataSrc = new thyDataSource(this.name+'_dataSrc');
		dataSrc.setServerClass('thyAPIMainPage');
		dataSrc.useSync = true;
		
		dataSrc.link(this, 'mainPage', ['startup','read','write']);
		dataSrc.startup();

		//* Communication to server initialization *\\
		thySync.setProtocol(new thyXMLRPCProtocol('server.php'));
		thySync.setSyncMethod('thyAPIServer.sync');

		this.addChild(this.left);
		this.addChild(this.right);
		this.addChild(this.main);
		this.addChild(this.ltBar);
		this.addChild(this.rtBar);
		this.addChild(this.logo);
		this.addChild(this.bBar);
		this.addChild(this.copy);

		// thyWindow is the only exception for CSS positioning
		// simply because when you open a window, it should be
		// positioned in a visible area.
		var self = this;
		this.left.onCreate(function ()
		{
			self.left.open();

			self.left.setX(10);
			self.left.setY(60);
		});

		this.right.onCreate(function ()
		{
			self.right.open();
		
			self.right.setX(710);
			self.right.setY(60);
		});

		this.main.onCreate(function ()
		{
			self.main.open();
			
			self.main.setX(160);
			self.main.setY(160);
		});
	}

	p = dynapi.setPrototype('MainPage','thyPanel');

	p.populate = general_populate;
	
	//* Initialize Main Page *\\
	var mainPage;
	function createPage(){
		mainPage = new MainPage();
		dynapi.document.addChild(mainPage);
	};

	dynapi.onLoad(createPage);
