// JavaScript Document

// HOME PAGE BG SLIDESHOW

function ssPlay() { ssInterval = setInterval("nextImage()", 8000); }

function nextImage(){
	if (i < t) {i++;} else { i = 0; }
	if (n < t) {n++;} else { n = 0; }
	changeImage(i,n);
}

function changeImage(i,n){
	if(toggle == 0){
		$('div#header-image3').fadeIn(400);
		$('div#header-image2').fadeOut(400, function () {
			$('div#header-image2').css('background-image','url('+photos[n].image+')');
		});				
		toggle = 1;
	}
	else {
		$('div#header-image2').fadeIn(400);
		$('div#header-image3').fadeOut(400, function () {
			$('div#header-image3').css('background-image','url('+photos[n].image+')');
		});
		toggle = 0;
	}
}


// PORTFOLIO SLIDESHOWS

function getPhotoCount (xPhotos, xProject) {
	
	// return the count of photos for a chosen project, based on the data array
	for (i in photos) {
		if (photos[i].project == xProject) { 
			return photos[i].photocount;
		}
	}
	
}


function showProject(xProject,xSlideCount){
		
	// show project text
	$('#txt-' + xProject).fadeIn();
	
	
	// show photos
	if ($('#ps-' + xProject + ' div').size() > 0) { // already constructed
		
		// reset controller buttons
		$('#ps-' + xProject + ' a.btn-play').hide(); 
		$('#ps-' + xProject + ' a.btn-pause').show(); 

		$('#ps-'  + xProject).fadeIn();
		$('#ps-' + xProject + ' .slides').cycle('resume');
	
	} else { 
		
		// construct slides markup, based on number of photos spec'd for this project
		var xSlideMarkup = "";
		for (i=1;i<=xSlideCount;i++) {
			xSlideMarkup = xSlideMarkup + '<div><img src="images/portfolio/' + xProject + '_0' + i + '.jpg" /></div>'
		}
		xSlideMarkup = '<div class="slides">' + xSlideMarkup + '</div>';
		// add to dom
		$('#ps-' + xProject).append(xSlideMarkup);
		
		if(xSlideCount == 1) { // no slideshow needed if one image...
			$('#ps-'+xProject).fadeIn();
		} else {
			initSlideshow(xProject);
		} 
	} 
	
}


function initSlideshow(xProject) { 
	
	var xID = "ps-" + xProject;
	
	// insert controls
	var playerControls = '<div class="controls-wrap"><div class="controls"><a class="btn-previous" href="#">Previous</a><a class="btn-pause" href="#">Pause</a><a class="btn-play" href="#">Play</a><a class="btn-next" href="#">Next</a></div></div>';
	$('#' + xID + ' .slides').before(playerControls);

	// init slideshow
	$('#' + xID + ' .slides').cycle({ 
		fx: 'fade',
		timeout: 5000, // for each slide
		delay:3000 // extra for first slide
	});
	setTimeout( function() { $('#'+xID).fadeIn(); }, 1000 );

	// set control btn actions
	$('#' + xID + ' a.btn-previous').click(function() { 
		$('#' + xID + ' .slides').cycle('prev'); 
	});
	$('#' + xID + ' a.btn-next').click(function() { 
		$('#' + xID + ' .slides').cycle('next'); 
	});
	$('#' + xID + ' a.btn-pause').click(function() { 
		$('#' + xID + ' a.btn-pause').hide(); 
		$('#' + xID + ' a.btn-play').show(); 
		$('#' + xID + ' .slides').cycle('pause'); 
	});
	$('#' + xID + ' a.btn-play').click(function() { 
		$('#' + xID + ' a.btn-play').hide(); 
		$('#' + xID + ' a.btn-pause').show(); 
		$('#' + xID + ' .slides').cycle('resume'); 
	});
	
}




