var common = function(){
	var urlLink = location.toString();
	var urlObj = Ext.urlDecode(urlLink.substring(urlLink.lastIndexOf('?')+1, urlLink.length));
	var carousel = null;
	
	function readPage(url, param, execute, scope, agument){
		if(!url) return false;
		var newParam = (param) ? param : {};
		if(typeof(newParam) != 'object') newParam = Ext.util.JSON.decode(newParam);
		var cb = function(a,success,rspn){
			if(success){
				if(!execute || typeof(execute) != 'string') return true;
				if(scope && typeof(scope) == 'object'){
					scope[execute](rspn.responseText, agument);
				}else{
					eval(execute+'(rspn.responseText, agument)');
				}
			}else{
				return false;
			}
		}
		var cn = new Ext.data.Connection({url: url}).request({
			method: "POST", 
			params: newParam, 
			callback: cb
		});
	}
		
	return {
		yearValue: new Array(), 
		backupTemp: null, 
		backupNew: new Array(),
		nowStartNum: 1, 
		
		init: function(){
			if(Ext.get('news1')){
				var template = Ext.getDom('news1').innerHTML.split('<!---while--->');
				this.backupTemp = template[0]+'<tpl for=".">'+template[1]+'</tpl>'+template[2];
				Ext.get('news1').update('');
				this.changeNews(this.nowStartNum);
			}
			
			if(Ext.get('profitCarousel')){
				carousel = new Ext.ux.Carousel({
					numVisible: 4,
					animationSpeed: 0.5,
					scrollInc: 4,
					firstVisible: 100, 
					//animationMethod: 'backBoth', 
					renderTo: 'profitCarousel', 
					prevElement: "profit_prev",
					nextElement: "profit_next"
				});
				var cc = function(a, b){
					if(b){
						a.show();
					}else{
						a.hide();
					}
				}
				carousel.on('prevButtonStateHandler', cc);
				carousel.on('nextButtonStateHandler', cc);
				carousel.show();
				if(carousel.size < 4){
					for(var i=0;i<carousel.size;i++){
						var li = Ext.get(carousel.allItem[i]).child('table');
						li.setWidth(li.getWidth()/carousel.size*4);
					}
				}
			}
		}, 
		
		newsWhile: function(str, inx){
			var store = Ext.util.JSON.decode(str);
			var tpl = new Ext.XTemplate(this.backupTemp);
			var word = store.length > 0 ? tpl.apply(store) : 'NO DATA';
			this.backupNew[inx] = word;
			Ext.get('news1').update(word);
//			Ext.get('news1').show(true);
		},
		
		changeNews: function(inx){
			if(this.backupNew[inx]){
				if(this.nowStartNum == inx) return;
				Ext.get('news1').update(this.backupNew[inx]);
				Ext.get('news1').show(true);
			}else{
				readPage('getdata.php?Type=getNews', {paramID: inx}, 'newsWhile', this, inx);
			}
			this.nowStartNum = inx;
		},
		
		insertValue: function(str, inx){
			this.yearValue[inx] = Ext.util.JSON.decode(str);
			this.showBarChart(Ext.getDom('select'));
		},
		
		contact: function(){
			Ext.getDom('request').submit();
		},
		
		showBarChart: function(){
			var sb = Ext.getDom('select');
			var inx = sb.options[sb.selectedIndex].value;
			if(!this.yearValue[inx]){
				readPage('getdata.php?Type=getYearValue', {paramID: inx}, 'insertValue', this, inx);
			}else{
				if(Ext.getDom('barChart')){
					Ext.getDom('barChart').runBarChart(this.yearValue[inx]);
				}else{
					document.embeds[1].runBarChart(this.yearValue[inx]);
				}
			}
		}
	}
}();

Ext.onReady(common.init, common);
