//==================================
//set product images start variables

var overlayOpacity = 0.3;	
var overlayDuration = 0.2;
var animateDuration = 0.3;
var resizeSpeed = 7;
var borderSize = 20;

if(resizeSpeed > 10){ resizeSpeed = 10;}
if(resizeSpeed < 1){ resizeSpeed = 1;}
resizeDuration = (11 - resizeSpeed) * 0.15;

var productViewMaster = 'images';
var loadingImage = '/images/productViewLoading.gif';

Object.extend(Element, {
	setWidth: function(element,w) {
	   	element = $(element);
    	element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   		element = $(element);
    	element.style.height = h +"px";
	},
	setTop: function(element,t) {
	   	element = $(element);
    	element.style.top = t +"px";
	},
	setLeft: function(element,l) {
	   	element = $(element);
    	element.style.left = l +"px";
	},
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src; 
	}
});

var productLargeView = Class.create();
productLargeView.prototype = {

	initialize: function()
	{
		if($(productViewMaster))
		{
			this.setupImages();
			
			var objBody = document.getElementsByTagName("body").item(0);
			
			var objOverlay = document.createElement("div");
			objOverlay.setAttribute('id','productViewOverlay');
			objOverlay.style.display = 'none';		
			objBody.appendChild(objOverlay);
			
			var objProductView = document.createElement("div");
			objProductView.setAttribute('id','productView');
			objProductView.style.display = 'none';		
			objBody.appendChild(objProductView);					
			
			var objProductViewCont = document.createElement("div");
			objProductViewCont.setAttribute('id','productViewOuterContainer');		
			objProductView.appendChild(objProductViewCont);	
			
			var objSampleSwitchContainer = document.createElement("div");
			objSampleSwitchContainer.setAttribute('id','sampleSwitchContainer');
			Element.setTop(objSampleSwitchContainer,5);
			Element.setStyle(objSampleSwitchContainer,{lineHeight:15+'px'});
			objProductViewCont.appendChild(objSampleSwitchContainer);				
			
			var objImageContainer = document.createElement("div");
			objImageContainer.setAttribute('id','productViewImageContainer');
			objProductViewCont.appendChild(objImageContainer);			
			
			var objLoading = document.createElement("div");
			objLoading.setAttribute('id','productViewLoading');
			objImageContainer.appendChild(objLoading);	
			Element.setStyle(objLoading,{opacity: 0.7});
			
			var objLoadingImage = document.createElement("img");
			objLoadingImage.setAttribute('src', loadingImage);
			objLoading.appendChild(objLoadingImage);
		
			var objViewImage = document.createElement("img");
			objViewImage.setAttribute('id','productViewImage');
			objImageContainer.appendChild(objViewImage);
			
			var objViewCloseLink = document.createElement("a");
			objViewCloseLink.setAttribute('id','productViewClose');
			objViewCloseLink.setAttribute('href','#');
			objViewCloseLink.setAttribute('title','Close this expanded product view');
			objViewCloseLink.onclick = function() { myProductView.end(); return false; }
			objImageContainer.appendChild(objViewCloseLink);
			
			var objViewClose = document.createElement("img");
			objViewClose.setAttribute('alt','Close');
			objViewClose.setAttribute('src','/images/close_button.gif');		
			objViewCloseLink.appendChild(objViewClose);
						
			//observe window resizes
			Event.observe(window, 'resize', this.realign, false);
		}		
	},
	
	setupImages: function()
	{	
		if (!document.getElementsByTagName){ return; }
		imagelinks = $(productViewMaster).getElementsByClassName('productLargeView');

		// loop through all anchor tags
		for (var i=0; i<imagelinks.length; i++){
			var imageLink = imagelinks[i];
			imageLink.onclick = function () {myProductView.start(); return false;}
		}
		
		imageLink.onclick = function () {myProductView.start(); return false;}
		
		if($('personaliseSampleLink'))
		{
			$('personaliseSampleLink').onclick = function () {myProductView.startSamples(); return false;}
		}		
	},
	
	start: function(samples)
	{	
		if(!samples)
		{
			$('sampleSwitchContainer').update();	
		} 
		$('productViewImage').src = 'about:blank';

		if(currentProductImage) {
			this.showImage(samples);
		} else {
			//stretch the overlay across the page
			var arrayPageSize = getPageSize();
			Element.setWidth('productViewOverlay',arrayPageSize[0]);
			Element.setHeight('productViewOverlay',arrayPageSize[1]);
	
			new Effect.Appear('productViewOverlay', { duration: overlayDuration, from: 0.0, to: overlayOpacity });				
						
			var arrayPageScroll = getPageScroll();
			var productViewTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
			var productViewLeft = arrayPageScroll[0];
			Element.setTop('productView', productViewTop);
			Element.setLeft('productView', productViewLeft);
    		
			this.showImage(samples);		
		}		
	},
	
	startSamples: function()
	{
		Element.update('sampleSwitchContainer','<a href="#" onclick="myProductView.switchSample(\'0\'); return false;"> 1 </a><a href="#" onclick="myProductView.switchSample(\'1\'); return false;"> 2 </a><a href="#" onclick="myProductView.switchSample(\'2\'); return false;"> 3 </a>');		
		this.start(1);		
	},
	
	switchSample: function(samplenum)
	{		
		if(!samplenum)
		{
			samplenum = 0;
		}
		currentSampleImage = samplenum;
		
		Element.show('productViewLoading');
		
		imgPreloader = new Image();
		imgPreloader.onload=function(){
			Element.setSrc('productViewImage', embossingSamples[currentSampleImage].src);		
			Element.hide('productViewLoading');
			//myProductView.resizeImageContainer(imgPreloader.width, imgPreloader.height);		
			imgPreloader.onload=function(){};
		}
		imgPreloader.src = embossingSamples[currentSampleImage].src;	
	},
	
	resizeImageContainer: function( imgWidth, imgHeight) {

		// get current width and height
		this.widthCurrent = Element.getWidth('productViewOuterContainer');
		this.heightCurrent = Element.getHeight('productViewOuterContainer');

		// get new width and height
		var widthNew = (imgWidth  + (borderSize * 2));
		var heightNew = (imgHeight  + (borderSize * 2));

		// scalars based on change from old to new
		this.xScale = ( widthNew / this.widthCurrent) * 100;
		this.yScale = ( heightNew / this.heightCurrent) * 100;

		// calculate size difference between new and old image, and resize if necessary
		wDiff = this.widthCurrent - widthNew;
		hDiff = this.heightCurrent - heightNew;

		if(!( hDiff == 0)){ new Effect.Scale('productViewOuterContainer', this.yScale, {scaleX: false, duration: resizeDuration, queue: 'front'}); }
		if(!( wDiff == 0)){ new Effect.Scale('productViewOuterContainer', this.xScale, {scaleY: false, delay: resizeDuration, duration: resizeDuration}); }

		// if new and old image are same size and no scaling transition is necessary, 
		// do a quick pause to prevent image flicker.
		if((hDiff == 0) && (wDiff == 0)){
			if (navigator.appVersion.indexOf("MSIE")!=-1){ pause(250); } else { pause(100);} 
		}
		
	},
	
	showImage: function(samples)
	{
		Element.show('productViewLoading');	
		
		if(samples)
		{				
			imgPreloader = new Image();
			imgPreloader.onload=function(){
				Element.setSrc('productViewImage', embossingSamples[currentSampleImage].src);	
				Element.hide('productViewLoading');	
				//myProductView.resizeImageContainer(imgPreloader.width, imgPreloader.height);	
				imgPreloader.onload=function(){};
			}
			imgPreloader.src = embossingSamples[0].src;
			new Effect.Appear('productView', { queue: 'end', duration: animateDuration });
		}
		else
		{
			
			var thisProductImage = "<img src=\"";
			thisProductImage += productImageLargeViews[currentProductImage].src;
			thisProductImage += "\" alt=\"\" />";
			page_lightbox.view({width: 620, content: thisProductImage});
						
		}
				
				
	},
	
	end: function()
	{
		new Effect.Fade('productView', { queue: 'end', duration: animateDuration });
		new Effect.Fade('productViewOverlay', { queue: 'end', duration: overlayDuration });
	},
	
	realign: function()
	{
		if(Element.visible('productView'))
		{
			//stretch the overlay across the page
			var arrayPageSize = getPageSize();
			Element.setWidth('productViewOverlay',arrayPageSize[0]);
			Element.setHeight('productViewOverlay',arrayPageSize[1]);
			
			//center the image in the current document view
			var arrayPageScroll = getPageScroll();
			var productViewTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
			var productViewLeft = arrayPageScroll[0];
			Element.setTop('productView', productViewTop);
			Element.setLeft('productView', productViewLeft);
		}			
	}
	
};


function getPageSize()
{
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function getPageScroll(){

	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}

function pause(ms){
	var date = new Date();
	curDate = null;
	do{var curDate = new Date();}
	while( curDate - date < ms);
}

function initProductView() { myProductView = new productLargeView(); }
document.observe('dom:loaded', initProductView);
