window.MCMS = window.MCMS || {};
MCMS = {
	
	
};				

MCMS.Console = {
	Container : null,
	Footer : null,
	InEditMode : false,
	DragTimer : null,
	
	Init : function() {
		if(document.getElementById("mcms-console-edit")) this.Render();
		},
	
	Render : function() {	
		this.Config.Load();
		
		// Set the page footer
		this.Footer = document.getElementById("mcms-console-state");
		
		// Calculate the XY position on screen
		var x = this.Config.Data.x;
		var y = this.Config.Data.y;
		
//		console.log(y);
		
		// Set the Console Container	
		this.Container = new YAHOO.widget.Panel("mcms-console-edit", { 
			xy:[x,y], width:"200px", visible:this.Config.Data.visible, constraintoviewport:true, iframe:true,
			effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration: 0.5}
			});
		this.Container.setHeader("MCMS Console"); 
		this.Container.render();		

		// Console Events		
		this.Container.dragEvent.subscribe(function(e) { 
			clearTimeout(MCMS.Console.DragTimer);
			MCMS.Console.Config.Data.x = MCMS.Console.Container.cfg.getProperty("x")-document.documentElement.scrollLeft;
			MCMS.Console.Config.Data.y = MCMS.Console.Container.cfg.getProperty("y")-document.documentElement.scrollTop;
			MCMS.Console.DragTimer = setTimeout("MCMS.Console.Config.Save()",500);
			});

		this.Container.hideEvent.subscribe(function() { 
			MCMS.Console.Config.Data.visible = false;
			MCMS.Console.Config.Save();
			});
			
		this.Container.showEvent.subscribe(function() { 
			MCMS.Console.Config.Data.visible = true;
			MCMS.Console.Config.Save();
			});		

		// Key Shortcut
		var consoleKey = new YAHOO.util.KeyListener(document, { keys:27 }, { fn:this.KeyHandler, scope:this, correctScope:true } ); 
		consoleKey.enable();
		}, // Init
		
	KeyHandler : function(type, args, obj) {
		if(this.Container.cfg.getProperty("visible")) {
			this.Container.hide();
		} else {
			this.Container.show();
			}
		}, // KeyHandler
	
	Config : {
		Data : {
			x: 0,
			y: 0,
			visible: true
		},

    	Load: function () {
			var search = "MCMSConsoleConfig=";  
			var cookie = null;
			if (document.cookie.length > 0) { 					// if there are any cookies      
				offset = document.cookie.indexOf(search);       
				if (offset != -1) { 							// if cookie exists          
					offset += search.length; 					// set index of beginning of value         
					end = document.cookie.indexOf(";", offset);	// set index of end of cookie value         
					if (end == -1) end = document.cookie.length;
					cookie = unescape(document.cookie.substring(offset, end));   
					this.Data = eval("("+unescape(cookie)+")"); //cookie.parseJSON();
					cookie = true;
					}    
				}		
			return cookie;
		},

    	Save: function () {
	
			var now = new Date();
			var expires = now.getTime() + 365 * 24 * 60 * 60 * 1000;
			var expires = new Date(expires);
			var valueToStore = '{x: '+MCMS.Console.Config.Data.x+',y: '+MCMS.Console.Config.Data.y+',	visible: '+MCMS.Console.Config.Data.visible+'}';
			document.cookie = "MCMSConsoleConfig=" + escape(valueToStore) + "; expires=" + expires.toGMTString() + "; path=/";
		}		
	} // Config
};

//for default properties setup when creating a new channel
var IDS_WIN_CREATECHANNELDLG_NAME = "WBC_winCreateChannelDlg";

if(typeof IDS_DEFAULT_OPEN_POSITION === "undefined" || !IDS_DEFAULT_OPEN_POSITION)
{
	IDS_DEFAULT_OPEN_POSITION = "";
}

var IDS_WIN_CREATECHANNELDLG_FEATURES = "width=650,height=280,resizable,scrollbars,status=yes" + IDS_DEFAULT_OPEN_POSITION;

//Delete Channel confirm UI dialogs
var IDS_DELETE_CHANNEL_CONFIRM = "Deleting this Channel will remove it permanently from the system. Continue?";

/// <summary>
/// Javascript function in repsonse to "Delete Channel" Console link in
/// the PresentationUnpublished mode.
/// </summary>
function CMS_onClickChannelDelete()
{
	// put up confirm() dialog first!
	return confirm(IDS_DELETE_CHANNEL_CONFIRM);
}

/// <summary>
/// Javascript function in response to "Create Channel"
/// Console link clicked in any Authoring case.
/// </summary>
function CMS_openWindowCreateChannel( strDlgURL )
{	
	// open a blank window and set the form target to that window
	WBC_openWindowOnFocus(strDlgURL, IDS_WIN_CREATECHANNELDLG_NAME, IDS_WIN_CREATECHANNELDLG_FEATURES);
}