function arrayShuffle(){
	var tmp, rand;
	for(var i=0; i<this.length; i++) {
		rand = Math.floor(Math.random() * this.length);
		tmp = this[i];
		this[i] = this[rand];
		this[rand] = tmp;
	}
}
Array.prototype.shuffle = arrayShuffle;

var galleryOff = false;
var maxProjects = 0;
var maxPacks = 0;
var curPack = 0;
var arrowsLocked = false;
var tempTitleProject = "";

$(document).ready(function() {

	$('div.galleryElementWrapper').fadeTo(0,0);
	$('div.galleryButton').hide().css('top','4px');
	maxProjects = $('div.galleryElementWrapper').length;
	maxPacks = Math.ceil(maxProjects/10);

	if (maxProjects > 0) showPack(1);

	$('div.galleryText').fadeTo(0,0);

	$('div.galleryElementWrapper').bind({
		click: function(){
		},
		mouseenter: function(){
			$(this).css("cursor","pointer");
			$(this).children('div.galleryTextWrapper').children('div.galleryText').animate({
				opacity: 0.80,
				top: -1
			}, 300, function() {
				$(this).parent().parent().children('div.galleryButton').show();
			});
		},
		mouseleave: function(){
			$(this).css("cursor","auto");
			$(this).children('div.galleryTextWrapper').children('div.galleryText').stop();
			$(this).children('div.galleryTextWrapper').children('div.galleryText').animate({
				opacity: 0.0,
				top: 171
			}, 200, function() {
				$(this).parent().parent().children('div.galleryButton').hide();
			});
		}
	});

	$('div.galleryArrowClass').bind({
		mouseenter: function(){ $(this).css("cursor","pointer"); },
		mouseleave: function(){ $(this).css("cursor","auto"); }
	});
	$('div#galleryArrowLeftLayer').bind({ click: function(){ if (!arrowsLocked) showPackPrev(); }});
	$('div#galleryArrowRightLayer').bind({ click: function(){ if (!arrowsLocked) showPackNext(); }});

	// arrows loading
	$('div.galleryArrowClass img').preload({
		find:'_off.gif',
		replace:'_on.gif'
	});
	// arrows rollover
	$('div.galleryArrowClass img').hover(function(){
		this.src = this.src.replace('_off.gif','_on.gif');
	},function(){
		this.src = this.src.replace('_on.gif','_off.gif');
	});

});

function showPackPrev() {
	if (curPack > 1) showPack(curPack-1); else showPack(maxPacks);
}

function showPackNext() {
	if (curPack < maxPacks) showPack(curPack+1); else showPack(1);
}

function showPack(num) {

	// hide all projects + move layer
	$('div.galleryElementWrapper').fadeTo(0,0);
	$('div#galleryWrapper').css('top', '-' + (380*(num-1)) + 'px');

	// number of picts in this pack
	var numberOfProjectsinThisPack = maxProjects - (10 * (num-1));
	if (numberOfProjectsinThisPack > 10) numberOfProjectsinThisPack = 10;

	var indexA = new Array();
	for (i=0; i<=numberOfProjectsinThisPack; i++) {
		indexA[i-1] = i;
	}
	indexA.shuffle();

	var fadeintimer = 175;
	var i = 0;
	arrowsLocked = true;
	for (i=0; i<numberOfProjectsinThisPack; i++) {
		tempObj = "div#gw" + (indexA[i]+(10 * (num-1)));
		setTimeout("$('" + tempObj + "').fadeTo(750,1);",fadeintimer);
		fadeintimer += 250;
	}
	setTimeout("arrowsLocked = false;",fadeintimer);

	curPack = num;

}

