var intPosition = 1;
var intLayer	= 1;
var blnAuto		= true;
var objFxFadeOut;
var objFxFadeIn;

// only initialise once the first image is loaded to prevent being able to see the images below loadin in
window.addEvent( 'domready', function() {

	// only on pages with big images
	if ( $('imgMain') ) {

		// this checks if the image is already loaded as IE does not trigger an onload on an image if it is already cached
		if ($('imgMain').width > 0) {
			initialise();
		} else {
			$('imgMain').addEvent( 'load', initialise );
		}
	}
});

function initialise() {

	// if has image switching
	if ( typeof(intImages) != 'undefined' ) {

		strPath = $('imgMain').src.substring( 0, $('imgMain').src.lastIndexOf('/' )+1 );

		// if product page
		if ( typeof( strFileName ) != 'undefined' ) {
			strPath += strFileName + '_';
			// set opacity
			$('productDetails').setOpacity( 0.87 );
		}

		$('text').style.height = "380px";
		$('imgMain').parentNode.style.position = 'relative';
		$('imgMain').className = 'slider';
		$('imgMain').style.zIndex = intImages;
		$('imgMain').id = 'image' + 1;

		if ( intImages > 1 ) {

		setTimeout( 'nextSlide()', 3000 );

			for ( i=2; i<= intImages; i++ ) {
				objImage = new Image();
				objImage.src = strPath + i + '.jpg';
				objImage.className = 'slider';
				objImage.style.zIndex = intImages - i;
				objImage.id = 'image' + i;
				$('imageContainer').appendChild( objImage );
				$('image' + i).setStyle( 'opacity', '0' );
			}

		}
	}

}

function nextSlide( intNext ) {
	// if soemone selects the same one we're on, do nothing but turn off auto
	if (intNext == intPosition) {
		blnAuto = false;
		return false;
	}
	// cancel an auto if nextSlide was queued up just before manual override
	if ( !blnAuto && !intNext ){
		return false;
	}

	objPhotoNew = $('image'+ intPosition );

	objFxFadeOutNew = new Fx.Tween( objPhotoNew, { property: 'opacity', duration: 2000 } ).start( 0 );
	// go back to BEGINING if at end
	if ( intPosition == intImages ) {
		intPosition = 0;
	}

	// if we've not selected a specific image, then use the next one
	if ( !intNext ) {
		intNext = ++intPosition;
	} else {
		blnAuto = false;
	}

	if ( typeof( objFxFadeIn ) != 'undefined' ){
		objFxFadeIn.cancel();
	}
	objFxFadeIn = new Fx.Tween( $('image'+ intNext ), { property: 'opacity', duration: 2000 } ).start( 0, 1 );

	intPosition = intNext;

	// only carry on auto if we've not clicked
	if ( blnAuto ) {
		setTimeout( 'nextSlide()', 3500 );
	}

	objFxFadeOut = objFxFadeOutNew;
	return false;
}

function toggleProductDetails() {
	blnShrink = $('productDetails').getStyle('right').slice( 0, -2) == 0 ;
	intRight =  blnShrink ? 270 : 0;
	new Fx.Tween( $('productDetails'), {property: 'right', duration: 1000} ).start( '-' + intRight + 'px' );
	new Fx.Tween( $('imgToggler'), { property: 'opacity', duration: 500 } ).start(0).chain( switchToggleImage );
}

function switchToggleImage() {
	$('imgToggler').src = $('imgToggler').src.indexOf( 'open.gif' ) > -1 ? '/assets/close.gif' : '/assets/open.gif';
	new Fx.Tween( $('imgToggler'), { property: 'opacity', duration: 500 } ).start(1 );
}