/* 	Copyright 2008 Maximum Processing Inc
	This script provides one object to store global variable settings

	ABOUT THIS SCRIPT

	Change Log
 	Created 11/20/2008 Nathan Townsend
 	11/22/2008 - added logging NT
*/



$(document).ready(function(){
  try{
	
      // setup the properties editor div
      StationaryDivs.Add($("#ComponentProperties"));
      
      // gets the properties editor ready
      PropertiesEditor.Initialize($("#ComponentProperties"));
      
      // gets the components dialog ready
      $("#ComponentDefinitions").dialog({position:['left', 'top'], width:220, height:350, stack: true, title:"Components"  });
      $("#ComponentDefinitions").dialog("close");
			
			// show session expired message
			var href = location.href;
			if(href.indexOf("sessionexpired=true")>-1)
			  Global.ShowMessage($("<h3>Your session has expired</h3><p>Due to inactivity on the system your session has expired. Please login to start a new session.</p>"));
	
  		$("#TooltipIcon").live("click", function(){
  		  var status = $(this).attr("status") == "0" ? "1" : "0";
  			Communication.CustomRequest("IconTray.max?status=" + status + "&action=tooltips", function(html){  
  				var temp = $(html);
 					$("#TooltipIcon").attr("alt", temp.attr("alt") );
 				  $("#TooltipIcon").attr("status", temp.attr("status"));
 				  $("#TooltipIcon").attr("src", temp.attr("src"));
  				Global.Tooltips(temp.attr("status") == "0" ? false : true); 
  			});
  		});
	
			// automatic login time saver
			return;
			$("#UserName").val("nate");
			$("input[name='password']").val("test");
			$("input[name='submit']").click();
			
  } catch(err){ Log.Add("Global.(document).ready", err, LogType.Error)};
});




// ScriptingContainer adds functions to this and calls reset to remove old functions
CustomScript = new function()
{

		// clear the previously added functions
		this.Reset = function()
		{
		  jQuery.each(
        CustomScript, 
        function(field, value){
				  if(field != "Reset")
					{
					  CustomScript[field] = null;					
					}
			  }
			);
		}
}




Global = new function()
{
  try{

    var oFCKeditor = null;
	

    /* Public Methods */
    return{
		
		    ClipboardSet: function(str)
				{
				  try{

  				  window.clipboardData.setData('text', str);
						  
          } catch(err){ Log.Add("Global.ClipboardSet", err, LogType.Error)};				
				},
				
				ClipboardGet: function()
				{
				  try{

  				  return window.clipboardData.getData('text');
						  
          } catch(err){ Log.Add("Global.ClipboardGet", err, LogType.Error)};				
				},
				
				WorkflowTimerInitialized: false,
				
				CheckWorkflow: function(param){
				  if(param == null && Global.WorkflowTimerInitialized == true)
					  return;
				
				  Communication.CustomRequest("IconTray.max?action=workflow", function(IconColor){
					  if(IconColor != "none")
  					  $("#WorkflowIcon").attr("src", "../../images/32px-Crystal_Clear_app_access_" + IconColor + ".png");
						else
						{
						  $("#WorkflowIcon").css("display", "none");
							return;
						}
					});
					
					Global.WorkflowTimerInitialized = true;
					
					// check status again in 5 minutes
			    setTimeout("Global.CheckWorkflow(true)", 5 * 60 * 1000);
				},
				
				InitializeAdmin: function()
				{
				  try{

            $('#AdminTabs > ul').tabs();
            
            // gets the logging ready
            Log.Initialize($("#Logging"));
      			Log.LoggingEnabled = false;    //setting to false improves speed but will not show any entries 
         
            // gets rules maker ready
            RulesMaker.Initialize($("#preproc"), $("#postproc"));
      			
      			// set the html components to default mode 
      			Editor.DisableEditor();

						// setup the automation tab
						AutomatedTesting.Initialize($("#Automation"));
      			
						// prepare the context menus
      			ContextMenu.Initialize();
						
      			// gets the reqlist tab ready
      			ReqList.Initialize($("#ReqListDiv"));
      			ReqList.Enabled = true;						
						
						// html editor for notes
						oFCKeditor = new FCKeditor('EditorNotesTextArea', '100%', '600');
						oFCKeditor.BasePath = "../../fckeditor/" ;
						oFCKeditor.ReplaceTextarea();
						
          } catch(err){ Log.Add("Global.InitializeAdmin", err, LogType.Error)};						
				},
				
				GetNotes: function()
				{
				  var oEditor = FCKeditorAPI.GetInstance('EditorNotesTextArea') ;

				  return oEditor.GetHTML();
				},
				
				SetNotes: function(html)
				{
				
				  var oEditor = FCKeditorAPI.GetInstance('EditorNotesTextArea') ;				
				  oEditor.SetData(html);
				},
				
				DisableNotes: function()
				{
					try{
  				  var oEditor = FCKeditorAPI.GetInstance('EditorNotesTextArea') ;					
  				  oEditor.SetHTML("");
					}catch(err){alert(err.message)}
				},
				
				// used in communication.RemoveTooltips to determine whether or not to remove tt from new html
				DisableTooltips: false,
				
				// used by developers to disable tooltips globally
				Tooltips: function(boolShow){
				  try{
  				  Global.DisableTooltips = boolShow;
						
  					// show tooltips
  					if(boolShow)
  					{
    					$("*[oldtitle]").each(function(){
    					  $(this).attr("title", $(this).attr("oldtitle"));
    						$(this).removeAttr("oldtitle"); 
    					});
  					}
  
   					// hide tooltips					
  					if(!boolShow)
						{
    					$("*[title]").each(function(){
    					  $(this).attr("oldtitle", $(this).attr("title"));
    						$(this).removeAttr("title"); 
    					});
  					}
					}catch(err){ Log.Add("Global.Tooltips", err, LogType.Error); }
				},
				
				
				// returns a template component or array of components matching the id passed in
				GetControl: function(ctrlID)
				{
				  try{

					  //var inpt = $("#" + ctrlID); - won't return two objects with same id such as radio buttons
						var inpt = $("*[name='" + ctrlID + "']");
						
						var arr = new Array();
						
						//Log.Add("GetControl inpt.length", inpt.length, LogType.Warning);

						inpt.each(function(){
  						var ctrl = $(this).parents(".component");
							if(ctrl.length > 0) // prevents problems where the same id is used on a non-control element (hack)
  						  arr.push(Helper.GetEditorComponent(ctrl[0]));					
						});
						
						if(arr.length == 1)
						  return arr[0];
							
						return arr;
						
					}catch(err){Log.Add("Global.GetControl", err, LogType.Error);}
				},
				
				HideMessage: function(){
				  try{
					  $("#ModalWindow").dialog("close");
					
					}catch(err){Log.Add("Global.HideMessage", err, LogType.Error);}
				},

ShowProgress: function(Act)
{
 try{
   if(Act=='open'){

	//get the possition of middle of the scrren;
 	// First, determine how much the visitor has scrolled
	var scrolledX, scrolledY; 
	if( self.pageYOffset ) { scrolledX = self.pageXOffset; scrolledY = self.pageYOffset; } 
	else if( document.documentElement && document.documentElement.scrollTop ) { scrolledX = document.documentElement.scrollLeft; scrolledY = document.documentElement.scrollTop; }
	else if( document.body ) { scrolledX = document.body.scrollLeft; scrolledY = document.body.scrollTop; } 
	// Next, determine the coordinates of the center of browser's window 
	var centerX, centerY; 
	if( self.innerHeight ) { centerX = self.innerWidth; centerY = self.innerHeight; } 
	else if( document.documentElement && document.documentElement.clientHeight ) { centerX = document.documentElement.clientWidth; centerY = document.documentElement.clientHeight; }
	else if( document.body ) { centerX = document.body.clientWidth; centerY = document.body.clientHeight; } 
	// Xwidth is the width of the div, Yheight is the height of the div passed as arguments to the function: 
	var leftOffset = scrolledX + (centerX - $('#ProgressBar').width()) / 2; 
	var topOffset = scrolledY + (centerY - $('#ProgressBar').height()) / 2;
      $('#ProgressBar').css("top", topOffset + 'px');
      $('#ProgressBar').css("left", leftOffset + 'px');

      $('#ProgressBar').show();
   } else {
      $('#ProgressBar').hide();
   }
 } catch(err){ Log.Add("Global.ShowProgess", err, LogType.Error)};
},
    
        ShowMessage: function(str, d_width, d_title, d_opac)
        {
          try{
if(!d_width){d_width=960};
if(!d_title){d_title='System Message'};
if(!d_opac){d_opac='0.5'};

//alert(d_width + ' - ' + d_title + ' - ' + $("#ModalWindow").dialog);


		if(!$("#ModalWindow").attr("dialoginit"))
		{
          	  $("#ModalWindow").css("display", "block");
              $("#ModalWindow").dialog({ modal: true, width:d_width, title:d_title, overlay: { opacity: d_opac, background: "black" } });
  		  $("#ModalWindow").attr("dialoginit", "true");
		  $("#ModalWindow").dialog("close");
		} 
		else 
		{
		  $("#ModalWindow").dialog('option', 'width', d_width);
		  $("#ModalWindow").dialog('option', 'title', d_title);
		  $("#ModalWindow").dialog('option', 'overlay', { opacity: d_opac, background: "black" });
		}

              $("#ModalWindow").html('');

						var html = $("<div/>");
						
						html.html(str);
				
            $("#ModalWindow").html(html);
						
						$("#ModalWindow").dialog("open");

          } catch(err){ Log.Add("Global.ShowMessage", err, LogType.Error)};
        },
				
				ShowEditModeMessage: function(strText)
				{
				  $("#EditModeMessage").text(strText);
				},
				
				// should delete this method
				AttachScripts: function()
				{
				  try{
					
					  $("input[filter]").each(function(){
						 
						  alert($(this)[0].outerHTML);
						
						});
				  
          } catch(err){ Log.Add("Global.AttachScripts", err, LogType.Error)};					
				},
				
				EditingTemplate: true

    }
    
  
  } catch(err){ Log.Add("Global", err, LogType.Error); }
}


