var Tabs = new Array(0, 1, 5, 1, 2, 1, 4, 5);
var Sections = new Array('home', 'program', 'competitors', 'results', 'gallery', 'sponsors', 'media', 'accomodation');
var ActiveTab = 0;
var InfoScript = "/2007/ajax/getInfo.php";
var InfoScriptActions = '/2007/ajax/actionsInfo.php';
var CurrentSection = 0;
var CurrentTab = 1;

function SI(obj, image)
{
	objindex = obj.id.substr(7,1);

	if (ActiveTab != objindex)
	{
		obj.style.backgroundImage = "url(img/" + image + ")";
	}
}

function AB(tab, index)
{
	ActiveTab = 0;
	for(i=0;i<Tabs[tab];i++)
	{
		SI($('tab['+tab+']['+(i+1)+']'), 'tab_inactiv.jpg', (i+1));
	}
	SI($('tab['+tab+']['+index+']'), 'tab_activ.jpg', index);
	ActiveTab = index;

	GetInfo(tab, index);
}

function ST(section, tab)
{
	for(i=0;i<Sections.length;i++)
	{
		$(Sections[i]).style.display = 'none';
	}

	$(Sections[section]).style.display = 'inline';

	AB(section, tab);
}

function GetInfo(section, tab)
{
	options = {
				 parameters:     	'&s='+section+'&t=' + tab + '&rand=' + Math.random(),
				 method:         	'get',
				 onSuccess:     	DisplayInfo
	};
	CurrentSection = section;
	CurrentTab = tab;

	new Ajax.Request(InfoScript, options);
}

function DisplayInfo(response)
{
	$('tab_content').innerHTML = response.responseText;
	if(CurrentSection == 4 && (CurrentTab == 1 || CurrentTab == 2 || CurrentTab == 3))
	{
		DisplayPictures(CurrentTab);
	}

	if (CurrentSection != 0)
	{
		$('tab_content').style.height = "422px";
		$('tab_content').style.overflow = "auto";
		$('tab_content').style.border = "1px solid #666666";
	}
	else
	{
		$('tab_content').style.height = "auto";
		$('tab_content').style.overflow = "hidden";
		$('tab_content').style.border = "none";
	}

}

function SubmitInformation()
{
//	alert('&namefirst='+escape($('namefirst').value) + '&namelast=' + escape($('namelast').value) + '&country=' + $('country').value + '&birthdate=' + $('year').value + '-'+$('month').value+'-'+$('day').value+'&federation='+escape($('federation').value)+'&address='+escape($('address').value)+'&email='+$('email').value+'&phone='+$('phone').value+'&dificulty='+$('dificulty').checked+'&speed='+$('speed').checked+'&speedcup='+$('speedcup').checked+'&route='+escape($('route').value)+'&transport='+$('transport').value+'&arrival_date='+escape($('arrival_date').value)+'&arrival_hour='+escape($('arrival_hour').value)+'&sejour='+escape($('sejour').value)+'&other='+escape($('other').value)+'&best='+escape($('best').value)+'&about='+escape($('about').value)+'&song='+escape($('favorite').value));
	options = {
				 parameters:     	'&namefirst='+escape($('namefirst').value) + '&namelast=' + escape($('namelast').value) + '&country=' + $('country').value + '&birthdate=' + $('year').value + '-'+$('month').value+'-'+$('day').value+'&federation='+escape($('federation').value)+'&address='+escape($('address').value)+'&email='+$('email').value+'&phone='+$('phone').value+'&dificulty='+$('dificulty').checked+'&speed='+$('speed').checked+'&speedcup='+$('speedcup').checked+'&route='+escape($('route').value)+'&transport='+$('transport').value+'&arrival_date='+escape($('arrival_date').value)+'&arrival_hour='+escape($('arrival_hour').value)+'&sejour='+escape($('sejour').value)+'&other='+escape($('other').value)+'&best='+escape($('best').value)+'&about='+escape($('about').value)+'&song='+escape($('favorite').value),
				 method:         	'post',
				 onSuccess:     	HandleSubmitInfo
	};

	new Ajax.Request(InfoScriptActions, options);
}

function HandleSubmitInfo(response)
{
	eval('ResponseVals='+response.responseText);

	$('error').innerHTML = ResponseVals.Message;
	
		
	if (ResponseVals.Status == 1)
	{
		$('form1').reset();
	}
}


var Images = new Array();
var ipp = 0;
var page = 0;

function DisplayPictures(CTab)
{
	page = 0;
			options = {
				 parameters:     	'',
				 method:         	'get',
				 onComplete:     	DisplayRecords
				};

	new Ajax.Request('ajax/GetImages.php?tab='+CTab+'&code=' + Math.random(0, 99999), options);
	Tab = CTab;
}

function DisplayRecords(response)
{
	Images = new Array();
	eval('Images='+response.responseText);
	DisplayImages(9,0, 2005 + Tab)
	
}

function DisplayImages(ipp, currentpage, prefix)
{
	if ((page + currentpage >= 0) && ((page + currentpage) * ipp < Images.length))
	{
		page = page + currentpage;
	}

	if ((page + 1) * ipp >= Images.length)
	{
		ValidImages = Images.length - (page) * ipp;
	}
	else
	{
		ValidImages = ipp;
	}

	for(var i = 0; i < ipp; i++)
	{
		if (ValidImages > i)
		{
			$('gg' + i).style.display = 'inline';
			$('g' + i).src = 'pictures/'+ prefix +'/small/'+Images[i + parseInt(ipp * page)];
			$('g' + i).style.width = '180px';
			$('g' + i).style.height = '85px';
			
		}
		else
		{
			$('gg' + i).style.display = 'none';
		}
	}
	$('gallery').style.display = 'block';

}

function PopupImage(img)
{
	var left = (document.body.clientWidth/2) - (800/2);

	$('preview').style.left = left - 20;
	img = img.split("/");

	$('close').style.display = 'inline';

	prefix = 2005 + Tab;

	$('preview').innerHTML = '<img src="pictures/'+prefix+'/big/' + img[img.length-1] + '" border=0>';
	$('preview').style.display = 'inline';
}


function HidePreview()
{
	$('close').style.display = 'none';
	$('preview').innerHTML = '';
	$('preview').style.display = 'none';
}
