var thePopup;

var galleryTimers = new Array();
var galleryExecuter;

function startGallery(id, count, current) {
	galleryTimers[id]				= {};
	galleryTimers[id].id			= id;
	galleryTimers[id].count			= count;
	galleryTimers[id].current		= current;
	
	if (galleryExecuter == undefined) {
		galleryExecuter	= new PeriodicalExecuter(function(pe) {
			for (gal in galleryTimers) {
				if (galleryTimers[gal].current != undefined) {
					
					var old = galleryTimers[gal].current;
					
					galleryTimers[gal].current++;
					if (galleryTimers[gal].current == galleryTimers[gal].count) galleryTimers[gal].current = 0;
					
					if (galleryTimers[gal].count > 1) {
						new Effect.Parallel([
							new Effect.Opacity($('img'+gal+old), 						{ sync: true, from: 1, to: 0 }),
							new Effect.Opacity($('img'+gal+galleryTimers[gal].current),	{ sync: true, from: 0, to: 1 }) 
						], { 
							duration: 0.5,
							beforeStart:function () {
								$('img'+gal+galleryTimers[gal].current).setOpacity(0);
							$('img'+gal+galleryTimers[gal].current).show();
							}
						});
					}
				}
			}
		}, 3	);
	}
}

function openProject(id) {
	
	var left = 0;
	var top  = 0;
	
	try {
		thePopup.window.close();
	} catch(e) {}
	
	thePopup = window.open('/ajax/getProject.php?id='+id,'','width='+screen.width+',height=620,resizable=no,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no,left='+left+',top='+top)
	thePopup.window.focus();
}

function getSubPage(id) {
	background();
	
	var url = '/ajax/getSubPage.php';
	new Ajax.Request(url, {
		method: 'post',
		postBody: 'lang='+lng+"&id="+id,
		onSuccess: function(t) {
			$('workContainer').update(t.responseText);
			
			for (var i = 0; i < clientArray.length; i++) {
				$('clientTitle'+clientArray[i]).className = 'clientTitle';
			}
			$('clientTitle'+id).className = 'clientTitleRed';
		}
	});
}

function background() {
	new Effect.Morph('body', {
		style: {
			background: '#eee'
		},
		duration: 0.2,
		afterFinish: function() {
			new Effect.Morph('body', {
				style: {
					background: '#fff'
				},
				duration: 0.5
			});	
		}
	});
}

function getPage(id, subId) {
	if (galleryExecuter != undefined) {
		galleryExecuter.stop();
		galleryExecuter = undefined;
	}
	
	if (subId == undefined) subId = 0;
	
	background();
	
	var url = '/ajax/getPage.php';
	new Ajax.Request(url, {
		method: 'post',
		postBody: 'lang='+lng+"&id="+id+"&subId="+subId,
		onSuccess: function(t) {
			$('content').update(t.responseText);
			
			for (var i = 0; i < menus.length; i++) {
				$('menuImageRed'+menus[i]).hide();
				$('menuImageBlack'+menus[i]).show();
			}
			if (id != 11) {
				$('menuImageRed'+id).show();
				$('menuImageBlack'+id).hide();
				if (supersleight != '') supersleight.fixImg($('menuImageRed'+id));
			}
		}
	});
}
