$(document).ready(function() {
	
	var photoDataURL = "cubehenge/json.php/LiveServerObjects.LiveServerObjects.search/photos";
	
	$.ajax({
	url: photoDataURL,
	dataType : 'json',
	data : {},
	success : function(data, textStatus){
		loadPhotos(data);
	},
	error : function(x, txt, e){
		//alert(txt+":"+x);
	}
	});
	
	var pageDataURL = escape( 'cubehenge/json.php/LiveServerObjects.LiveServerObjects.search/pages/["title","orderIndex"]' );
	
	$.ajax({
	url: pageDataURL,
	dataType : 'json',
	data : {},
	success : function(data, textStatus){
		listPages(data);
	},
	error : function(x, txt, e){
		//alert(txt+":"+x);
	}
	});
	
	var testimonialCountDataURL = escape( 'cubehenge/json.php/LiveServerObjects.LiveServerObjects.search/testimonials/["lsoUID"]///0/1/1/1' );
	
	$.ajax({
	url: testimonialCountDataURL,
	dataType : 'json',
	data : {},
	success : function(data, textStatus){
		getRandomTestimonial(data);
	},
	error : function(x, txt, e){
		//alert(txt+":"+x);
	}
	});
	
	var contentDataURL = escape( 'cubehenge/json.php/LiveServerObjects.LiveServerObjects.search/pages/["text","title"]/orderIndex/' + pageID );
	
	$.ajax({
	url: contentDataURL,
	dataType : 'json',
	data : {},
	success : function(data, textStatus){
		loadContent(data);
	},
	error : function(x, txt, e){
		//alert(txt+":"+x);
	}
	});
	
});

function loadPhotos( data ){
	
	var photos = data.data;
	
	var photoHTML = "";
	
	for( var i = 0; i <= photos.length - 1; i++ ){
	
		photoHTML += "<a class='photoLinks' href='" + photos[ i ].image + "'><img src='" + photos[ i ].thumb + "' /></a>";
	
	}
	
	$('#photoBox').html( photoHTML );
	
	$('a.photoLinks').zoomimage( { centered:true, controls:false, hideSource:true, preload:'none' } );
	
	$('#photoBox').cycle({
		fx: 'shuffle' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});
	
}

function listPages( data )
{

	var pages = data.data;
	
	var pageHTMLArray = [];
	
	for( var i = 0; i <= pages.length - 1; i++ ){
	
		pageHTMLArray.push( { index:pages[ i ].orderIndex, html:"<a href='?page=" + pages[ i ].orderIndex + "'>" + pages[ i ].title + "</a>" } );
	
	}
	
	pageHTMLArray.sort(function(a,b){return (a.index > b.index);});
	
	var pageHTML = "";
	
	for( var j = 0; j <= pageHTMLArray.length - 1; j++ ){
		
		var spacer = "&nbsp;&nbsp;&nbsp;";
		
		if( j == pageHTMLArray.length - 1 ){
		
			spacer = "";
		
		}
		
		pageHTML += pageHTMLArray[ j ].html + spacer;
	
	}
	
	$('#pageLinks').html( pageHTML );
	$('#footer').html( pageHTML );

}

function getRandomTestimonial( data )
{

	var totalTests = data.totalPages;
	
	var randTest = Math.round( Math.random() * ( totalTests - 1 ) ) + 1;
	
	// Add more randomization.
	// Changing the random approach changes the affinity for different numbers.
	if( ( 10 * Math.random() ) > 5 ){
	
		randTest = Math.ceil(totalTests*Math.random());
	
	}
	
	var testimonialDataURL = escape( 'cubehenge/json.php/LiveServerObjects.LiveServerObjects.search/testimonials/["*"]///0/1/' + randTest + '/1' );
	
	$.ajax({
	url: testimonialDataURL,
	dataType : 'json',
	data : {},
	success : function(data, textStatus){
		loadTestimonial(data);
	},
	error : function(x, txt, e){
		//alert(txt+":"+x);
	}
	});

}

function loadTestimonial( data )
{

	$('#testimonialText').html( "<div class='testimonialName'>" + data.data[ 0 ].title + "</div><br /><img width='100%' src='" + data.data[ 0 ].photo + "' /><br />" + data.data[ 0 ].text );

}

function loadContent( data ){
	
	$('#contentTitle').html( data.data[ 0 ].title );
	$('#contentText').html( data.data[ 0 ].text );
	
}

//

