var ChatClosed=true;
var hbHelp='';
var hbDefineIt='';
window.onload = function(){
	if ($('cow1')){
		new Draggable('cow1');	
	}
	if ($('cow2')){
		new Draggable('cow2');	
	}
	if ($('cow3')){
		new Draggable('cow3');	
	}
	if ($('gnome1')){
		new Draggable('gnome1');	
	}
	if ($('gnome2')){
		new Draggable('gnome2');	
	}
	if ($('plane')){
		new Draggable('plane');	
	}
	if ($('photographer')){
		new Draggable('photographer');	
	}
	hbHelp = new HelpBalloon({
								title: 'Help (F9)',
								content: '<div id="helpContent"></div>',
								icon: $('helpAnchor'),
								id: 'helpBallonId',
								fixedPosition: HelpBalloon.POS_BOTTOM_LEFT
							});
	hbDefineIt = new HelpBalloon({
								title: 'Define It (F8)',
								content: '<div id="defineItContent"></div>',
								icon: $('defineToolAnchor'),
								id: 'defineItBallonId',
								fixedPosition: HelpBalloon.POS_BOTTOM_LEFT
							});
	Event.observe(document, 'keydown', function(e){
		/*Use keydown instead of keypress, key press fails in IE.*/
		var code;
		if (!e) var e = window.event;
		if (e.keyCode) code = e.keyCode;
		else if (e.which) code = e.which;
		if (code == 119) {
			/*F-8 pressed*/
			window.scrollTo(0,0);
			defineIt(true);
		}else if (code == 120) {
			/*F-10 pressed*/
			window.scrollTo(0,0);
			helpMe('HOME',true);
		}
	});
}
/*validate the contact form*/
function validateContact(){
	var message='';
	if (!$('EmailerContactName').value){
		message+='Please provide your name.<br />';	
	}
	if (!$('EmailerContactPhone').value){
		message+='Please provide your phone number.<br />';	
	}
	if (!$('EmailerContactEmail').value){
		message+='Please provide your email.<br />';	
	}
	if (!$('EmailerContactMessage').value){
		message+='Please provide your message.<br />';	
	}
	/*if (($('EmailerMyCellNum').value) || ($('EmailerMyAddress').value)){
		message+='An error has occured.<br />';	
	}*/
	if (!message) {
		return true;
	}else{
		$('jsContactMessage').innerHTML='<h5>'+message+'</h5>';
		return false;
	}
}
/*Chat Toggle*/
function ToggleChat(){
	if(ChatClosed){
		new Effect.Appear('liveChatContainer',{from: 0.0, to: 1.0});
		ChatClosed=false;
	}else{
		new Effect.Fade('liveChatContainer',{from: 1.0, to: 0.0});
		ChatClosed=true;
	}
	return true;
}
/*Define it tool*/
function getSelectedText(){
	var txt = '';
     if (window.getSelection)
    {
        txt = window.getSelection();
    }
    else if (document.getSelection)
    {
        txt = document.getSelection();
    }
    else if (document.selection)
    {
        txt = document.selection.createRange().text;
     }
    else return;
	return txt;
}
function defineIt(keyPressed){
	var content=getSelectedText();
	if (keyPressed){
		hbDefineIt.show();		
	}
	new Ajax.Request(ServerName+'/define_tools/findit/'+content,
					{
					onLoading: function(){$('loadingDiv').style.display='inline';$('defineItContent').innerHTML='Loading...';},
					onSuccess: function(transport){
					    var response = transport.responseText || '';
						$('defineItContent').innerHTML=response;
						$('loadingDiv').style.display='none';
						hbDefineIt.show();		
					  },
					  onFailure: function(){ alert('Something went wrong...') }
					});
	return true;
}
function helpMe(content,keyPressed){
	if (keyPressed){
		hbHelp.show();		
	}
	new Ajax.Request(ServerName+'/helps/helptext/'+content,
					{
					onLoading: function(){$('loadingDiv').style.display='inline';},
					onSuccess: function(transport){
				    	var response = transport.responseText || '';
						$('helpContent').innerHTML=response;
						$('loadingDiv').style.display='none';
						hbHelp.show();	
					  },
					  onFailure: function(){ alert('Something went wrong...') }
					});
	return true;	
}
