var fxPad = new Array();
var arrFx = new Array();
var objSelected = '';
var idProduct = 1;
var arrImages;

window.addEvent( 'domready', initialiseSubMenu );

function initialiseSubMenu() {

	arrMenuItems = $$( '#sidebar a' );

	objFirstLink = objFirstLink = arrMenuItems[0];
	toggleHover( objFirstLink, true );
	objSelected = objFirstLink;
	initialiseExtra();

	for ( i=0; i < arrMenuItems.length; i++ ) {
		arrMenuItems[i].href = '#';
	}
}

// FADE OUT current content
function loadSubPage( objLink ) {

	// check collection menu
	// if open and
	if ( ( $('ulCollection').style.height == '30px' ) && ( objLink != $('aCollection') ) && ( objLink.getParent().getParent().getParent().getFirst() != $('aCollection') ) && objLink.id != 'aAndree' && objLink.id != 'aTerence' ) {
		showSubMenu( $('aCollection') );
	}
	if ( ( $('ulBespoke').style.height == '57px' ) && ( objLink != $('aBespoke') ) && ( objLink.getParent().getParent().getParent().getFirst() != $('aBespoke') ) && objLink.id != 'aConsumer' && objLink.id != 'aCommercial' ) {
		showSubMenu( $('aBespoke') );
	}
	if ( ( $('ulNews').style.height == '6px' ) && ( objLink != $('aNews') ) && ( objLink.getParent().getParent().getParent().getFirst() != $('aNews') ) && objLink.id != 'aClippings' ) {
		showSubMenu( $('aNews') );
	}

	blnPrev = false;

	if ( objSelected ) {
		objPrevSelected = objSelected;
		blnPrev = true;
	}

	// reset previously selected link
	objSelected = objLink;

	if ( blnPrev ) {
		toggleHover( objPrevSelected, false );
	}

	// set this link as selected
	var strSection = objLink.id.substring( 1 ).toLowerCase();

	var fxPageFade = new Fx.Style('content', 'opacity', { transition: Fx.Transitions.Quart.easeInOut } );
	fxPageFade.start( 1, 0 ).chain( function(){
		// hide so doesn't show scrollbars - doesn't work
		$( 'content' ).style.visibility = 'hidden';

		var myXHR = new Ajax( 'index.php?strPage=' + strSection + '&blnAjax=1', {method: 'get', evalScripts: true, autoCancel: true, onSuccess: showSubPage } ).request();
	});

	if ( strSection == 'home' ) {
		intOpacity = 0.8;
		strColour = '#000000';
	} else {
		intOpacity = 1;
		strColour = '#474748';
	}

	new Fx.Style( $('divBackground' ), 'opacity', {duration: 1000, wait: false} ).start( intOpacity );
	new Fx.Style( $('divBackground' ), 'background-color', {duration: 1000, wait: false} ).start( strColour );

	objLink.removeEvents('mouseout');

	return false;
}

function showSubPage( strReturn ) {

	$( 'content' ).innerHTML = strReturn;
	var fxPageFade = new Fx.Style('content', 'opacity', { transition: Fx.Transitions.Quart.easeInOut } );
	$( 'content' ).style.visibility = 'visible';
	fxPageFade.start( 0, 1 );

}

function showSubMenu( objLink ) {

	var strSection = objLink.id.substring( 1 ).toLowerCase();

	// get target height
	switch( strSection ) {
		case 'bespoke':
			intHeight = 57;
			break;
		case 'collection':
			intHeight = 30;
			break;
		default:
			intHeight = 6;
			break;
	}

	// ucfirst() substitute
	chrFirst		= strSection.charAt(0).toUpperCase();
    strIdSubMenu	= 'ul' + chrFirst + strSection.substr(1, strSection.length-1);

	if ( $( strIdSubMenu ) ) {
		if ( $( strIdSubMenu ).style.height == intHeight + 'px' ) {
			intHeightStart	= intHeight;
			intHeightEnd	= 0;
			intPaddingStart = 10;
			intPaddingEnd	= 0;
		} else {
			intHeightStart	= 0;
			intHeightEnd	= intHeight;
			intPaddingStart = 0;
			intPaddingEnd	= 10;
		}

		new Fx.Style( $( strIdSubMenu ), 'height', {duration: 750, wait: false} ).start( intHeightStart, intHeightEnd );
		new Fx.Style( $( strIdSubMenu ), 'padding-top', {duration: 750, wait: false} ).start( intPaddingStart, intPaddingEnd );
		new Fx.Style( $( strIdSubMenu ), 'padding-bottom', {duration: 750, wait: false} ).start( intPaddingStart, intPaddingEnd );
	}
}

function toggleHover( objLink, blnIndent ) {
	// don't toggle if this is the SELECTED item
	if ( objLink != objSelected ) {
		intEnd = blnIndent ? 5 : 0;

		strSection = objLink.id.substring( 1 ).toLowerCase();

		// stop the PREVIOUS EFFECT so we don't get the judder of it trying to do both at the same time
		if (fxPad[strSection]) {
			fxPad[strSection].stop();
		}

		fxPad[strSection] = new Fx.Style( objLink, 'padding-left', {duration: 150, wait: false} );
		fxPad[strSection].start( intEnd );
	}
}

function hoverFace( objImgGlow, mixImgFade, blnHover ) {

	if ( objImgGlow && mixImgFade ) {

		if ( blnHover ) {
			intOpacityGlow = 1;
			intOpacityFade = 0.3;
			intOpacityBox = 0.9;
		} else {
			intOpacityGlow = 0.75;
			intOpacityFade = 0.75;
			intOpacityBox = 0;
		}

		if ( arrFx[ objImgGlow.src ] ) {
			arrFx[ objImgGlow.src ].stop();
		}
		if ( arrFxBox[ objImgGlow.src ] ) {
			arrFxBox[ objImgGlow.src ].stop();
		}

		arrFx[ objImgGlow.src ] = new Fx.Style( objImgGlow, 'opacity', { duration: 500 } ).start( intOpacityGlow );

		if ( !mixImgFade instanceof Array ) {
			arrImageFade = new Array();
			arrImageFade[0] = mixImgFade;
		} else {
			arrImageFade = mixImgFade;
		}

		for ( i=0; i<arrImageFade.length; i++ ) {

			objImgFade = arrImageFade[i];

			if ( arrFx[ objImgFade.src ] ) {
				arrFx[ objImgFade.src ].stop();
			}
		}

		for ( i=0; i<arrImageFade.length; i++ ) {

			objImgFade = arrImageFade[i];
			arrFx[ objImgFade.src ] = new Fx.Style( objImgFade, 'opacity', { duration: 750 } ).start( intOpacityFade );
		}

		arrFxBox[ objImgGlow.src ] = new Fx.Style( objImgGlow.getParent().getParent().getNext().id, 'opacity', { duration: 750 } ).start( intOpacityBox );
	}
}

function loadProduct( objLink, blnFirst ) {

	// check if this is not the first one
	if ( !blnFirst ) {
		new Fx.Style( $('divBio'), 'opacity', { duration: 750, transition: Fx.Transitions.Quart.easeInOut } ).start( 0 );
		new Fx.Style( $('divBio'), 'height', { duration: 1000, transition: Fx.Transitions.Quart.easeInOut } ).start( 0 ).chain( function() {
			$('divBio').style.display = 'none';
		});
	}

	idProduct = objLink.id.substring( objLink.id.indexOf('_')+1 );
	// preload next image
	idProductNext = idProduct.charAt(0) + ((idProduct.charAt(1)*1)+1);

	// preload image
	objPreload = new Image;
	objPreload.src = 'assets/products/' + idProduct + '.jpg';
	fxFadeProductImage = new Fx.Style( $('imgProduct'), 'opacity', { duration: 750, transition: Fx.Transitions.Quart.easeInOut } );
	fxFadeProductImage.start(0).chain( function() {
		$( 'imgProduct' ).src = objPreload.src;
		fxFadeProductImage.start(1);
	});

	var ajaxProduct = new Ajax( 'product.php?idProduct=' + idProduct + '&blnAjax=1', {method: 'get', autoCancel: true, onSuccess: showProduct } ).request();

	// preload image
	objPreloadNext = new Image;
	objPreloadNext.src = 'assets/products/' + idProductNext + '.jpg';

	return false;
}

function showProduct( strResponse ) {
	var objReturn = Json.evaluate( strResponse ) ;

	arrImages = objReturn['arrImages'];

	var fxFadeProductName		= new Fx.Style( $('hProduct'), 'opacity', { duration: 750, transition: Fx.Transitions.Quart.easeInOut } );
	var fxFadeProductDesc		= new Fx.Style( $('spnProductDesc'), 'opacity', { duration: 750, transition: Fx.Transitions.Quart.easeInOut } );
	var fxFadeProductGallery	= new Fx.Style( $('aGallery'), 'opacity', { duration: 750, transition: Fx.Transitions.Quart.easeInOut } );

	fxFadeProductGallery.start(0).chain( function() {
		fxFadeProductGallery.start(1);
	});
	fxFadeProductDesc.start(0).chain( function() {
		$( 'spnProductDesc' ).innerHTML = objReturn['strDescription'];
		fxFadeProductDesc.start(1);
	});
	fxFadeProductName.start(0).chain( function() {
		$( 'hProduct' ).innerHTML = objReturn['strName'];
		fxFadeProductName.start(1);
	});
}

function toggleHoverProduct( objLink, blnHover ) {
	if ( blnHover ) {
		objLink.className = 'btnProductHover';
	} else {
		objLink.className = 'btnProduct';
	}
}

function launchGallery() {
	Slimbox.open( arrImages, 0  );
}

function initialiseProducts() {
	new Fx.Style( $('imgProduct'), 'opacity' ).set(0);
	new Fx.Style( $('hProduct'), 'opacity').set(0);
	new Fx.Style( $('spnProductDesc'), 'opacity').set(0)
	new Fx.Style( $('aGallery'), 'opacity').set(0)

	arrLinksProducts = $$( '#divButtons a' );
	loadProduct( arrLinksProducts[0], true );

	for ( i=0; i<arrLinksProducts.length; i++ ) {
		arrLinksProducts[i].href = '#';
	}
}
