// global variables...
$(document).ready(function(){
	setupButtonActions();
});

function setupButtonActions(){

	// control the button hover color...
	$('.button').hover(
			function(){
				$(this).addClass('buttonHoverColor');
			},
			function(){
				$(this).removeClass('buttonHoverColor');
			}
	);

}

var processingMessage = '<img src="http://application.jff.org/images/loading.gif"  />';
function getProcessingMessage(){
	return processingMessage;
}


// parses out URL variables and returns an array...
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        if(hash[1] != undefined) {
        	hash[1] = hash[1].replace('#', '');  // get rid of errant pound signs
        }
        vars.push(hash[0]);
		//alert(hash[0] + ' / ' + hash[1]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
	    {
	    c_start=c_start + c_name.length+1;
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    }
	  }
	return "";
}

function deleteCookie(c_name){
	setCookie(c_name, '', -1);
}


document.onmousemove = getMouseXY_v1;
var mouseX = 0;
var mouseY = 0;

function getMouseXY_v1(e){
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	// posx and posy contain the mouse position relative to the document
	// Do something with this information
	mouseX = posx - document.body.scrollLeft - document.documentElement.scrollLeft;
	mouseY = posy - document.body.scrollTop - document.documentElement.scrollTop;
}

function displayPopupCentered( name ){
    var viewport = {
            o: function() {
                if (self.innerHeight) {
        			this.pageYOffset = self.pageYOffset;
        			this.pageXOffset = self.pageXOffset;
        			this.innerHeight = self.innerHeight;
        			this.innerWidth = self.innerWidth;
        		} else if (document.documentElement && document.documentElement.clientHeight) {
        			this.pageYOffset = document.documentElement.scrollTop;
        			this.pageXOffset = document.documentElement.scrollLeft;
        			this.innerHeight = document.documentElement.clientHeight;
        			this.innerWidth = document.documentElement.clientWidth;
        		} else if (document.body) {
        			this.pageYOffset = document.body.scrollTop;
        			this.pageXOffset = document.body.scrollLeft;
        			this.innerHeight = document.body.clientHeight;
        			this.innerWidth = document.body.clientWidth;
        		}
        		return this;
            },
            init: function(name) {
                $(name).css("left",Math.round(viewport.o().innerWidth/2) + viewport.o().pageXOffset - Math.round($(name).width()/2));
                $(name).css("top",Math.round(viewport.o().innerHeight/2) + viewport.o().pageYOffset - Math.round($(name).height()/2));
            }
        };
    	viewport.init( name );
    	$(name).show();
}

function displayPopupAtMousePosition( name ){
    var viewport = {
            o: function() {
                if (self.innerHeight) {
        			this.pageYOffset = self.pageYOffset;
        			this.pageXOffset = self.pageXOffset;
        			this.innerHeight = self.innerHeight;
        			this.innerWidth = self.innerWidth;
        		} else if (document.documentElement && document.documentElement.clientHeight) {
        			this.pageYOffset = document.documentElement.scrollTop;
        			this.pageXOffset = document.documentElement.scrollLeft;
        			this.innerHeight = document.documentElement.clientHeight;
        			this.innerWidth = document.documentElement.clientWidth;
        		} else if (document.body) {
        			this.pageYOffset = document.body.scrollTop;
        			this.pageXOffset = document.body.scrollLeft;
        			this.innerHeight = document.body.clientHeight;
        			this.innerWidth = document.body.clientWidth;
        		}
        		return this;
            },
            init: function(name) {
            	var xPos = Math.round(viewport.o().innerWidth/2) + viewport.o().pageXOffset - Math.round($(name).width()/2)
                $(name).css("left", xPos);
            	
                //$(name).css("left", mouseX );
                $(name).css("top", mouseY );
                
                // save window position...
                setCookie('mouseX', xPos, 365);
                setCookie('mouseY', mouseY, 365 );
            }
        };
    	viewport.init( name );
    	$(name).show();
}


function displayPopupAtPreviousPosition( name ){
    var viewport = {
            o: function() {
                if (self.innerHeight) {
        			this.pageYOffset = self.pageYOffset;
        			this.pageXOffset = self.pageXOffset;
        			this.innerHeight = self.innerHeight;
        			this.innerWidth = self.innerWidth;
        		} else if (document.documentElement && document.documentElement.clientHeight) {
        			this.pageYOffset = document.documentElement.scrollTop;
        			this.pageXOffset = document.documentElement.scrollLeft;
        			this.innerHeight = document.documentElement.clientHeight;
        			this.innerWidth = document.documentElement.clientWidth;
        		} else if (document.body) {
        			this.pageYOffset = document.body.scrollTop;
        			this.pageXOffset = document.body.scrollLeft;
        			this.innerHeight = document.body.clientHeight;
        			this.innerWidth = document.body.clientWidth;
        		}
        		return this;
            },
            init: function(name) {
            	var xPos = getCookie('mouseX');
            	var yPos = getCookie('mouseY');
                $(name).css("left", xPos);
                $(name).css("top", yPos );
            }
        };
    	viewport.init( name );
    	$(name).show();
}

