
function showChooseLanguage ()
{
	setTimeout("$('#languages').toggle()",100);
}

function hideChooseLanguage ()
{
	$('#languages').hide();
	setTimeout("$('#sectionSelectorTop').hide()",1);
	setTimeout("$('#locationSelectorTop').hide()",1);
	setTimeout("$('#companySelectorTop').hide()",1);
}

var sWidth = null;
var bShowLeft = true;

function userSetting (sName, sValue, sType)
{
	$.get('/user/setting/?actiontype=ajax&name=' + sName + '&value=' + sValue );
}

function showLeft (iSpeed, pCallback)
{
	bShowLeft = false;
	toggleLeft(700, pCallback);
}

function selectLeftTab (sCurrent, bSkipSyncSetting)
{

	if (sCurrent != '')
	{
		if (!bSkipSyncSetting)
		{
			userSetting ('left_tab', sCurrent);
		}

		var func = function ()
		{
			$('#leftContent .tab').removeClass('visible');
			$('#leftContent #tab' + sCurrent.substring(0,1).toUpperCase() + sCurrent.substring(1)).addClass('visible');
		}
		if (!bShowLeft)
		{
			showLeft(700, func);
		}
		else
		{
			if (sCurrent == 'chat')
			{
				redrawItem('chat');
			}
			else if (sCurrent == 'shout')
			{
				redrawItem('shout');
			}
			func();
		}

	}

	if( $("#graphWrapper").css('width') != 0 ) {

		var graphWidth = (window.innerWidth * 0.7);

		if( graphWidth > 800 )
		graphWidth = 800;

		$("#graphWrapper").width(graphWidth);
		$("#graphContainer").width(graphWidth + 50);
		// Commented out due to error.
		// rerender();
	}

}

function toggleLeft (iSpeed, pCallback, bSkipSyncSetting)
{

	//console.debug('Toggle');
	$('#leftContent .tab').removeClass('visible');
	if (iSpeed == null)
	{
		iSpeed = 700;
	}
	//iSpeed = 15700;
	
	if (!bSkipSyncSetting)
		{
		if (bShowLeft)
		{
			userSetting('show_leftpanel', false);
	
		}
		else
		{
			userSetting('show_leftpanel', true);
		}
	}


	if (bShowLeft)
	{

		$('#leftContent h1').css({'width':'251px'});
		$('#leftMover').css({'background':'none'});

		$('#leftTopMonitor').hide('slow');

		$('#leftMover').animate(
		{
			width:'60px'
		}, iSpeed
		)

		$('#leftContent').animate
		(
		{
			marginLeft:'-228px'

		}, iSpeed, null,

		function()
		{
			pCallback && pCallback();


		}
		)
	}
	else
	{
		$('#leftContent h1').css({'width':'260px'});
		$('#leftTopMonitor').show('slow');
		$('#leftMover').animate(
		{
			width:'290px'
		}, iSpeed
		)
		$('#leftContent').animate
		(
		{
			marginLeft:'0px'
		}, iSpeed, null,
		function()
		{
			pCallback && pCallback();
			//$('#leftContent h1').css({'margin-right':'0px'});
			$('#leftMover').css({'background':'url(/library/image/leftmenu_sliver.png) repeat-y top right'});

		}
		)

	}
	bShowLeft = !bShowLeft;

	if( $("#graphWrapper").css('width') != 0 ) {

		var graphWidth = (window.innerWidth * 0.7);

		if( graphWidth > 800 )
		graphWidth = 800;

		$("#graphWrapper").width(graphWidth);
		$("#graphContainer").width(graphWidth + 50);
		// Commented out due to error.
		// rerender();
	}
}

function showTextAreaFooter (sValue)
{
	$('#txtFooterReactions').css('height','100px');
	$('#txtFooterReactions').css('color','#000');
	$('#txtFooterReactions').css('font-style','normal');

	$('.sendFeedback').css('display','block');

	if (sValue == $('#txtFooterReactions').val())
	{
		$('#txtFooterReactions').val('');
	}

}

function hideTextAreaFooter (sValue)
{
	if ($('#txtFooterReactions').val() == '')
	{
		$('#txtFooterReactions').css('height','50px');
		$('#txtFooterReactions').css('color','#999');
		$('#txtFooterReactions').css('font-style','italic');
		$('.sendFeedback').css('display','none');

		$('#txtFooterReactions').val(sValue);
	}

}


function sendTextAreaFooter ()
{
	var callback = function (sResponse)
	{
		$('#footer .right').html(sResponse);
	}

	jQuery.post('/home/send_feedback/?actiontype=ajax',{'feedback' : $('#txtFooterReactions').val()},callback);
}


function deleteCropAdviser (iId)
{
	// sType must be cropadviser, colleague
	var callback = function (sResponse)
	{
		$('#cropadviser_response').html(sResponse);
	}
	$('#cropadviser_response').html('');
	jQuery.post('/user/delete_cropadviser/?actiontype=ajax',{'id' : iId},callback);


}

function invite (oEmailaddress, sType, oResponse)
{
	// sType must be cropadviser, colleague
	var callback = function (aResponse)
	{

		oResponse.html(aResponse['html']);
		if (aResponse['error'] != 1)
		{
			if (sType == 'colleague')
			{
				var oList = $('ul.colleagues_pending_sent');
				var oDiv = $('div.colleagues_pending_sent');
			}
			else if (sType == 'customer')
			{
				var oList = $('ul.customers_pending_sent');
				var oDiv = $('div.customers_pending_sent');
			}
			else
			{
				var oList = $('ul.cropadvisers_pending_sent');
				var oDiv = $('div.cropadvisers_pending_sent');
			}
			oList.append('<li>' + oEmailaddress.val() + '</li>');
			oDiv.css('display','block');
			oEmailaddress.val('');
		}

	}
	oResponse.html('');
	jQuery.post('/user/invite_' + sType + '/?actiontype=ajax',{'emailaddress' : oEmailaddress.val()},callback,'json');


}

function remove (sType)
{
	var callback = function (sResponse)
	{
		$('#' + sType + '_response').html(sResponse);

	}
	$('#' + sType + '_response').html('');
	jQuery.post('/user/delete_' + sType + '/?actiontype=ajax',callback);

}



function addOnloadDelayed (sFunc)
{
	setTimeout(sFunc, 3000);
}



function initializeGoogleMap(aCoords)
{
	if (GBrowserIsCompatible())
	{

		var map = new GMap2(document.getElementById("map_canvas"));
		var size = new GSize();

		var currentIcon = new GIcon(G_DEFAULT_ICON);
		currentIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/red-dot.png";
		currentIcon.iconSize = size;

		var otherIcon = new GIcon(G_DEFAULT_ICON);
		otherIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/yellow-dot.png";
		otherIcon.iconSize = size;

		var colleagueIcon = new GIcon(G_DEFAULT_ICON);
		colleagueIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png";
		colleagueIcon.iconSize = size;

		var colleagueIconOther = new GIcon(G_DEFAULT_ICON);
		colleagueIconOther.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/green-dot.png";
		colleagueIconOther.iconSize = size;

		function createMarker(point, value, type)
		{
			if (type == 0)	// Current
			{
				markerOptions = { icon:currentIcon };
			}
			if (type == 1)	// Other location
			{
				markerOptions = { icon:otherIcon };
			}
			if (type == 2)	// Colleague location
			{
				markerOptions = { icon:colleagueIcon };
			}
			if (type == 3)	// Colleague location
			{
				markerOptions = { icon:colleagueIconOther };
			}

			var marker = new GMarker(point,markerOptions);
			marker.value = value;

			if (value != '')
			{
				GEvent.addListener(marker, "click", function() {
					map.openInfoWindowHtml(point, value);
				});
			}
			return marker;
		}

		map.setUIToDefault();
		var isCentered = false;

		for (i=0; i<aCoords.length; i++)
		{

			var point = new GLatLng(aCoords[i]['x'], aCoords[i]['y']);
			if (isNaN(aCoords[i]['id']))
			{
				aCoords[i]['html'] = '';
			}

			map.addOverlay(createMarker(point, aCoords[i]['html'],aCoords[i].type));

			if (aCoords[i].type == 0)
			{

				map.setCenter(point, 11);
				isCentered = true;
			}

		}

		if (!isCentered)
		{
			map.setCenter(point, 11);
		}



	}
}


function sendCultivationCommunityAddRequest (oName)
{
	var callback = function (aResponse)
	{
		if (aResponse['error'])
		{
			$('.addCultivationCommQuestion').show();
		}
		else
		{
			oName.val('');
			$('.addCultivationCommQuestion').hide();
		}

		$('#addCultivationCommResponse').html(aResponse['content']);
	}
	$('#addCultivationCommResponse').html('');
	jQuery.post('/company/add_cultivationcommunity/?actiontype=ajax',{'name' : oName.val()},callback, 'json');
}

function hoverImg (oImg)
{
	oImg.src = oImg.src.replace('1','2');
}


function restoreImg (oImg)
{
	oImg.src = oImg.src.replace('2','1');
}

function focusInput (oInput, sValue)
{
	if (oInput.value == sValue)
	{
		$(oInput).removeClass('focusLost');
		oInput.value = '';
	}
}



function blurInput (oInput, sValue)
{
	if (oInput.value == '')
	{
		$(oInput).addClass('focusLost');
		oInput.value = sValue;
	}

}



function doLogin (sEmailaddress, sPassword, sDestination)
{
	$('#' + sDestination).html('');
	$('#loginModal .loader').show();
	$.post('/user/login/?actiontype=ajax', { emailaddress:sEmailaddress, password:sPassword},
	function(aData)
	{

		if (aData.success)
		{
			var sReturnUrl = aData.return_url;
			$.get('/user/init_session_check/?actiontype=ajax',{ } ,
			function (aResponse)
			{

				if (aResponse.success)
				{
					$('#frmLoginModal').unbind('submit');
					$('#frmLoginModal').submit(); // This is that the browser remembers the login info!
					sExec = "location.href = '" + sReturnUrl + "';";
					setTimeout(sExec,1000);
				}
				else
				{
					$('#loginModal .loader').hide();
					$('#' + sDestination).html(aResponse.html);
				}
			}
			, 'json');

		}
		else
		{
			$('#loginModal .loader').hide();
			$('#' + sDestination).html(aData.html);
		}

	}, 'json');

}


function doSendNewPasswordRequest (sEmailaddress, sDestination)
{
	$.post('/user/lostpassword/?actiontype=ajax', { emailaddress:sEmailaddress},
	function(aData)
	{
		$('#' + sDestination + '').html(aData.html);

	}, 'json');

}

function errorMessage (sTitle, sMessage)
{
	$('#errorModal h1').html(sTitle);
	$('#errorModal div.message').html(sMessage);

	$('#errorModal').modal();
	return false;
}

function confirmation (sTitle, sQuestion, pCallback)
{
	$('#confirmationModal h1').html(sTitle);
	$('#confirmationModal div.message').html(sQuestion);

	$('#confirmationModal #btn_yes').click(pCallback);
	$('#confirmationModal').modal();
	return false;
}

function confirmUserstate (sTitle, sQuestion)
{
	return confirmation (sTitle, sQuestion, function () {document.location.href='/user/';});
}

function convertLuxMol (oType,oInput)
{

	if (oType.val() == 'lux')
	{
		oInput.val(Math.round(oInput.val() / 0.0132));
	}
	else
	{
		oInput.val(Math.round(oInput.val() * 0.0132));
	}
	//console.debug(oType);
	//console.debug(oInput);
}

function sendPoll (sName)
{
	var sOption = $("input[name='" + sName + "']:checked").val();
	if (sOption != null)
	{
		$.post('/home/poll/?actiontype=ajax', { option:sOption},
		function(aData)
		{
			$('#poll .window').html(aData);

		});
	}

}

/* Needs still some optimization */
function positionFooter ()
{
	if (window.innerHeight - 440 - $('#contentFull').height() < 0)
	{
		$('#footer').css('position','relative');
		//alert('REL');
	}
	else
	{
		$('#footer').css('position','absolute');
		$('#footer').css('bottom','0px');
		$('#footer').css('height','179px');
		$('#footer').css('background-color','#55585D');
		$('#footer').css('border-bottom','10px solid #B8CC33');
		//alert('ABS');
	}
}

function initialCap(sValue)
{
	var sReturn = new String();
	sReturn = sValue;
	if (sReturn == '')
	{
		return false;
	}
	return (sReturn.substr(0, 1).toUpperCase() + sReturn.substr(1));
}

function forceNumericInput(vnt,obj)
{
	var base;

	if(window.event)
	{
		base=window.event.keyCode;
	}
	else
	{
		if(obj)
		{
			base=obj.which;
		}
		else
		{
			return true;
		}
	}


	if((base>=58&&base<=126)||(base>=32&&base<=43)||base==45||base==47||base==44||base==46)
	{
		return false;
	}
	else
	{

		return true;
	}
}


function printNode(sId)
{
	var pContent = document.getElementById(sId);
	var wPrint =
	window.open('','','left=0,top=0,width=1,height=1,t oolbar=0,scrollbars=0,status=0');
	wPrint.document.write(pContent.innerHTML);
	wPrint.document.close();
	wPrint.focus();
	wPrint.print();
	wPrint.close();
}

function calcLocationSurface ()
{
	aFields = $('.sectionsurfacefield');
	//debugger;
	var iTotal = 0;
	for (var i=0; i<aFields.length; i++)
	{
		iTotal +=  parseInt(aFields[i].value);
	}
	if (isNaN(iTotal))
	{
		iTotal = 0;
	}
	$('#surface').html(parseInt(iTotal) + ' m&sup2;');

}

function prepareInputsForHints() {
	/*
	form hints are no longer clickable


	var aNodes = $('.hint');

	aNodes.unbind('click');
	aNodes.click(function() {
	var aSpans = this.parentNode.parentNode.getElementsByTagName('span');
	for (var i=0; i < aSpans.length; i++)
	{
	if (aSpans[i].className == 'hintContent')
	{
	var node = aSpans[i];
	}
	}
	if (node.style.display == 'block')
	{
	node.style.display = 'none';
	}
	else
	{
	node.style.display = 'block';
	}
	})
	*/
}

//setTimeout ('prepareInputsForHints()',250);


function updateHintContent (sName)
{
	var func = function (sResponse)
	{
		if (sResponse != 'TRUE')
		{
			alert('Something went wrong with saving the data!');
		}
		else
		{
			$('#input_' + sName.toLowerCase()).parent().toggle();
		}
	}

	jQuery.post('/development/hintcontent/?actiontype=ajax', {'name' : sName, 'value' : $('#input_' + sName.toLowerCase()).val()},func);


}

