// JavaScript Document
var to = null;
var scrollamount = 5;
var toamount	= 10;
var count 		= 0;
$(document).ready(function(){
						   
	$('.gallery_left').mouseover(
		function(){
			doScroll('left');
			return false;
		}
	);
	$('.gallery_left').mouseout(
		function(){
			endScroll();
		}
	);
	$('.gallery_right').mouseover(
		function(){
			doScroll('right');
			return false;
		}
	);
	$('.gallery_right').mouseout(
		function(){
			endScroll();			
		}
	);
	$('.gallery_scroller img').live('mouseover',function(){
			$(this).stop();
			$(this).css({borderColor:	'#a4a4a4',zIndex:100});
			$(this).animate({
				top:			-10,
				left:			-10,
				width:			233,
				height:			144
				
			},'fast','swing');
		}
	);
	$('.gallery_scroller img').live('mouseout',function(){
			$(this).stop();
			$(this).css({borderColor:	'#000000',zIndex: 50});
			$(this).animate({
				top:			0,
				left:			0,
				width:			201,
				height:			124
			},'fast','swing');				
		}											
	);
	$('#selectgallery').change(
		function(){
			var id = $(this).val();
			$.each(galleries,function(idx,val){
				if(val.albumId==id){
					$('.gallery_scroller').empty().width(val.images.length*212).css({left: 0});
					$.each(val.images,function(ind,im){
						$('.gallery_scroller').append("<div class='picholder'>" +
														"<a href='admin/functions/showImage.php?id="+im.imageId+"&type=gallery' rel='lightbox-"+val.albumName.replace(' ','')+"'>" +
															"<img src='admin/functions/makesquare.php?id="+im.imageId+"&type=gallery&resize=233&resizey=144' width='201' height='124' />" +
														"</a>" +
													"</div>");						   
					});
				}
			});
			$('a[rel]').slimbox();
		}
	);
	$('#frm_mail').submit(function(){
		if($.trim($('#name').val())==''){
			$('#name').jpopup({title: 'Missing Field',message: 'Must fill in your name before submitting your enquiry.',align: 'left',decay:10000});	
			return false;
		}
		if($.trim($('#email').val())==''){
			$('#email').jpopup({title: 'Missing Field',message: 'Must fill in your email address so that we can reply to your enquiry.',align: 'left',decay:10000});	
			return false;
		}
		var regex	= /^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/gi;
		if(!regex.test($.trim($('#email').val()))){
			$('#email').jpopup({title: 'Invalid Email Address',message: 'This email address does not seem to be valid, please try again.',align: 'left',decay:10000});	
			return false;	
		}
		
		if($.trim($('#message').val())==''){
			$('#message').jpopup({title: 'Missing Field',message: 'Enter your enquiry.',align: 'left',decay:10000});	
			return false;
		}
		$.ajax({
			   url: 'includes/checksecurity.php',
			   dataType: 'json',
			   type:	'post',
			   data:	'security='+$('#security').val(),
			   success:	function(data){
					if(data.result){
						$('#submit_btn').attr('src','images/sendmessage.gif');
						params = {
							name:		$('#name').val(),
							email:		$('#email').val(),
							message:	$('#message').val(),
							security:	$('#security').val()
						}
						$.ajax({
							url: 'mail.php',
							dataType: 'json',
							type: 'post',
							data:	$.param(params),
							success: function(data){
								$('#submit_btn').attr('src','images/sendmessage.png');
								if(data.result){
									clearForm();
									$('.sentclass').show();		
								}else{
									$('#securityimg').attr('src','includes/security.php?='+Math.random());
									$('#submit_btn').jpopup({title: 'Error',message: data.msg,align: 'left',decay:10000});
								}
							},
							error:	function(){
								$('#submit_btn').attr('src','images/sendmessage.png');
								$('#securityimg').attr('src','includes/security.php?='+Math.random());
								$('#submit_btn').jpopup({title: 'Error',message: 'Could not communicate with the server at this time, please try again later.',align: 'left',decay:10000});	
							}
						});
					}else{
				   		$('#securityimg').attr('src','includes/security.php?='+Math.random());
						$('#security').jpopup({title: 'Error',message: 'Input Security Code does not match the shown code, please try again with the new code.',align: 'left',decay:10000});	
					}	   
			   },
			   error: function(){
				   $('#securityimg').attr('src','includes/security.php?='+Math.random());
					$('#security').jpopup({title: 'Error',message: 'Input Security Code does not match the shown code, please try again with the new code.',align: 'left',decay:10000});	   
			   }
		});
		return false;
	});
});
function clearForm(){
	$('#name').val('').change().blur();
	$('#email').val('').change().blur();
	$('#message').val('').change().blur();
	$('#security').val('').change().blur();
	$('#securityimg').attr('src','includes/security.php?='+Math.random());
}
function doScroll(dir){
	count++;
	var innerwidth 		= $('.gallery_scroller').width();	
	var outerwidth 		= $('.gallery_holder').width();	
	var maxscroll 		= (innerwidth-outerwidth) > 0 ? innerwidth-outerwidth : 0;
	var posx			= $('.gallery_scroller').css('left').replace('px','');
	to = null;
	if(dir=='left'){
		if(posx<0){
			posx = Number(posx) + Number(scrollamount);
			if(posx>0)posx = 0;
			$('.gallery_scroller').css({left: posx});
			to = setTimeout("doScroll('left');",toamount);
		}	
	}else{
		if(Math.abs(posx)<maxscroll){
			posx-=scrollamount;
			$('.gallery_scroller').css({left: posx});
			to = setTimeout("doScroll('right');",toamount);
		}
	}
}
function endScroll(){
	if(to){
		clearTimeout(to);
		to = null;
	}
}
