/*
 * Copyright (c) 2009, Techtribe B.V.
 * Url: http://www.techtribe.nl
 * Author: Remon de Boer
 *
 * SCHUBERG.controller.Controller
 */
var Controller = function() {
	this.init();
}

Controller.prototype =
{
	resizeController: null,

	init: function(){
		this.resizeController = new SCHUBERG.controller.ResizeController();
	}
}
SCHUBERG.controller.Controller = Controller;



/*
 *** SCHUBERG.controller.ResizeController ***
 */
var ResizeController = function() {
	this.init();
}

ResizeController.prototype =
{
	model: null,
	commonEvents: null,
	scrollbarWidth: 16,

	init: function(){
		this.model = SCHUBERG.model.Model.getInstance();
		this.commonEvents = SCHUBERG.events.CommonEvents.getInstance();
		this.resizeHandler();
		this.commonEvents.startApplicationEvent.fire();
		this.scrollbarWidth = this.getScrollbarWidth();
		this.resizeHandler();
	},

	resizeHandler: function(){
		this.model.stageWidth = (true) ? (YAHOO.util.Dom.getViewportWidth()-this.scrollbarWidth) : YAHOO.util.Dom.getViewportWidth();
		this.model.stageHeight = YAHOO.util.Dom.getViewportHeight();
		if(this.model.stageWidth > this.model.MAX_STAGE_WIDTH){
			this.model.resizeFactor = 1;
		} else if(this.model.stageWidth < this.model.MIN_STAGE_WIDTH){
			this.model.resizeFactor = 0;
		} else {
			this.model.resizeFactor = ((this.model.stageWidth-this.model.MIN_STAGE_WIDTH)/((this.model.MAX_STAGE_WIDTH-this.model.MIN_STAGE_WIDTH)/100)/100);
		}
		this.commonEvents.viewUpdateEvent.fire();
	},

	scrollDetect: function(elemId) {
		var elem = document.getElementById(elemId);
		if(elem.scrollHeight > elem.clientHeight) {
			return true;
		} else {
			return false;
		}
	},

	getScrollbarWidth: function() {
		var is_ie = (navigator.appName=="Microsoft Internet Explorer");
		var sBarWidth = 0;
		var scr = null;
		var inn = null;
		if(is_ie) {
			scr = document.createElement('form');
			scr.style.position = 'absolute';
			scr.style.top = '-1000px';
			scr.style.left = '-1000px';
			inn = document.createElement('textarea');
			inn.setAttribute('id', 'ta');
			scr.appendChild(inn);
			document.getElementsByTagName("body")[0].appendChild(scr);
			var tarea = document.getElementById('ta');
			tarea.wrap = 'off';
			sBarWidth = tarea.offsetHeight;
			tarea.wrap = 'soft';
			sBarWidth -= tarea.offsetHeight;
		} else {
			var wNoScroll = 0;
			var wScroll = 0;
			scr = document.createElement('div');
			scr.style.position = 'absolute';
			scr.style.top = '-1000px';
			scr.style.left = '-1000px';
			scr.style.width = '100px';
			scr.style.height = '50px';
			scr.style.overflow = 'hidden';
			inn = document.createElement('div');
			inn.style.width = '100%';
			inn.style.height = '200px';
			scr.appendChild(inn);
			document.body.appendChild(scr);
			wNoScroll = inn.offsetWidth;
			scr.style.overflow = 'auto';
			wScroll = inn.offsetWidth;
			sBarWidth = (wNoScroll - wScroll);
		}
		document.body.removeChild(document.body.lastChild);
		return sBarWidth;
	}
}
SCHUBERG.controller.ResizeController = ResizeController;



/*
 *** SCHUBERG.controller.ConnectionManager ***
 */
var ConnectionManager = function() {
}

ConnectionManager.prototype =
{
    request: null,
    callback: null,

    asyncRequestGet: function(url, callback){
        this.callback = {success: callback, failure: TECHTRIBE.Delegate.create(this, this.connectionFailure)};
        this.request = YAHOO.util.Connect.asyncRequest('GET',  url, this.callback);
    },

    asyncRequestPost: function(url, postValues, callback){
        if(callback != null){
            this.callback = {success: callback, failure: TECHTRIBE.Delegate.create(this, this.connectionFailure)};
        } else {
            this.callback = {success: TECHTRIBE.Delegate.create(this, this.connectionSucces), failure: TECHTRIBE.Delegate.create(this, this.connectionFailure)};
        }
        this.request = YAHOO.util.Connect.asyncRequest('POST',  url, this.callback, postValues);
    },

    abortConnection: function(){
        YAHOO.util.Connect.abort(this.request, this.callback, false);
    },

    connectionSucces: function(response){
    },

    connectionFailure: function(){
    }
}
SCHUBERG.controller.ConnectionManager = ConnectionManager;