Ext.namespace('Kelda', 'Kelda.Info');

Kelda.Info.Status = function(callMe) {
	Ext.Ajax.request({
		url:window.location.protocol+"//"+window.location.hostname+"/"+basePath+'kelda/status',
		success : function(response,options) {
			if(response.responseText=='1')
				callMe(true);
			else
				callMe(false);
		},
		failure : function() {
			
		}
	});
};


Kelda.Info.Message = function(){
	 var msgCt;

	    function createBox(t, s){
	        return ['<div class="msg x-box">',
	                '<div class="x-box-tl"><div class="x-box-tr"><div class="x-box-tc"></div></div></div>',
	                '<div class="x-box-ml"><div class="x-box-mr"><div class="x-box-mc"><h3>', t, '</h3>', s, '<a id="keldan-msg-close" href="#">Loka</a></div></div></div>',
	                '<div class="x-box-bl"><div class="x-box-br"><div class="x-box-bc"></div></div></div>',
	                '</div>'].join('');
	    }
	    return {
	        msg : function(title, format,closeTimeout){
	            if(!msgCt){
	                msgCt = Ext.DomHelper.insertFirst(Ext.get('keldan-msg'), {id:'msg-div'}, true);
	            }
	            //msgCt.alignTo(Ext.get('keldan-frontpage-btn'),'l-l',[0,Ext.getCmp('main-portal').getSize().height-300]);
	            var s = String.format.apply(String, Array.prototype.slice.call(arguments, 1));
	            var m = Ext.DomHelper.append(msgCt, {html:createBox(title, s)}, true);
	            //msgCt.alignTo(Ext.get('search_premium'),'l-l',[0,Ext.getCmp('main-portal').getSize().height-m.getHeight()]);
	            msgCt.alignTo(Ext.get('ux-taskbar-ribbon'),'tl-bl',[0,0]);
	            Ext.get('keldan-msg-close').on('click',function(){
	            	m.ghost('b',{remove:true});
	            });
	            m.slideIn("t");//.pause(4).ghost('b',{remove:true});
	            if(closeTimeout > 0) {
	            	(function() {
	            		m.ghost('b',{remove:true});
	            	}).defer(closeTimeout);
	            }
	        },

	        init : function(){
	            var t = Ext.get('exttheme');
	            if(!t){ // run locally?
	                return;
	            }
	            var theme = Cookies.get('exttheme') || 'aero';
	            if(theme){
	                t.dom.value = theme;
	                Ext.getBody().addClass('x-'+theme);
	            }
	            t.on('change', function(){
	                Cookies.set('exttheme', t.getValue());
	                setTimeout(function(){
	                    window.location.reload();
	                }, 250);
	            });

	            var lb = Ext.get('lib-bar');
	            if(lb){
	                lb.show();
	            }
	        }
	    };
}();

function includeJavascript(src) {
    if (document.createElement && document.getElementsByTagName) {
        var head_tag = document.getElementsByTagName('body')[0];
        var script_tag = document.createElement('script');
        script_tag.setAttribute('type', 'text/javascript');
        script_tag.setAttribute('src', src);
        head_tag.appendChild(script_tag);
    }
}
function keldanCloseStoreMask() {
	if(Ext.Msg.isVisible()) {
		keldan_store_count--;
		Ext.Msg.updateProgress((keldan_fixed_stcount-keldan_store_count)/keldan_fixed_stcount
				,keldan_fixed_stcount-keldan_store_count+(keldan_lang=='is' ? ' af ' : ' of ')+keldan_fixed_stcount);
		if(keldan_store_count==0) {
			Ext.Msg.hide();
			keldan_store_count=-1;
		}
	}
}

function keldanNewExportAction(width) {
	var rowaction = new Ext.ux.grid.RowActions({
		header:'Excel',
		tooltip:'Skoða tímaröð í Excel',
		autoWidth:false,
		width:width,
		align:'right',
		hideMode:'display',
		keepSelection:true,
		actions:[{
			iconCls:'icon-excel',
			qtip:'Færa í Excel'
			
		}]
	});
	
	return rowaction;
}
function keldanNewCurrencyExcel(width) {
	var rowAction = keldanNewExportAction(width);
	
	return rowAction;
}

function keldanCurrencyGraph(width) {
	var rowaction = new Ext.ux.grid.RowActions({
		header:keldan_lang_text('plot'),
		autoWidth:false,
		width:width,
		align:'right',
		hideMode:'display',
		keepSelection:true,
		actions:[{
			iconCls:'icon-graph',
			qtip:'Graf'
			
		}]
	});
	rowaction.on({
		action : function(grid,record,action,row,col) {
			if(action=='icon-excel') {
				window.open(window.location.protocol+"//"+window.location.hostname+"/"+basePath+"connector/getBankFxExcel.xls?code="+record.data.name);
				return;
			}
			if(record.data.name.indexOf('XDR') >= 0 || record.data.name.indexOf('ISK') >= 0)return;
			var graphWin = Ext.getCmp('bankfx-graph');
			if(!graphWin) {
			  graphWin = new Ext.Window({
				id : 'bankfx-graph',
                title: 'Gengisskráning - söguleg gögn.',
                resizeable: true,
                width: 800,
                height: 450,
                //autoWidth:true,
                //autoHeight:true,
                layout: 'fit',
                closeAction:'hide',
                items : [{
                	xtype:'CurrencyChartPanel',
                	type : record.data.name
                }],
                listeners : {
                	'resize' : function(t,width,height) {
                		if(keldan_bankfx_chart == 'undefined' || keldan_bankfx_chart == undefined || !keldan_bankfx_chart)
                			return;
                		else
                			keldan_bankfx_chart.setSize(t.bwrap.getSize().width,t.bwrap.getSize().height);
                	},
                	'close' : function() {
                		keldan_bankfx_chart.destroy();
                	},
                	'hide' : function() {
                		keldan_bankfx_chart.destroy();
                	}
                }
			});
			  graphWin.setPosition(6,138);
			}else {
				keldan_currencychart_init(record.data.name,graphWin.bwrap.getSize().width,graphWin.bwrap.getSize().height)
			}
			
			graphWin.show();
		}
	});
	return rowaction;
}
function keldanFundGraph(width) {
	var rowaction = new Ext.ux.grid.RowActions({
		header:keldan_lang_text('plot'),
		autoWidth:false,
		width:width,
		align:'right',
		hideMode:'display',
		keepSelection:true,
		actions:[{
			iconCls:'icon-graph',
			qtip:'Graf'
			
		}]
	});
	rowaction.on({
		action : function(grid,record,action,row,col) {
			if(action=='icon-excel') {
				//alert(window.location.protocol+"//"+window.location.hostname+"/"+basePath+"connector/getFundExcel.xls?code="+record.data.fundcode,record.data.altDescription);
				window.open(window.location.protocol+"//"+window.location.hostname+"/"+basePath+"connector/getFundExcel.xls?code="+record.data.fundcode,record.data.altDescription);
				return;
			}
			var fundCharts = Ext.getCmp('fund_chart_win');
			if(!fundCharts) {
				
				fundCharts = new Keldan.Funds.Chart.MainWindow({
					id:'fund_chart_win',
					fund:record.data.fundName,
					row:row
				});
				fundCharts.on('move',function(){
					this.suspendEvents();
					//console.log('y pos is '+this.getPosition()[1]);
					if(this.getPosition()[1] <= 130)
						this.setPosition(fundCharts.getPosition()[0],130);
    				this.resumeEvents();
				});
				fundCharts.on('afterrender',function(){
					
					var frame = Ext.getCmp('fund_graph_iframe');
					
					var w = frame.getSize().width-30;
					var h = frame.getSize.height-30;
					frame.setSrc(window.location.protocol + "//"
								+ window.location.hostname + "/" + basePath+'funds/chart/'+record.data.owner+'_'+record.data.fundName+'/'+w+'/'+h);
				});
				fundCharts.render(Ext.get('main-portal'));
				
				fundCharts.show();
			}else {
				fundCharts.addFund(record.data.fundName);
			}
			
		}
	});
	return rowaction;
}

function keldanNewRowaction(width) {
	var rowaction = new Ext.ux.grid.RowActions({
		header:keldan_lang_text('plot'),
		autoWidth:false,
		width:width,
		align:'right',
		hideMode:'display',
		keepSelection:true,
		actions:[{
			iconCls:'icon-graph',
			qtip:'Graf'
			
		}]
	});
	rowaction.on({
		action:function(grid,record,action,row,col) {
			/*var chartWin = new Ext.ux.Chart.amStock.Window({
				chartUrl : 'js/amstock/amstock.swf',
				dataUrl : 'js/amstock/data.csv',
				settingsUrl : 'js/amstock/settings.xml'
			});
			chartWin.show();
			return;
			*/
			if(action=='icon-excel') {
				window.open(window.location.protocol+"//"+window.location.hostname+"/"+basePath+"connector/getDmExcel?code="+record.data.dmcode,record.data.altDescription);
				return;
			}else {
			if(record.data.name=='ISCDS_DUMMY')
				return;
			if(record.data.name=='ISK') {
				alert('Til að skoða gengisvísitölu, smelltu á grafhnapp fyrir GVT.');
				return;
			}
			var urlPrefix = '';
			if(record.data.name.indexOf('RIKV') == 0) {
				alert('Því miður eru engin gröf til fyrir '+record.data.name);
				return;
			}
			if(record.data.dmcode) {
				urlPrefix = 'http://keldan.datamarket.com/?ds='+record.data.dmcode;
			}
			else
				urlPrefix = '/graph/historical?ticker='+record.data.name;
			
			var objMgr = Kelda.Info.ObjectMgr.getInstance();
			var desk = objMgr.get('main-desktop');
			var win = desk.createStandAloneIWin({
					//renderTo:Ext.getBody(),
					title:record.data.name+" - "+keldan_lang_text('historical').toLowerCase(),
					closeAction:'close',
					header:false,
					height:408,
					width:558,
					defaultSrc:urlPrefix+'&w=540&h=370&cache_refresh'
					
				});
				
			
			win.on('resize',function(e,w,h) {
				if (parseInt(w)) {
					this.setSrc(urlPrefix+'&w='+(w-20)+'&h='+(h-30)+'&cache_refresh');
				}	
			},win);
			win.show();
			}
		}
	});
	return rowaction;
	
}

