images = new Array();
descriptions = new Array();
imgThemes = new Array();
themes = new Array();

noImages = 0;
currentImg = 0;
allLoaded = false;
imgLoaded = [true];

$j(window).load(function(){

$j('body').append('<div class="galleries" style="display: none;"></div>')
$j('.galleries').load('/gallery-src .photoGalleryElement', function(){
	currimg = 0;
	currthm = 0;
	$j('.photoGalleryElement').each(function(){
		themes[currthm] = currimg;
		$j(this).children('.thumbShadow').each(function(){
			images[currimg] = $j(this).find('a').attr('href');
			descriptions[currimg] = $j(this).find('p').text();
			imgThemes[currimg] = currthm;
			currimg++;
		})
		currthm++;
	})
	$j('.images').html('<img alt="" style="left: 0" src="'+images[0]+'" />');
	$j('.info p').html(descriptions[0]);
	noImages = images.length;
	for (j=1;j<noImages; j++){
		imgLoaded[j] = false;
	}
	$j('#quantity').html('1 of ' + noImages);
	loadImage(currentImg);
});


// close button
	if(document.referrer.indexOf(window.location.host) > 0) {
		$j('#info_bar .close').click(function(){
			$j(this).attr('href', '#')
			history.back();
			return false;
		});
	}

// left and right nav buttons
	$j('#l_arrow').click(function(){
		iSlideRight();
		return false;
	});
	$j('#r_arrow').click(function(){
		iSlideLeft();
		return false;
	});

// theme navigation
	$j('#gallery_nav a:eq(0)').click(function(){
		if (currentImg != themes[0]) {
			if(imgLoaded[themes[0]]) {
				$j('.images_wrap').stopTime('slideshow');
				$j('.images_wrap').oneTime(5000, 'slideshow', startSlideshow);
				nextImg = themes[0];
				moveImage(nextImg, 'fw');
			}
		}
		return false;
	});
	$j('#gallery_nav a:eq(1)').click(function(){
		if (currentImg != themes[1]) {
			if(imgLoaded[themes[1]]) {
				$j('.images_wrap').stopTime('slideshow');
				$j('.images_wrap').oneTime(5000, 'slideshow', startSlideshow);
				nextImg = themes[1];
				moveImage(nextImg, 'fw');
			}
		}
		return false;
	});
	$j('#gallery_nav a:eq(2)').click(function(){
		if (currentImg != themes[2]) {
			if(imgLoaded[themes[2]]) {
				$j('.images_wrap').stopTime('slideshow');
				$j('.images_wrap').oneTime(5000, 'slideshow', startSlideshow);
				nextImg = themes[2];
				moveImage(nextImg, 'fw');
			}
		}
		return false;
	});

});



function loadImage(i) {
	if(i+1 < noImages) {
		$j('.images img:eq('+i+')').after('<img class="image'+(++i)+'" alt="" src="'+images[i]+'">');
		$j('.image'+i).load(function(){
			imgLoaded[i] = true;
			loadImage(i);
			if(i == noImages-1)
				allLoaded = true;
				startSlideshow();
		});
	}
}
function startSlideshow() {
	if(allLoaded) {
		$j('.images_wrap').everyTime(5000, 'slideshow', function(){
			nextImg = currentImg + 1;
			if(nextImg == noImages) nextImg = 0;
			moveImage(nextImg, 'fw');
		});
	}
}
function iSlideLeft() {
	$j('.images_wrap').stopTime('slideshow');
	$j('.images_wrap').oneTime(5000, 'slideshow', startSlideshow);
	if((currentImg < noImages-1) && imgLoaded[currentImg+1]) {
		nextImg = currentImg + 1;
	} else if(currentImg == noImages-1) {
		nextImg = 0;
	}
	moveImage(nextImg, 'fw')
}
function iSlideRight() {
	$j('.images_wrap').stopTime('slideshow');
	$j('.images_wrap').oneTime(5000, 'slideshow', startSlideshow);
	if(currentImg > 0){
		nextImg = currentImg - 1;
	} else if((currentImg == 0) && imgLoaded[noImages-1]) {
		nextImg = noImages-1;
	}
	moveImage(nextImg, 'bk')
}
function moveImage(nextImg, dir) {
	if (dir == 'fw') {
		$j('.images img:eq('+nextImg+')').css({left: '1330px'}).animate({left: '0px'});
		$j('.images img:eq('+currentImg+')').animate({left: '-1330px'});
	} else if (dir == 'bk') {
		$j('.images img:eq('+nextImg+')').css({left: '-1330px'}).animate({left: '0px'});
		$j('.images img:eq('+currentImg+')').animate({left: '1330px'});
	} else {
		return false;
	}
	currentImg = nextImg;

	$j('#gallery_nav a').removeClass('on');
	$j('#gallery_nav a:eq('+imgThemes[currentImg]+')').addClass('on');
	$j('.info p').html(descriptions[currentImg]);
	if( $j('.info p').height() > 16 ) {
		$j('.info p').animate({marginTop: '-7px'});
	}  else {
		$j('.info p').animate({marginTop: '0px'});
	}
	$j('#quantity').html(currentImg+1 + ' of ' + noImages)
}








