/*
 * TubeMogul Core JS Library
 * Copyright(c) 2009, TubeMogul, Inc.
 */


function addChartToDash(url){

  	var ajaxUrl = '';
	ajaxUrl = window.location.protocol +"//"+ window.location.host+'/user/add_dash_item';
	//alert(ajaxUrl);
	theUrl = escape(url);
	theUrl = theUrl.replace("+", "%2B");
	theUrl = theUrl.replace("/", "%2F"); 

	ajaxUrl = ajaxUrl + '?url='+theUrl;
	//alert(ajaxUrl);
	//return;
	Ext.Ajax.request({
	   url: ajaxUrl,
		success: function (response) {
	        Ext.MessageBox.alert('Status', 'This chart has been added to your dashboard.');
		}

	});
	
}

function replaceFavChart(url){

  	var ajaxUrl = '';
	ajaxUrl = window.location.protocol +"//"+ window.location.host+'/user/replace_default_chart';
	//alert(ajaxUrl);
	theUrl = escape(url);
	theUrl = theUrl.replace("+", "%2B");
	theUrl = theUrl.replace("/", "%2F"); 

	ajaxUrl = ajaxUrl + '?url='+theUrl;
	//alert(ajaxUrl);
	//return;
	Ext.Ajax.request({
	   url: ajaxUrl,
		success: function (response) {
	        Ext.MessageBox.alert('Status', 'Your favorite chart has been updated.');
		}

	});
	
};



var overlayGridRowClick = function(grid, rowIndex, e) {
	
	  e.stopEvent();
		var record = grid.getStore().getAt(rowIndex);
		
		var alreadyCharted = false;
		
		for(i = 0; i < params.items.length; i++){
			if( params.items[i][0] == record.get('id') && params.items[i][1] == record.get('type') ){
				alreadyCharted = true;
				params.items.splice(i,1);
				break;			
			}
		}
		
		if(!alreadyCharted){
			params.items.push([ record.get('id') , record.get('type') ] );
		}

		refreshAll();

	}


var popUpUrl;
function popUpNewUrl(link, anchor, width, height, callback){

	if(popUpUrl && !popUpUrl.hidden){
	  popUpUrl.close();
	  return;
	}	
	popUpUrl = new Ext.Window({
		closeAction :'close',
		closeable :true,
		plain       : true,
		layout      : 'fit',
		width       : parseInt ( width ),
		height      : parseInt( height ),
		items       : new Ext.Panel({
					autoLoad       : {url:link, scripts: true},
					overflow	: 'auto',
					autoScroll	: true,
					border    	: false
				}),
		buttons: [{
			text     : 'Close',
			handler  : function(){
				popUpUrl.close();
			}
		}]
	});

	popUpUrl.show(Ext.get(anchor));
	popUpUrl.on('close', callback);

}


var popUpDiv = null;
function popUpNewDiv(box, anchor, width, height, callback){
	
	if(popUpDiv && !popUpDiv.hidden){
		popUpDiv.hide();
		return;
	}

	if(!popUpDiv || popUpDiv.hidden){
		
		popUpDiv = new Ext.Window({
			layout      : 'fit',
			width       : parseInt ( width ),
			height      : parseInt ( height ),
			closeAction :'hide',
			plain       : true,
			items       : new Ext.Panel({
				contentEl  	   : box,
				autoTabs       : true,
				id			: 'kjsd',
				autoScroll	: true,
				activeTab      : 0,
				deferredRender : false,
				border         : false
			}),

			buttons: [{
				text     : 'Close',
				handler  : function(){
					popUpDiv.hide();
				}
			}]
		});
	}
	
	
	popUpDiv.show(Ext.get(anchor));
	popUpDiv.on('hide', callback);		
	
	
}