var curElementCount = 1;

function ShiftCounter(amount)
{
	// NOTE: curElementCount should be a global
	ItemElements = $('thumbs_colour').getElementsByTagName('a');

	NumItemElements = ((ItemElements.length) -1);

	if((curElementCount + amount) > 0 && (curElementCount + amount) < NumItemElements)
	{
		curElementCount = curElementCount + amount;
	}

        UpdateLinks();

	return curElementCount;
}

function UpdateLinks()
{
    ItemElements = $('thumbs_colour').getElementsByTagName('a');

    NumItemElements = ((ItemElements.length) -1);

    // Update the previous/next links
    if((curElementCount - 1) > 0 && $('recenly-viewed-previous-link'))
    {
        new Effect.Appear('recenly-viewed-previous-link');
    }
    if((curElementCount + 1) < NumItemElements && $('recenly-viewed-next-link'))
    {
        new Effect.Appear('recenly-viewed-next-link');
    }

    if((curElementCount - 1) < 1 && $('recenly-viewed-previous-link'))
    {
        new Effect.Fade('recenly-viewed-previous-link');
    }
    if((curElementCount + 1) >= NumItemElements && $('recenly-viewed-next-link'))
    {
        new Effect.Fade('recenly-viewed-next-link');
    }


}

/*
* The following extension of Scriptaculous comes from
* http://elia.wordpress.com/2007/01/18/overflow-smooth-scroll-with-scriptaculous/
*/
/*
Effect.Scroll = Class.create();

Object.extend(Object.extend(Effect.Scroll.prototype, Effect.Base.prototype), {

	initialize: function(element) {
		this.element = $(element);
		var options = Object.extend({
			x:    0,
			y:    0,
			mode: 'absolute'
		} , arguments[1] || {}  );
		this.start(options);
	},

	setup: function() {
		if (this.options.continuous && !this.element._ext ) {
			this.element.cleanWhitespace();
			this.element._ext=true;
			this.element.appendChild(this.element.firstChild);
		}

		this.originalLeft=this.element.scrollLeft;
		this.originalTop=this.element.scrollTop;

		if(this.options.mode == 'absolute') {
			this.options.x -= this.originalLeft;
			this.options.y -= this.originalTop;
		} else {

		}
	},

	update: function(position) {
		$('thumbs_colour').scrollLeft = this.options.x * position + this.originalLeft;
		$('thumbs_colour').scrollTop  = this.options.y * position + this.originalTop;
		$('colour_check').value = this.element.scrollLeft;
	}

});

function moveTo(container, element){

  if(typeof($(element)) != 'undefined')
  {
	  Position.prepare();
	  container_x = Position.cumulativeOffset($(container))[0];
	  element_x = Position.cumulativeOffset($(element))[0];
	  new Effect.Scroll(container, {x:(element_x-container_x), y:0});
  }
  return false;
}
*/





function updatePersonalisationInputs()
{
	personalisationCode = $('personalisation_option').value;
	if('' != personalisationCode)
	{
		personalisationType = personalisationCode.substring(personalisationCode.length - 2, personalisationCode.length);
		switch (personalisationType)
		{
			case ('/I'):
				$('personaliation_1stline_surround').hide();
				$('personaliation_2ndline_surround').hide();
				$('personaliation_initials_surround').show();
				break;

			case ('1L'):
				$('personaliation_1stline_surround').show();
				$('personaliation_2ndline_surround').hide();
				$('personaliation_initials_surround').hide();
				break;

			case ('2L'):
				$('personaliation_1stline_surround').show();
				$('personaliation_2ndline_surround').show();
				$('personaliation_initials_surround').hide();
				break;

			case ('LH'):

				if(personalisationCode.substring(personalisationCode.length - 3, personalisationCode.length) == '1LH')
				{
					$('personaliation_1stline_surround').show();
					$('personaliation_2ndline_surround').hide();
					$('personaliation_initials_surround').hide();
				}
				if(personalisationCode.substring(personalisationCode.length - 3, personalisationCode.length) == '2LH')
				{
					$('personaliation_1stline_surround').show();
					$('personaliation_2ndline_surround').show();
					$('personaliation_initials_surround').hide();
				}
				break;

			default:
				$('personaliation_1stline_surround').hide();
				$('personaliation_2ndline_surround').hide();
				$('personaliation_initials_surround').hide();
				break;
		}
	}
}

function activatePersonalisation()
{
	$('cbo_personalisation-yes').checked = true;
}

function activateGiftWrapping()
{
	$('cbo_gift-wrap-yes').checked = true;

	var ddlPackage = $('ddl_package');
	if(ddlPackage.selectedIndex == 0)
	{
		ddlPackage.selectedIndex = 1;
	}
}

function updatePersonalisationDisplay()
{
	if($('cbo_personalisation-yes').checked == true)
	{
		$('personalisation_surround').show();
	}
	else
	{
		$('personalisation_surround').hide();
	}
}

function updateGiftWrappingDisplay()
{
	if($('cbo_gift-wrap-yes').checked == true)
	{
		$('giftwrapping_surround').show();
		activateGiftWrapping();
	}
	else
	{
		$('giftwrapping_surround').hide();
	}
}

function ShowMoneyBack()
{
	//$('send_to_a_friend-content').show();
	//try { $('txt_staf-friend-name').value = ''; } catch(e) {};
	//try { $('txt_staf-friend-email').value = ''; } catch(e) {};
	//try { $('txta_staf-message').value = ''; } catch(e) {};
	//$('send_to_a_friend-message').update('&nbsp;');
	//$('send_to_a_friend-message').hide();
	modalPopupShowMoneyBack.show()
}

function ShowSendToAFriend()
{
	$('send_to_a_friend-content').show();
	try { $('txt_staf-friend-name').value = ''; } catch(e) {};
	try { $('txt_staf-friend-email').value = ''; } catch(e) {};
	try { $('txta_staf-message').value = ''; } catch(e) {};
	$('send_to_a_friend-message').update('&nbsp;');
	$('send_to_a_friend-message').hide();
	modalPopupSendToAFriend.show()
}

function SendToAFriend(requestUrl)
{
	new Ajax.Request(requestUrl, {
		method: 'post',
		parameters: Form.serialize($('send_friend-form')),
		onComplete: function(AjaxResponse){
			$('send_to_a_friend-content').update(AjaxResponse.responseText);
			$('send_to_a_friend-message').hide();
			$('send_to_a_friend-content').show();
		},
		onLoading: function() {
			$('send_to_a_friend-content').hide();
			$('send_to_a_friend-message').update('<p>Sending...</p>');
			$('send_to_a_friend-message').show();
		}
	});
}

function TrimGiftwrap(TextArea)
{
	MaxChars = 200;
	if($(TextArea).value.length <= MaxChars)
	{
		return;
	}

	$(TextArea).value = $(TextArea).value.substr(0, MaxChars);
}

function ValidateAddToBasket()
{
	if (undefined != $('cbo_personalisation-yes') && true == $('cbo_personalisation-yes').checked)
	{
		var RegExpObj = new RegExp("[^A-Za-z0-9- .,\s!?&\"']");

		if (undefined != $('txt_1st-line') && '' != $('txt_1st-line').value && null != $('txt_1st-line').value.match(RegExpObj))
		{
			$('txt_1st-line').select();
			alert('Personalisation line 1 contains invalid characters. You are allowed upper and lower case alphabetical letters, numbers and the following symbols - . , " ? \' & .');
			return false;
		}
		if (undefined != $('txt_2nd-line') && '' != $('txt_2nd-line').value && null != $('txt_2nd-line').value.match(RegExpObj))
		{
			$('txt_2nd-line').select();
			alert('Personalisation line 2 contains invalid characters. You are allowed upper and lower case alphabetical letters, numbers and the following symbols - . , " ? \' & .');
			return false;
		}
		if (undefined != $('txt_initials') && '' != $('txt_initials').value && null != $('txt_initials').value.match(RegExpObj))
		{
			$('txt_initials').select();
			alert('Personalisation initials contains invalid characters. You are allowed upper and lower case alphabetical letters, numbers and the following symbols - . , " ? \' & .');
			return false;
		}
	}

	if(null != $('ddl_size') && '' == $('ddl_size').value)
	{
		alert('Please select a size');
		return false;
	}
	return true;
}







/**
* 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;
var LastViewable = 0;
var Slideactive = 'false';
var ActiveLink = 1;

/**
* Sets values for recently viewed variables in order to show next
*
**/
function ShowNextRecentlyViewed(Container)
{
	if(Slideactive == 'false')
	{
		CurrentlyInList = Container.getElementsByTagName('a').length;

		LastViewable = CurrentlyInList;

		FirstShowing = FirstShowing + RecentlyViewedIncrement;

		setTimeout('MoveSlider($(\''+Container.id+'\'), \'next\', '+(RecentlyViewedIncrement*108)+')', 200);
		ActiveLink = ActiveLink + 1;
	}
}

/**
* Sets values for recently viewed variables in order to show previous
*
**/
function ShowPreviousRecentlyViewed(Container)
{
	if(Slideactive == 'false')
	{
		if(-1 < FirstShowing - RecentlyViewedIncrement)
		{
			FirstShowing = FirstShowing - RecentlyViewedIncrement;
			CurrentlyInList = Container.getElementsByTagName('a').length;

			MoveSlider(Container, 'previous', (RecentlyViewedIncrement*108));
		}
		ActiveLink = ActiveLink - 1;
	}
}

function MoveSlider(Container, Direction, Offset)
{
	if('next' == Direction)
	{
		Offset = Offset * -1;
	}
	new Effect.Move(Container, { x:  (Offset), y: 0, duration: 0.5, beforeStart: function() { Slideactive = 'true'; }, afterFinish: function() { setTimeout("Slideactive = 'false', 100"); } });

	//hide/show next link
	if((ActiveLink + 2) == (LastViewable))
	{
		$('recenly-viewed-next-link').hide();
		ActiveLink = ActiveLink - 1;
	}
	else
	{
		$('recenly-viewed-next-link').show();
	}

	//hide/show previous link
	if(0 == FirstShowing)
	{
		$('recenly-viewed-previous-link').hide();
		ActiveLink = ActiveLink + 1;
	}
	else
	{
		$('recenly-viewed-previous-link').show();
	}
}
