//	preload
sub = new Image(67,67); 
sub.src = "images/submit.gif"; 

//	variables for the popup gliding on the stage
var animation;
//	globals for the draggin functionality
var dragDropTimer = -1;
var currentZIndex = 1000;
var drag = false;
var browser = navigator.appName;
var popuplive = true;

var __x;
var __y;

//	function called when page loads
function init()
{
	$('#Name').mousedown(function(e){
		putFocus(0);
    });
	$('#Name').mouseup(function(e){
		restore();
    });
    $('Email').mousedown(function(e){
		putFocus(1);
    });       
    $('Email').mouseup(function(e){
		restore;
    });  

	var p = $('p');
	
    for(var i=0;i<p.length;i++)	{
		 
		var teststring = p[i].innerHTML;
		var matchPos = teststring.search(keyword);

		if(matchPos != -1)	
		{
			teststring = teststring.replace(keyword,'<a href="#" class="pounce">'+keyword+'</a>');
			p[i].innerHTML = teststring;
		}	
	}	
	
	$('.pounce').mouseover(function(e){
		pounce(e);
    }); 
	
	panel = $("#registerform");
	panel.css('top',panel.height()*(-1));
	panel.css('left', -300);
	//	start moving it
	var trigger = setTimeout(triggerPopUp,start_time);

}

function triggerPopUp()	
{
	animation = setInterval("move()",1);
}

function move()		
{
	//	basic movement with easing to the target point
	var vx = (targetX - parseInt(panel.css('left')))*easing;
	var vy = (targetY - parseInt(panel.css('top')))*easing;

	panel.css('left', parseInt(panel.css('left')) + vx/10);
	panel.css('top', parseInt(panel.css('top')) + vy/10	);
		
	//	once there, stop the repeating function
	if (parseInt(panel.css('top'))>= targetY-20 && parseInt(panel.css('left'))>=targetX-20 )	
	{
		if (browser != "Microsoft Internet Explorer")
			panel.css('position', "fixed");

		clearInterval(animation);
		popuplive = true;
		panel.className = 'dragableElement';
		//	this line allow the dragging. comment out to prohibit it.
		if(allowdragging)
			initdragableElements();
		document.forms[0].elements[0].focus();
	}
}

function initdragableElements()		
{
	$("#headline").css('cursor', 'move');
	
	$("#headline").mousedown(function(e){
		initDragDropElement(e)
    }); 
	
	$(document).mousemove(function(e){
      moveDragableElement(e);
    }); 
  
	$("#registerform").mouseup(function(e){
      stop_dragDropElement(e);
    }); 
}

function initDragDropElement(e)		
{
	// relative mouse pos from (0,0) on regsiterform
	__x = e.pageX - document.getElementById('registerform').offsetLeft;
	__y = e.pageY - document.getElementById('registerform').offsetTop;
	
	currentZIndex++;
	$("#registerform").css('zIndex', currentZIndex);
	drag = true;
}

//	CALLED EVERY TIME THE MOUSE MOVES
function moveDragableElement(e)		
{
	if(drag == true)	
	{
		$('#registerform').css('left', e.pageX - __x );
		$('#registerform').css('top', e.pageY - __y );
	}
}

function secondChance(nextpage)
{
	if(submitted == true)
		return true;
	else	
	{
		alert(leavingmessage);
		return true;
	}
}
 
function closethis()	
{
	popuplive = false;
	secondChance();
	$("#registerform").css('display', 'none');
}
  
function putFocus(elementInst)
{ 
   	document.forms[0].elements[elementInst].focus();
}

function stop_dragDropElement()   	
{  	
	drag = false;
}

function restore()					
{
	$("#headline").mousedown(function(e){
		initDragDropElement(e)
    }); 
	drag = true;  
}

//	move popup to a link on the page
function pounce(e)	{

	//	if haven't regsitered....
	if(popuplive == false)	
	{

		targetX = e.pageX - 50;
		targetY = e.pageX - 80;

		if (browser != "Microsoft Internet Explorer")
			targetY = 200;
		
		$("#registerform").css('display', 'block');
		$("#registerform").css('top',panel.height()*(-1));
		$("#registerform").css('left',-300);

		popuplive = true;

		triggerPopUp();
	}
	
}

