// Image number tracking varibale
var ZoomImageNumber = 1;

function AddToBag()
{
	$('shopping-bag').show();
	$('ajax_loadingmsg2').show();
	$('ajax_basket').hide();

	new Ajax.Request('/ajax/eshop/basket/addproduct/', {
		method: 'post',
		parameters: $('buynow').serialize(true),
		onSuccess: function(transport)
		{
			var Response = EvalResponse(transport);
			
			if (true == Response.error)
			{
				$('ajax_loadingmsg2').hide();
				$('ajax_basket').update('<div class="product">' + Response.html_string + '</div>');
				$('ajax_basket').show();
				
				SetHideBasketTimeout();
			}
			else
			{
				$('ajax_loadingmsg2').hide();
				$('ajax_basket').update(Response.html_string);
				$('ajax_basket').show();
				
				SetHideBasketTimeout();
			}
		},
		onFailure: function(transport, jsonreponse)
		{
			window.location.href = '/checkout/basket';
		},
		onException: function()
		{
			$('shopping-bag').hide();
			$('ajax_loadingmsg2').hide();
			alert('An error has occured while processing your request, please try again. \n\nIf the problem persists please contact the administrator.');
		}
	});
	
	return false;
}


/**
* MORE IMAGES SECTION
*
**/
function DisplayImage(ProductLine, ImageNumber)
{
	new Ajax.Request('/ajax/eshop/getmainimage/'+ProductLine+'/'+ImageNumber, {
		onSuccess: function(transport){
			ImageCollection = transport.responseXML.getElementsByTagName('img');

			new Effect.Fade('active_image', {duration : 0.5,
								afterFinish : function ()
								{
									$('active_image').update(transport.responseText);
									new Effect.Appear('active_image', {duration : 0.5});
								}
			});
		}
	});
	
	// Update the global image number so that the zoom can pull the correct image
	ZoomImageNumber = ImageNumber;
}


function more_images()
{
	if(Element.visible($('more_images_thumbs')))
	{
		new Effect.Fade('more_images_thumbs', { duration: 0.5 });
		$('more_images_link').update('more images');
	}
	else
	{
		new Effect.Appear('more_images_thumbs', { duration: 0.5 });
		$('more_images_link').update('hide images');
	}
	$('more_images_thumbs').blur();
}


/**
* RECENTLY VIEWED SECTION
* 
**/
//var FirstShowing = 0; -- removed for fall back as its not always the first element showing then
var ShowingAtATime = 3;
var RecentlyViewedIncrement = 1;
var CurrentlyInList	= 0;


/**
* Sets values for recently viewed variables in order to show next
* 
**/
function ShowNextRecentlyViewed(Container)
{
	CurrentlyInList = Container.getElementsByTagName('a').length;

	if((window.FirstShowing + ShowingAtATime - 1) < (window.LastViewable))
	{
		window.FirstShowing = window.FirstShowing + RecentlyViewedIncrement;
		AddNeeded();
		setTimeout('MoveSlider($(\''+Container.id+'\'), \'next\', '+(RecentlyViewedIncrement*136)+')', 200);
	}
}

/**
* Sets values for recently viewed variables in order to show previous
* 
**/
function ShowPreviousRecentlyViewed(Container)
{
	if(-1 < window.FirstShowing - RecentlyViewedIncrement)
	{
		window.FirstShowing = window.FirstShowing - RecentlyViewedIncrement;
		CurrentlyInList = Container.getElementsByTagName('a').length;
		MoveSlider(Container, 'previous', (RecentlyViewedIncrement*136));
	}
}

function MoveSlider(Container, Direction, Offset)
{
	if('next' == Direction)
	{
		Offset = Offset * -1; 
	}
	new Effect.Move(Container, { x:  (Offset), y: 0, duration: 0.5});
		
	//hide/show next link
	if((window.FirstShowing + ShowingAtATime - 1) == (window.LastViewable))
	{
		$('recenly-viewed-next-link').hide();
	}
	else
	{
		$('recenly-viewed-next-link').show();
	}
	
	//hide/show previous link
	if(0 == window.FirstShowing)
	{
		$('recenly-viewed-previous-link').hide();
	}
	else
	{
		$('recenly-viewed-previous-link').show();
	}
}

/**
* Shows elements from window.FirstShowing up to Shoiwing at a time
* Also determines if next link should be working or not.
* 
**/

function ShowHide()
{
	for (i = 0; i < CurrentlyInList; i++)
	{
		if($('viewed_'+i))
		{
			if(i < window.FirstShowing || i > (window.FirstShowing + ShowingAtATime -1))
			{
				$('viewed_'+i).hide();
			}
			else
			{
				$('viewed_'+i).show();
			}
		}
	}
}

function AddNeeded()
{
	for(i = window.FirstShowing; i < (window.FirstShowing + ShowingAtATime); i++)
	{
		if(!$('viewed_'+i) && i < (window.LastViewable + 1))
		{
			AddToRecentlyViewed($('recently-viewed-div'), i);
		}
	}
}

function AddToRecentlyViewed(Container, Index)
{
	new Ajax.Request('/ajax/eshop/getviewed/'+Index, {
		onComplete: function(transport){

			var Response = EvalResponse(transport);
			if(Response.status && 'ok' == Response.status)
			{
				LinkElement 		= document.createElement('a');
				LinkElement.title 	= Response.title;
				LinkElement.id 		= Response.id;
				LinkElement.setAttribute('href', Response.href); 
				LinkElement.style.width = '142px';
		
				Container.appendChild(LinkElement);
				//THIS WAS THE ONLY WAY TO GET THAT IMAGE SHOWING IN IE
				$(LinkElement.id).update(Response.image);

				SpanElement = document.createElement('span');
				SpanElement.appendChild(document.createTextNode(' | '));
				Container.appendChild(SpanElement);
			}
		}
	});
}

var CurrentOverlay = '';

function DisplayDetailOverlay(Overlay)
{
	// For IE6, we hide the colour and size drop downs
	if ('details_zoom' == Overlay)
	{
		try { $('choose-size-select').hide() } catch (e) {}
		try { $('choose-colour-select').hide() } catch (e) {}
	}
	
	if ('' != CurrentOverlay && Element.visible(CurrentOverlay))
	{
		new Effect.Fade(CurrentOverlay, { duration: 0.5 });
		CurrentOverlay = '';
	}

	if (Overlay == CurrentOverlay)
	{
		CurrentOverlay = '';
	}
	else if ('' != Overlay )
	{
		new Effect.Appear(Overlay, { duration: 0.5, afterFinish: function() { $(Overlay).style.opacity = '1'; } });
		CurrentOverlay = Overlay;
	}
	
	if ('' == Overlay )
	{
		try { $('choose-size-select').show() } catch (e) {}
		try { $('choose-colour-select').show() } catch (e) {}
	}
	
	setTimeout('ActivateScroller(\''+Overlay+'\')', 500);
		
	return false;
	
}

function ActivateScroller(Overlay)
{
	switch(Overlay)
	{		
		case 'details_size-overlay':
			ScrollNum = '2';
			break;
		case 'details_information':
		default:
			ScrollNum = '1';
			break;
	}
}

function closeWindow()
{
	DisplayDetailOverlay('');
}

// scroll the element vertically based on its width and the slider maximum value
function scrollVertical(value, element, slider) {
	element.scrollTop = Math.round(value/slider.maximum*(element.scrollHeight-element.offsetHeight));
}

// scroll the element horizontally based on its width and the slider maximum value
function scrollHorizontal(value, element, slider) {
	element.scrollLeft = Math.round(value/slider.maximum*(element.scrollWidth-element.offsetWidth));
}