var speedDefault = 10;
var speed = 10;
var GALLERY_LOADING = 0;
var paramsArray = new Array();

function galleryCreate() {
	GALLERY_LOADING = 1;
	$.post(
		'/common/createGallery.php', 
		{}, 
		function(obj) {
			try {
				eval(obj);	
				$("#galleryMainConteiner").empty();
				
				for (var i in galleryData) {
					inner = '<table width="99%" cellspacing="0" style="margin-top: 40px;">'+
							'	<tr height="90">'+
							'		<td width="200">'+
							'			<a href="'+galleryData[i]['link']+'" class="bigBlack">'+galleryData[i]['name']+'</a> <br/>'+
							'			<p class="textSmall">'+galleryData[i]['description']+'</p>'+
							'		</td>'+
							'		<td width="20">'+
							'			<img id="galleryMoveLeft'+galleryData[i]['id']+'" src="images/move_left_d.gif" onmousedown="galleryMove('+galleryData[i]['id']+', 1)" onmouseup="stopMove()" alt="<">'+
							'		</td>'+
							'		<td>'+
							'			<div id="galleryDisplay'+galleryData[i]['id']+'" style="overflow: hidden; height: 100px; width: expression(document.body.offsetWidth-590+\'px\');">'+
							'				<div id="galleryConteiner'+galleryData[i]['id']+'" style="position: relative; left: 0; height: 100px; width: 0px;"></div>'+
							'			</div>'+
							'		</td>'+
							'		<td width="20">'+
							'			<img id="galleryMoveRight'+galleryData[i]['id']+'" src="images/move_right.gif" onmousedown="galleryMove('+galleryData[i]['id']+', -1)" onmouseup="stopMove()" alt=">" style="cursor: pointer;">'+
							'		</td>'+
							'	</tr>'+
							'</table>'+
							'<input type="hidden" id="maxPhotos'+galleryData[i]['id']+'" value="0">'+
							'<input type="hidden" id="currentPagen'+galleryData[i]['id']+'" value="1">';
					paramsArray[galleryData[i]['id']] = {'maxPhotos': parseInt(galleryData[i]['elements']), 'currentPhotos': 0, 'currentPagen': 1};
					$("#galleryMainConteiner").append(inner);
					galleryLoad(galleryData[i]['id'], 1);
					GALLERY_LOADING = 0;
				}
			} catch (error) {
				GALLERY_LOADING = 0;
				$("#galleryMainConteiner").html('Content error: "<b>' + error + '</b>"');
			} finally {	
				GALLERY_LOADING = 0;
			}
		}
	);
}

function galleryLoad(id, page) {
	GALLERY_LOADING = 1;
	$.post(
		'/common/getGallery.php', 
		{"ID": id, "PAGEN_1": page}, 
		function(obj) {
			try {
				eval(obj);		
				g_width = 0;
				for (var i in galleryData) {
					inner = '<a href="'+galleryData[i]['detail']+'" target="_blank" title="'+galleryData[i]['title']+'" rel="fancy-tour" >'+
							'	<img src="'+galleryData[i]['preview']+'" class="galleryImage" onmouseover="this.className=\'galleryImage_hover\'" onmouseout="this.className=\'galleryImage\'">'+
							'</a>';
					$("#galleryConteiner"+id).append(inner);
					g_width++;
				}
				if (g_width > 0) {
					if ($("#galleryMoveRight"+id).attr("src") == 'images/move_right_d.gif') {
						$("#galleryMoveRight"+id).css("cursor", 'pointer');
						$("#galleryMoveRight"+id).attr("src", 'images/move_right.gif');
					}
				}
				paramsArray[id]['currentPhotos'] += g_width;
				if (g_width*90 < parseInt($("#galleryDisplay"+id).css("width"))) {
					$("#galleryConteiner"+id).css('width', parseInt($("#galleryDisplay"+id).css("width"))+'px');
					$("#galleryMoveRight"+id).css("cursor", 'default');
					$("#galleryMoveRight"+id).attr("src", 'images/move_right_d.gif');
				} else {
					if (g_width > 0) $("#galleryConteiner"+id).css('width', paramsArray[id]['currentPhotos']*102+'px');
				}
				GALLERY_LOADING = 0;
			} catch (error) {
				GALLERY_LOADING = 0;
				$("#galleryConteiner"+id).html('Content error: "<b>' + error + '</b>"');
			} finally {	
				GALLERY_LOADING = 0;
			}
		}
	);
}

function galleryMove(id, sign) {
	if (((sign > 0) && (parseInt($("#galleryConteiner"+id).css("left")) < 0)) || ((sign < 0) && (Math.abs(parseInt($("#galleryConteiner"+id).css("left"))) < parseInt($("#galleryConteiner"+id).css("width"))-parseInt($("#galleryDisplay"+id).css("width"))))) {
		if (sign > 0) {
			if ($("#galleryMoveRight"+id).attr("src") == 'images/move_right_d.gif') {
				$("#galleryMoveRight"+id).css("cursor", 'pointer');
				$("#galleryMoveRight"+id).attr("src", 'images/move_right.gif');
			}
		} else {
			if ((paramsArray[id]['currentPhotos'] < paramsArray[id]['maxPhotos']) && !GALLERY_LOADING) {
				if (Math.abs(parseInt($("#galleryConteiner"+id).css("left"))) > (paramsArray[id]['currentPhotos']-3)*102-parseInt($("#galleryDisplay"+id).css("width"))) {
					page = paramsArray[id]['currentPagen'];
					new_page = parseInt(page) + 1;
					paramsArray[id]['currentPagen'] = new_page;
					galleryLoad(id, new_page);
				}
			}
			if ($("#galleryMoveLeft"+id).attr("src") == 'images/move_left_d.gif') {
				$("#galleryMoveLeft"+id).css("cursor", 'pointer');
				$("#galleryMoveLeft"+id).attr("src", 'images/move_left.gif');
			}
		}
		$("#galleryConteiner"+id).css("left", parseInt($("#galleryConteiner"+id).css("left"))+sign*speed+'px');
		speed += 2;
		galleryMoveTimer = setTimeout("galleryMove("+id+", "+sign+")", 20)
	} else {
		if (sign > 0) {
			$("#galleryConteiner"+id).css("left", 0);
			if ($("#galleryMoveLeft"+id).attr("src") == 'images/move_left.gif') {
				$("#galleryMoveLeft"+id).css("cursor", 'default');
				$("#galleryMoveLeft"+id).attr("src", 'images/move_left_d.gif');
			}
		} else {
			$("#galleryConteiner"+id).css("left", -parseInt($("#galleryConteiner"+id).css("width"))+parseInt($("#galleryDisplay"+id).css("width")));
			if ($("#galleryMoveRight"+id).attr("src") == 'images/move_right.gif') {
				$("#galleryMoveRight"+id).css("cursor", 'default');
				$("#galleryMoveRight"+id).attr("src", 'images/move_right_d.gif');
			}
		}
	}
}

function stopMove() {
	speed = speedDefault;
	if (window.galleryMoveTimer) clearTimeout(galleryMoveTimer);
}

$(document).ready(function(){
	//initdocument();
});	
function initdocument()
{
	if (window.navigator.userAgent.indexOf ("Opera") >= 0)
	  {
		document.getElementById('imgMain').style.height="378px";
	  }
	else
	if (window.navigator.userAgent.indexOf ("Gecko") >= 0) // (Mozilla, Netscape, FireFox)
	  {        // Их врядли стоит разделять, так как их разделение – только попытка вдохнуть новую жизнь в одного и того же вымирающего динозавра
		document.getElementById('imgMain').style.height="487px";
	  }
}