$(function(){
	//all hover and click logic for buttons
	$(".fg-button:not(.ui-state-disabled)")
	.hover(
		function(){ 
			$(this).addClass("ui-state-hover"); 
		},
		function(){ 
			$(this).removeClass("ui-state-hover"); 
		}
	)
	.mousedown(function(){
			$(this).parents('.fg-buttonset-single:first').find(".fg-button.ui-state-active").removeClass("ui-state-active");
			if( $(this).is('.ui-state-active.fg-button-toggleable, .fg-buttonset-multi .ui-state-active') ){ $(this).removeClass("ui-state-active"); }
			else { $(this).addClass("ui-state-active"); }	
	})
	.mouseup(function(){
		if(! $(this).is('.fg-button-toggleable, .fg-buttonset-single .fg-button,  .fg-buttonset-multi .fg-button') ){
			$(this).removeClass("ui-state-active");
		}
	});
});


function MotifSoft_OpenNewWindow(strURL, windowName, width, height)
{
    if (windowName == '')
        windowName = 'NewWindow';
        
    if (width == '')
        width = 800;
    if (height == '')
        height = 600;

    window.open(strURL, windowName, 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,directories=no,location=no,width=' + width + ',height=' + height + ',top=200,left=200');
}

function Motif_OpenWindow(strURL, windowName, widthPec, heightPec)
{
    if (windowName == '')
        windowName = 'NewWindow';
        
    if (widthPec == '')
        widthPec = .8;
    else
        widthPec = widthPec / 100;
    if (heightPec == '')
        heightPec = .6;
    else
        heightPec = heightPec / 100;

 	var width	= parseInt(screen.width * widthPec);
 	var height = parseInt(screen.height* heightPec);
 	var WindowTop	= parseInt((screen.height-height)/2);
 	var WindowLeft= parseInt((screen.width-width)/2);

    window.open(strURL, windowName, 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,directories=no,addressbar=no,location=no,width=' + width + ',height=' + height + ',top=' + WindowTop + ',left=' + WindowLeft);
}

function KMSViewArticle(intArticleId)
{
    var url = 'ViewArticle.aspx?Id=' + intArticleId;
    Motif_OpenWindow(url, 'KMSViewArticle', '60', '70');
}

// Open Common Windows
function GlobalUtility_KMS_EmailArticle(strURL)
{
    Motif_OpenWindow(strURL, 'EmailArticle', 65, 70);
}
function GlobalUtility_KMS_OpenArticle(strURL)
{
    Motif_OpenWindow(strURL, 'KMSViewArticle', 65, 70);
}
function GlobalUtility_KMS_PrintArticle(strURL)
{
    Motif_OpenWindow(strURL + "&Print=yes", 'KMSPrint', 65, 70);
}
function GlobalUtility_Email_EmailView(strURL)
{
    Motif_OpenWindow(strURL, 'EmailView', 65, 70);
}
function GlobalUtility_EmailQueue_Edit(strURL)
{
    Motif_OpenWindow(strURL, 'EmailQueue_Edit', 65, 70);
}
function GlobalUtility_OpenTemplate(strURL)
{
    Motif_OpenWindow(strURL, 'EmailTemplate', 65, 70);
}
function GlobalUtility_OpenSelectTemplate()
{
    var strURL = '/Setup/TemplateFolder/SelectTemplate.aspx';
    Motif_OpenWindow(strURL, 'SelectTemplate', 65, 70);
}

// ------------------ BEGIN POPUP CODE --------------------

// popup window object, set properties (e.g. url, width, height) and then open it
// it will cascade closing of children popups when you leave the current screen
function noError()
{
	// ignore this error - use to avoid permission denied errors when crossing http/https windows
	// may leave some windows open in this circumstance
	return true;
}

var oldErrorHandler = window.onerror;
window.onerror = noError;

// keep and array of windows on the original opener 
// we use this only to count the total windows to give them a unique windowname
// without the unique name we will end up closing the wrong windows
try
{
	var popups = window.opener.getPopups();
}
catch (e)
{ 
	var popups = new Array();
}

window.onerror = oldErrorHandler;
if (!popups) var popups = new Array();
// myChildren holds the windows that this window (may be a second or third level popup) has opened
myChildren = new Array();

function getPopups()
{
	// function called by child popup to get back to opener's list of popups
	return popups;
}

function popupWindow()
{
	this.fullscreen = "no";
	this.toolbar= "no";
	this.status= "no";
	this.menubar = "no";
	this.scrollbars = "yes";
	this.resizable="yes";
	this.directories="no";
	this.location="no";
	this.width="300";
	this.height="400";
	this.top="100";
	this.left="100";
	this.url ="";
	this.open = function (fNew) 
	{	
		// remove any closed child windows from the array
		count = myChildren.length;
		for (var i=0; i<count; i++)
		{
			if (myChildren[myChildren.length-1].closed)
				myChildren.slice(myChildren.length-1, 1);
		}
									
		var fNeeded = true;
		if ( (myChildren.length > 0) && !fNew  ) 
		{
			// resuse an existing popup (the last one created)
			if(!myChildren[myChildren.length - 1].closed)
			{
				try
				{
					myChildren[myChildren.length - 1].location = this.url;
					myChildren[myChildren.length - 1].resizeTo(this.width,this.height);					
				} 
				catch (e)
				{
					// permission denied (e.g. https page called from http page)
				}
				myChildren[myChildren.length - 1].focus();	
				fNeeded = false;
			}
		} 
		
		// need to create (not reuse) a new popup
		if (fNeeded)
		{
			// MUST have a unique name
			this.windowname = "wPopup" + popups.length;
			if ((this.height > 500) && (screen.height <= 600)) 
			{
				var agent = navigator.userAgent.toLowerCase();
				if (agent.indexOf("mac")==-1) this.height = 500;
			}
			// create the window and add it to the myChildren array
			if (this.url.indexOf('http') == -1)
				this.url = 'http://' + window.location.hostname + this.url;
			myChildren[myChildren.length] = window.open(this.url ,this.windowname,
				'fullscreen=' + this.fullscreen + 
				',toolbar=' + this.toolbar + 
				',status=' + this.status + 
				',menubar=' + this.menubar + 
				',scrollbars=' + this.scrollbars + 
				',resizable=' + this.resizable +  
				',directories=' + this.directories + 
				',location=' + this.location +
				',width=' + this.width + 
				',top=' + this.top +
				',left=' + this.left + 
				',height=' + this.height);
			// also add it to the opener array so we have an accurate count
			popups[popups.length] = myChildren[myChildren.length -1];
			// every window will take care of closing its children
			window.onunload = this.close;
			myChildren[myChildren.length -1].focus();
		}
	}		
	this.close = function()
	{	
		// go through the children - newest to oldest and close
		var count = myChildren.length;
		for (var i=0; i<count; i++)
		{
			try
			{
				if (!myChildren[myChildren.length-1].closed)
					myChildren[myChildren.length-1].close();
			}
			catch(e)
			{}
			if (myChildren.length > 0)
				myChildren = myChildren.slice(0, myChildren.length-1);
		}
	}		
}
var winPopup = new popupWindow();

// ------------------ END POPUP CODE --------------------

function createCookie(name, value, days)
{
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
    }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
  var ca = document.cookie.split(';');
  var nameEQ = name + "=";
  for(var i=0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
  return null;
}

function eraseCookie(name)
{
  createCookie(name, "", -1);
}