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

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('keldan-frontpage-btn'),'l-l',[0,Ext.getCmp('main-portal').getSize().height-m.getHeight()]);
	            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 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) {
			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.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;
	
}
