/**  **  ** **  **  ** **  **  ** **  **  ** **  **  ** **  **  ** **  **  **
 * Loader object
 */
var Loader = {
	activeCalls : 0,
	busyElement : null,
	
	/**
	 * init()
	 * Initializes busy indicator and hides it.
	 */
	init : function() {
		//console.log("Loader.init()");
		this.busyElement = '#ajaxLoader';
		this.hideBusy();
	},
	
	/**
	 * load()
	 * Load content with AJAX. Request type = POST, Response JSON.
	 * @param	string(controller) Requested controller
	 * @param	string(data) Posted parameters (json)
	 * @param   string(callback) Name of callback function. NO '()' at end, only name.
	 */
	load : function(controller,data,callback) {
		
		this.increaseCallCount();
		
		if (!data) data = {};
		
		$.ajax(
		{
			type: "POST",
			url: controller,
			data : data,
			
			success: function(json)
			{
			    //success
			    eval('var jsonData = '+json+';');
			    eval( callback + '(jsonData)' );
			},
				   
			failure: function()
			{
				//alert('Tapahtui virhe tiedonv�lityksess�');
			}
						   				   				   
		});
	},
	
	/**
	 * increaseCallCount()
	 * Increases activeCalls count - should not be called directly!
	 * @param	integer(i) Count used to increse activeCalls
	 */
	increaseCallCount : function(i) {
		//console.log("Loader.increaseCallCount('%s')",i);
		if(i) {
			this.activeCalls = this.activeCalls + i;
		} else {
			this.activeCalls++;
		}
		this.updateBusy();
	},
	
	/**
	 * decreaseCallCount()
	 * Decreases activeCalls count
	 */
	decreaseCallCount : function() {
		if(this.activeCalls > 0) {
			this.activeCalls--;
		}
		this.updateBusy();
	},
	
	/**
	 * updateBusy()
	 * Updates the busy indicator.
	 */
	updateBusy : function() {
		//console.log("Loader.updateBusy() [%s]",this.activeCalls);
		if( this.activeCalls ) {
			this.showBusy();
		} else {
			this.hideBusy();
		}
	},
	
	/**
	 * showBusy()
	 * Shows the busy indicator element.
	 */
	showBusy : function() {
		//console.log("Loader.showBusy()");
		if(this.busyElement) $(this.busyElement).fadeIn("fast");
		$("body").css("cursor","wait");
	},
	
	/**
	 * hideBusy()
	 * Hides the busy indicator element.
	 */
	hideBusy : function() {
		//console.log("Loader.hideBusy()");
		if(this.busyElement) $(this.busyElement).fadeOut("slow");
		$("body").css("cursor","default");
	}
}

var Page = {
	load : function(url,pageName)
	{
		window.open(url,pageName,'width=1024,height=768');
	}
}

var Base = {
	initialize : function()
	{
		Loader.init();	
	}
};

Array.prototype.inArray = function (value) {
    var i;
    for (i=0; i < this.length; i++) {
        if (this[i] === value) {
            return true;
        }
    }
    return false;
};