function ClosePopup() {
	var ImageDiv = document.getElementById( 'ImageLargePopup' );
	ImageDiv.innerHTML = '';
	$( '#PopupHolderExtra2' ).hide();
}

function OpenPopup( imageID, productID ) {
	$( '#PopupHolderExtra2' ).show();
	if( productID > 0 ) {
		GetImage2( 0, productID );
	}
	if( imageID > 0 ) {
		GetImage2( imageID, 0 );
	}
}

var Scroller2HolderWidth = 704;
var Scroller2SlideWidth = 88;
// Slider: to move one slide at a time
// Holder: to move by all slides that fit in one holder
var Scroller2MoveBy = 'Holder';

var Scroller2ID = 'Scroller2';
var Scroller2Speed = 7500;

var LargeImage2Holder = 'ImageLargePopup';

$(document).ready(function() {
	
	$('div.Scroller2 div.Thumb').bind('mouseenter', function() {		
		var imageID = $(this).attr('id');
		imageID = imageID.substr(13); // remove prefix Image_Thumb2_
		displayID = 'Enlarge_Thumb_' + imageID;
		$('#' + displayID ).show();
	});
	
	$('div.Scroller2 div.Thumb').bind('mouseleave', function() {		
		var imageID = $(this).attr('id');
		imageID = imageID.substr(13); // remove prefix Image_Thumb2_
		displayID = 'Enlarge_Thumb_' + imageID;
		$('#' + displayID ).hide();
	});
	
	$('div.Scroller2 div.Thumb').bind('click', function() {		
		var imageID = $(this).attr('id');
		var ProductID = 0;
		if( $('#' + imageID ).hasClass( 'Main' ) ) {
			ProductID = imageID.substr(13);
		}
		imageID = imageID.substr(13); // remove prefix Image_Thumb2_
		GetImage2( imageID, ProductID );
	});

	$('div.Scroller2Body div.Left').bind('click', function() {
		MoveScroller2( 'Left' );		
	});

	$('div.Scroller2Body div.Right').bind('click', function() {
		MoveScroller2( 'Right' );		
	});

	$('#ImageLargePopup, .MediaClose').bind('click', function() {
		ClosePopup();		
	});
		
});

function GetImage2( imageID, ProductID ) {
	var httpObject = getHTTPObject();
	var ScriptURL = AbsPath + 'includes/Templates/Producten/ImageAjaxPopup.php';
	
	if ( httpObject != null) {
		//Opening the connection
		httpObject.open( "POST", ScriptURL, false );
		httpObject.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
		
		//Setting up params and sending them
		var Params = "ID=" + imageID + "&ProductID=" + ProductID;
		httpObject.send( Params );
		
		//Based on the ajax response we will determin if it went ok or not.
		var response = httpObject.responseText;

		if ( response !== 'false' ) {			
			var ImageDiv = document.getElementById( LargeImage2Holder );
			ImageDiv.innerHTML = response;
		} else {
			throw 'Not a valid image';
		}		
	} else {
		throw 'HttpObject was not set!';
	}
	return false;
}

function MoveScroller2( Direction ) {
	if( Scroller2MoveBy == 'Holder' ) {
		var MoveX = Scroller2HolderWidth;
	} else {
		var MoveX = Scroller2SlideWidth;
	}
	var position = $('#' + Scroller2ID ).position();
	if( Direction == 'Left' ) {
		if( position.left < 0 ) {
			$( '#' + Scroller2ID ).animate({
				left: '+=' + MoveX + ''
			}, ' + Scroller2Speed + ', function() {
			    // Animation complete.
			});				
		}	
	} else {
		var Scroller2Width = $('#' + Scroller2ID ).width();
		if( ( position.left - Scroller2SlideWidth - Scroller2HolderWidth ) > ( 0 - Scroller2Width - 1 ) ) {
			$( '#' + Scroller2ID ).animate({
				left: '-=' + MoveX + ''
			}, ' + Scroller2Speed + ', function() {
			    // Animation complete.
			});				
		}
	}		
}
