var currentGallery;
var imgTotal;
var random;
var lastProject;

function loadGal(galVar,initVar) {

	if(currentGallery != galVar || initVar != null && initVar != random) {
		
		currentGallery = galVar;
		adjustSlide();	
				
		var imagesLoading = 0;
		var randomImage = false;
		
		//if(galVar == 'home' && initVar == null || galVar == 'cvv' && initVar == null || galVar == 'oow' && initVar == null || galVar == 'dch' && initVar == null) {
		if(
			galVar == 'home' && initVar == null || 
			galVar == 'cvv' && initVar == '1' || 
			galVar == 'cvv' && initVar == '5' || 
			galVar == 'oow' && initVar == '2' || 
			galVar == 'oow' && initVar == '0' || 
			galVar == 'dch' && initVar == '2' ||
			galVar == 'osv' && initVar == '0'
		) {
			//randomImage = true;
			randomImage = false;
			fillMode = 'fill';
//alert(galVar + '-' + initVar);
			
		}
		else if(
			//galVar == 'home' && initVar == 'all' || 
			galVar == 'home' || 
			galVar == 'cvv' && initVar == 'all' || 
			galVar == 'oow' && initVar == 'all' || 
			galVar == 'dch' && initVar == 'all' || 
			galVar == 'osv' && initVar == 'all'
		) {		
			randomImage = false;
			//fillMode = 'fit';
			fillMode = 'fill';
			
		}
		else {
			fillMode = 'fit';
		};
		
		if(
			initVar != null && 
			galVar != 'home' && 
			galVar != 'cvv' && 
			galVar != 'oow' && 
			galVar != 'dch' && 
			galVar != 'occ' && 
			galVar != 'osv'
		) {
			$('.middle').animate({'opacity':'0'},250);
			// $('.bottom').fadeOut(250);		
			$('#loading').fadeIn(250);
			$('#loading').css('top','0');
		}
		
		else {
			$('.middle').css('opacity','0');
			// $('.bottom').hide();		
			$('#loading').show();
			//$('#loading').css('top','0');
			$('#loading').css('top','-41px');
		}

		$.ajax({
			url: "/Gallery/" + galVar + "/photos.html",
			success: function(data) {
				imagesLoading = 0;
				
				if(randomImage == true || initVar >= 0) {
					imgTotal = $(data).filter('img').size();
					random = Math.floor(Math.random() * imgTotal)
					if(initVar >= 0) { random = initVar; }
					var src = $(data).filter('img').eq(random).attr('src');
					var title = $(data).filter('img').eq(random).attr('title');
					var longdesc = $(data).filter('img').eq(random).attr('longdesc');
					var alt = $(data).filter('img').eq(random).attr('alt');
					data = '<img src="' + src + '" title="' + title + '" longdesc="' + longdesc + '" alt="' + alt + '">'
				}
		
				$(data).filter('img').each(function() {
					imagesLoading++;
					imgTotal = imagesLoading;
		
					var image = new Image();
		
					image.onload = function() {
						imagesLoading--;
						if(imagesLoading != 0) {
							$('#loading').html('Loading Images... Remaining: ' + imagesLoading);
						};
						if(imagesLoading == 0) {
							$('#loading').html('All Images Loaded');
							dataParsed(data);
						};
					};
		
					image.src = $(this).attr('src');
				});
			}
		});
	
	};
	
}

function dataParsed(data) {
	$('#galNav').fadeIn();
	$('.expcol').fadeIn();
	if(imgTotal == 1) {
		// $('#galNav').hide();
		// $('.expcol').hide();	
		$('.top').show();
		if(galleryMode == false) {
			$('.middle').animate({'opacity':'1'});
		};
	};
	$('.bottom').fadeIn();
	$('#loading').fadeOut();
	$('#bigDiv').html(data);
	currentImage = 1;
	$('#bigDiv > img').css('display','none');
	$('#bigDiv > img:nth-child(1)').fadeIn(fadeDuration);
	
	numGallery();
	updateCaption();
	
	$('#galMenu').hide();
	
	setTimeout(function(){
		adjustSlide();
		adjustHeights();
		modalHeights();	
		setupButtonDisabler();
	},100);
}

function setupButtonDisabler(){

	$('.expGallery').unbind('mouseenter mouseleave');
	$('.galControls li a').css({'opacity' : 1});
	$('.expcol a').css({'opacity' : 1});
	$('.galSelect a').css({'opacity' : 1});
	$('.infoButton a').css({'opacity' : 1});
	$('.buttonDisabler').hide();

	if(galleryMode == false) {
		
		$('.buttonDisabler').show();
		
		$('.galControls li a').css({'opacity' : .35});
		$('.expcol a').css({'opacity' : .35});
		$('.galSelect a').css({'opacity' : .35});
		$('.infoButton a').css({'opacity' : .35});
		
		$('.expGallery').unbind('mouseenter mouseleave').hover(
			function(){
				$('.galControls li a').animate({
					'opacity' : 1
				},250);
				$('.expcol a').animate({
					'opacity' : 1
				},250);
				$('.galSelect a').animate({
					'opacity' : 1
				},250);
				$('.infoButton a').animate({
					'opacity' : 1
				},250);
			},
			function() {
				$('.galControls li a').animate({
					'opacity' : .35
				},250);
				$('.expcol a').animate({
					'opacity' : .35
				},250);
				$('.galSelect a').animate({
					'opacity' : .35
				},250);
				$('.infoButton a').animate({
					'opacity' : .35
				},250);
			}
		);
	}
	
}

function updateCaption() {

	var proj = $('#bigDiv > img:nth-child(' + currentImage + ')').attr('title');
	var loca = '';
	var desc = $('#bigDiv > img:nth-child(' + currentImage + ')').attr('longdesc');
	var date = $('#bigDiv > img:nth-child(' + currentImage + ')').attr('alt');
	var link = '#'
	
	lastProject = proj;
	
	$('#galTitle .desc span').fadeOut(fadeDuration / 2,function(){

		$('.numContainer').html('');
	
		var CaptionPrefix = '';
		
		switch(proj){
			
			case 'sculpture':
				proj = 'David Hovey Sculpture';
				loca = 'Scottsdale, Arizona';
				link = '/';
			break;
			
			case 'osv':
				proj = 'Optima Sonoran Village';
				loca = 'Scottsdale, Arizona';
				link = '/Homes/SonoranVillage/';
			break;
			
			case 'occ':
				proj = 'Optima Center Chicago';
				loca = 'Chicago, Illinois';
				link = '/Homes/OptimaCenter/';
			break;
			
			case 'oow':
				proj = 'Old Orchard Woods';
				loca = 'Skokie, Illinois';
				link = '/Homes/SkokieCondos/';
			break;
			
			case 'cvv':
				proj = 'Camelview Village';
				loca = 'Scottsdale, Arizona';
				link = '/Homes/ScottsdaleCondos/';
			break;
			
			case 'dch':
				proj = 'DCHGlobal Relic Rock';
				loca = 'Scottsdale, Arizona';
				link = '/Homes/DCHGlobal/';
			break;

			case 'obt':
				proj = 'Optima Biltmore Towers';
				loca = 'Phoenix, Arizona';
				link = '/Company/portfolio.html';
			break;
			
			case 'hor':
				proj = 'Optima Horizons';
				loca = 'Evanston, Illinois';
				link = '/Company/portfolio.html';
			break;
			
			case 'sr':
				proj = 'Sterling Ridge';
				loca = 'Scottsdale, Arizona';
				link = '/Company/portfolio.html';
			break;
			
			case 'cc':
				proj = 'Cloud Chaser';
				loca = 'Scottsdale, Arizona';
				link = '/Company/portfolio.html';
			break;
			
			case 'ov':
				proj = 'Optima Views';
				loca = 'Evanston, Illinois';
				link = '/Company/portfolio.html';
			break;
			
			case 'ot':
				proj = 'Optima Towers';
				loca = 'Evanston, Illinois';
				link = '/Company/portfolio.html';
			break;
			
			case 'vr':
				proj = 'Vanishing Rain';
				loca = 'Scottsdale, Arizona';
				link = '/Company/portfolio.html';
			break;
			
			case 'sc':
				proj = 'Shadow Caster';
				loca = 'Scottsdale, Arizona';
				link = '/Company/portfolio.html';
			break;
			
			case 'mp':
				proj = 'Michigan Place';
				loca = 'Chicago, Illinois';
				link = '/Company/portfolio.html';
			break;
			
			case 'ta':
				proj = 'The Arbors';
				loca = 'Highland Park, Illinois';
				link = '/Company/portfolio.html';
			break;
			
			case 'tc':
				proj = 'The Cloisters';
				loca = 'Glenview, Illinois';
				link = '/Company/portfolio.html';
			break;
			
			case 'dc':
				proj = 'Dewes Court';
				loca = 'Glenview, Illinois';
				link = '/Company/portfolio.html';
			break;
			
			case 'lc':
				proj = 'Lake Courts';
				loca = 'Wilmette, Illinois';
				link = '/Company/portfolio.html';
			break;
			
			case 'ocw':
				proj = 'Optima Center Wilmette';
				loca = 'Wilmette, Illinois';
				link = '/Company/portfolio.html';
			break;
			
			case 'rb':
				proj = 'Ravine Bluff';
				//loca = 'Winnetka, Illinois';
				loca = 'Illinois';
				link = '/Company/portfolio.html';
			break;
			
			case 'hr':
				proj = 'Hedgerow';
				loca = 'Winnetka, Illinois';
				link = '/Company/portfolio.html';
			break;
			
			case 'cm':
				proj = 'Coromandel';
				loca = 'Deerfield, Illinois';
				link = '/Company/portfolio.html';
			break;
			
			case 'ml':
				proj = 'Meadowlake';
				loca = 'Northfield, Illinois';
				link = '/Company/portfolio.html';
			break;
			
			case 'shr':
				proj = '1618 Sheridan';
				loca = 'Wilmette, Illinois';
				link = '/Company/portfolio.html';
			break;
			
			case 'npe':
				proj = 'North Pointe';
				loca = 'Evanston, Illinois';
				link = '/Company/portfolio.html';
			break;
			
			case 'con':
				proj = 'The Courts of Northbrook';
				loca = 'Northbrook, Illinois';
				link = '/Company/portfolio.html';
			break;
			
			case 'fst':
				proj = '1770 First Street';
				loca = 'Highland Park, Illinois';
				link = '/Company/portfolio.html';
			break;
			
			case 'stj':
				proj = '2020 St. Johns';
				loca = 'Highland Park, Illinois';
				link = '/Company/portfolio.html';
			break;
			
			case 'vrn':
				proj = '630 Vernon';
				loca = 'Glencoe, Illinois';
				link = '/Company/portfolio.html';
			break;
			
			case 'mic':
				proj = '840 Michigan';
				loca = 'Evanston, Illinois';
				link = '/Company/portfolio.html';
			break;
			
			case 'hod':
				proj = '17905 Hood';
				loca = 'Homewood, Illinois';
				link = '/Company/portfolio.html';
			break;
			
			case 'lst':
				proj = '4900 N. Lester';
				loca = 'Chicago, Illinois';
				link = '/Company/portfolio.html';
			break;
			
			case 'elm':
				proj = '575 Elm Street';
				loca = 'Winnetka, Illinois';
				link = '/Company/portfolio.html';
			break;
			
			case 'mpl':
				proj = '300 N. Maple';
				loca = 'Oak Park, Illinois';
				link = '/Company/portfolio.html';
			break;
			
			case 'est':
				proj = '1605 E. 50th Street';
				loca = 'Chicago, Illinois';
				link = '/Company/portfolio.html';
			break;
			
		}
				
		$('#galTitle .desc').html(
			'<span class="descProj">' + proj + '</span><span class="descBull">&#149;</span><span class="descLoca">' + loca + '</span><span class="descBull">&#149;</span><span class="descDate">' + date + '</span><span class="descDesc">' + desc + '</span>'
		);
		
		$('.infoButton a').attr('href',link);
	
	});
	
	$('#galNav #galIndex #display a').html(currentImage);
	$('#galNums li').removeClass('activeNumber');
	$('#galNums li').eq(currentImage - 1).addClass('activeNumber');

}

function showDate() {
	var dateNumber = $('.numContainer').html();
	var newDateCode = '';
	for (i=0;i<dateNumber.length;i++) {
		newDateCode += '<div id="num' + dateNumber.substring(i,i+1) + '"></div>';
	}
	dateNumber = $('.numContainer').html(newDateCode);
}

function calcimgTotal() {
	imgTotal = $('#bigDiv img').size();
	currentImage = 1;
}

function numGallery() {

	$('#galNums').html('')
	
	//for( x = 1 ; x <= $('#bigDiv img').size() ; x ++ ) {
	var imageCounter =  $('#bigDiv img').size();

	for( x = 1 ; x <= 6; x ++ ) {

		if ( x <= imageCounter ) {

			$('#galNums').append('<li>' + x + '</li>');

		} else {

			$('#galNums').append('<li></li>');

		};
	};
	
	$('#galNums li').click(function(){
		$('#galNums li').removeClass('activeNumber');
		slideJump( $(this).html() );
		$(this).addClass('activeNumber');
	});
		
	$('#galNums').css({	
		'margin-top' : - Math.ceil($('#galNums').children('li').size() / 3) * ( $('#galNums li').outerHeight() + 1 ) - 44 + 'px'
	});
	
	$('li#galIndex #galNums li:nth-child(3n+3)').css({
		'margin-right' : '0'
	})
}

function newImage() {

	$('#bigDiv > img:nth-child(' + currentImage + ')').fadeIn(fadeDuration);
	adjustSlide();
	updateCaption();

}

function slideBackward() {
	
	$('#bigDiv > img:nth-child(' + currentImage + ')').fadeOut(fadeDuration);
	
	if(currentImage > 1) {
		currentImage--
	}
	
	else if(currentImage == 1) {
		currentImage = imgTotal;
	};
	
	newImage();		
			
};

function slideForward() {	
		
	$('#bigDiv > img:nth-child(' + currentImage + ')').fadeOut(fadeDuration);
	
	if(currentImage < imgTotal) {
		currentImage++
	}
	
	else if(currentImage == imgTotal) {
		currentImage = 1;
	};
	
	newImage();	
		
};

function slideJump(index) {	
		
	if (index != currentImage) {
		
		$('#bigDiv > img:nth-child(' + currentImage + ')').fadeOut(fadeDuration);
		
		currentImage = index;
					
		newImage();
		
	};
		
};

