// JavaScript Document
var ImageIndex			=	0;		//текущая картинка
var currentpos			=	0;
var currentthumbpos	=	0;
var moveamount			=	106;
//var maxthumbvisible = 5; //Define how many thumbnails will be visible at one time in the thumbbox--for now this should only be 3 since it's actually the css that controls the size of the viewable box

var moving = false;
var preloadImage		= 0;		//Preload images (0 or 1):
var loadedImages 		= new Array();	//уже подгруженные картинки
var fadeout					= 1;

var transition_speed	=	500;	//How fast you want the animations to render
var fade_speed				= 300;	//How fast you want the photos to fade in and out
//var galleryStyle = "lightgray";


function imageLoad(){	
//	if (document.getElementById) {
//				if(current_imgid!=img_id){	
	//var fade_image = new Fx.Opacity('imgloader', {duration:fade_speed });
	//fade_image.hide();
	//alert(fade_speed);
	$('#imgloader').fadeOut(fade_speed);
	$('#imgtitle').fadeOut(transition_speed);
	//var fade_image_title = new Fx.Opacity('imgtitle', {duration:transition_speed });
	//fade_image_title.hide();
	
	if (! loadedImages[ImageIndex]) {
		tmp = new Image()
		tmp.src = MyGallery[ImageIndex][0];
		loadedImages[ImageIndex] = tmp
		//alert(ImageIndex);
		//mynewimage.onload = function(){imageLoad();} 
	}
	$('#imgloader').html(getImgCode(MyGallery[ImageIndex]));	
	$('#photocount').html((ImageIndex+1)+"/"+MyGallery.length);
	$('#imgtitle').html(MyGallery[ImageIndex][4]);	//+" "+MyGallery[img_id][7];


	//fade_image.toggle();
	//fade_image_title.toggle();
	$('#imgloader').fadeIn(fade_speed);
	$('#imgtitle').fadeIn(transition_speed);

	
//	var iptch=document.getElementById('iptc_info');	
//					iptch.innerHTML=returniptc(MyGallery[imgindex]);

/*
	var fader = new Fx.Opacity('loadarea', {duration:fade_speed});
	fader.hide();


		var fademe = new Fx.Opacity('iptc_info', {duration:transspeed });
		fademe.custom(0,.75);


	//main_image_wrapper
	//fader.toggle();

		var myEffects = new Fx.Styles(loadarea, {duration: transspeed, transition: Fx.Transitions.linear}).addEvent('onComplete', function(){
			function(){areweready(loadarea, imgindex, img_id);																															 
		});
		var myFx = new Fx.Style('imgloader', 'opacity').addEvent('onComplete', function(){
			myEffects.start({
				'height': height,
				'width': width
			});
		});
		myFx.start(1,0);



	var setfirstheight = new Fx.Height('main_image_wrapper',{duration:transspeed});
	var setfirstwidth =  new Fx.Width ('main_image_wrapper',{duration:transspeed});
	setfirstwidth.custom(currentwidth,imggallery[0][1]);
	setfirstheight.custom(currentheight,imggallery[0][2]);

	firstimageload.onload=function(){
		var firstimg=document.getElementById('imgloader');
		firstimg.innerHTML=returnimgcode(imggallery[0]);
		var firsttitle=document.getElementById('imgtitle');
		firsttitle.innerHTML="<strong>"+tempgallery[0][4]+"</strong> "+tempgallery[0][7];
		
		fadefirst.toggle();
		firsttitlefade.toggle();
	}





//		var resizeDivHeight = new Fx.Height('main_image_wrapper',{duration:transspeed, onComplete: function(){areweready(loadarea, imgindex, img_id);currentheight=height;} });
//		var resizeDivWidth =  new Fx.Width ('main_image_wrapper',{duration:transspeed, onComplete: function(){areweready(loadarea, imgindex, img_id);currentwidth=width;} });

		//var fader = new Fx.Opacity('imgloader', {duration:fadespeed, onComplete: function(){resizeDivWidth.custom(currentwidth,width);resizeDivHeight.custom(currentheight,height);} });
		//fader.toggle();	


	var fadeiptc = new Fx.Opacity('iptc_btn', {duration:transition_speed });
		fadeiptc.hide();
		fadeiptc.toggle();
	if(fadeout==0){
		var fademe = new Fx.Opacity('iptc_info', {duration:transition_speed });
		fademe.hide();
		fadeout=0;
		}else{
			var fademe = new Fx.Opacity('iptc_info', {duration:transition_speed });
			fademe.toggle();
			fadeout=1
	}
*/

}

function getImgCode(theimg){	
	if (theimg[1] == "") return "";	
	result = '<img src="'+theimg[0]+'" border="0" id="'+theimg[8]+'" />';
	//if (theimg[1] != "") imghtml += ''
	return result;
}

/*
function returniptc(theimg){
	var iptchtml = ''+
	'<div class=\'iptc_left\'><br/>'+
			'	Title <br/>'+
			'	Author <br/>'+
			'	Copyright <br/><br/>'+
			'	Description <br/><br/>'+
			'</div>'+
			'<div class=\'iptc_right\'><br/>'+
			'	'+theimg[4]+'<br/>'+ //title
			'	'+theimg[5]+'<br/>'+ // author
			'	'+theimg[6]+'<br/><br/>'+ //copyright
			'	'+theimg[7]+'<br/><br/>'+ // description
			'</div>';
	return iptchtml
}
*/

function checkbutton(moveTo){	
	if (moveTo == '<') {
		if ( currentpos > 0 ) {
			currentpos--;
			movethumbs(1);
		}
	} else {
		if ( currentpos < thumbmax ) {
			currentpos++;
			movethumbs(-1);
		}
	}
	
//	if (! moving ) {
// if animagic is still moving the image..don't update the current position till it's done
	thumbmax=(MyGallery.length-1);
/*
	if ( currentpos == 0 ) {
		mm_shl('back','hidden');
		mm_shl('more','visible');
	} else if ( currentpos < thumbmax ) {	// - maxthumbvisible
		mm_shl('back','visible');
		mm_shl('more','visible');
	} else {
		mm_shl('back','visible');
		mm_shl('more','hidden');
	}
*/
}

//навигатор в окне
function checknext(){
	thumbmax=(MyGallery.length-1);	
	if ( ImageIndex < 1 ) {
		mm_shl('prev','hidden');
		mm_shl('next','visible');
	} else if ( ImageIndex <  thumbmax ) {
		mm_shl('prev','visible');
		mm_shl('next','visible');
	} else {
		mm_shl('prev','visible');
		mm_shl('next','hidden');
	}
	//movethumbs(0+ImageIndex);
}


function movethumbs(moveTo){
	move=(currentthumbpos+moveTo*moveamount);
	//alert(move);
	//$('#thumbgall').fadeOut(transition_speed);
	//var movethumbs = new Fx.Styles('thumbgall', {duration: transition_speed, transition: Fx.Transitions.quadOut});
	//$('#thumbgall').custom({ left: [currentthumbpos, move]});
	$('#thumbgall').css({"left":move});
	currentthumbpos += moveTo*moveamount;
}


function mm_shl() { //v6.0
	var obj,args=arguments;
	if ((obj=MM_findObj(args[0]))!=null) {
		if (obj.style) {
			obj=obj.style;
		}
		obj.visibility=args[1];
	}
}


function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

var thumbopen=0

function thumbs(){
		var resizethumb = new Fx.Height('thumbhide',{duration:transition_speed, transition: Fx.Transitions.quadOut});
		var movethumbs = new Fx.Styles('thumbbox', {duration: transition_speed, transition: Fx.Transitions.quadOut});
		if(thumbopen==1){				
			resizethumb.custom(0,119);
			movethumbs.custom({ top: [-120, 0]});
			thumbopen=0
		} else {
			resizethumb.custom(119,0);
			movethumbs.custom({ top: [0, -120]});
			thumbopen=1
		}
}

// JavaScript Document
function initGallery () {	
	if (preloadImage == 1){
		for (x=0; x < MyGallery.length; x++){
			tmp = new Image()
			tmp.src = MyGallery[x][0]
			loadedImages[x] = tmp
		}
	}	
		//window.onLoad=function(){showImage(0);}	//showImage(0)() loadfirstimage()
	showImage(0)
}


function showImage(moveTo){	
	if (moveTo == "<") {
		ImageIndex--;
	} else if (moveTo == ">") {	//>
		ImageIndex++;
	} else {
		ImageIndex = moveTo;
	}
	checknext();
	//var fade_image_title = new Fx.Opacity('imgtitle', {duration:transition_speed });	
	//var firsttitlefade = new Fx.Opacity('imgtitle', {duration:transition_speed });
	//firsttitlefade.hide();
	imageLoad();	
	//firsttitlefade.toggle();
}
