function getID(strID)
{
	return strID.replace(/[^0-9]/gi, '');
}

$(document).ready( function()
{

	/**************************************** clear textareas *********************************************/
	$('textarea').each( function() {
		if (' '==$(this).text())
			$(this).text('');
	});

	/**************************************** new window links *********************************************/

	$('a[rel="newwindow"]').bind('click', function() {
		this.blur();
		window.open($(this).attr('href'));
		return false;
	});

	/**************************************** basket *********************************************/
	
	if (0!=$('form#basket').length)
	{
		$('.checkoutbutton-nojavascript').hide();
		$('.checkoutbutton-javascript').show();

		$('.diplomasubjectoption select').bind('change', function() {
			if (''!=this.value)
			{
				if (0==$('#updatebasketfield').length)
					$('#basket').append('<input type="hidden" id="updatebasketfield" name="updatebasket"/>');
					
				$('#basket').submit();
			}
		});
	}

	/**************************************** product showcase *********************************************/
	
	if (0!=$('#productshowcase-slide-controller').length)
	{
		$('#productshowcase-slides').addClass('productshowcasejavascipt-slides');
		$('.productshowcase-slide').addClass('productshowcasejavascript-slide').show();
		$('#productshowcase-slides-slider').addClass('productshowcasejavascript-slides-slider');
		$('#productshowcase-slide-controller').show();

		$('.slidenumber').click( function () {
			var intNewSlide=getID(this.id);
			if (intNewSlide!=intCurrentSlide)
			{
				//alert('intCurrentSlide: '+intCurrentSlide);
				//alert('intNewSlide: '+intNewSlide);

				var intIncrement=-1*(intNewSlide-intCurrentSlide);
				doMoveSlides1(intIncrement);

				return false;
			}
		});

		$('#slide_next').click( function () {
			doMoveSlides1(-1);
			return false;
		}
		);

		$('#slide_prev').click( function () {
			doMoveSlides1(1);
			return false;
		}
		);

		var intCurrentSlide=1;
		function doMoveSlides1(intIncrement)
		{
			//alert(intIncrement);

			var strLeft = $("#productshowcase-slides-slider").css('left');
			if (strLeft=='auto') 
			{
				strLeft = '0';
			}
			var intLength = parseInt(strLeft, 10);

			//alert('intLength: '+intLength);

			var oldOffset = Math.floor(intLength/613);
			var newOffset = oldOffset + intIncrement;

			//alert('oldOffset: '+oldOffset);
			//alert('newOffset: '+newOffset);

			var maxOffset = ($(".productshowcase-slide").length * -1);
			if (newOffset <= 0 && newOffset > maxOffset)
			{
				$('#productshowcase-slide-'+intCurrentSlide+' embed').hide();
				
				if (0!=$('#showcaseimageflv_'+intCurrentSlide).length)
				{
					$('#showcaseimageflv_'+intCurrentSlide)[0].SetVariable('player:jsStop', '');
				}
			
				$('#slidenumber_'+intCurrentSlide).removeClass('slidenumber-selected');

				intCurrentSlide=Math.abs(newOffset)+1;
				//alert('intCurrentSlide: '+intCurrentSlide);

				$('#slidenumber_'+intCurrentSlide).addClass('slidenumber-selected');

				if (1==intCurrentSlide)
				{
					$('#slide_prev').addClass('prevnextdisabled');
				}
				else
				{
					$('#slide_prev').removeClass('prevnextdisabled');
				}

				if (Math.abs(maxOffset)==intCurrentSlide)
				{
					$('#slide_next').addClass('prevnextdisabled');
				}
				else
				{
					$('#slide_next').removeClass('prevnextdisabled');
				}

				//alert('new left: '+(613 * newOffset))
				$("#productshowcase-slides-slider").animate({left: (613 * newOffset) }, 'fast');
				
				$('#productshowcase-slide-'+intCurrentSlide+' embed').show();
			}
		}
	}
	
	/**************************************** product contents *********************************************/
	
	if (0!=$('#productcontents').length)
	{
		$('#productcontents span.L2').addClass('L2-javascript');

		$("#productcontents").treeview({
			collapsed: true,
			animated: "fast"
		});
	}
	
	/**************************************** product upgrade showcase *********************************************/
	
	if (0!=$('#upgradeshowcase-slide-controller').length)
	{
		$('#upgradeshowcase-slides').addClass('upgradeshowcasejavascipt-slides');
		$('.upgradeshowcase-slide').addClass('upgradeshowcasejavascript-slide').show();
		$('#upgradeshowcase-slides-slider').addClass('upgradeshowcasejavascript-slides-slider');
		$('#upgradeshowcase-slide-controller').show();
		var bLock=false;

		$('.slidenumber').click( function () {
			if (!bLock)
			{
				bLock=true;

				intCurrentSlide=parseInt(getID($('#upgradeshowcase-slide-controller .slidenumber-this').attr('id')), 10);
				intNewSlide=getID(this.id);

				intIncrement=-1*(intNewSlide-intCurrentSlide);
				doMoveSlides2(intIncrement);
				doSlideController2(intNewSlide);

				bLock=false;
			}

			$(this).blur();
			return false;
		});

		$('#slide_next').click( function () {
			if ($(this).attr('class').match('nextdisabled'))
				return false;

			if (!bLock)
			{
				bLock=true;

				doMoveSlides2(-1);
				doSlideController2(parseInt(getID($('#upgradeshowcase-slide-controller .slidenumber-this').attr('id')), 10)+1);

				bLock=false;
			}

			$(this).blur();
			return false;
		}
		);

		$('#slide_prev').click( function () {
			if ($(this).attr('class').match('prevdisabled'))
				return false;

			if (!bLock)
			{
				bLock=true;

				doMoveSlides2(1);
				doSlideController2(parseInt(getID($('#upgradeshowcase-slide-controller .slidenumber-this').attr('id')), 10)-1);

				bLock=false;
			}

			$(this).blur();
			return false;
		}
		);
		
		function doSlideController2(intNewSlide)
		{
				var slidecontroller=$('#upgradeshowcase-slide-controller');

				$('a.slidenumber', slidecontroller).removeClass('slidenumber-lower');
				$('a.slidenumber', slidecontroller).removeClass('slidenumber-this');
				$('a.slidenumber', slidecontroller).removeClass('slidenumber-higher');
				$('a.slidenumber', slidecontroller).css('behavior', '');

				$('a.slidenumber:lt('+(intNewSlide-1)+')', slidecontroller).addClass('slidenumber-lower');
				$('a.slidenumber:eq('+(intNewSlide-1)+')', slidecontroller).addClass('slidenumber-this');
				$('a.slidenumber:gt('+(intNewSlide-1)+')', slidecontroller).addClass('slidenumber-higher');



				if (1==intNewSlide)
				{
					$('#slide_prev').addClass('prevdisabled');
				}
				else
				{
					$('#slide_prev').removeClass('prevdisabled');
				}

				if (getID($('a.slidenumber:last', slidecontroller).attr('id'))==intNewSlide)
				{
					$('#slide_next').addClass('nextdisabled');
				}
				else
				{
					$('#slide_next').removeClass('nextdisabled');
				}
		}

		function doMoveSlides2(intIncrement)
		{
			var strLeft = $("#upgradeshowcase-slides-slider").css('left');
			if (strLeft=='auto') 
			{
				strLeft = '0';
			}
			var intLength = parseInt(strLeft, 10);

			//alert('intLength: '+intLength);

			var oldOffset = Math.floor(intLength/613);
			var newOffset = oldOffset + intIncrement;
			//alert('oldOffset: '+oldOffset);
			//alert('newOffset: '+newOffset);

			var maxOffset = ($(".upgradeshowcase-slide").length * -1);
			if (newOffset <= 0 && newOffset > maxOffset)
			{
				//alert('new left: '+(613 * newOffset))
				$("#upgradeshowcase-slides-slider").animate({left: (613 * newOffset) }, 'fast');
			}
		}
	}

	/************************************************** diplomas ***********************************************/
	
	$('#diploma-subnav li a').each( function() {
		strURL=$(this).attr('href').replace('/ns', '');
		$(this).attr('href', strURL);
	}
	);

	/************************************** standards correlation ***********************************************/

	if (0!=$('#standardscorrelation').length)
	{
		var correlation=$('#standardscorrelation');
		var currentState='';
		
		function chooseState()
		{
			if (''==currentState)
			{
				$('#standardscorrelationstate_'+$('#standardscorrelationselect', correlation).val()).slideDown('fast');
			}
			else
			{
				$('#standardscorrelationstate_'+currentState, correlation).slideUp('fast', function()
				{
					$('#standardscorrelationstate_'+$('#standardscorrelationselect', correlation).val()).slideDown('fast');
				});
			}
			
			currentState=$('#standardscorrelationselect', correlation).val();
						
			$.cookie('standardscorrelation', currentState, { expires:1, path: '/' });
			
			$('#standardscorrelationselect', correlation).blur();
		}
	
		
		
		correlation.prepend('<select id="standardscorrelationselect" style="display:none;"><option value="">Select</option></select>');
		correlation.prepend('<label id="standardscorrelationselectlabel" for="standardscorrelationselect" style="display:none;">Choose a state:</label>');
		
		for (i=0; i<arrStates.length; i++)
		{
			$('#standardscorrelationselect', correlation).append('<option value="'+arrStates[i][1]+'">'+
				arrStates[i][0]+'</option>');
		}
		
		if (
			''!=$.cookie('standardscorrelation') &&
			0!=$('#standardscorrelationselect option[value='+$.cookie('standardscorrelation')+']', correlation).length
		)
		{
			currentState=$.cookie('standardscorrelation');
			$('#standardscorrelationselect', correlation).val(currentState);
			$('#standardscorrelationstate_'+currentState).show();
		}
		
		$('#standardscorrelationselect', correlation).bind('change', chooseState).show();
		$('#standardscorrelationselectlabel', correlation).show();

	}
	
	/****************************************** login form ************************************************/
	
	if (0!=$('#loginform form').length)
	{
		var loginform=$('#loginform form');
		var strUsernameLabel='email';

		$('#loginusername', loginform).bind('focus', function() {
			if (strUsernameLabel==$(this).attr('value'))
			{
				$(this).attr('value', '');
			}
		});

		$('#loginusername', loginform).bind('blur', function() {
			if ('' == $(this).val())
			{
				$(this).val(strUsernameLabel);
			}
		});

		$('#loginpasswordlabel', loginform).show();

		$('#loginpassword', loginform).bind('focus', function() {
			if ($('#loginpasswordlabel', loginform).is(':visible'))
			{
				$('#loginpasswordlabel', loginform).hide();
			}
		});

		$('#loginpassword', loginform).bind('blur', function() {
			if ('' == $(this).attr('value'))
			{
				$('#loginpasswordlabel', loginform).show();
			}
		});
	}
	
	/********************* try to stop ie drawing boxes round big links **********************************/
	
	$('#leftcol a').bind('click', function() {
		this.blur();
	});

	$('#rightcol a').bind('click', function() {
		this.blur();
	});

	$('#centrecol .arrowmask').bind('click', function() {
		this.blur();
	});
	
	/********************* ussite redirect popup **********************************/
	
	var usasitepopup=$('#usasitepopup');
	
	if (0!=usasitepopup.length && 1!=$.cookie('boardworksusasitepopup'))
	{
		usasitepopup.fadeIn('slow');
	
		$('.close').bind('click', function() {
			this.blur();
			
			if ('usecookies'==usasitepopup.attr('class'))
			{
				$.cookie('boardworksusasitepopup', 1, { expires:30, path: '/' });
			}
			
			usasitepopup.fadeOut('fast');
			
			return false;
		});
	}
	
	
	/*********************************** customer area navigation *******************************************/

	$('#customer-nav a').each( function () {
		strURL=$(this).attr('href').replace('/ns', '/');
		$(this).attr('href', strURL);
	});
	
	/*********************************** purchase history *******************************************/

	var purchasehistory=$('#purchasehistory');
	
	if (0!=purchasehistory.length)
	{
		$('form .nojavascriptsubmit', purchasehistory).hide();
		$('#filterjavascriptsubmit', purchasehistory).show();
		
		$('#filterjavascriptsubmit', purchasehistory).bind('click', function () {
			$('form', purchasehistory).submit();
			return false;
		});
		
		$('#purchasehistory-treeviewcontrols', purchasehistory).append('<div id="purchasehistory-collapseall"><a href=""><span>Collapse All</span></a></div>'+
			'<div id="purchasehistory-expandall"><a href=""><span>Expand All</span></a></div>');
		
		$('#purchasehistory-controls .right', purchasehistory).prepend('<div id="purchasehistory-print"><a href=""><span>Print</span></a></div>');
		$('#purchasehistory-print', purchasehistory).bind('click', function () {
			window.print();
			return false;
		});
		
		$('#inpDateFrom', purchasehistory).datepicker({
			showOn: 'button',
			buttonImage: 'images/calendar.gif',
			buttonImageOnly: true,
			buttonText: 'Choose date',
			dateFormat: 'dd/mm/yy',
			showAnim: 'fadeIn',
			changeMonth: true,
			changeYear: true

		});
		
		$('#inpDateTo', purchasehistory).datepicker({
			showOn: 'button',
			buttonImage: 'images/calendar.gif',
			buttonImageOnly: true,
			buttonText: 'Choose date',
			dateFormat: 'dd/mm/yy',
			showAnim: 'fadeIn',
			changeMonth: true,
			changeYear: true
		});
		
		$("#purchasehistory-tree", purchasehistory).treeview({
			collapsed: false,
			animated: "fast",
			control: "#purchasehistory-treeviewcontrols"
		});
		
		$('#purchasehistory-tree span.L1').addClass('L1-javascript');
	}

	/*********************************** topbar customer login *******************************************/
	
	var customerlogin=$('#customerlogin');
	
	if (0!=customerlogin.length)
	{
		$('.nojavascriptsubmit', customerlogin).hide();
		$('.formcontrols', customerlogin).append('<a href="" class="javascriptsubmit">Login</a>');
		$('.javascriptsubmit', customerlogin).bind('click', function()
		{
			customerlogin.submit();
			return false;
		});
	}
	
	/*********************************** customer login page *******************************************/
	
	var customerarealogin=$('#customerarealogin');
	
	if (0!=customerarealogin.length)
	{
		$('.nojavascriptsubmit', customerarealogin).hide();
		$('.secondinputline', customerarealogin).append('<a href="" class="javascriptsubmit">Login</a>');
		$('.javascriptsubmit', customerarealogin).bind('click', function()
		{
			customerarealogin.submit();
			return false;
		});
	}
	
	/***************************** customer contact details page *******************************************/
	
	var customerareacontactdetailsform=$('#customerareacontactdetailsform');
	
	if (0!=customerareacontactdetailsform.length)
	{
		$('.nojavascriptsubmit', customerareacontactdetailsform).hide();
		$('.submitline', customerareacontactdetailsform).append('<a href="" class="javascriptsubmit">Save</a>');
		$('.javascriptsubmit', customerareacontactdetailsform).bind('click', function()
		{
			customerareacontactdetailsform.submit();
			return false;
		});
	}
	
	/***************************** customer fogotten token *******************************************/
	
	var customerareaforgottentoken=$('#customerareaforgottentoken');
	
	if (0!=customerareaforgottentoken.length)
	{
		$('.nojavascriptsubmit', customerareaforgottentoken).hide();
		$('.inputline', customerareaforgottentoken).append('<a href="" class="javascriptsubmit">Get Customer ID</a>');
		$('.javascriptsubmit', customerareaforgottentoken).bind('click', function()
		{
			customerareaforgottentoken.submit();
			return false;
		});
	}
	
	/************************** interactive content showcase *******************************************/
	
	var picslides=$('#productinteractivecontent-slides');
	
	if (0!=picslides.length)
	{
		var intSlideWidth=605;
	
		picslides.addClass('productinteractivecontentjavascipt-slides');
		$('.productinteractivecontent-slide', picslides).addClass('productinteractivecontentjavascript-slide').show();
		$('#productinteractivecontent-slides-slider', picslides).addClass('productinteractivecontentjavascript-slides-slider');
		$('#productinteractivecontent-slide-controller').show();
	
		var oFirstSlide=$('.productinteractivecontent-slide:first', picslides);
		var oLastSlide=$('.productinteractivecontent-slide:last', picslides);
	
		$('.productinteractivecontent-slide', picslides).each( function () {
			if ($(this).attr('id')!=oFirstSlide.attr('id')) {
				$(this).append('<div class="prevbutton">Prev</div>');
			}
			
			if ($(this).attr('id')!=oLastSlide.attr('id')) {
				$(this).append('<div class="nextbutton">Next</div>');
			}
		});
		
		$('.prevbutton', picslides).bind('click', function() {
			doMoveSlides3(1);
			doSlideController3(parseInt(getID($('#productinteractivecontent-slide-controller .slidenumber-selected').attr('id')), 10)-1);
			
			return false;
		});
		
		$('.nextbutton', picslides).bind('click', function() {
			doMoveSlides3(-1);
			doSlideController3(parseInt(getID($('#productinteractivecontent-slide-controller .slidenumber-selected').attr('id')), 10)+1);
			
			return false;
		});
		
		$('#productinteractivecontent-slide-controller .slidenumber').click( function () {
			var strLeft = $("#productinteractivecontent-slides-slider", picslides).css('left');
			if (strLeft=='auto') 
			{
				strLeft = '0';
			}
			var intLeft = parseInt(strLeft, 10);
		
			if (0==Math.abs(intLeft) % intSlideWidth)
			{
				intCurrentSlide=parseInt(getID($('#productinteractivecontent-slide-controller .slidenumber-selected').attr('id')), 10);
				intNewSlide=getID(this.id);

				intIncrement=-1*(intNewSlide-intCurrentSlide);
				doMoveSlides3(intIncrement);
				doSlideController3(intNewSlide);
			}

			$(this).blur();
			return false;
		});
		
		function doMoveSlides3(intIncrement)
		{
			var strLeft = $("#productinteractivecontent-slides-slider", picslides).css('left');
			if (strLeft=='auto') 
			{
				strLeft = '0';
			}
			var intLeft = parseInt(strLeft, 10);

			//alert('intLeft: '+intLength);
			
			//if the left is not a multiple of intSlideWidth, we're actually sliding right now, so ditch out
			if (0!=Math.abs(intLeft) % intSlideWidth)
			{
				return;
			}

			var oldOffset = Math.floor(intLeft/intSlideWidth);
			var newOffset = oldOffset + intIncrement;
			//alert('oldOffset: '+oldOffset);
			//alert('newOffset: '+newOffset);

			var maxOffset = ($(".productinteractivecontent-slide").length * -1);
			if (newOffset <= 0 && newOffset > maxOffset)
			{
				//alert('new left: '+(605 * newOffset))
				$("#productinteractivecontent-slides-slider", picslides).animate({left: (intSlideWidth * newOffset) }, 'fast');
			}
		}
		
		function doSlideController3(intNewSlide)
		{
			$('#productinteractivecontent-slide-controller .slidenumber').removeClass('slidenumber-selected');
			$('#slidenumber_'+intNewSlide).addClass('slidenumber-selected');
		}
		
	}
	
	if (0!=$('#productinteractivecontent-nojsform').length)
	{
		$('#productinteractivecontent-nojsform').attr('id', 'productinteractivecontent-jsform');
		jsform=$('#productinteractivecontent-jsform');
		
		$('#nojs-submit', jsform).remove();
		$('.smallprint', jsform).before('<a href=""id="js-submit">Register</a>');
		
		jsform.dialog({
			width: 400,
			minHeight: 80,
			modal: true,
			dialogClass: 'productinteractivecontent-jsform',
			autoOpen: false
		});
		
		$('#productinteractivecontent-button').live('click', function() {
			$('#inpEmail').attr('value', '');
			jsform.dialog('open');
			return false;
		});
		
		$('#js-submit').bind('click', function() {
			$('.feedbacks', jsform).remove();
			
			if ($('#inpState', jsform).length) {
				$.post(window.location.pathname, {validatejsregistration: 1, inpEmail: $('#inpEmail', jsform).attr('value'), inpState: $('#inpState option:selected="selected"', jsform).attr('value')}, function(xml)
				{
					if (1==parseInt($('Success', xml).text(), 10))
					{
						jsform.submit();
					}
					else
					{
						$('.smallprint', jsform).before('<div class="feedbacks"><p>'+$('Feedback', xml).text()+'</p></div>');
					}
				});
			}
			else {
				$.post(window.location.pathname, {validatejsregistration: 1, inpEmail: $('#inpEmail', jsform).attr('value')}, function(xml)
				{
					if (1==parseInt($('Success', xml).text(), 10))
					{
						jsform.submit();
					}
					else
					{
						$('.smallprint', jsform).before('<div class="feedbacks"><p>'+$('Feedback', xml).text()+'</p></div>');
					}
				});
			}

			return false;
		});
		
		$('#inpEmail', jsform).keypress(function(e) {
			if(e.which == 13) {
				$('#js-submit').click();
				e.preventDefault();
				return false;
			}
		});
	}

});

