/**
 * Dynamic content panel component - javascript file
 *
 * @package nova
 * @subpackage com
 * @author Liquid Edge Solutions
 * @copyright Copyright Liquid Edge Solutions. All rights reserved.
 *///--------------------------------------------------------------------------------
var com_panel = new Class({
	//--------------------------------------------------------------------------------

	// properties

	//--------------------------------------------------------------------------------
	id: false,
	elPanel: false,
	urlArr: new Array(),
	currentUrl: false,
	urlHistoryArr: new Array(),
	currentHistory: false,
	parent: false,
    //--------------------------------------------------------------------------------

    // methods

	//--------------------------------------------------------------------------------
    initialize: function(id){
        this.id = id;
        this.elPanel = $('panel_' + this.id);
    },
    //--------------------------------------------------------------------------------
    clear: function() {
    	this.elPanel.set('text', '');
    	this.elPanel.setStyle('display', 'none');
    },
    //--------------------------------------------------------------------------------
    addUrl: function(url, overwrite) {
    	// params
    	overwrite = $pick(overwrite, false);
    	
    	// add panel id to url
    	url += '&p=' + this.id;

    	// overwrite current url
    	if (overwrite && this.currentUrl !== false) {
    		this.urlArr[this.currentUrl] = url;
    		urlIndex = this.currentUrl;
    	}
    	else {
        	// use existing enty if found
        	var urlIndex = this.urlArr.indexOf(url);
        	
	    	// add and load the given url
	    	if (urlIndex == -1) urlIndex = this.urlArr.push(url) - 1;
    	}

    	// end
    	return urlIndex;
    },
    //--------------------------------------------------------------------------------
    refresh: function(urlIndex, options) {
    	// set current url if not set
    	if (this.currentUrl === false) this.currentUrl = 0;

    	// use current url if no index given
    	urlIndex = $pick(urlIndex, this.currentUrl);

    	// check if new url exists
    	if (urlIndex >= this.urlArr.length && urlIndex >= 0) return;

    	// manage history
    	if (urlIndex != this.currentUrl) {
	    	// add history item when not currently in history
	    	if (this.currentHistory === false) this.addHistory(urlIndex);
	    	else {
	    		// truncate history when loading new url inside history
	    		this.urlHistoryArr.length = this.currentHistory + 1;
	    		this.currentHistory = false;
	    	}
    	}

    	// load new url
    	this.currentUrl = urlIndex;

    	// request updated panel information from server
    	this.elPanel.setStyle('display', 'block');
    	core.ajax.requestUpdate(this.urlArr[this.currentUrl], this.elPanel, options);

		// debug message
		//alert(this.urlHistoryArr + ' [' + this.currentUrl + '] : ' + this.currentHistory);
    },
    //--------------------------------------------------------------------------------
    back: function() {
    	// check if currently in history
    	if (this.currentHistory === false) {
    		// break when no history
    		if (this.urlHistoryArr.length == 0) return;
    		else {
    			// go to lastest step in history
    			this.currentHistory = this.urlHistoryArr.length - 1;

    			// add current url to history
    			this.urlHistoryArr.extend([this.currentUrl]);
    		}
    	}
    	else {
    		// break when at start of history
    		if (this.currentHistory == 0) return;
    		else {
    			// go one step back in history
    			this.currentHistory--;
    		}
    	}

    	// load history url
    	this.currentUrl = this.urlHistoryArr[this.currentHistory];
    	this.refresh();
    },
    //--------------------------------------------------------------------------------
    foward: function() {
    	// check if no history
    	if (this.currentHistory === false) return;

    	// go one step foward in history
    	this.currentHistory++;
    	this.currentUrl = this.urlHistoryArr[this.currentHistory];

    	// remove step from history when latest
    	if (this.currentHistory == this.urlHistoryArr.length - 1) {
    		this.currentHistory = false;
    		this.urlHistoryArr.length--;
    	}

    	// load history url
    	this.refresh();
    },
    //--------------------------------------------------------------------------------
    request: function(url, options) {
    	// intercept get data from inputs
    	if ($defined(options) && $defined(options.get)) {
    		url = core.form.urlAddValues(url, options.get);
    		options.get = false;
    	}
    	
    	// add panel id to url
    	url += '&p=' + this.id;

    	// init options
    	options = $extend({
    		el: 'panel_' + this.id,
    		_panel: this.id
    	}, $pick(options, {}));

    	// run request
    	core.ajax.request(url, options);
    },
    //--------------------------------------------------------------------------------
    requestUpdate: function(url, options) {
    	// init options
    	options = $extend({
    		get: false,
    		overwrite: false
    	}, $pick(options, {}));
    	
    	// intercept get data from inputs
    	if (options.get) {
    		url = core.form.urlAddValues(url, options.get);
    		options.get = false;
    	}
    	
    	// run request
		var urlIndex = this.addUrl(url, options.overwrite);
		this.refresh(urlIndex, options);
    },
    //--------------------------------------------------------------------------------
    requestRefresh: function(url, options) {
    	// init options
    	options = $extend({
    		callback: C_FUNCTION,
    		func: function(responseText, requestOptions) { eval(requestOptions._panel + '.refresh();'); }
    	}, $pick(options, {}));

    	// run request
    	this.request(url, options);
    },
    //--------------------------------------------------------------------------------
    requestRefreshNr: function(url, nr, options) {
    	// init options
    	options = $extend({
    		callback: C_FUNCTION,
    		_nr: nr,
    		func: function(responseText, requestOptions) { eval(requestOptions._panel + '.refresh(' + requestOptions._nr + ');'); }
    	}, $pick(options, {}));

    	// run request
    	this.request(url, options);
    },
    //--------------------------------------------------------------------------------
    requestBack: function(url, options) {
    	// init options
    	options = $extend({
    		callback: C_FUNCTION,
    		func: function(responseText, requestOptions) { eval(requestOptions._panel + '.back();'); }
    	}, $pick(options, {}));

    	// run request
    	this.request(url, options);
    },
    //--------------------------------------------------------------------------------
    setParent: function(parentName) {
    	this.parent = eval(parentName);
    },
    //--------------------------------------------------------------------------------

    // protected methods

    //--------------------------------------------------------------------------------
    addHistory: function(urlIndex) {
    	// add url number to history if not the same as current url
    	if (urlIndex != this.currentUrl) this.urlHistoryArr.push(this.currentUrl);
    }
    //--------------------------------------------------------------------------------
});
