/*
    Copyright (C) 2008  Ivo Toby, Internet Voor Ondernemers, http://www.i-v-o.nl / http://syntacticsugar.nl 

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

	Crudder Version : 0.50b
 */


	var crudderController = Class.create({
		
		initialize: function(container, config, rpcPath, noDrawAfterBoot){
			this.instID = 'crudderController.' + parseInt(Math.random()*1000000);
			this.config = config;
			this.container = container;
			this.langAttempts = 0;
			this.booted = false;
			this.tinyCount = 0; // needed for WPS (uses OLD tiny lib)
			if (!rpcPath) rpcPath = '/index.php';
			if (rpcPath.indexOf(window.location.protocol) > -1){
				this.rpcPath = rpcPath;
			}else{
				this.rpcPath = window.location.protocol + '//' +window.location.hostname + rpcPath +"?";
			}
			if (noDrawAfterBoot) {
				this.noDrawAfterBoot = true;
			}else{
				this.noDrawAfterBoot = false;
			}
			var RPCObj = new crudderRPC(this.config, this.rpcPath);
			RPCObj.debug = true;
			RPCObj.createCall(this.boot.bind(this), this);
			RPCObj.call('getMetaData');		
		},
		
		boot : function(req){
			this.metaData = req.responseJSON;
			this.setEnv();
			this.waiterImg = new Element('img', {src:this.imgPath + "waiter.gif", className:'crudderWaiter'});
			this.container.insert(this.waiterImg);
			// create 2 containers for both the views:
			this.gridContainer = new Element('div', {id:this.instID +"_gridContainer"});
			this.container.insert(this.gridContainer);
			
			this.recordContainer = new Element('div', {id:this.instID +"_recordContainer"});
			this.container.insert(this.recordContainer);
			this.handleCustomEvent('beforeopen');
			this.factory = new crudderDataFactory(this.config, this.rpcPath, this.metaData);
			this.table = new crudderTable(this.metaData, this.gridContainer, this);
			this.booted = true;
			var hash = window.location.hash;
			
			if (hash){
				hash = hash.replace("#","");
				console.log(hash)
				this.factory.set('pKey', hash);
				this.factory.execute(
						(function(req){
							this.editRecord(req.responseJSON.data[0]);
						}).bind(this)
				)
				
			}else{
				if (!this.noDrawAfterBoot) this.table.drawGrid();
			}
			
		},
		
		setEnv : function(){
			this.theme = this.metaData.config.theme;
			if (!this.theme) this.theme = 'default';
			this.theme = gCrudderPath + '/themes/' + this.theme;
			this.lang = this.metaData.config.lang;
			if (!this.lang) this.lang = 'enUS';
			this.langScript = gCrudderPath + '/js/lang/' + this.lang + '.js';
			
			var link = new Element('script', {type:"text/javascript", src:this.langScript})
			if (document.getElementsByTagName('head').length > 0){
				$(document.getElementsByTagName('head').item(0)).insert(link);
			}
			
			// set <link> tag in head and add lang-library
			var link = new Element('link', {type:"text/css", rel:"stylesheet", href: this.theme + '/default.css'})
			if (document.getElementsByTagName('head').length > 0){
				$(document.getElementsByTagName('head').item(0)).insert(link);
			}
			this.importLang();
			this.imgPath = this.theme + '/images/';
		},
		
		handleCustomEvent : function(type){
			if (!type) return;
			// loop up event-node:
			if (this.metaData.config[type]){
				var exec = this.metaData.config[type];
				eval(exec);
			}
		},
		
		importLang : function(){
			try{
				var lang = eval(this.lang)
			}catch(e){
				if (this.langAttempts > 10){
					// LANG FILE NOT FOUND!
					alert("CRUDDER could not find the language " + this.lang);
					return;
				}
				setTimeout(this.importLang.bind(this), 500);
				this.langAttempts++;
				return;
			}
			this.lang = new crudderLang(lang);
		},
		
		editRecord : function(row){
			this.gridContainer.hide();
			this.recordContainer.show();
			this.record = new crudderRecord(this.metaData,this.recordContainer, this);
			this.record.draw(row);
		},

		addRecord : function(){
			this.handleCustomEvent('beforecreate');
			this.gridContainer.hide();
			this.recordContainer.show();
			this.record = new crudderRecord(this.metaData,this.recordContainer, this);
			this.record.draw();
		},
		
		closeRecord : function(){
			this.gridContainer.show();
			this.recordContainer.hide();
			this.record = null;
		},
		
		waiter : function(){
			this.waiterImg.show();
		},
		
		unWaiter : function(){
			this.waiterImg.hide();
		}
	})
	