$(document).ready(ajGal);


function ajGal()
{             
	//check to see if the front gallery exist
	if($("#frontGallery").length > 0) getNewWork();
	
	if($("#portfolio").length > 0) portLoader();
}
                     

function getImages(type, index, func)
{                              
	params = new Object;
	
	if(type!=null)  params.type = type;
	if(index!=null) params.index = index;
	
	$.getJSON('/ajax/getImages.php',params, func);
}

function getNewWork()
{
	getImages('newWork', null, displayNewWork);
}                                              

function createImage(imageData)
{       
	var image = $('<img/>')
		.attr('src', imageData.path);
		//.attr('alt', imageData.description);

	if(imageData.height != null) image.attr('height', imageData.height);
	if(imageData.width != null) image.attr('width', imageData.width);
		
	if(imageData.link != null)
	{
		var linker = $('<a/>').attr('href', imageData.link);
		linker.html(image);
		image = linker;
	
	}                            
	                             
	return image;
	
}

function displayNewWork(data)
{                           
	var gal = $("#frontGallery");
	$.each(data, function(index, value)
	{               
		value = createImage(value);
		value
			.hide()
			.addClass('rotate')
			.prependTo(gal);
		centerItem(value, gal);
			
	});
	startRotatingGallery();
}

var currentImg = 0;
var inMotion =false;

function portLoader()
{
	$('a#prev').bind('click', getPrevImage);
	$('a#next').bind('click', getNextImage);
		
	if($.url.param('imageNumber')!=null) currentImg = $.url.param('imageNumber');
	
	//set first image
	getImages(getType(), currentImg, function(data)
	{                                   
		data = data[0];
		var portfolio = $('#portfolio');
		var img = createImage(data);
		
		var description = $('#description');
		description.empty();
		description.html(data.description);
		
		img.addClass('portImage');  
		img.prependTo(portfolio)
		centerItem(img, portfolio);
	})
	preloadSelect();
}

function getPrevImage()
{
	if(!inMotion)
	{
		currentImg = currentImg - 1;
		moveToNext(currentImg, 'left');
	}
	
	return false;
}

function getNextImage()
{
	if(!inMotion)
	{
		currentImg = currentImg + 1;
		moveToNext(currentImg, 'right');
	}
	return false;
}

function moveToNext(num, direction)
{

    
	inMotion  = true;    
	var speed = 'normal';
	
	var outDir;
	if(direction == 'right') outDir = 'left';
	else outDir = 'right';

	var description = $("#description");
	
	currentImg = num;
	
	$('.portImage').fadeOut(speed,
		function()
		{   
			var description = $("#description");
			description.empty();

			getImages(
				getType(), 
				currentImg,
			    function(data) 
				{
					var portfolio = $("#portfolio");
					
					$('.portImage').remove();
					data = data[0];
					var image = createImage(data);
					image = image.addClass('portImage');

					portfolio.prepend(image.hide());

					centerItem(image, portfolio); 
					description.html(data.description);            
					
                    if(data.index != null) currentImg = data.index;
					
					$('.portImage').fadeIn(speed);
					inMotion = false;
					preloadSelect();
				}
			);
		}
	
	);
		
}


//only preload a few images. +-2 around current image
function preloadSelect()
{
	var type   = getType();
	var select = new Array();
	
	for(var i=-2; i<3; i++)
	{
		if(i!=0)
		{
			select.push(currentImg + i);
		}                    
	}	                     
	
	getImages(type, select, 
		function(data)
		{
			ar = [];
			$.each(data, 
				function(index, value)
				{
					ar.push(value.path);
				}
			);       
			$.preLoadImages(ar);
		}
	);
}
function getType()
{
	var type; 
	if(RegExp('/print/').test(document.location.href)) type ='print';
	if(RegExp('/web/').test(document.location.href)) type ='web';
	if(RegExp('/misc/').test(document.location.href)) type ='misc';
	if(RegExp('/logos/').test(document.location.href)) type ='logos';
	//if(RegExp('/logos/').test(document.location.href)) type ='print';
	return type;	
}

function centerItem(theItem, parent)
{
	if(parent == null) parent = $(window);
	else parent = $(parent);
    var winWidth     = parent.outerWidth();
    var winHeight    = parent.outerHeight();
    var windowCenter = winWidth/2;
    var itemCenter   = $(theItem).width()/2;
    var theCenter    = windowCenter-itemCenter;
    var windowMiddle = winHeight/2;
    var itemMiddle   = $(theItem).height()/2;
    var theMiddle    = windowMiddle-itemMiddle;

	theItem.css('position', 'absolute');
	
    if(winWidth>$(theItem).width()){ //horizontal
        $(theItem).css('left',theCenter);
    } else {
        $(theItem).css('left','0');
    }
    if(winHeight>$(theItem).height()){ //vertical
        $(theItem).css('top',theMiddle);
    } else {
        $(theItem).css('top','0');
    }
}

