var onPageScroll = function()
{
	if(!$.browser.msie)moveMenu();
		else $('#backLine').css('display', 'none');

	$(document).stopTime('selectmenu');
	$(document).oneTime(100, 'selectmenu', afterScroll);
}//onPageScroll

var afterScroll = function()
{
	selectMenu();
	$('#menuNeedle').hide();
	$('body').css('overflow', '');

	fixTitle();

	if($(window).data('IE6Alert') == 1)
	{
		messageBox(fuckIE6Msg, null, 10000);
		$(window).data('IE6Alert', 0);
	}//if ie6Alert
}//afterScroll

var selectMenu = function()
{
	var st = $(window).scrollTop();
	var wh = $(window).height();
	var pos = $(window).data('sectionPos');
	var alias = '';

	if($.browser.msie)
	{
		for(var b = 1; b <= 5; b++)
		{
			var bpos = $('#box_' + b).position().top;
			var bh = $('#box_' + b).height();

			if(bpos >= 0 && bpos < bh)
			{
				alias = pos[b - 1];
				break;
			}//if in
		}//for b
	}//if ie
	else
	{
		for(var b = 1; b <= 5; b++)
		{
			var bpos = $('#box_' + b).position().top;
			var bh = $('#box_' + b).height();

			if(st + wh < bh + bpos)
			{
				alias = pos[b - 2];
				break;
			}//if in
		}//for b
	}//else everyone

	if(alias == '')alias = 'start';

	if(alias != 'start')
	{
		var player = getFlash("playerHome");
		if(player != undefined && player != null)player.pause();
	}//if !start

	/*
	if(alias == 'contact')
	{
		if($.browser.mozilla)
		{
			$.scrollTo(0);
		}//if ff
	}//if contact
	*/

	$('.selected').removeClass('selected');
	$('#' + alias + 'Li').addClass('selected');
}//selectMenu

var moveMenu = function()
{
	var pos = $(window).data('startPos');
	if(pos == undefined)
	{
		setLine();
		return;
	}//get startpos

	var diff = $(window).scrollTop() - pos.st;
	var top = (pos.lt + diff) + 'px';
	$('#backLine').css('top', top);

	$('#menuNeedle').show();
}//moveMenu

var setLine = function()
{
	if($.browser.msie)var lt = Number($('#backLine').css('top').replace('px', ''));
		else var lt = $('#backLine').position().top;

	var start_pos = {st:($(document).height() - $(window).height()), lt:lt};
	$(window).data('startPos', start_pos);
}//setLine

var onWindowResize = function()
{
	var mg = $(window).height() - (($.browser.msie)?(710):(620));
	$('#menu').css('margin-top', mg);

	$(window).data('startPos', undefined);
}//onWindowResize

$(document).ready(function(){
	if($.browser.msie && $.browser.version == 6)
	{
		$(window).data('IE6Alert', 1);
	}//if ie6
	else if(!$.browser.msie)
	{
		$('body').css('overflow', '');
		$('#startLi').addClass('selected');
	}//else GOOD BROWSERS

	// scroll da página
	if(!$.browser.msie)
	{
		$.localScroll({
			axis:'y',
			duration:500
		});
	}//if !ie

	$(window).scroll(onPageScroll);
	$(window).resize(onWindowResize);

	onWindowResize();

	if(window.location.href.indexOf('#job') >= 0)
	{
		var job_id = getHashVar('job');
		if(job_id != 0)callAction(WEB_ROOT + '/open/job/index/id/' + job_id);
			else $(document).oneTime(500, function(){$('#startLink').click()});
	}//else if
	if(window.location.href.indexOf('#cli') >= 0)
	{
		var cli_id = getHashVar('cli');
		if(cli_id != 0)callAction(WEB_ROOT + '/open/job/clientjob/id/' + cli_id);
			else $(document).oneTime(500, function(){$('#startLink').click()});
	}//else if
	else gotoAnchor('start');

	//posições
	var pos = new Array();
	pos[0] = 'contact';
	pos[1] = 'clients';
	pos[2] = 'services';
	pos[3] = 'about';
	pos[4] = 'start';

	$(window).data('sectionPos', pos);
});