function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

requestID = '';
setter = encodeURIComponent||escape;

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function createUser() {
	$.cookie('user',sg,{path:'/',expires:10*365});
}

function showEmailPage() {
	$('#emailPage').fadeIn('fast');	
	var pageTitle = document.title.replace(/ - The Official Website of British Showjumping/i,'');
	$('#emailPageDocTitle strong').text(pageTitle);
	$('#sendFriendPageTitle').val(pageTitle);
	$('#sendFriendPage').val(document.location.href);
}

function isValidEmail(email) {
		var emailRegExp = /^\w(?:\w|-|\.(?!\.|@))*@\w(?:\w|-|\.(?!\.))*\.\w{2,4}/ 
		var result = email.match(emailRegExp);
		if ((result && result[0].length != email.length) || !result) {
			return false
		} else {
			return true
		}
	}
	
function chkSubscribe() {
	if (!$('#newsletter_email_subscribe').val().length || !isValidEmail($('#newsletter_email_subscribe').val())) {
		alert('Please provide a valid email address.');
		return false
	} else {
		return true	
	}
}

function chkSendToAFriend() {
	var errArr = new Array();
	if (!$('#sendFriendTo').val().length || !isValidEmail($('#sendFriendTo').val())) {
		errArr[errArr.length] = 'your friend\'s valid email address';
	}
	if (!$('#sendFriendFrom').val().length || !isValidEmail($('#sendFriendFrom').val())) {
		errArr[errArr.length] = 'your valid email address';
	}
	if (!errArr.length) {
		document.emailPageForm.submit();
	} else {
		alert('Please provide the following:\n\n- '+errArr.join('\n- '));
		return false
	}
}

function setLink() {
	var linkvar = this.href.split('#');
	this.href = '/link/'+setter(linkvar[0])+'/'+setter($(this).text())+'/'+setter(window.location.href)+'/'+$.cookie('user');
	$(this).unbind('mousedown');
}


$(document).ready(function() {
	$('a').each(function() {
		var $a = $(this);
	  	if ($a.attr('href') != undefined) {
		  var hrefvar = $a.attr('href');
		  var text = $a.text();
		  if ( (hrefvar.match(/^http/)) && (! hrefvar.match(document.domain)) ) {
			$a.attr('target','_blank');
			 //$a.mousedown(setLink);
		  }
	  	} 			 
	});
	$('#nav li').each(function() {
		$(this).hover(
			function() {
				$(this).toggleClass('over');	
			},
			function() {
				$(this).toggleClass('over');
			}
		);	
		var contents = $('a',this).attr('href');
		if ( (contents.match(/^http/)) && (! contents.match(document.domain)) ) {
			/*$(this).mousedown(function() {
					$('a',this).unbind('mousedown');
					$('a',this).attr('href','javascript:void();');
					var linkvar = contents.split('#');
					var goTo = '/link/'+setter(linkvar[0])+'/'+setter($('a',this).text())+'/'+setter(window.location.href)+'/'+$.cookie('user');	
					window.location.href = goTo;
			});	*/
			// do nothing - can't open window with target blank for non link events.
			var s = 0;
		} else {
			$(this).click(function() {
				window.location.href = contents;					   
			});
		}
		
		//$('a',this).removeAttr('href').unbind('click');
		/*$(this).click(function() {
			if ($('a',this).attr('href') != undefined) {
				$.get('_ajax/record_link.cfm',{rand:Math.random(),linkvar:$('a',this).attr('href'),text:$('a',this).text(),from:document.location.href,user:$.cookie('user')},function(data) {
							
							window.location.href=data;	
							
				});
				
			} else if ($('a',this).attr('forward') != undefined) {
				$.get('_ajax/record_link.cfm',{rand:Math.random(),linkvar:$('a',this).attr('forward'),text:$('a',this).text(),from:document.location.href,user:$.cookie('user')},function(data) {
							
							window.location.href=data;	
							
				});	
			}
																															
			
		});*/
	});
	
	
	$('#closeEmailPage').click(function() {
		$('#emailPage').fadeOut('fast');									
	});
	
	
	
	/*if (!$.cookie('user')) {
		createUser();
	}*/
	
	
	/*$.get('_ajax/page_start.cfm',{rand:Math.random,requestDoc:document.location.href,title:document.title,user:$.cookie('user')},function(data) {
		requestID = data;																																	  
	});*/
});
/*$(window).unload(function() {
	$.get('_ajax/page_end.cfm',{rand:Math.random,requestID:requestID});
});*/

function clickIE(){
	if (event.button==2){
		return false;
	}
}

function clickNS4(e){
	if (document.layers||document.getElementById&&!document.all){
		if (e.which==2||e.which==3){
			return false;
		}
	}
}

if (document.layers){
	document.captureEvents(Event.MOUSEDOWN);
	document.onmousedown=clickNS4;
}
else if (document.all&&!document.getElementById){
	document.onmousedown=clickIE4;
}

document.oncontextmenu=new Function("return false");

