/**
 * Common Javascript
 * Common functions / classes used throughout the application
 *
 * @author Erik St. Martin <alakriti@gmail.com>
 * @package CDs-2-Go
 * @version 2.0.0
 * @copyright 2007 CDs-2-Go
 */

Ext.UpdateManager.defaults.loadScripts = true;

App = {
	logout: function () {
		Ext.get(document.body).load(App.Registry.get('baseUrl')+'auth/logout');
	}
};

App.Registry = {
 	registry: new Array(),
 	get: function(key) {
        return this.registry[key];
    },
    set: function(key, value) {
        this.registry[key] = value;
    }
};

App.Util = {
	/**
	 * processResponse
	 *
	 * Processes JSON response return from server to check for redirects etc.
	 *
	 * @param object request
	 * @return void
	 */
	processResponse : function (response) {
		if(response.redirectURL){
			window.location = App.Registry.get('baseUrl') + response.redirectURL;
		}
		if(response.pageErrors){
			var pageErrors = Ext.get('pageErrors');
			pageErrors.dom.innerHTML = response.pageErrors;
		}
	}
};
App.Main = {
    panels: 0,
    layout: null,

	// initialization
    init : function(){
    	this.layout = new Ext.BorderLayout(document.body, {
    		north: { initialSize: 50, titlebar: true, initialWidth: 50 },
    		west: { split:false, initialSize: 275,  minSize: 275, maxSize: 500, titlebar: true, collapsible: true, autoScroll:true,collapsedTitle:'Search',hideWhenEmpty : true, showPin: true, closeOnTab: true, alwaysShowTabs:true},
    		south: { split:false, initialSize: 225, minSize: 125, maxSize: 400, titlebar: true, collapsible: true, autoScroll:true, collapsedTitle:'List Panel', hideWhenEmpty : true, showPin: true, closeOnTab: true, alwaysShowTabs:true },
    		center: { titlebar: false, tabPosition: 'top', autoScroll:true, resizeTabs: false, minTabWidth: 50, preferredTabWidth: 150, closeOnTab: true }
		});
        // Lets add some content to our layout.
	    this.layout.beginUpdate();
	    this.layout.add('north', new Ext.ContentPanel('north', {title: (App.Registry.get('applicationName') ? App.Registry.get('applicationName') + ' ' + App.Registry.get('version') : 'MyProjectCenter' + ' ' + App.Registry.get('version')), closable: false}));
        this.layout.add('center', new Ext.ContentPanel('centerMain', {title: (App.Registry.get('dashboardTitle') ? App.Registry.get('dashboardTitle') : 'Dashboard'), closable: false}));
        this.layout.endUpdate();

		var headerImage = new Ext.get('header');
		headerImage.alignTo(document.body, "tr",[-295,0]);
		headerImage.setStyle('z-index', 12);
		headerImage.setStyle('width', 295);
		headerImage.show();

        // build our menu
        Menu.init();

        // add listener for when south region is resized to resize grids
        var south = App.Main.layout.getRegion('south');
        south.addListener('resized', function(region, size){
            region.panels.each(function(panel) {

                var p = panel.getEl();
                if(p.dom.firstChild.id != undefined) {
                    // if the first child is defined then we resize it (corrects IE bug).
	               p.dom.firstChild.style.height = p.dom.parentNode.style.height;
                } else {
                    // otherwise we set current elements height to its parent.
                    p.dom.style.height = p.dom.parentNode.style.height;
                }
            });
        });
    }
};

/**
 * Window onload
 *
 * Overrides window onload to animate hiding of loading window
 *
 * @return void
 */
window.onload = function () {
	var loading = Ext.get('loading');
	var mask = Ext.get('loading-mask');
	if(loading && mask){
		mask.setOpacity(.8);
		mask.shift({
	  	    xy:loading.getXY(),
	  	    width:loading.getWidth(),
	  	    height:loading.getHeight(),
	  	    remove:true,
	  	    duration:1,
	 	    opacity:.3,
	  	    easing:'bounceOut',
	  	    callback : function(){
	        loading.fadeOut({duration:.2,remove:true});
	      }
		});
	}
};
/**
 * Help Page Popup
 *
 * Creates new window ("win") for the targeted help file
 *
 * 
 */
function open_window(url) {
      mywin = window.open(url,"win",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=700,height=480');
}

