/**
 * @author alex
 */

 
$(function(){ 
	$('img#n_thumb_selector').css({'top' : '4px'});
	$('img#g_thumb_selector').css({'top' : '4px'});
	
	$('img#main_gallery_image').attr({
		'src': $('div#gallery_thumb_holder > img:first').attr('src').replace('74x49','417x226') ,
		'title': $('div#gallery_thumb_holder > img:first').attr('title'),
		'alt': $('div#gallery_thumb_holder > img:first').attr('alt')
	});
	
	$('img#main_news_image').attr({
		'src': $('div#news_thumb_holder > img:first').attr('src').replace('74x49','417x226') ,
		'title': $('div#news_thumb_holder > img:first').attr('title'),
		'alt': $('div#news_thumb_holder > img:first').attr('alt')
	});
	
	var id = $('div#news_thumb_holder > img:first').attr('id').replace('news_','');
	$('#news_title').html($("div#text_" + id + " > h2").html());
	$('#news_summary').html($("div#text_" + id + " > p").html());
	
	$("img.news.thumbnail, img.gallery.thumbnail").bind("click", function(e){
	    var position = $(this).position();
		if($(this).hasClass('gallery'))
		{
			$('img#g_thumb_selector').css({'top' : (Number(position.top)-3) + "px"});
			$('img#main_gallery_image').attr({
				'src': $(this).attr('src').replace('74x49','417x226') ,
				'title': $(this).attr('title'),
				'alt': $(this).attr('alt')
			});
		}
		else
		{
			$('img#n_thumb_selector').css({'top' : (Number(position.top)-3) + "px"});
			var id = $(this).attr('id').replace('news_','');
			$('img#main_news_image').attr({
				'src': $(this).attr('src').replace('74x49','417x226') ,
				'title': $(this).attr('title'),
				'alt': $(this).attr('alt')
			});
			
			$('#news_title').html($("div#text_" + id + " > h2").html());
			$('#news_summary').html($("div#text_" + id + " > p").html());
		}					
	 });
	 
	 if (GBrowserIsCompatible()) {
       var map = new GMap2(document.getElementById("map_one"));
	   var geocoder = new GClientGeocoder();
	   var address = "10 Pilgrims Way, dartford uk";
	   geocoder.getLatLng( address,
		    function(point)
		    {
			      if (!point)
			      {
			        alert(address + " not found");
			      }
			      else
			      {
			        map.setCenter(point, 15);
			        var marker = new GMarker(point);
			       map.addOverlay(marker);
				   //marker.openInfoWindow("Erith Carpet Center");

			      }
		     }
		  );
	 
	 var map2 = new GMap2(document.getElementById("map_two"));
	   var geocoder2 = new GClientGeocoder();
	   var address2 = "22 Wood Lane, dartford uk, DA2 7LR";
	   geocoder2.getLatLng( address2,
		    function(point2)
		    {
			      if (!point2)
			      {
			        alert(address2 + " not found");
			      }
			      else
			      {
			        map2.setCenter(point2, 15);
			        var marker2 = new GMarker(point2);
			       map2.addOverlay(marker2);
				   //marker.openInfoWindow("Erith Carpet Center");

			      }
		     }
		  );
	}
	
	$('button#submit_tuwut').bind('click', function(){
		var email = $('#feedback_email').val();
		var feedback = $('#feedback').val();
		if(trim(email) == "" || !validateEmail(email) || trim(feedback) == "")
		{
			alert("You must supply a valid email address and feedback to submit this form.");	
		}
		else
		{
			$.post("/ajax/email_feedback", { email: email, feedback: feedback },
			  function(data){
				    if(data == "success")
					{
						alert("Thank you for your feedback.");
					}
					else
					{
						alert("Unfortunately there was an issue with the form and we did not receive your feedback, please try again later.");
					}
			  });
		}
	});
	
	$('button#submit_mail').bind('click', function(){
		var name = $('#name').val();
		var email = $('#newsletter_email').val();
		if(trim(email) == "" || !validateEmail(email) || trim(name) == "")
		{
			alert("You must supply a valid email address and name to sign up for the newsletter.");	
		}
		else
		{
			$.post("/ajax/email_newsletter", { email: email, name: name },
			  function(data){
				    if(data == "success")
					{
						alert("Thank you for signing up to our newsletter.");
					}
					else
					{
						alert("Unfortunately there was an issue with the form and we did not receive your request to join our mailing list, please try again later.");
					}
			  });
		}
	});
	 
});

function trim(stringToTrim) 
{
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function isAlpha(val)
{
	val = trim(val);
	if(isValid(val,'abcdefghijklmnopqrstuvwxyz' + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + " ") != true)
	{
			return false;
	}
	else
	{
			return true;
	}
}

function validateEmail(email)
{
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false)
   {
		return false;
   }
	return true;
}
