var widthStage;
var shadedWidth;
var leftLiveChat;
var widthLeftLiveChat;


function adaptLiveChatWrap(wWidth, sWidth){

	if(shadedWidth != null || shadedWidth > 1){
		leftLiveChat = ((wWidth / 2 + sWidth / 2) - widthLeftLiveChat) - 33;
		$('#liveChatWrap').css('left', leftLiveChat);	
	}else{
		$('#liveChatWrap').css('right', '14px');
	}
}

function bgImgGet( alt ){
	var bgImg = '';
	$( "#leftContent > .mid > .list > .thumbnailImage" ).each( function(){
		if( $( this ).find( 'img' ).attr( 'alt' ) == alt ) bgImg = $( this ).find( 'div' ).attr( 'rel' );
	});
	if( bgImg.length == 0 ){
		bgImg = $( "#leftContent > .mid > .list > .thumbnailImage > div" ).eq( 0 ).attr( 'rel' );
	}
	return bgImg;
}

function stretchVignette(){
	var windowHeight  = $( window ).height(),
		imageoffset   = parseInt( $( 'div#thumbSwitchBckgndImage' ).css( 'paddingTop' )),
		contentHeight = $( 'div#leftContent').height();
	$( 'div#leftContent').css({ paddingBottom: windowHeight - ( imageoffset + contentHeight + 16 )});
}

$(document).ready (function()  {

	widthStage = $(window).width();
	shadedWidth = $('.shadedContainer').width();
	widthLeftLiveChat = $('#liveChatWrap').width();
	adaptLiveChatWrap(widthStage,shadedWidth);

	if( typeof( $.address.value ) == 'function' ){
		var value = $.address.value(),
			addressRegEx = /\/(.*)/,
			result = null;
		//if deep link is set change the inital image selected
		if( addressRegEx.test( value )){
			result = addressRegEx.exec( value );
		}
		//set up the listner for the adress change
		$.address.change( function(){
			var value = $.address.value(),
				addressRegEx = /\/(.*)/,
				result = null;
			if( addressRegEx.test( value )){
				result = addressRegEx.exec( value );
				appendVal = parseInt( result[1] );
				//remove overlay and border
				$( "#leftContent > .mid > .list > .thumbnailImage > div" ).each( function(){
					$( this ).removeClass( 'active' );
				});
				//add border and overlay
				$( "#leftContent > .mid > .list > .thumbnailImage > div" ).each( function(){
					if( $( this ).find( 'img' ).attr( 'alt' ) == result[1] ){
						$( this ).addClass( 'active' );
					}
				});
				$( "div#background > img" ).attr( 'src', bgImgGet( result[1] ));
			}
		});
	}
	//set the inital background image
	if( $( "div#background > img" ).length <=0 ) $( "div#background" ).append( '<img/>' );
	$( "div#background > img" ).attr( 'src', bgImgGet( result[1] ));


	$( "#leftContent > .mid > .list > .thumbnailImage > div" ).click ( function(){
		if( typeof( $.address.value ) == 'function' ){
			$.address.value( $( this ).find( 'img' ).attr( 'alt' ));
		}
		//return false;
	});
	

	//stretch the content pain to fill the height
	stretchVignette();
	$( window ).resize( function(){
		stretchVignette();
	}).load( function(){ stretchVignette(); });
});


$(window).resize(function() {
	widthStage = $(window).width();
	shadedWidth = $('.shadedContainer').width();
	adaptLiveChatWrap(widthStage, shadedWidth);
});

