$(document).ready(function()
{
	// capture keystrokes
	$.ctrl = function(key, callback, args) {
		$(document).keydown(function(e) {
			if(!args) args=[]; // IE barks when args is null
			if(e.keyCode == key.charCodeAt(0) && e.ctrlKey) {
				callback.apply(this, args);
				return false;
			}
		});
	};

	// CTRL + L to login!
	$.ctrl('L', function() 
	{
		$('.headerlogin').slideToggle('fast', function()
		{
			if($(this).css('display') == 'none')
			{
				return;
			}

			if($('.headerusername').length)
			{
				$('.headerusername').focus();
			}
			else
			{
				$('#headerlogout').focus();
			}
		});
	});

	// ESC closes login box
	$('.headerlogin').keyup(function(event)
	{
		if(event.keyCode == '27')
		{
			$('.headerusername').blur();
			$('#headerlogout').blur();
			$('.headerlogin').slideToggle('fast');
		}
	});

	// global basket reminder
	if($("#headerbasket"))
	{
		$("#headerbasket").delay(300).animate(
		{
			top: '0px'
		}, 300);
	}

	// buy it now force scroll to associations action!
	if($(this).find('.itembuynow'))
	{
		$(this).find('.itembuynow').click(function()
		{
			if($('.viewassociations'))
			{
				$('.viewassociations').remove();
			}
			if($('.viewassociationsid'))
			{
				$('.viewassociationsid').remove();
			}
			
			var id = $(this).parent().find('.qty').attr('id').split('_');
			var buynowform = $(this).parent().parent().parent().find('.buynowform');

			$('<input type="hidden" name="viewassociations" value="viewassociations" class="viewassociations" />').appendTo(buynowform);
			$('<input type="hidden" name="viewassociationsid" value="'+id[1]+'" class="viewassociationsid" />').appendTo(buynowform);
		});
	}

	var hoverActive = false;

	// homepage/shop index entries
	if($(".homeshopentriesitem"))
	{
		$(".homeshopentriesitem").each(function()
		{
			var homeshopentriesprice = $(this).find('.homeshopentriesprice'),
				shopindexrecommendedtext = $(this).find('.shopindexrecommendedtext');

			$(this).hover(function()
			{
				hoverActive = setTimeout(function()
				{
					homeshopentriesprice.animate(
					{
						height: '55px'
					}, 200);

					shopindexrecommendedtext.animate(
					{
						top: '0px'
					}, 200);
				}
				, 200);
			},
			function()
			{
				clearTimeout(hoverActive);

				homeshopentriesprice.animate(
				{
					height: '0px'
				}, 200);

				shopindexrecommendedtext.animate(
				{
					top: '-60px'
				}, 200);
			});
		});
	}

	// global basket reminder
	if($("#usebillingdetails"))
	{
		$("#usebillingdetails").click(function()
		{
			var thisCheck = $(this);

			if (thisCheck.is(':checked'))
			{				
				$('.shipping_title').val($('.billing_title').val());
				$('.shipping_name').val($('.billing_name').val());
				$('.shipping_address').val($('.billing_address').val());
				$('.shipping_postcode').val($('.billing_postcode').val());
				$('.shipping_country').val($('.billing_country').val());
				$('.shipping_email').val($('.billing_email').val());
				$('.shipping_phone').val($('.billing_phone').val());
			}
		});
	}

	// auto update on delivery option select
	if($('.shippingselect').length)
	{
		$('.shippingselect').each(function()
		{
			$(this).change(function()
			{
				$('.overlay').fadeIn('fast');
				$('.updatebasket').click();
			});
		});
	}

	// check for form buttons and add an overlay show
	if($('.formsubmit').length)
	{
		$('.formsubmit').each(function()
		{
			$(this).click(function()
			{
				$('.overlay').fadeIn('fast');
			});
		});
	}

	if($('.submit').length)
	{
		$('.submit').each(function()
		{
			$(this).click(function()
			{
				$('.overlay').fadeIn('fast');
			});
		});
	}

	if($("#sortorder"))
	{
		$("#sortorder").change(function()
		{
			$('.overlay').fadeIn('fast');
			$('#sortorderform').submit();
		});
	}

	if(!$('.overlay').length)
	{
		$('<div class="overlay"><div class="overlayspinner"><!-- overlay --></div></div>').appendTo(document.body).hide();
	}



});

function assScroll()
{
	setTimeout(function()
	{
		$.scrollTo('#associations', 
		{
			duration: 500,
			offset: -80
		});
	}, 1000);
}

function toggleSlider(elem)
{
	$('#' + elem).slideToggle('slow', function()
	{
		$('.overlay').fadeOut('fast');
	});
}


function repStr(str,txt,by) {
 var strLength=str.length,txtLength=txt.length;
 if((strLength==0) || (txtLength==0))return str;
 var i=str.indexOf(txt);
 if((!i) && (txt!=str.substr(0,txtLength)))return str;
 if(i==-1)return str;
 var newstr=str.substr(0,i) + by;
 if(i+txtLength < strLength){newstr+=repStr(str.substring(i+txtLength,strLength),txt,by)}
 return newstr;
}

function hideElements(){
 var els = getElementsByClassName(document, "*", "effect_hide");
 for(a=0; a < els.length; a++){
  els[a].style.display = "none";
 }
}

function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

function getElementByClassName(oElm, strTagName, strClassName){
 var els = oElm.getElementsByTagName(strTagName);
 var el = false;
 for(a=0; a < els.length; a++){
  if(els[a].className == strClassName){
   el = els[a];
  }
 }
 if(el){
  return el;
 }else{
  return false;
 }
}

function sleeper(duration) 
{
	d = new Date().getTime() + duration;
	while(new Date().getTime() < d);
}


function stripSlashes(str)
{
	return str.replace(/\\/g, '');
}

var imgbank = new Array();

function viewImage(n, text)
{
	n -= 1;

	var image = $('#mainentryimage'), desc = $('#mainentryimagedesc');

	image.fadeOut(200, function()
	{
		image.attr('src', imgbank[n].src);

		if(desc)
		{
			desc.html((text));
		}
	})
	.fadeIn(200, function()
	{
	});

	return false;
}

function preloadImagebank()
{
	if(el = getElementByClassName(document, "DIV", "imagebank"))
	{
		var imgs = el.getElementsByTagName("IMG");
		for(i = 0; i < imgs.length; i++)
		{
			imgbank[i] = new Image();
			imgbank[i].src = repStr(imgs[i].src,"thumbs/","");
		}
	}
}



