// -----------------------------------------------------------------------------------
//
//  Photoshoot Handler
//  requires Prototype and Scriptaculous
//
//	Photoshoot v0.unversioned
//
// -----------------------------------------------------------------------------------
/*

	var myPhotoshoot = new photoshoot( { url : 'mytest.json', locale: 'GBP' } );
	
	myPhotoshoot.show();
	myPhotoshoot.hide);
	
*/
// -----------------------------------------------------------------------------------

// Check for prototype and scriptaculous

if((typeof Effect =='undefined') || 
   (typeof Prototype =='undefined') || 
   (typeof Element == 'undefined') || 
   (typeof Element.Methods =='undefined') ||
   parseFloat(Prototype.Version.split(".")[0] + "." +
              Prototype.Version.split(".")[1]) < 1.6) {
	throw("modal generator requires the Prototype JavaScript framework >= 1.6.0 and script.aculo.us");
}

// constructor

function photoshoot (settings) {
	// initialise variables
	this.json = null;
	this.currentPage = 1;
	this.totalPages = 1;
	this.totalImages = 0;

	this.settings = $H( { imagesPerPage: 9, locale: 'GBP', name: '' } ).merge(settings);
	
	this.loadJSON();
}

photoshoot.prototype.loadJSON = function () {
	
	var thisSelf = this;
	
	if (null == this.settings.get('url')) {
		alert('no url specified');
		return false;
	}
	new Ajax.Request(this.settings.get('url'), {
		method: 'post',
		contentType: 'application/json',
		postBody: '{currencyCode: "' + this.settings.get('locale') + '", groupName: "' + this.settings.get('name') + '"}',
		onSuccess: function ( transport, json ) {
			var jsonResponse = json;
			if (!json) {
				var jsonResponse = eval("(" + transport.responseText.substring(1, transport.responseText.length - 1).replace(/\\/g, "") + ")");
			}

			thisSelf.json = jsonResponse;
			
			thisSelf.totalImages = Object.values(thisSelf.json.photoArray).length;
			thisSelf.totalPages = Math.ceil(thisSelf.totalImages / parseFloat(thisSelf.settings.get('imagesPerPage')));
			
			thisSelf.populatePage(1);
		}/*,
		onException: function(req,exception) {
			alert('The request had a fatal exception thrown.\n\n' + exception);
			return true;
		},
		onFailure: function(request) {
			alert('failture' + request.responseText);
		}*/
	});
}


photoshoot.prototype.canGoNext = function () {
	return (this.currentPage < this.totalPages);
}

photoshoot.prototype.canGoPrevious = function () {
	return (this.currentPage > 1);
}

photoshoot.prototype.updatePagination = function () {
	
	if (this.canGoNext()) {
		$('ps_next').show();
	} else {
		$('ps_next').hide();
	}
	if (this.canGoPrevious()) {
		$('ps_previous').show();
	} else {
		$('ps_previous').hide();
	}
	
}

photoshoot.prototype.populatePage = function (page) {
	if(page < 0 || page > this.totalPages) {
		//alert(this.totalPages + 'returning false');
		return false;
	}
	
	this.currentPage = page;
	
	var pageOffset = this.settings.get('imagesPerPage') * (page - 1);
	
	for(var imageNumber = 1; imageNumber < 10; imageNumber++) {
		var offsetImageNumber = imageNumber + pageOffset;
		if(typeof(this.json.photoArray[offsetImageNumber]) != 'undefined') {
			$('ps_img_' + imageNumber).style.display = 'none';
			$('ps_img_' + imageNumber).onload = function() { new Effect.Appear(this); };
			$('ps_img_' + imageNumber).src = this.json.imgPath + this.json.photoArray[offsetImageNumber].thumbnailImg;
		} else {
			$('ps_img_' + imageNumber).style.display = 'none';
			$('ps_img_' + imageNumber).onload = function() { new Effect.Appear(this); };
			$('ps_img_' + imageNumber).src = '/images/photoshoot_thumb_noimage.jpg';
		}
	}
	
	this.selectPhoto(1);
	this.updatePagination();
}

photoshoot.prototype.nextPage = function () {
	this.populatePage(this.currentPage + 1);
	this.updatePagination();
}

photoshoot.prototype.previousPage = function () {
	this.populatePage(this.currentPage - 1);
	this.updatePagination();
}

photoshoot.prototype.selectPhoto = function (photo) {
	var pageOffset = this.settings.get('imagesPerPage') * (this.currentPage - 1);
	var photoOffset = (pageOffset + photo);

	if(photoOffset > this.totalImages) {
		return false;
	}
	
	var photoInfo = this.json.photoArray[photoOffset];
	
	$('ps_mainimage').src = this.json.imgPath + photoInfo.fullsizeImg;
	$('ps_mainimage').alt = photoInfo.altTag;
	$('ps_viewlink').href = photoInfo.viewLink;
	$('ps_detail').innerHTML = photoInfo.productDetail;
	$('ps_price').innerHTML = photoInfo.productPrice;
	
	$('ps_mainimage').style.display = 'none';
	$('ps_mainimage').onload = function() {
		$('ps_mainimage').style.display = 'inline';
		var imgWidth = $('ps_mainimage').offsetWidth;
		$('ps_mainimage').style.display = 'none';
		$('ps_viewlink').style.left = (306 + imgWidth) + 'px';
		$('ps_musthave').style.left = (245 + imgWidth) + 'px';
		new Effect.Appear(this);
	};
		
}

photoshoot.prototype.show = function () {
	$('container_photoshoot_popup').setStyle( {display: 'block'} );
}

photoshoot.prototype.hide = function () {
	$('container_photoshoot_popup').setStyle( {display: 'none'} );
}